[pmwiki-users] Category Markup

DaveG pmwiki at solidgone.com
Sun Dec 18 21:10:50 CST 2005


I finally worked it out. I'll post the result as a Cookbook.

  ~ ~ Dave

DaveG wrote:
> Well, here's where I'm at now. I'm totally unable to get tge regex in 
> CategoriesToTags to convert from "Cat: [[!a]],[[!b]]" to "Tag: a,b". 
> Apart from that everything seems to fire okay.
> 
> function TagsToCategories($tags) {
>    return 'Cat:'.preg_replace('/\\w+/', '[[!$0]]', $tags);
> }
> #----
> function CategoriesToTags($tags) {
>    return '*'.$tags.'* Tags:'.preg_replace('/([a-z]+)/', '$0', $tags);
> }
> 
> function MyConvertTags($pagename, &$page, &$new) {
>    $x=$new['text'];
> 
>    $new['text'] = preg_replace("/^Tags?: *([\\w, ]+)/ie", 
> "TagsToCategories('$1')", $new['text']);
>    if ($x==$new['text'])
>       $new['text'] = preg_replace('/^Cat?: *([\\[\\[!\\w\\]\\], 
> ]+)/ie',"CategoriesToTags('$1')", $new['text']);
> }
> 
>  ~ ~ Dave
> 
> DaveG wrote:
> 
>> 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
>>>
>>
>> _______________________________________________
>> pmwiki-users mailing list
>> pmwiki-users at pmichaud.com
>> http://host.pmichaud.com/mailman/listinfo/pmwiki-users
>>
> 




More information about the pmwiki-users mailing list