I’ve been working with TWiki corporately but the internals were not robust enough for my corporate requirements so, being me, I decided to restructure it. The first thing I worked on was redoing the way TWiki handles its “variable” processing. In TWiki, a variable is set off by ‘%’ characters and can be either a string or a function. However, internally the processing is done in a very ad hoc and scattered way. I wanted a unified piece of code that did all of the basic processing in one place. I built that and then to test it I built an adapter that allowed it to be used in Movable Type.

Since then I’ve moved much more toward doing the Wiki stuff that I want inside Movable Type, having decided that it was easier to build Wiki features in Movable Type than to fix up existing Wiki implementations (the biggest issue being getting control of the text formatting of entries because, let’s face it, Wiki formatting syntax is horrible).

Using WikiVars

The basic functionality is to allow the assignment and use of string variables.

To use a WikiVar, one types ‘$var$’ This string is replaced by the value of var. A WikiVar name must consist of letters and the characters ‘-’ and ‘_’.

The standard Wiki format for assignment is ugly so I invented my own with inspiration from PERL and Textile. To assign a value to a WikiVar one types ‘$var=δvalueδ$’ where δ is a delimiter character. This can be any printable character that is not a valid character for a WikiVar name. Therefore $var=”value”$ or $var=%value%$ are equivalent. This avoid the question “but how do I put this character in the string?”. If you need a ” then you can use % as a delimiter instead. As a special case, any of the four characters <,[,{,( is not matched by the same character but by the matching “right” character >,],},) respectively. Therefore another way to do the assignment is $var=<value>$. The assignment itself is completely removed from the output text.

This turns out to be handy for doing repitive things inside a post. For instance, an acronym that you want to use multiple times with the same hover text. One can do

$OJ=”<acronym title=”Orrin Judd”>OJ</acronym>”$
I was reading a post by $OJ$ the other day. This time $OJ$ was going off on …

Each instance of $OJ$ will be replaced with the ACRONYM element to give a text ‘OJ’ with the hover text ‘Orrin Judd’. Astute readers at this point will ask, ‘didn’t you use a ” as a delimiter and in the string itself? Isn’t that a typo?’. No, it isn’t. Because the ” isn’t immediately followed by a $ that’s not the end of the value. This makes putting characters like ” in values even easier.

This is reasonably useful but there are additional features that make it even more so. Among the WikiVar template tags are ones that can perform these assignments. Therefore one can put the assignments in an MT template for use in posts or comments. More commonly, one would put them in an MT template module and then include that module. Another WikiVar tag will read a file, process all of the assignments in the file and then discard the results. By using that in MT templates one can have an external file with WikiVar style defines and comment text to define all of the standard variables. Further, changing WikiVar to use ‘:’ instead of ‘$’ is a one character change in the source by design. One could with very little work do text smileys of the form ‘:grin:’, ‘:smile:’, ‘:sad:’ in an easy to use syntax and easy to edit file.

Function WikiVars

Beyond this, WikiVar also supports function variables. When these are used, a PERL subroutine is called and its return value is used as the value for the WikiVar. The subroutine recieves the MT context object and the argument string and then returns whatever it likes. Such function variables can only be defined in other PERL code or via MT tags - I consider it too dangerous to allow in direct text. Note that these function variables are simply to use in text once defined.

To give an example, I have a function variable named ‘note’ that I can use. If I wrote $note(This is cool stuff, eh?)$ the output would be an icon like this that expands into the note text when clicked (and folds back up when done). This requires some Javascript support in the Template but all of the local HTML hackery is done by the ‘note’ function variable. For work I have another one that looks like $drone(aog, Annoying Old Guy)$ which turns in to a link with the text ‘Annoying Old Guy’ that links to my corporate directory entry (which is keyed by my e-mail address, ‘aog’). Beyond that, I have another one that doesn’t spit out the HTML to do that but defines another WikiVar that contains the appropriate HTML so I can reuse it.

Overall, this provides a relatively clean way to expose PERL subroutines to authors and commentors. An alternative method is to build MT tags to do it, but I like this syntax better. It’s also much cleaner, IMHO than the MTPerl plugin. One can also compare WikiVar to the MTMacro plugin. WikiVar is not as powerful in that the form of the macro is limited and it doesn’t do containers but the syntax is much easier to use and it suffices for my purposes.

Miscellaneous

MTWikiVar provides scoping to prevent assignments in a post or comment from propagating to other posts / comments. This is under template control.

MTWikiVar also provides the ability (in a template) to invoke a WikiVar and then reprocess the output for MT tags. I use this do this things like this year in review. The actual template code that creates the calendar is

<MTWikiVarEval>$YearArchive(2003)$</MTWikiVarEval>