[pmwiki-users] Category Markup

DaveG pmwiki at solidgone.com
Sat Dec 17 17:05:10 CST 2005


Your sample didn't work as it stood, I just get the text 
"TagsToCategories('a,b')", on *Save*, where I would expect no action 
until Edit.

1] In this case, does the EditFunctions simply act when Editing, or also 
when Saving? It *appears* to do both.

2] What does the /m switch do? Things appear to work better when I use 
/e instead.

3] Based on the answer to (1), shouldn't we create a "CategoriesToTags 
routine, and change the pattern appropriately to convert "Tags: [[!a]]" 
into "Tags: a"?

Based on a "yes" to (3) I created routines to do the reverse 
replacement. What fails on my code is the reverse conversion from "Tags: 
a,b" to "Tags: [[!a]],[[!b]]" when switching to Edit.

Here's what I have so far (ref 
http://www.solidgone.com/pmwiki/index.php?n=Main.WikiSandbox):
$ROSPatterns["/^Tags?: *([\\w, ]+)/ie"] = "TagsToCategories('$1')";
function TagsToCategories($tags) {
    return 'Tags:'.preg_replace('/\\w+/', '[[!$0]]', $tags);
}
#----
function CategoriesToTags($tags) {
    return 'Tags:'.preg_replace('/[\\[\\[![\\w]\\]\\]]+/', '$1', $tags);
}

function MyConvertTags($pagename, &$page, &$new) {
    $new['text'] = preg_replace('/^Tags?: *([\\[\\[!\\w\\]\\], ]+)/e',
					"CategoriesToTags('$1')", $new['text']);
}

array_unshift($EditFunctions, 'MyConvertTags');
#----


  ~ ~ Dave

Patrick R. Michaud wrote:
> On Sat, Dec 17, 2005 at 12:55:39AM -0500, DaveG wrote:
> 
>>I abandoned the regex from hell, and used a function instead :) Is there 
>>such a thing as ReplaceOnEdit?
> 
> 
> It can be done with an EditFunction:
> 
>    function MyConvertTags($pagename, &$page, &$new) {
>      $new['text'] = preg_replace('/^Tags?: *([\\w, ]+)/m', 
>                                 "TagsToCategories('$1')", $new['text']);
>    }
> 
>    array_unshift($EditFunctions, 'MyConvertTags');
> 
> But it might also be worthwhile for me to define a $ROEPatterns
> that can corresponds to $ROSPatterns.
> 
> Pm
> 




More information about the pmwiki-users mailing list