html-builder 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/html-builder.rb +190 -127
  2. metadata +1 -1
data/lib/html-builder.rb CHANGED
@@ -1,145 +1,208 @@
1
- #!/usr/bin/ruby
1
+ class HtmlBuilderNode
2
+ INDENT = "\t"
2
3
 
3
- class HtmlBuilder
4
- # HtmlBuilder
5
- #
6
- # Example:
7
- # require 'html-builder'
8
- #
9
- # puts HtmlBuilder.html {
10
- # head {
11
- # title "Hello HtmlBuilder"
12
- # }
13
- # body {
14
- # text "Hello ", a("HtmlBuilder", :href => "http://github.com/jinchizhong/html-builder"), "."
15
- # }
16
- # }
17
- # Got:
18
- # <html>
19
- # <head>
20
- # <title>Hello HtmlBuilder</title>
21
- # </head>
22
- # <body>
23
- # Hello <a href="http://github.com/jinchizhong/html-builder">HtmlBuilder</a>.
24
- # </body>
25
- # </html>
26
- # Just so easy...
27
-
28
- def initialize name, attrs, text
4
+ def initialize parent, name, *args
5
+ @attrs = args.last.is_a?(Hash) ? args.pop : {}
29
6
  @name = name
30
- @attrs = attrs
31
- @text = text
7
+ if not args.empty?
8
+ @text = args.collect do |a|
9
+ if a.is_a? HtmlBuilderNode
10
+ parent.children.delete a
11
+ a.to_s(0, true)
12
+ else
13
+ a.to_s
14
+ end
15
+ end.join
16
+ end
17
+ parent.children << self if parent
32
18
  @children = []
33
- @refed = false
34
19
  end
35
- attr_accessor :refed
36
- def escape str
37
- # TODO
38
- str.to_s
20
+ attr_accessor :children
21
+ def format_node_inner
22
+ [@name, *@attrs.collect{|k,v| "#{k}=\"#{v}\""}].join ' '
39
23
  end
40
- def quote str
41
- '"' + escape(str) + '"'
42
- end
43
- def self.html *args, &block
44
- attrs, text = HtmlBuilder.parse_arg *args
45
- h = HtmlBuilder.new "html", attrs, text
46
- h.instance_eval &block if block
47
- h.generate
48
- end
49
- def format_node
50
- str = escape @name
51
- if @attrs
52
- @attrs.each do |k, v|
53
- str += " #{escape k}=#{quote v}"
24
+ def to_s ind = 0, inl = false
25
+ raise "Cannot own text and child both!" if @text and not @children.empty?
26
+ if inl
27
+ if @text
28
+ return "<#{format_node_inner}>#{@text}</#{@name}>"
29
+ elsif not @children.empty?
30
+ return ["<#{format_node_inner}>", *@children.collect{|c| c.to_s 0, true}, "</#{@name}>"].join
31
+ else
32
+ return "<#{format_node_inner}/>"
54
33
  end
55
- end
56
- str
57
- end
58
- def generate_internel lines, dep, inl
59
- if @name == nil
60
- @text.each_line do |l|
61
- lines << " " * dep + l.chomp
34
+ else
35
+ if @text
36
+ return "#{INDENT * ind}<#{format_node_inner}>#{@text}</#{@name}>\n"
37
+ elsif not @children.empty?
38
+ return ["#{INDENT * ind}<#{format_node_inner}>\n", *@children.collect{|c| c.to_s(ind + 1)}, "#{INDENT * ind}</#{@name}>\n"].join
39
+ else
40
+ return "#{INDENT * ind}<#{format_node_inner}/>\n"
62
41
  end
63
- return
64
42
  end
65
- dep = 0 if inl
66
- cs = @children.keep_if {|c| !c.refed}
67
- raise "Cannot own block and text both." if @text and not cs.empty?
43
+ end
44
+ end
68
45
 
69
- if not cs.empty?
70
- lines << " " * dep + "<" + format_node + ">"
71
- cs.each do |c|
72
- c.generate_internel lines, dep + 1, inl
73
- end
74
- lines << " " * dep + "</#{escape @name}>"
75
- elsif @text
76
- lines << " " * dep + "<#{format_node}>#{escape @text}</#{escape @name}>"
77
- else
78
- lines << " " * dep + "<#{format_node}/>"
79
- end
46
+ class HtmlBuilder
47
+ def self.html *args, &block
48
+ generate :html, *args, &block
80
49
  end
81
- def generate dep = 0, inl = false
82
- lines = []
83
- generate_internel lines, dep, inl
84
- if inl
85
- return lines.join
86
- else
87
- return lines.join "\n"
88
- end
50
+ def self.generate root_ele, *args, &block
51
+ x = self.new
52
+ x.build_tree root_ele, *args, &block
53
+ x.to_s
89
54
  end
90
- # do not def to_s for this class!!!
91
- def self.parse_arg *args
92
- if args.last.class == Hash
93
- attrs = args.pop
94
- else
95
- attrs = {}
96
- end
97
- text = ""
98
- args.each do |arg|
99
- if arg.instance_of? HtmlBuilder
100
- text += arg.generate(0, true)
101
- else
102
- text += arg.to_s
103
- end
104
- end
105
- text = nil if text == ""
106
- return attrs, text
55
+ def initialize
56
+ @root = nil
57
+ @stack = []
107
58
  end
