Hacking Thy Fearful Symmetry

Generating Template::Declare Code from a HTML Baseline

December 19th, 2010
PerlTemplate::DeclareXML::XSS

Generating Template::Declare Code from a HTML Baseline

For the templating system of my web applications, I usually go for HTML::Mason. It's a wonderful system but, as it mixes Perl code with HTML, no matter how hard I try to be well-behaved, my templates always seem to grow into giant miss-indented smorgasborgs.

Because of that, recently I've also been playing around with Template::Declare. As the templates of Template::Declare are pure Perl, not only do I have a fighting chance to keep my indentation consistent, but if I fail, Perl::Tidy is there to save my bacon.

Now, one thing with Template::Declare is that if I already have some HTML, writing the template from scratch is a little bit daunting. On the other hand, wouldn't that conversion be a perfect occasion to showcase the latest development on my own XML templating system, XML::XSS? Well, yes, I believe it would. :-)

In the latest development of XML::XSS, we can not only create stylesheets as classes, but I've introduced a style keyword that makes the syntax much cleaner. Follow me, I'll show you.

First, nothing too fancy. We just create a stylesheet called XML::XSS::Stylesheet::HTML2TD that inherits from XML::XSS.

part1.pl

And then, we begin with the fun stuff. For all HTML elements, we want to morph

<div class="[..]"> [..] </div>

into something like

div { attr { class => "[..]" }; outs "[..]"; }

Since we want all HTML elements to be transformed, we use the catchall element of the stylesheet:

part2.pl

For the text, we want to wrap it with calls to outs. Except for text nodes that are nothing but empty spaces, which we'll gladly skip:

part3.pl

For the pièce de résistance, since we'll eventually ask Perl::Tidy to clean up our code, why not do it directly as we are transforming the document?

part4.pl

And we are done. Now we can take a semi-badly formated HTML snippet like this one,

snippet.html

and pass it through XML::XSS very new xss command-line utility to get the corresponding Template::Declare code:

final.bash

There is still a lot to do, but I'm rrrreally liking the direction XML::XSS is taking.

Seen a typo or an error? Submit an edit on GitHub!