From: Steve H. <sh...@zi...> - 2002-12-05 13:35:17
|
On Thu, 2002-12-05 at 04:39, Oren Ben-Kiki wrote: > Michael G Schwern wrote: > > (Last one, I swear) > > :-) > > > I was very surprised to find that the canonical YAML spec is written in > > HTML. This makes it very, very hard to machine parse and hard to > > transfer > > to another format without it coming out looking horrid. > [...] > > YAML is not an ideal language for "documents". XML is and will remain the > best choice for that. > I used to think YAML was bad for documents, but then my clever coworker Lex showed me a few tricks. Basically, the way to leverage YAML with more traditional document-oriented languages is to make a very thin YAML preprocessor: 1) Strings don't get translated at all. 2) Lists/arrays have their elements concatenated. 3) Dictionaries/hashes get expanded by a widget library. Suppose you start with this document: <html> This is <b>bold</b> text. Imagine lots of other text here that you're comfortable keeping in HTML. <table> <td valign='top' width='47'>foo</td> <td valign='top' width='47'>bar</td> </table> </html> Suppose that you're comfortable with HTML for inline formatting, but having to build the tables in HTML is starting to drive you nuts. Here's how YAML comes to the rescue: --- - | <html> This is <b>bold</b> text. Imagine lots of other text here that you're comfortable keeping in HTML. - table: tds: - foo - bar - </html> You then write a few widgets like this: def table(data): return '<table>' + widgetize(data) + '</table>' def tds(data): return ''.join(["<td valign='top' width='47'>" + widgetize(item) + "</td>" for item in data]) And then you write a little engine that knows to recursively walk your YAML-driven document specification. Then engine leaves strings alone, concatenates lists, and calls to the widget library for dictionaries. We've generated many thousands of lines of HTML using this approach, and we're quite happy with it. Using this approach for the YAML spec, or at least the POY spec, would be a great way to "dog food" YAML. It's tasty dog food! Cheers, Steve |