From: Phlip Date: 2005-04-17T23:44:35+09:00 Subject: Re: Html Tag Generation Karsten Meier wrote: > I'm working on a project which translates programming examples from the > perl cookbook to several other languages > (http://pleac.sourceforge.net/) Then why you are not using Perl? ;-) > To create the following html: > #
  1. red
  2. blue
  3. green
> > in perl you can write: > use CGI qw(:standard :html3) > print ol( li([ qw(red blue green)]) ); Ruby/CGI might have a bigger problem. I could not find a way to make it produce XHTML. A project should test its complex features, and XHTML tests super-easy when XPath queries out target details. But Ruby/CGI writes tags like , not . For MiniRubyWiki, I just rolled my own: class XhtmlGenerator def initialize(stream) @stream = stream end def balance_(tag, attributes, pretty = "\n") write(pretty) write("<") write(tag) write(" " + attributes) if ! attributes.nil? and attributes != '' write(">") write(pretty) begin yield() ensure write(pretty) write("") write(pretty) end end def small(attributes = '') balance_('small', attributes, '') { yield() } end def em(attributes = '') balance_('em', attributes, '') { yield() } end def table(attributes = '') balance_('table', ' summary="cosmetic table"' + attributes) { yield() } end def form(attributes = '') balance_('form', attributes) { yield() } end def th(attributes = nil) balance_('th', attributes) { yield() } end def td(attributes = nil) balance_('td', attributes, '') { yield() } end def tr(attributes = nil) balance_('tr', attributes) { yield() } end def h1(attributes = nil) balance_('h1', attributes, '') { yield() } end def body(attributes = nil) balance_('body', attributes) { yield() } end def head(attributes = nil) balance_('head', attributes) { yield() } end def title(attributes = nil) balance_('title', attributes, '') { yield() } end def xhtml() write(' ') attributes = 'xmlns="http://www.w3.org/1999/xhtml"' balance_('html', attributes) { yield() } end def a_href(path, stuff = nil) attributes = "href=\"#{path}\"" attributes += " " + stuff if stuff balance_('a', attributes, '') { yield() } end def textarea(attributes) balance_('textarea', attributes, '') { yield() } end def nbsp() write(' ') end def space() write(' ') end def endl() write("\n") end def write(q) @stream.write(q) end end That provides this: x = XhtmlGenerator.new() .... x.xhtml() { titleStuff() style = "style=\"margin-left: 0.5cm; margin-right: 0.5cm; margin-top: 0.2cm\"" x.body("bgcolor=\"#eeeeee\" text=\"black\" " + style) { formatFileBody(x, searchTags) { wikiFrame(x, contents) } } } > There ruby solution has two problems: > 1) because the methods like CGI::li() take a block as parameter, we can not > use a list as content parameter: > cgi.li{%w(red blue green)} returns "
  • redbluegreen
  • " instead of > "
  • red
  • blue
  • green
  • " > > 2) we need to write cgi.li{..} instead of just li(..) myList.each{|n| x.li(n)} > The result is that the scripts looks more confusing than the solution > with perls cgi module. Perl uses the design technique Yet Another Parser Hack to distinguish arrays and scalars as parameters. You could enhance my def li() to detect array arguments (using clean elegant reflection, not YAPH), and then iterate thru the array. -- Phlip http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces