From: Phlip Date: 2005-03-31T13:09:46+09:00 Subject: SNIPPET: YamlToHtml.rb (Or; Yet Another YAML Addict prowls the night..) =begin Rubistas: This be a mostly harmless way to convert YAML lists into cute HTML tables. The goals of the "project" are: - comments and lists convert into fragmentary XHTML - comments get small and emphatic - lists become tables - low-level details pass thru safely but un-cutened - we can't do vertical lists of lists, or keys of keys, yet Note that last "goal" implies I can't usefully convert most of the YAML appearing on my drive... Any help - especially preening or obscuring the regular expressions - moderately appreciated. The goal of the goals is to allow a Wiki-style web site convert YAML into simple tables, essentially the way some Wikis convert |nebulous|specifications| into tables. YAML, naturally, avoids the "yet another markup language" syndrome. If you color and border the tables cutely, then "- [...]\n- [...] # comment" transforms into this HTML: +-----+-----+ | Foo | Bar | +-----+-----+ | You | Two | # comment +-----+-----+ So comments that decorate rows appear to hang off their right ends. That feature is not implemented here, but you could do something with CSS. Another long-term goal is Jamis Buck's Syntax modules support this kind of output, possibly as a "pretty-html" back-end. But is the world ready for "YASTL Ain't a Structured Transformation Language" yet? -- Phlip http://www.c2.com/cgi/wiki?TestFirstUserInterfacesPrinciples "Be brave, little Piglet", said Pooh as he started the chain saw =end require('test/unit') def _yamlToRow(line, gotTable) q = line if line =~ /\s*\[(.*)\]\s*/ then q = $1 q = q.split(', ').join('') end comment = '' if line =~ /\s*\#\s*(.*)/ then comment = '' + $1 + '' end tab = '' tab = '' if not gotTable return tab + '' + comment + '' end def yamlToHtml(yaml) xhtml = yaml xhtml.gsub!(/\&|\<|\>/) do |q| if q == '&' then q = '&' end if q == '<' then q = '<' end if q == '>' then q = '>' end q end xhtml.gsub!(/^(\s*-\s*\[.*,\s*)\n(.*)/) do |q| $1+$2 end nu = '' gotTable = false xhtml.each_line do |line| if line =~/^-(.*)/ then q = _yamlToRow($1, gotTable) gotTable = true nu += q elsif line =~/^(\s*)\#\s*(.*)/ then nu += '
' + q + '
' if gotTable nu += $1 + '' + $2 + "
" gotTable = false else nu += line + '

' end end nu += '' if gotTable return nu end if __FILE__ == $0 then if ARGV.size == 0 then class TestYaml2Html < Test::Unit::TestCase def test_rawHTML() yaml = '& the devil\'s voice' xhtml = yamlToHtml(yaml) assert_equal '& the devil\'s <dark> voice

', xhtml end def test_brokeLine() yaml = "- [ format, froo,\n groo ]" xhtml = yamlToHtml(yaml) assert_equal ''+ "
formatfroo groo
", xhtml end def test_comment_after_table_closes_table_and_reopens_another() yaml = "- [ format ]\n # comment \n- [ frobmat ]" xhtml = yamlToHtml(yaml) assert_equal '
format
' + ' comment
'+ '
frobmat
', xhtml end def test_list() yaml = '- list' xhtml = yamlToHtml(yaml) assert_equal '
list
', xhtml yaml = "- [ node1, node2 ]" xhtml = yamlToHtml(yaml) assert_equal '
node1node2
', xhtml yaml = "- [ node1, node2 ]\n- [ node3, node4 ]" xhtml = yamlToHtml(yaml) assert_equal '' + "" + '' + '
node1node2
node3node4
', xhtml end def test_list_with_comment() yaml = "- [ node1, node2 ]\n- [ node3, node4 ] # I be comment" xhtml = yamlToHtml(yaml) assert_equal '' + "" + '' + '
node1node2
node3node4 ' + 'I be comment
', xhtml end def test_comment() yaml = '# I\'m a little comment short and stout' xhtml = yamlToHtml(yaml) assert_equal 'I\'m a little comment short and stout
', xhtml yaml = ' # nother comment' xhtml = yamlToHtml(yaml) assert_equal ' nother comment
', xhtml yaml = "#comment one\n # comment two" xhtml = yamlToHtml(yaml) assert_equal "comment one
"+ " comment two
", xhtml end end elsif ARGV.size = 1 then title = ARGV[0] print '' + title + '' print yamlToHtml(File.read(ARGV[0])) print '' end end