webget_ruby_html 1.0.8 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/README.rdoc +76 -5
- data/lib/webget_ruby_html.rb +4 -323
- data/lib/webget_ruby_html/lists.rb +61 -0
- data/lib/webget_ruby_html/misc.rb +56 -0
- data/lib/webget_ruby_html/tables.rb +215 -0
- data/test/webget_ruby_html.rb +9 -0
- data/test/webget_ruby_html/lists_test.rb +29 -0
- data/test/webget_ruby_html/misc_test.rb +21 -0
- data/test/{webget_ruby_html_test.rb → webget_ruby_html/tables_test.rb} +2 -30
- metadata +13 -3
- metadata.gz.sig +1 -1
data.tar.gz.sig
CHANGED
Binary file
|
data/README.rdoc
CHANGED
@@ -6,10 +6,81 @@ Copyright:: Copyright (c) 2006-2010 Joel Parker Henderson
|
|
6
6
|
License:: CreativeCommons License, Non-commercial Share Alike
|
7
7
|
License:: LGPL, GNU Lesser General Public License
|
8
8
|
|
9
|
-
==
|
9
|
+
== Table Methods
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
* table - Builds an HTML table from the supplied parameters: string CSS: ID, Class; array of strings: Headers, Footers; array of array of string: Rows
|
12
|
+
* tables - Builds one or more HTML tables from a supplied array of arrays (each sub-array holding the params specified for the table method)
|
13
|
+
* thead - Builds the thead section of a table from an array of strings
|
14
|
+
* th - Returns a TH table cell
|
15
|
+
* ths - Returns a string of TH table cells
|
16
|
+
* tbody - Builds the tbody section of a table from an array of arrays of strings (each sub-array becomes a table row)
|
17
|
+
* tr - Returns a TR table row
|
18
|
+
* trs - Returns a string of TR table rows
|
19
|
+
* td - Returns a TD table cell
|
20
|
+
* tds - Returns a string of TD table cells
|
21
|
+
* tfoot - Builds the tfoot section of a table from an array of strings
|
22
|
+
|
23
|
+
Examples:
|
24
|
+
headers = ['a','b','c']
|
25
|
+
footers = ['x','y','z']
|
26
|
+
rows=[['d','e,'f']]
|
27
|
+
table(:id=>'foo', :class=>'bar', :headers=>headers, :footers=>footers, :rows=>rows)
|
28
|
+
=>
|
29
|
+
<table id="foo" class="bar">
|
30
|
+
<thead>
|
31
|
+
<tr>
|
32
|
+
<th>a</th>
|
33
|
+
<th>b</th>
|
34
|
+
<th>c</th>
|
35
|
+
</tr>
|
36
|
+
</thead>
|
37
|
+
<tbody>
|
38
|
+
<tr>
|
39
|
+
<td>d</td>
|
40
|
+
<td>e</td>
|
41
|
+
<td>f</td>
|
42
|
+
</tr>
|
43
|
+
</tbody>
|
44
|
+
<tfoot>
|
45
|
+
<tr>
|
46
|
+
<th>x</th>
|
47
|
+
<th>y</th>
|
48
|
+
<th>z</th>
|
49
|
+
</tr>
|
50
|
+
</tfoot>
|
51
|
+
</table>
|
52
|
+
|
53
|
+
|
54
|
+
== List Methods
|
55
|
+
|
56
|
+
* ul - Builds an HTML unordered list from an array of strings or Enumerables
|
57
|
+
* uls - Builds one or more HTML unordered lists from an array of arrays of strings or Enumerables (sub-arrays can be a mix of both)
|
58
|
+
* li - Returns an LI item
|
59
|
+
* lis - Returns a string of LI items
|
60
|
+
|
61
|
+
Examples:
|
62
|
+
ul(['a','b','c']) =>
|
63
|
+
<ul><li>a</li><li>b</li><li>c</li></ul>
|
64
|
+
|
65
|
+
uls(['a','b','c'],['d','e','f'],[1,2,3]) =>
|
66
|
+
<ul><li>a</li><li>b</li><li>c</li></ul><ul><li>d</li><li>e</li><li>f</li></ul><ul><li>1</li><li>2</li><li>3</li></ul>
|
67
|
+
|
68
|
+
== Misc Methods
|
69
|
+
|
70
|
+
* comment - Return the text parameter wrapped in an HTML comment
|
71
|
+
* wrap - Return the text parameter wrapped in a tag pair
|
72
|
+
|
73
|
+
Examples:
|
74
|
+
comment('the following comes from') =>
|
75
|
+
<!-- the following comes from -->
|
76
|
+
|
77
|
+
wrap('foo', 'bar') =>
|
78
|
+
<bar>foo</bar>
|
79
|
+
|
80
|
+
== Changes
|
81
|
+
|
82
|
+
* 1.0.6 add to gemcutter
|
83
|
+
* 1.0.5 comment & wrap
|
84
|
+
* 1.0.4 tables
|
85
|
+
* 1.0.0 lists
|
15
86
|
|
data/lib/webget_ruby_html.rb
CHANGED
@@ -9,6 +9,7 @@ License:: LGPL, GNU Lesser General Public License
|
|
9
9
|
|
10
10
|
==Changes
|
11
11
|
|
12
|
+
1.1.0 add files for lists, misc, tables
|
12
13
|
1.0.6 add to gemcutter
|
13
14
|
1.0.5 comment & wrap
|
14
15
|
1.0.4 tables
|
@@ -16,327 +17,7 @@ License:: LGPL, GNU Lesser General Public License
|
|
16
17
|
|
17
18
|
=end
|
18
19
|
|
19
|
-
|
20
|
+
['lists','misc','tables'].map{|x|
|
21
|
+
require File.dirname(__FILE__) + "/webget_ruby_html/#{x}.rb"
|
22
|
+
}
|
20
23
|
|
21
|
-
# Return the text wrapped in a comment.
|
22
|
-
#
|
23
|
-
# ==Example
|
24
|
-
# comment('foo') => "<!--foo-->"
|
25
|
-
|
26
|
-
def comment(text)
|
27
|
-
"<!--#{text}-->"
|
28
|
-
end
|
29
|
-
|
30
|
-
# Return the text wrapped in a tag pair.
|
31
|
-
#
|
32
|
-
# ==Example
|
33
|
-
# wrap('foo','<bar>') => "<bar>foo</bar>"
|
34
|
-
#
|
35
|
-
# ==Example: you can omit the tag angle brackets
|
36
|
-
# wrap('foo','bar') => "<bar>foo</bar>"
|
37
|
-
#
|
38
|
-
# ==Example: you can use arbitrary tag attributes
|
39
|
-
# wrap('foo','<bar x="1" y="2">') => "<bar x=1 y=2>foo</bar>"
|
40
|
-
#
|
41
|
-
|
42
|
-
def wrap(text,tag)
|
43
|
-
t=tag
|
44
|
-
t.sub!(/^</,'')
|
45
|
-
t.sub!(/>$/,'')
|
46
|
-
open=t
|
47
|
-
shut=t.split.first
|
48
|
-
"<#{open}>#{text}</#{shut}>"
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
# Options:
|
53
|
-
# :text : the complete text of the table, e.g. <table>text</table>
|
54
|
-
# :headers
|
55
|
-
# :footers
|
56
|
-
# :rows
|
57
|
-
# :class is the table's css class, e.g. :class=>'sortable'
|
58
|
-
# :id is the table's css id, e.g. :id=>'mytable'
|
59
|
-
#
|
60
|
-
# ==Example
|
61
|
-
# headers = ['a','b','c']
|
62
|
-
# footers = ['x','y','z']
|
63
|
-
# rows=[['d','e,'f'],['g','h,'i'],['j','k','l']
|
64
|
-
# table(:id=>'foo', :class=>'bar', :headers=>headers, :footers=>footers, :rows=>rows
|
65
|
-
#
|
66
|
-
# ==Return HTML
|
67
|
-
# <table id="foo" class="bar">
|
68
|
-
# <thead>
|
69
|
-
# <tr>
|
70
|
-
# <th>a</th>
|
71
|
-
# <th>b</th>
|
72
|
-
# <th>c</th>
|
73
|
-
# </tr>
|
74
|
-
# </thead>
|
75
|
-
# <tbody>
|
76
|
-
# <tr>
|
77
|
-
# <td>d</td>
|
78
|
-
# <td>e</td>
|
79
|
-
# <td>f</td>
|
80
|
-
# </tr>
|
81
|
-
# <tr>
|
82
|
-
# <td>g</td>
|
83
|
-
# <td>h</td>
|
84
|
-
# <td>i</td>
|
85
|
-
# </tr>
|
86
|
-
# <tr>
|
87
|
-
# <td>j</td>
|
88
|
-
# <td>k</td>
|
89
|
-
# <td>l</td>
|
90
|
-
# </tr>
|
91
|
-
# </tbody>
|
92
|
-
# <tfoot>
|
93
|
-
# <tr>
|
94
|
-
# <th>
|
95
|
-
# <th>x</th>
|
96
|
-
# <th>y</th>
|
97
|
-
# <th>z</th>
|
98
|
-
# </tr>
|
99
|
-
# </tfoot>
|
100
|
-
# </table>
|
101
|
-
#
|
102
|
-
# ==Special cases
|
103
|
-
#
|
104
|
-
# If headers or row or footers are nil, then this skips them.
|
105
|
-
#
|
106
|
-
# If footers==[] then this method will use a footers array
|
107
|
-
# with blanks that is the same length as the headers array.
|
108
|
-
|
109
|
-
def table(ops={})
|
110
|
-
text=ops[:text]
|
111
|
-
if !text
|
112
|
-
headers=ops[:headers]
|
113
|
-
footers=(defined?(ops[:footers]) ? ((ops[:footers]==true) ? Array.new(headers.size,'') : ops[:footers]) : false)
|
114
|
-
rows=ops[:rows]
|
115
|
-
text=((headers ? thead(headers) : '') + (rows ? tbody(rows) : '') + (footers ? tfoot(footers) : ''))
|
116
|
-
end
|
117
|
-
return "<table#{attrs_to_string(ops,[:id,:class])}>\n" + text + "</table>\n"
|
118
|
-
end
|
119
|
-
|
120
|
-
|
121
|
-
# Options:
|
122
|
-
# :texts
|
123
|
-
#
|
124
|
-
# Return:
|
125
|
-
# <table>
|
126
|
-
# texts[0]
|
127
|
-
# </table>
|
128
|
-
# <table>
|
129
|
-
# texts[0]
|
130
|
-
# </table>
|
131
|
-
# ...
|
132
|
-
|
133
|
-
def tables(ops={})
|
134
|
-
ops[:texts].map{|text| table(ops.merge(:text=>text))}.join
|
135
|
-
end
|
136
|
-
|
137
|
-
|
138
|
-
# Return:
|
139
|
-
# <thead>
|
140
|
-
# <th>header[0]</th>
|
141
|
-
# <th>header[1]</th>
|
142
|
-
# <th>header[2]</th>
|
143
|
-
# </thead>
|
144
|
-
|
145
|
-
def thead(headers)
|
146
|
-
"<thead>\n" + tr(ths(headers)) + "</thead>\n"
|
147
|
-
end
|
148
|
-
|
149
|
-
|
150
|
-
# Return:
|
151
|
-
# <th>header</th>
|
152
|
-
|
153
|
-
def th(header)
|
154
|
-
"<th>" + header.to_s + "</th>\n"
|
155
|
-
end
|
156
|
-
|
157
|
-
|
158
|
-
# Return:
|
159
|
-
# <th>header[0]</th>
|
160
|
-
# <th>header[1]</th>
|
161
|
-
# <th>header[2]</th>
|
162
|
-
|
163
|
-
def ths(headers)
|
164
|
-
headers.map{|x| th(x)}.join
|
165
|
-
end
|
166
|
-
|
167
|
-
|
168
|
-
# Return:
|
169
|
-
# <tbody>
|
170
|
-
# <tr>
|
171
|
-
# <td>row[0][0]</td>
|
172
|
-
# <td>row[0][1]</td>
|
173
|
-
# <td>row[0][2]</td>
|
174
|
-
# </tr>
|
175
|
-
# <tr>
|
176
|
-
# <td>row[1][0]</td>
|
177
|
-
# <td>row[1][1]</td>
|
178
|
-
# <td>row[1][2]</td>
|
179
|
-
# </tr>
|
180
|
-
# <tr>
|
181
|
-
# <td>row[2][0]</td>
|
182
|
-
# <td>row[2][1]</td>
|
183
|
-
# <td>row[2][2]</td>
|
184
|
-
# </tr>
|
185
|
-
# </tbody>
|
186
|
-
|
187
|
-
def tbody(rows)
|
188
|
-
"<tbody>\n" + trs(rows) + "</tbody>\n"
|
189
|
-
end
|
190
|
-
|
191
|
-
|
192
|
-
# Return:
|
193
|
-
# <tr>
|
194
|
-
# row
|
195
|
-
# </tr>
|
196
|
-
#
|
197
|
-
# Return when row is enumerable:
|
198
|
-
# <tr>
|
199
|
-
# <td>row[0]</td>
|
200
|
-
# <td>row[1]</td>
|
201
|
-
# <td>row[2]</td>
|
202
|
-
# </tr>
|
203
|
-
|
204
|
-
def tr(row)
|
205
|
-
"<tr>\n" + (row.is_a?(String) ? row : row.is_a?(Enumerable) ? tds(row) : row.to_s) + "</tr>\n"
|
206
|
-
end
|
207
|
-
|
208
|
-
|
209
|
-
# Return:
|
210
|
-
# <tr>
|
211
|
-
# <td>row[0][0]</td>
|
212
|
-
# <td>row[0][1]</td>
|
213
|
-
# <td>row[0][2]</td>
|
214
|
-
# </tr>
|
215
|
-
# <tr>
|
216
|
-
# <td>row[1][0]</td>
|
217
|
-
# <td>row[1][1]</td>
|
218
|
-
# <td>row[1][2]</td>
|
219
|
-
# </tr>
|
220
|
-
# <tr>
|
221
|
-
# <td>row[2][0]</td>
|
222
|
-
# <td>row[2][1]</td>
|
223
|
-
# <td>row[2][2]</td>
|
224
|
-
# </tr>
|
225
|
-
|
226
|
-
def trs(rows)
|
227
|
-
rows.map{|x| tr(x)}.join
|
228
|
-
end
|
229
|
-
|
230
|
-
|
231
|
-
# Return:
|
232
|
-
# <td>cell</td>
|
233
|
-
|
234
|
-
def td(cell)
|
235
|
-
"<td>" + cell.to_s + "</td>\n"
|
236
|
-
end
|
237
|
-
|
238
|
-
|
239
|
-
# Return:
|
240
|
-
# <td>cells[0]</td>
|
241
|
-
# <td>cells[1]</td>
|
242
|
-
# <td>cells[2]</td>
|
243
|
-
|
244
|
-
def tds(cells)
|
245
|
-
cells.map{|x| td(x)}.join
|
246
|
-
end
|
247
|
-
|
248
|
-
|
249
|
-
# Return:
|
250
|
-
# <tfoot>
|
251
|
-
# <th>
|
252
|
-
# <th>footer[0]</th>
|
253
|
-
# <th>footer[1]</th>
|
254
|
-
# <th>footer[2]</th>
|
255
|
-
# </tfoot>
|
256
|
-
|
257
|
-
def tfoot(footers)
|
258
|
-
"<tfoot>\n" + tr(ths(footers)) + "</tfoot>\n"
|
259
|
-
end
|
260
|
-
|
261
|
-
|
262
|
-
# Return:
|
263
|
-
# <ul>
|
264
|
-
# list
|
265
|
-
# </ul>
|
266
|
-
#
|
267
|
-
# Return when list is enumerable:
|
268
|
-
# <ul>
|
269
|
-
# <li>list[1]</li>
|
270
|
-
# <li>list[2]</li>
|
271
|
-
# <li>list[3]</li>
|
272
|
-
# </ul>
|
273
|
-
|
274
|
-
def ul(list)
|
275
|
-
"<ul>\n" + (list.is_a?(String) ? list : list.is_a?(Enumerable) ? lis(list) : list.to_s) + "</ul>\n"
|
276
|
-
end
|
277
|
-
|
278
|
-
|
279
|
-
# Return:
|
280
|
-
# <ul>
|
281
|
-
# <li>list[0][1]</li>
|
282
|
-
# <li>list[0][2]</li>
|
283
|
-
# <li>list[0][3]</li>
|
284
|
-
# </ul>
|
285
|
-
# <ul>
|
286
|
-
# <li>list[1][1]</li>
|
287
|
-
# <li>list[1][2]</li>
|
288
|
-
# <li>list[1][3]</li>
|
289
|
-
# </ul>
|
290
|
-
# <ul>
|
291
|
-
# <li>list[2][1]</li>
|
292
|
-
# <li>list[2][2]</li>
|
293
|
-
# <li>list[2][3]</li>
|
294
|
-
# </ul>
|
295
|
-
|
296
|
-
def uls(lists)
|
297
|
-
lists.map{|x| ul(x)}.join
|
298
|
-
end
|
299
|
-
|
300
|
-
|
301
|
-
# Return:
|
302
|
-
# <li>item</li>
|
303
|
-
|
304
|
-
def li(item)
|
305
|
-
"<li>" + item.to_s + "</li>\n"
|
306
|
-
end
|
307
|
-
|
308
|
-
|
309
|
-
# Return:
|
310
|
-
# <li>item[0]</li>
|
311
|
-
# <li>item[1]</li>
|
312
|
-
# <li>item[2]</li>
|
313
|
-
|
314
|
-
def lis(items)
|
315
|
-
items.map{|x| li(x)}.join
|
316
|
-
end
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
protected
|
321
|
-
|
322
|
-
# There's likely a better more-standard way to do this.
|
323
|
-
#
|
324
|
-
# This method is only used by the #table method.
|
325
|
-
#
|
326
|
-
# Return a string of the attributes suitable for HTML
|
327
|
-
#
|
328
|
-
# ==Example
|
329
|
-
# hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
|
330
|
-
# attrs(hash) => ' foo="bar" goo="car" hoo="dar"'
|
331
|
-
#
|
332
|
-
# ==Example with selected keys
|
333
|
-
# hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
|
334
|
-
# attrs(hash,[:foo,:hoo]) => ' foo="bar" hoo="dar"'
|
335
|
-
|
336
|
-
def attrs_to_string(ops,keys=nil)
|
337
|
-
return '' if !ops or ops=={}
|
338
|
-
keys||=ops.keys
|
339
|
-
return keys.inject(''){|s,k| s = (ops[k] ? s+=" #{k}=\"#{ops[k]}\"" : s)}
|
340
|
-
end
|
341
|
-
|
342
|
-
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
|
2
|
+
module HTML
|
3
|
+
|
4
|
+
# Return:
|
5
|
+
# <ul>
|
6
|
+
# list
|
7
|
+
# </ul>
|
8
|
+
#
|
9
|
+
# Return when list is enumerable:
|
10
|
+
# <ul>
|
11
|
+
# <li>list[1]</li>
|
12
|
+
# <li>list[2]</li>
|
13
|
+
# <li>list[3]</li>
|
14
|
+
# </ul>
|
15
|
+
|
16
|
+
def ul(list)
|
17
|
+
"<ul>\n" + (list.is_a?(String) ? list : list.is_a?(Enumerable) ? lis(list) : list.to_s) + "</ul>\n"
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
# Return:
|
22
|
+
# <ul>
|
23
|
+
# <li>list[0][1]</li>
|
24
|
+
# <li>list[0][2]</li>
|
25
|
+
# <li>list[0][3]</li>
|
26
|
+
# </ul>
|
27
|
+
# <ul>
|
28
|
+
# <li>list[1][1]</li>
|
29
|
+
# <li>list[1][2]</li>
|
30
|
+
# <li>list[1][3]</li>
|
31
|
+
# </ul>
|
32
|
+
# <ul>
|
33
|
+
# <li>list[2][1]</li>
|
34
|
+
# <li>list[2][2]</li>
|
35
|
+
# <li>list[2][3]</li>
|
36
|
+
# </ul>
|
37
|
+
|
38
|
+
def uls(lists)
|
39
|
+
lists.map{|x| ul(x)}.join
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
# Return:
|
44
|
+
# <li>item</li>
|
45
|
+
|
46
|
+
def li(item)
|
47
|
+
"<li>" + item.to_s + "</li>\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
# Return:
|
52
|
+
# <li>item[0]</li>
|
53
|
+
# <li>item[1]</li>
|
54
|
+
# <li>item[2]</li>
|
55
|
+
|
56
|
+
def lis(items)
|
57
|
+
items.map{|x| li(x)}.join
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module HTML
|
4
|
+
|
5
|
+
# Return the text wrapped in a comment.
|
6
|
+
#
|
7
|
+
# ==Example
|
8
|
+
# comment('foo') => "<!--foo-->"
|
9
|
+
|
10
|
+
def comment(text)
|
11
|
+
"<!--#{text}-->"
|
12
|
+
end
|
13
|
+
|
14
|
+
# Return the text wrapped in a tag pair.
|
15
|
+
#
|
16
|
+
# ==Example
|
17
|
+
# wrap('foo','<bar>') => "<bar>foo</bar>"
|
18
|
+
#
|
19
|
+
# ==Example: you can omit the tag angle brackets
|
20
|
+
# wrap('foo','bar') => "<bar>foo</bar>"
|
21
|
+
#
|
22
|
+
# ==Example: you can use arbitrary tag attributes
|
23
|
+
# wrap('foo','<bar x="1" y="2">') => "<bar x=1 y=2>foo</bar>"
|
24
|
+
#
|
25
|
+
|
26
|
+
def wrap(text,tag)
|
27
|
+
t=tag
|
28
|
+
t.sub!(/^</,'')
|
29
|
+
t.sub!(/>$/,'')
|
30
|
+
open=t
|
31
|
+
shut=t.split.first
|
32
|
+
"<#{open}>#{text}</#{shut}>"
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# There's likely a better more-standard way to do this.
|
37
|
+
#
|
38
|
+
# This method is only used by the #table method.
|
39
|
+
#
|
40
|
+
# Return a string of the attributes suitable for HTML
|
41
|
+
#
|
42
|
+
# ==Example
|
43
|
+
# hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
|
44
|
+
# attrs(hash) => ' foo="bar" goo="car" hoo="dar"'
|
45
|
+
#
|
46
|
+
# ==Example with selected keys
|
47
|
+
# hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
|
48
|
+
# attrs(hash,[:foo,:hoo]) => ' foo="bar" hoo="dar"'
|
49
|
+
|
50
|
+
def attrs_to_string(ops,keys=nil)
|
51
|
+
return '' if !ops or ops=={}
|
52
|
+
keys||=ops.keys
|
53
|
+
return keys.inject(''){|s,k| s = (ops[k] ? s+=" #{k}=\"#{ops[k]}\"" : s)}
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,215 @@
|
|
1
|
+
|
2
|
+
module HTML
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
# Options:
|
7
|
+
# :text : the complete text of the table, e.g. <table>text</table>
|
8
|
+
# :headers
|
9
|
+
# :footers
|
10
|
+
# :rows
|
11
|
+
# :class is the table's css class, e.g. :class=>'sortable'
|
12
|
+
# :id is the table's css id, e.g. :id=>'mytable'
|
13
|
+
#
|
14
|
+
# ==Example
|
15
|
+
# headers = ['a','b','c']
|
16
|
+
# footers = ['x','y','z']
|
17
|
+
# rows=[['d','e,'f'],['g','h,'i'],['j','k','l']]
|
18
|
+
# table(:id=>'foo', :class=>'bar', :headers=>headers, :footers=>footers, :rows=>rows)
|
19
|
+
#
|
20
|
+
# ==Return HTML
|
21
|
+
# <table id="foo" class="bar">
|
22
|
+
# <thead>
|
23
|
+
# <tr>
|
24
|
+
# <th>a</th>
|
25
|
+
# <th>b</th>
|
26
|
+
# <th>c</th>
|
27
|
+
# </tr>
|
28
|
+
# </thead>
|
29
|
+
# <tbody>
|
30
|
+
# <tr>
|
31
|
+
# <td>d</td>
|
32
|
+
# <td>e</td>
|
33
|
+
# <td>f</td>
|
34
|
+
# </tr>
|
35
|
+
# <tr>
|
36
|
+
# <td>g</td>
|
37
|
+
# <td>h</td>
|
38
|
+
# <td>i</td>
|
39
|
+
# </tr>
|
40
|
+
# <tr>
|
41
|
+
# <td>j</td>
|
42
|
+
# <td>k</td>
|
43
|
+
# <td>l</td>
|
44
|
+
# </tr>
|
45
|
+
# </tbody>
|
46
|
+
# <tfoot>
|
47
|
+
# <tr>
|
48
|
+
# <th>x</th>
|
49
|
+
# <th>y</th>
|
50
|
+
# <th>z</th>
|
51
|
+
# </tr>
|
52
|
+
# </tfoot>
|
53
|
+
# </table>
|
54
|
+
#
|
55
|
+
# ==Special cases
|
56
|
+
#
|
57
|
+
# If headers or row or footers are nil, then this skips them.
|
58
|
+
#
|
59
|
+
# If footers==[] then this method will use a footers array
|
60
|
+
# with blanks that is the same length as the headers array.
|
61
|
+
|
62
|
+
def table(ops={})
|
63
|
+
text=ops[:text]
|
64
|
+
if !text
|
65
|
+
headers=ops[:headers]
|
66
|
+
footers=(defined?(ops[:footers]) ? ((ops[:footers]==true) ? Array.new(headers.size,'') : ops[:footers]) : false)
|
67
|
+
rows=ops[:rows]
|
68
|
+
text=((headers ? thead(headers) : '') + (rows ? tbody(rows) : '') + (footers ? tfoot(footers) : ''))
|
69
|
+
end
|
70
|
+
return "<table#{attrs_to_string(ops,[:id,:class])}>\n" + text + "</table>\n"
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
# Options:
|
75
|
+
# :texts
|
76
|
+
#
|
77
|
+
# Return:
|
78
|
+
# <table>
|
79
|
+
# texts[0]
|
80
|
+
# </table>
|
81
|
+
# <table>
|
82
|
+
# texts[0]
|
83
|
+
# </table>
|
84
|
+
# ...
|
85
|
+
|
86
|
+
def tables(ops={})
|
87
|
+
ops[:texts].map{|text| table(ops.merge(:text=>text))}.join
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
# Return:
|
92
|
+
# <thead>
|
93
|
+
# <th>header[0]</th>
|
94
|
+
# <th>header[1]</th>
|
95
|
+
# <th>header[2]</th>
|
96
|
+
# </thead>
|
97
|
+
|
98
|
+
def thead(headers)
|
99
|
+
"<thead>\n" + tr(ths(headers)) + "</thead>\n"
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
# Return:
|
104
|
+
# <th>header</th>
|
105
|
+
|
106
|
+
def th(header)
|
107
|
+
"<th>" + header.to_s + "</th>\n"
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
# Return:
|
112
|
+
# <th>header[0]</th>
|
113
|
+
# <th>header[1]</th>
|
114
|
+
# <th>header[2]</th>
|
115
|
+
|
116
|
+
def ths(headers)
|
117
|
+
headers.map{|x| th(x)}.join
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
# Return:
|
122
|
+
# <tbody>
|
123
|
+
# <tr>
|
124
|
+
# <td>row[0][0]</td>
|
125
|
+
# <td>row[0][1]</td>
|
126
|
+
# <td>row[0][2]</td>
|
127
|
+
# </tr>
|
128
|
+
# <tr>
|
129
|
+
# <td>row[1][0]</td>
|
130
|
+
# <td>row[1][1]</td>
|
131
|
+
# <td>row[1][2]</td>
|
132
|
+
# </tr>
|
133
|
+
# <tr>
|
134
|
+
# <td>row[2][0]</td>
|
135
|
+
# <td>row[2][1]</td>
|
136
|
+
# <td>row[2][2]</td>
|
137
|
+
# </tr>
|
138
|
+
# </tbody>
|
139
|
+
|
140
|
+
def tbody(rows)
|
141
|
+
"<tbody>\n" + trs(rows) + "</tbody>\n"
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
# Return:
|
146
|
+
# <tr>
|
147
|
+
# row
|
148
|
+
# </tr>
|
149
|
+
#
|
150
|
+
# Return when row is enumerable:
|
151
|
+
# <tr>
|
152
|
+
# <td>row[0]</td>
|
153
|
+
# <td>row[1]</td>
|
154
|
+
# <td>row[2]</td>
|
155
|
+
# </tr>
|
156
|
+
|
157
|
+
def tr(row)
|
158
|
+
"<tr>\n" + (row.is_a?(String) ? row : row.is_a?(Enumerable) ? tds(row) : row.to_s) + "</tr>\n"
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
# Return:
|
163
|
+
# <tr>
|
164
|
+
# <td>row[0][0]</td>
|
165
|
+
# <td>row[0][1]</td>
|
166
|
+
# <td>row[0][2]</td>
|
167
|
+
# </tr>
|
168
|
+
# <tr>
|
169
|
+
# <td>row[1][0]</td>
|
170
|
+
# <td>row[1][1]</td>
|
171
|
+
# <td>row[1][2]</td>
|
172
|
+
# </tr>
|
173
|
+
# <tr>
|
174
|
+
# <td>row[2][0]</td>
|
175
|
+
# <td>row[2][1]</td>
|
176
|
+
# <td>row[2][2]</td>
|
177
|
+
# </tr>
|
178
|
+
|
179
|
+
def trs(rows)
|
180
|
+
rows.map{|x| tr(x)}.join
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
# Return:
|
185
|
+
# <td>cell</td>
|
186
|
+
|
187
|
+
def td(cell)
|
188
|
+
"<td>" + cell.to_s + "</td>\n"
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
# Return:
|
193
|
+
# <td>cells[0]</td>
|
194
|
+
# <td>cells[1]</td>
|
195
|
+
# <td>cells[2]</td>
|
196
|
+
|
197
|
+
def tds(cells)
|
198
|
+
cells.map{|x| td(x)}.join
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
# Return:
|
203
|
+
# <tfoot>
|
204
|
+
# <th>
|
205
|
+
# <th>footer[0]</th>
|
206
|
+
# <th>footer[1]</th>
|
207
|
+
# <th>footer[2]</th>
|
208
|
+
# </tfoot>
|
209
|
+
|
210
|
+
def tfoot(footers)
|
211
|
+
"<tfoot>\n" + tr(ths(footers)) + "</tfoot>\n"
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'webget_ruby_html'
|
3
|
+
|
4
|
+
class ListsTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include HTML
|
7
|
+
|
8
|
+
ITEMS=['a','b','c']
|
9
|
+
|
10
|
+
def test_li
|
11
|
+
assert_equal("<li>a</li>\n",li('a'))
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_lis
|
15
|
+
assert_equal("<li>a</li>\n<li>b</li>\n<li>c</li>\n",lis(ITEMS))
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_ul
|
19
|
+
assert_equal("<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n",ul(ITEMS))
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_uls
|
23
|
+
assert_equal("<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n",uls([ITEMS,ITEMS]))
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'webget_ruby_html'
|
3
|
+
|
4
|
+
class MiscTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include HTML
|
7
|
+
|
8
|
+
def test_comment
|
9
|
+
assert_equal("<!--a-->",comment('a'))
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_wrap
|
13
|
+
assert_equal("<b>a</b>",wrap('a','<b>'))
|
14
|
+
assert_equal("<b>a</b>",wrap('a','b'))
|
15
|
+
assert_equal("<b c d e>a</b>",wrap('a','b c d e'))
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
|
@@ -1,41 +1,13 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'webget_ruby_html'
|
3
3
|
|
4
|
-
class
|
4
|
+
class TablesTest < Test::Unit::TestCase
|
5
5
|
|
6
|
-
|
6
|
+
include HTML
|
7
7
|
|
8
|
-
# Mock Inputs
|
9
8
|
CELLS=['a','b','c']
|
10
|
-
ITEMS=['a','b','c']
|
11
9
|
ROWS=[['a','b','c'],['d','e','f'],['g','h','i']]
|
12
10
|
|
13
|
-
def test_comment
|
14
|
-
assert_equal("<!--a-->",comment('a'))
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_wrap
|
18
|
-
assert_equal("<b>a</b>",wrap('a','<b>'))
|
19
|
-
assert_equal("<b>a</b>",wrap('a','b'))
|
20
|
-
assert_equal("<b c d e>a</b>",wrap('a','b c d e'))
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_li
|
24
|
-
assert_equal("<li>a</li>\n",li('a'))
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_lis
|
28
|
-
assert_equal("<li>a</li>\n<li>b</li>\n<li>c</li>\n",lis(ITEMS))
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_ul
|
32
|
-
assert_equal("<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n",ul(ITEMS))
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_uls
|
36
|
-
assert_equal("<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n",uls([ITEMS,ITEMS]))
|
37
|
-
end
|
38
|
-
|
39
11
|
def test_td
|
40
12
|
assert_equal("<td>a</td>\n",td('a'))
|
41
13
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webget_ruby_html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WebGet
|
@@ -32,7 +32,7 @@ cert_chain:
|
|
32
32
|
DXnLFY0cVuBnNDMOOFl8vk1qIcZjcTovhzgcixpG6Uk5qmUsKHRLQf4oQJx7TfLK
|
33
33
|
-----END CERTIFICATE-----
|
34
34
|
|
35
|
-
date: 2010-02-
|
35
|
+
date: 2010-02-20 00:00:00 -08:00
|
36
36
|
default_executable:
|
37
37
|
dependencies: []
|
38
38
|
|
@@ -48,6 +48,13 @@ files:
|
|
48
48
|
- README.rdoc
|
49
49
|
- LICENSE.txt
|
50
50
|
- lib/webget_ruby_html.rb
|
51
|
+
- lib/webget_ruby_html/lists.rb
|
52
|
+
- lib/webget_ruby_html/misc.rb
|
53
|
+
- lib/webget_ruby_html/tables.rb
|
54
|
+
- test/webget_ruby_html.rb
|
55
|
+
- test/webget_ruby_html/lists_test.rb
|
56
|
+
- test/webget_ruby_html/misc_test.rb
|
57
|
+
- test/webget_ruby_html/tables_test.rb
|
51
58
|
has_rdoc: true
|
52
59
|
homepage: http://webget.com/
|
53
60
|
licenses: []
|
@@ -77,4 +84,7 @@ signing_key:
|
|
77
84
|
specification_version: 3
|
78
85
|
summary: "WebGet Ruby Gem: HTML helpers for tables, headers, rows, cells, lists, etc."
|
79
86
|
test_files:
|
80
|
-
- test/
|
87
|
+
- test/webget_ruby_html.rb
|
88
|
+
- test/webget_ruby_html/lists_test.rb
|
89
|
+
- test/webget_ruby_html/misc_test.rb
|
90
|
+
- test/webget_ruby_html/tables_test.rb
|
metadata.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
���v��Y5Yc;:_����ۨ��Wz�m��>�߷�!u-hX:��0���)�}�����*N��B��c���V�����a���ϋ �H���X�V��������-;KF��3��ͬ������{�X
|