UseModWiki | RecentChanges | Preferences

$LateRules is a configuration variable that allows you to add your own functionality without changing the core wiki.pl file. It contains perl commands acting on $text, which will be the HTML source of the formatted wiki page. (To change it before it's HTML, use $EarlyRules.)

For example, to change text (: enclosed in smileys :) to bold (yes, I know there's already a bold), try this:

$LateRules .= <<EOLR;
  \$text =~ s#\(:([^:]*):\)#<b>$1</b>#g;

Does this work? Dont $ and / have to be masked?
Yes, it works. s#foo#bar# is s/foo/bar/ with the added benefit of not needing to escape slashes, and $1 etc. are OK in the right side of an s/// operator. --DenisMoskowitz

Changing $reportError and $origText

The comment in the code says that the rules code could also change $reportError and $origText--why would we want to?

If your eval results in an error, you can set reportError to false to prevent the wiki from reporting the error on the screen. If you change origText, that would prevent undo-ing your rule if it fails. Personally, I don't think it's really worthwhile to change either one.
This is correct, and I agree that it probably isn't worthwhile to change $reportError. Changing $origText could be useful for debugging purposes to find where an error occurs. For example, one could have the following code:

$LateRules .= <<EOLR;
  \$text =~ s#\(:([^:]*):\)#<b>$1</b>#g;
  \$origText .= "<hr>smile-bold done<hr>";  # FIXME: debug

...which would add the text "smile-bold done" if this rule completed successfully, even if a later error caused a failure. (You would want to remove or comment out the #FIXME line when you are finished debugging the wiki.) --CliffordAdams

Can you put in more than one line of Perl code?

I think so, it just eval's the whole thing. Just make sure to put the code in a single scalar.
Yes. I recommend using .= to append your code/rules to the end of $LateRules. (I changed the example above to use .= instead of = to make this clear.) --CliffordAdams

Finally, thanks for the user-contributed documentation above--I really appreciate it. --CliffordAdams

Glad to help - thanks for making such an easy Wiki! --DenisMoskowitz

Changing HTML tags

Is it possible to change HTML tags using $LateRules?

I came up with this, but it doesn't appear to work, i.e. no error message but it does not change the resulting code:

$LateRules .= '$text =~ s{a (href="http://dx\.doi\.org/)}{a target="_blank" $1}g;';

This doesn't work this way. When $LateRules where evaluated the url is still stored away and thus can't be found. Putting this in $EarlyRules doesn't work either, because then the url string is not expanded to the link yet. Hmm. -- MarkusLude

UseModWiki | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited April 7, 2016 8:58 am by h062040168154.moe.cm.kabsi.at (diff)