MT-Macros, baby. MT-Macros.

For instance, defining acronyms. Typing <acronym title="...">...</acronym> gets tiresome quickly, and I’m qualified to complain about that, because I’ve been doing it for months. No more. MT-Macros, baby. MT-Macros.

<MTMacroDefine name="DTD" pattern="m/\bDTD\b(?!<)/"><acronym title="Document Type Definition">DTD</acronym></MTMacroDefine>

Which, in English, states that anywhere that DTD appears as its own word and not followed by a < sign, mark it up as an acronym with the given title. \b means word boundary, and (?!<) means not followed by a < sign. The latter rule is for cases where DTD is already marked up as an acronym, and we don’t want to mark it up twice.

Then, where you have this:

<$MTEntryBody$>

Change it to this:

<$MTEntryBody apply_macros="1"$>

Lather, rinse, repeat, one macro for each acronym. Here are my macros; feel free to steal them. I define them all in a template module called macros, then put this at the top of each of my templates:

<$MTInclude module="macros"$>

I’ve defined about 50 acronyms, plus everything I used to do with MT-regex is now done with macros. (Automatic curly quotes, automatic apostrophes, changing <abbr> to <acronym> to appease IE, and so forth.) MT-Macros, baby. MT-Macros.

Note that once this is set up, you don’t need to do anything special to invoke a specific macro. You don’t need to enclose the acronym in quotes or exclamation points or anything else. Just type, and let MT-Macros worry about marking it up properly. Ditto apostrophes. Ditto whatever else you can think up and express in regular expressions.

Some people, when confronted with a problem, think I know, I’ll use regular expressions. Now they have two problems.

- Jamie Zawinski, alt.religion.emacs

Update: Brad was nice enough to come up with an additional hack using his MT-Perlscript plugin. It allows defining multiple markup-related macros all at once. As you can see, the syntax is easy to maintain:

"AOL" acronym title "America Online"

Or, more generally:

"literal" tag attribute "attribute value"

Which transforms literal into <tag attribute="attribute value">literal</tag>. Note that this is not acronym-specific; you could use it to set up hotlinks on specific words, like this:

"Jackie" a href "http://diveintoaccessibility.org/day_1_jackie.html"

There is also one special piece of logic in the Perl script which only expands the first occurrence of each acronym. This is actually the recommended way to mark up acronyms, and it keeps my posts from being littered with redundant acronym markup.

Yeah, yeah, Perl makes baby Jesus cry… but at least all this code is stored in my templates, completely separated from the base Movable Type code. When the next version of Movable Type comes out, I won’t have to reapply anything.

§

Respond privately

I am no longer accepting public comments on this post, but you can use this form to contact me privately. (Your message will not be published.)



§

firehosecodemusicplanet

© 2001–8 Mark Pilgrim