00163: Function ReplaceOnSave

Summary: Function ReplaceOnSave
Created: 2004-11-16 05:18
Status: Closed - already exists
Category: Feature
From: Klonk
Assigned:
Priority: 1
Version: 2.0 devel25
OS:

Description: Function ReplaceOnSave is intended to replace some strings with other strings when saving a page.

But in the array $ROSPatterns only replacement strings e.g. "sometext" are allowed. But it is not possible to create the replacement string as result of a called function.

Samples: Works: $ROSPatterns["/\\[\\[test:([^\\s$UrlExcludeChars]*)/"] = "Sometext\$1";

Does not work: $ROSPatterns["/\\[\\[test:([^\\s$UrlExcludeChars]*)/"] = SomeFunction($1);

SomeFunction should convert $1 as result of the pattern to some different string and return it for replacement.

Would be great if it could be changed somehow to make it work. This would give most flexibility for future additions or additional cookbook scripts.

The only other solution is to change $EditFunction and replace ReplaceOnSave with some own routine and afterwards calling ReplaceOnSave, which is IMHO not a really good idea. Imagine just some cookbook authors that try it the same way, but only the first script will run.

My english is not too good, I hope you know what I mean Klonk


Found a workaround (for writers of additional modules):

array_splice ($EditFunctions, array_search('ReplaceOnSave',$EditFunctions), 1, array('myFunction','ReplaceOnSave'));

With this statement 'myFunction' is called before 'ReplaceOnSave'. There you can perform all stuff you desire. Klonk


Actually, you can get ReplaceOnSave to call functions -- just be sure to add '/e' to the pattern to be replaced:

  $ROSPatterns["/\\[\\[test:([^\\s$UrlExcludeChars]*)/e"] = "SomeFunction('$1')";

-- Pm