I’m desperately trying to code a bit of Perl here. I’m attempting to write a template macro for MT that turns all links in posts into valid XML. But my Perl Fu isn’t strong enough.
To make a link XML valid, it mustn’t contain ampersands — “&”. They must instead be written as “&”. If not, the HTML validator complains. It’s really not a big deal, but I take personal pride in writing validating pages. I could fix any links I post manually, but I’d be very likely to forget doing it or breaking the links. Neither is an option. So I want to automate it in MT.
I’ve managed to do a quick Perl hack that does what I want — sort of. But it replaces globally, and I want to make sure that it doesn’t replace stuff outside of HTML tags — or more specifically, outside of A tags. I’m pondering how to write a regular expression for that…
Well, I have a possible solution in mind, but it does require a bit more code than the simple regular expression I was hoping for. I’ll give it a try tomorrow — need to go to bed now. If I still can’t get it to work, I’ll consult with one of the Perl Fu Masters of MT — Brad Choate and John Gruber come to mind.
Update: There’s a new MovableType plugin from John Gruber on Daring Fireball, that replaces his earlier Smart Quote educator. I’ve switched to it, let’s see if it does its mojo.
Try using Brad’s regex plugin and then add the following to your template:
<MTAddRegex name=”escapeamp”>s/(<a [^>]>)/my $i = $1;$i =~ s|&|&|g;$i/ie</MTAddRegex>
<MTEntryBody regex=”escapeamp”>
I haven’t tried this regex using the plugin, but ot works in Perl, so it should work in the plugin.