# 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("t", g.make("n:b"){'t'}) ok('t', g.make('a', :href=>'u'){'t'}) html = g.html {[ g.head {[ g.title {"タイトル"}, ]}, g.body {[ g.pre {[g.b {'world'}, 'hello']}, g.pre {['This is ', g.b{'bold'}, ' text.']}, g.pre {['This is ', g.i{'italic'}, ' text.']}, g.p {['This is ', g.a(:href=>'hoge'){'anchor'}, ' text.']}, ]} ]} ok("タイトル
worldhello
This is bold text.
This is italic text.

This is anchor text.

", html) # test_with_underbar ok("", g.ab) ok("", g.a_b) ok("", g.make("a_b")) ok("", g.make("a-b")) # test_ordered_hash ok("", g.a({'b'=>'c'})) ok("", g.a([{'b'=>'c'}])) ok("", g.a([{'b'=>'c'}, {'d'=>'e'}])) ok("", g.a({'b'=>'c'}, {'d'=>'e'})) # test_multi_attr ok("", g.make('a', {:href=>'h'}, {:class=>'c'})) ok("", g.make('a', {:class=>'c'}, {:href=>'h'})) end end end