-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxml_simple.rb
65 lines (51 loc) · 1.01 KB
/
xml_simple.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class XML
def initialize(out)
@out = out # Remember where to send our output
end
def content(text)
@out << text.to_s
nil
end
def comment(text)
@out << "<!-- #{text} -->"
nil
end
def tag(tagname, attributes={})
#p caller
@out << "<#{tagname}"
attributes.each {|attr,value| @out << " #{attr}='#{value}'" }
if block_given?
@out << ">\n"
content = yield
if content
@out << content.to_s
end
@out << "</#{tagname}>"
else
@out << '/>'
end
nil
end
def init
yield
end
alias method_missing tag
def self.generate(out, &block)
XML.new(out).instance_eval &block
end
end
XML.generate(STDOUT) do
html do
head do
title { "Test Page for XML.generate" }
comment "This is a test"
end
body do
h1(:style => "font-family:sans-serif") { "Test Page for XML.generate" }
ul :type=>"square" do
li { Time.now }
li { RUBY_VERSION }
end
end
end
end