I finally came up with a super-elegant way to do this in about 5 lines of code, based on the fact that simple YAML is very similar to Markdown .
We start with this:
Use regular expressions (in this case, in Perl) to remove the initial --- , and place hyphens in front of the key on each line:
$data =~ s/^---\n//s; $data =~ s/^(\s*)(\S.*)$/$1- $2/gm;
Voila, Markdown:
- all: - foo: 1025 - bar: - baz: 37628 - quux: - a: 179 - b: 7
Now just run it through the Markdown processor:
use Text::Markdown qw( markdown ); print markdown($data);
And you get a list of HTML - clean, semantic, backward compatible:
<ul> <li>all: <ul> <li>foo: 1025</li> <li>bar:</li> <li>baz: 37628</li> <li>quux: <ul> <li>a: 179</li> <li>b: 7</li> </ul></li> </ul></li> </ul>
YUI Treeview can improve existing listings, so we will complete everything:
<html><head> <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.6.0/build/treeview/assets/skins/sam/treeview.css"> <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&2.6.0/build/treeview/treeview-min.js"></script> </head><body> <div id="markup" class="yui-skin-sam"> <ul> <li>all: <ul> <li>foo: 1025</li> <li>bar:</li> <li>baz: 37628</li> <li>quux: <ul> <li>a: 179</li> <li>b: 7</li> </ul></li> </ul></li> </ul> </div> <script type="text/javascript"> var treeInit = function() { var tree = new YAHOO.widget.TreeView("markup"); tree.render(); }; YAHOO.util.Event.onDOMReady(treeInit); </script> </body></html>
So, all this works up to about 5 lines of code (turn YAML into Markdown, turn Markdown into an HTML list and put this HTML code into an HTML template file. The generated HTML is progressively expanded / decomposable, as it is fully viewable in non-JavaScript browsers, in the form of a simple old list.
Anirvan
source share