108
- def sub_node name, *args, &block
109
- attrs, text = HtmlBuilder.parse_arg *args
110
- args.each do |a|
111
- @children.delete a if a.instance_of? HtmlBuilder
112
- end
113
- @children << HtmlBuilder.new(name, attrs, text)
114
- if block
115
- @children.last.instance_eval &block
116
- end
117
- @children.last
59
+ def inspect
60
+ nil
118
61
  end
119
- def self.def_node name
120
- define_method name do |*args, &block|
121
- sub_node name, *args, &block
122
- end
62
+ def to_s *args
63
+ @root.to_s *args
64
+ end
65
+ def build_tree root_name, *args, &block
66
+ @root = HtmlBuilderNode.new nil, root_name, *args
67
+ yield_block @root, &block
123
68
  end
124
- def text *args, &block
125
- sub_node nil, *args, &block
69
+ def yield_block e, &block
70
+ return unless block
71
+ @stack << e
72
+ instance_eval &block
73
+ @stack.pop
126
74
  end
127
- def_node :head
128
- def_node :title
129
- def_node :meta
130
- def_node :link
131
- def_node :script
132
- def_node :body
133
- def_node :a
134
- def_node :h1
135
- def_node :div
136
- def_node :img
137
- def_node :span
138
- def javascript code
139
- script(:type => "text/javascript"){
140
- text code
141
- }
75
+ def add_child name, *args, &block
76
+ e = HtmlBuilderNode.new @stack.last, name, *args
77
+ yield_block e, &block
142
78
  end
79
+ def self.def_tag name
80
+ define_method name do |*args, &block|
81
+ add_child name, *args, &block
82
+ end
83
+ end
84
+ def_tag :a
85
+ def_tag :abbr
86
+ def_tag :acronym
87
+ def_tag :address
88
+ def_tag :applet
89
+ def_tag :area
90
+ def_tag :article
91
+ def_tag :aside
92
+ def_tag :audio
93
+ def_tag :b
94
+ def_tag :base
95
+ def_tag :basefont
96
+ def_tag :bdi
97
+ def_tag :bdo
98
+ def_tag :big
99
+ def_tag :blockquote
100
+ def_tag :body
101
+ def_tag :br
102
+ def_tag :button
103
+ def_tag :canvas
104
+ def_tag :caption
105
+ def_tag :center
106
+ def_tag :cite
107
+ def_tag :code
108
+ def_tag :col
109
+ def_tag :colgroup
110
+ def_tag :command
111
+ def_tag :datalist
112
+ def_tag :dd
113
+ def_tag :del
114
+ def_tag :details
115
+ def_tag :dir
116
+ def_tag :div
117
+ def_tag :dfn
118
+ def_tag :dialog
119
+ def_tag :dl
120
+ def_tag :dt
121
+ def_tag :em
122
+ def_tag :embed
123
+ def_tag :fieldset
124
+ def_tag :figcaption
125
+ def_tag :figure
126
+ def_tag :font
127
+ def_tag :footer
128
+ def_tag :form
129
+ def_tag :frame
130
+ def_tag :frameset
131
+ def_tag :h1
132
+ def_tag :h2
133
+ def_tag :h3
134
+ def_tag :h4
135
+ def_tag :h5
136
+ def_tag :h6
137
+ def_tag :head
138
+ def_tag :header
139
+ def_tag :hr
140
+ def_tag :html
141
+ def_tag :i
142
+ def_tag :iframe
143
+ def_tag :img
144
+ def_tag :input
145
+ def_tag :ins
146
+ def_tag :isindex
147
+ def_tag :kbd
148
+ def_tag :keygen
149
+ def_tag :label
150
+ def_tag :legend
151
+ def_tag :li
152
+ def_tag :link
153
+ def_tag :map
154
+ def_tag :mark
155
+ def_tag :menu
156
+ def_tag :menuitem
157
+ def_tag :meta
158
+ def_tag :meter
159
+ def_tag :nav
160
+ def_tag :noframes
161
+ def_tag :noscript
162
+ def_tag :object
163
+ def_tag :ol
164
+ def_tag :optgroup
165
+ def_tag :option
166
+ def_tag :output
167
+ def_tag :p
168
+ def_tag :param
169
+ def_tag :pre
170
+ def_tag :progress
171
+ def_tag :q
172
+ def_tag :rp
173
+ def_tag :rt
174
+ def_tag :ruby
175
+ def_tag :s
176
+ def_tag :samp
177
+ def_tag :script
178
+ def_tag :section
179
+ def_tag :select
180
+ def_tag :small
181
+ def_tag :source
182
+ def_tag :span
183
+ def_tag :strike
184
+ def_tag :strong
185
+ def_tag :style
186
+ def_tag :sub
187
+ def_tag :summary
188
+ def_tag :sup
189
+ def_tag :table
190
+ def_tag :tbody
191
+ def_tag :td
192
+ def_tag :textarea
193
+ def_tag :tfoot
194
+ def_tag :th
195
+ def_tag :thead
196
+ def_tag :time
197
+ def_tag :title
198
+ def_tag :tr
199
+ def_tag :track
200
+ def_tag :tt
201
+ def_tag :u
202
+ def_tag :ul
203
+ def_tag :var
204
+ def_tag :video
205
+ def_tag :wbr
206
+ def_tag :xmp
143
207
  end
144
208
 
145
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: