pod2html.js: Some JavaScript love for POD in a browser

agent on 2008-09-28T03:23:08

It's fun to do POD (Plain Old Documentation) in a web browser and I've hacked up a JavaScript implementation for the pod2html utility (actually the output is more like Pod::Simple::HTML).

The pod2html.js script is in OpenResty's SVN repository:

   http://svn.openfoundry.org/openapi/trunk/demo/Onccf/js/pod2html.js

The API is straightforward, for instance,

   var pod = "=head1 Blah\n\nI<Hello>, C<world>!\n";
   var html = pod2html(pod);

The following web site is already making use of it:

   http://agentzh.org/misc/onccf/out/

By sniffing the background AJAX requests (e.g. using Firebug), you can see raw POD is retrieved from the OpenResty server and converted to HTML on-the-fly in your browser.

It's worth mentioning that I had a lot of fun combining Test::Base and JavaScript::SpiderMonkey to test this piece of JavaScript code in pure Perl. You can checkout the test script here:

   http://svn.openfoundry.org/openapi/trunk/demo/Onccf/t/01-pod2html.t

By looking at the (declarative) test cases, it's trivial to see what it can do (and hopefully what it can't) :)

For the record, as of this writing, the following POD directives are supported:

  =headN, =over, =item *, =item NUM., =item TEXT, =back, =begin html, =end html, =begin ANY, =end ANY, =cut (it's a no-op), =encoding ANY (it's a no-op)

and the following POD markups are implemented:

   C<...>, I<...>, B<...>, L<...>, F<...>

I've also implemented the (non-standard) =image directive for convenience. For example,

   =image gate.jpg

will be converted to

   <p><img src="gate.jpg"/></p>

Have fun!

P.S. This journal was originally posted to my personal blog site: http://blog.agentzh.org/#post-93