# Copyright (C) 2003-2006 Kouichirou Eto, All rights reserved. # This is free software with ABSOLUTELY NO WARRANTY. # You can redistribute it and/or modify it under the terms of the GNU GPL 2. $LOAD_PATH << 'compat' unless $LOAD_PATH.include? 'compat' require 'htree' $LOAD_PATH.unshift '..' unless $LOAD_PATH.include? '..' module HTree module GeneratorModule def method_missing(symbol, *args, &block) make(symbol, *args, &block) end def p(*args, &block) make(:p, *args, &block) end def make(symbol, *args, &block) ar = make_ar(symbol, *args, &block) Elem.new(*ar) end def make_ar(symbol, *args, &block) ar = [] ar << symbol.to_s.gsub(/_/, '-') # tag name if 0 < args.length a = add_args(args) ar += a end if block y = block.call ar += add_elems(y) end ar end def add_args(args) args.flatten.map {|arg| symbol_to_hash(arg) } end def add_elems(args) return [] if args.nil? return [args] unless args.kind_of? Array ar = [] args.each {|a| next if a.nil? ar << a } ar = [''] if ar.empty? ar end def symbol_to_hash(s) h = {} s.each {|k, v| next if v.nil? h[k.to_s] = v.to_s } h end end class Generator; include GeneratorModule; end end if $0 == __FILE__ require 'qwik/testunit' require 'qwik/htree-format-xml' $test = true end if defined?($test) && $test class TestHTreeGenerator < Test::Unit::TestCase def ok(e, s) ok_eq(e, s.format_xml) end def test_all g = HTree::Generator.new # test_htree_generator ok("
", g.p) doc = HTree::Doc.new(g.p) ok("", doc) str = '' doc.display_xml(str) ok_eq("", str) ok("", g.a) ok("", g.b{}) ok("", g.b{''}) ok('', g.img(:src=>'u')) ok("t", g.b{'t'}) ok("b
", g.p{g.b{'b'}}) ok("t
b
t", g.p{['t', g.p{'b'}, 't']}) ok('t', g.a(:href=>'u'){'t'}) ok('t', g.font(:size => 7){'t'}) # test_namespace ok("t", g.make('b'){'t'}) ok("worldhello
This is bold text.
This is italic text.
This is anchor text.
", html) # test_with_underbar ok("