webget-html_output 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,295 @@
1
+ # = HTML Output
2
+ #
3
+ # Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
4
+ # Copyright:: Copyright (c) 2006-2008 Joel Parker Henderson
5
+ # License:: CreativeCommons License, Non-commercial Share Alike
6
+ # License:: LGPL, GNU Lesser General Public License
7
+ #
8
+ ##
9
+
10
+ module HTMLOutput
11
+
12
+ # Options:
13
+ # :text : the complete text of the table, e.g. <table>text</table>
14
+ # :headers
15
+ # :footers
16
+ # :rows
17
+ # :class is the table's css class, e.g. :class=>'sortable'
18
+ # :id is the table's css id, e.g. :id=>'mytable'
19
+ #
20
+ # Return:
21
+ # <table id="foo" class="bar">
22
+ # <thead>
23
+ # <tr>
24
+ # <th>header[0]</th>
25
+ # <th>header[1]</th>
26
+ # <th>header[2]</th>
27
+ # </tr>
28
+ # </thead>
29
+ # <tbody>
30
+ # <tr>
31
+ # <td>row[0][0]</td>
32
+ # <td>row[0][1]</td>
33
+ # <td>row[0][2]</td>
34
+ # </tr>
35
+ # <tr>
36
+ # <td>row[1][0]</td>
37
+ # <td>row[1][1]</td>
38
+ # <td>row[1][2]</td>
39
+ # </tr>
40
+ # <tr>
41
+ # <td>row[2][0]</td>
42
+ # <td>row[2][1]</td>
43
+ # <td>row[2][2]</td>
44
+ # </tr>
45
+ # </tbody>
46
+ # <tfoot>
47
+ # <tr>
48
+ # <th>
49
+ # <th>footer[0]</th>
50
+ # <th>footer[1]</th>
51
+ # <th>footer[2]</th>
52
+ # </tr>
53
+ # </tfoot>
54
+ # </table>
55
+ #
56
+ # ==Special cases
57
+ #
58
+ # If headers or row or footers are nil, then this skips them.
59
+ #
60
+ # If footers==[] then this method will use a footers array
61
+ # with blanks that is the same length as the headers array.
62
+
63
+ def table(ops={})
64
+ text=ops[:text]
65
+ if !text
66
+ headers=ops[:headers]
67
+ footers=(defined?(ops[:footers]) ? ((ops[:footers]==true) ? Array.new(headers.size,'') : ops[:footers]) : false)
68
+ rows=ops[:rows]
69
+ text=((headers ? thead(headers) : '') + (rows ? tbody(rows) : '') + (footers ? tfoot(footers) : ''))
70
+ end
71
+ return "<table#{attrs_to_string(ops,[:id,:class])}>\n" + text + "</table>\n"
72
+ end
73
+
74
+
75
+ # Options:
76
+ # :texts
77
+ #
78
+ # Return:
79
+ # <table>
80
+ # texts[0]
81
+ # </table>
82
+ # <table>
83
+ # texts[0]
84
+ # </table>
85
+ # ...
86
+
87
+ def tables(ops={})
88
+ ops[:texts].map{|text| table(ops.merge(:text=>text))}.join
89
+ end
90
+
91
+
92
+ # Return:
93
+ # <thead>
94
+ # <th>header[0]</th>
95
+ # <th>header[1]</th>
96
+ # <th>header[2]</th>
97
+ # </thead>
98
+
99
+ def thead(headers)
100
+ "<thead>\n" + tr(ths(headers)) + "</thead>\n"
101
+ end
102
+
103
+
104
+ # Return:
105
+ # <th>header</th>
106
+
107
+ def th(header)
108
+ "<th>" + header.to_s + "</th>\n"
109
+ end
110
+
111
+
112
+ # Return:
113
+ # <th>header[0]</th>
114
+ # <th>header[1]</th>
115
+ # <th>header[2]</th>
116
+
117
+ def ths(headers)
118
+ headers.map{|x| th(x)}.join
119
+ end
120
+
121
+
122
+ # Return:
123
+ # <tbody>
124
+ # <tr>
125
+ # <td>row[0][0]</td>
126
+ # <td>row[0][1]</td>
127
+ # <td>row[0][2]</td>
128
+ # </tr>
129
+ # <tr>
130
+ # <td>row[1][0]</td>
131
+ # <td>row[1][1]</td>
132
+ # <td>row[1][2]</td>
133
+ # </tr>
134
+ # <tr>
135
+ # <td>row[2][0]</td>
136
+ # <td>row[2][1]</td>
137
+ # <td>row[2][2]</td>
138
+ # </tr>
139
+ # </tbody>
140
+
141
+ def tbody(rows)
142
+ "<tbody>\n" + trs(rows) + "</tbody>\n"
143
+ end
144
+
145
+
146
+ # Return:
147
+ # <tr>
148
+ # row
149
+ # </tr>
150
+ #
151
+ # Return when row is enumerable:
152
+ # <tr>
153
+ # <td>row[0]</td>
154
+ # <td>row[1]</td>
155
+ # <td>row[2]</td>
156
+ # </tr>
157
+
158
+ def tr(row)
159
+ "<tr>\n" + (row.is_a?(String) ? row : row.is_a?(Enumerable) ? tds(row) : row.to_s) + "</tr>\n"
160
+ end
161
+
162
+
163
+ # Return:
164
+ # <tr>
165
+ # <td>row[0][0]</td>
166
+ # <td>row[0][1]</td>
167
+ # <td>row[0][2]</td>
168
+ # </tr>
169
+ # <tr>
170
+ # <td>row[1][0]</td>
171
+ # <td>row[1][1]</td>
172
+ # <td>row[1][2]</td>
173
+ # </tr>
174
+ # <tr>
175
+ # <td>row[2][0]</td>
176
+ # <td>row[2][1]</td>
177
+ # <td>row[2][2]</td>
178
+ # </tr>
179
+
180
+ def trs(rows)
181
+ rows.map{|x| tr(x)}.join
182
+ end
183
+
184
+
185
+ # Return:
186
+ # <td>cell</td>
187
+
188
+ def td(cell)
189
+ "<td>" + cell.to_s + "</td>\n"
190
+ end
191
+
192
+
193
+ # Return:
194
+ # <td>cells[0]</td>
195
+ # <td>cells[1]</td>
196
+ # <td>cells[2]</td>
197
+
198
+ def tds(cells)
199
+ cells.map{|x| td(x)}.join
200
+ end
201
+
202
+
203
+ # Return:
204
+ # <tfoot>
205
+ # <th>
206
+ # <th>footer[0]</th>
207
+ # <th>footer[1]</th>
208
+ # <th>footer[2]</th>
209
+ # </tfoot>
210
+
211
+ def tfoot(footers)
212
+ "<tfoot>\n" + tr(ths(footers)) + "</tfoot>\n"
213
+ end
214
+
215
+
216
+ # Return:
217
+ # <ul>
218
+ # list
219
+ # </ul>
220
+ #
221
+ # Return when list is enumerable:
222
+ # <ul>
223
+ # <li>list[1]</li>
224
+ # <li>list[2]</li>
225
+ # <li>list[3]</li>
226
+ # </ul>
227
+
228
+ def ul(list)
229
+ "<ul>\n" + (list.is_a?(String) ? list : list.is_a?(Enumerable) ? lis(list) : list.to_s) + "</ul>\n"
230
+ end
231
+
232
+
233
+ # Return:
234
+ # <ul>
235
+ # <li>list[0][1]</li>
236
+ # <li>list[0][2]</li>
237
+ # <li>list[0][3]</li>
238
+ # </ul>
239
+ # <ul>
240
+ # <li>list[1][1]</li>
241
+ # <li>list[1][2]</li>
242
+ # <li>list[1][3]</li>
243
+ # </ul>
244
+ # <ul>
245
+ # <li>list[2][1]</li>
246
+ # <li>list[2][2]</li>
247
+ # <li>list[2][3]</li>
248
+ # </ul>
249
+
250
+ def uls(lists)
251
+ lists.map{|x| ul(x)}.join
252
+ end
253
+
254
+
255
+ # Return:
256
+ # <li>item</li>
257
+
258
+ def li(item)
259
+ "<li>" + item.to_s + "</li>\n"
260
+ end
261
+
262
+
263
+ # Return:
264
+ # <li>item[0]</li>
265
+ # <li>item[1]</li>
266
+ # <li>item[2]</li>
267
+
268
+ def lis(items)
269
+ items.map{|x| li(x)}.join
270
+ end
271
+
272
+
273
+ protected
274
+
275
+ # There's likely a better more-standard way to do this.
276
+ #
277
+ # This method is only used by the #table method.
278
+ #
279
+ # Return a string of the attributes suitable for HTML
280
+ #
281
+ # ==Example
282
+ # hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
283
+ # attrs(hash) => ' foo="bar" goo="car" hoo="dar"'
284
+ #
285
+ # ==Example with selected keys
286
+ # hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
287
+ # attrs(hash,[:foo,:hoo]) => ' foo="bar" hoo="dar"'
288
+
289
+ def attrs_to_string(ops,keys=nil)
290
+ return '' if !ops or ops=={}
291
+ keys||=ops.keys
292
+ return keys.inject(''){|s,k| s = (ops[k] ? s+=" #{k}=\"#{ops[k]}\"" : s)}
293
+ end
294
+
295
+ end
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ require 'html_output'
3
+
4
+ class HTMLOutputTest < Test::Unit::TestCase
5
+
6
+ # Mock Inputs
7
+ CELLS=['a','b','c']
8
+ ITEMS=['a','b','c']
9
+ ROWS=[['a','b','c'],['d','e','f'],['g','h','i']]
10
+
11
+
12
+ def test_li
13
+ assert_equal("<li>a</li>\n",h.li('a'))
14
+ end
15
+
16
+ def test_lis
17
+ assert_equal("<li>a</li>\n<li>b</li>\n<li>c</li>\n",h.lis(ITEMS))
18
+ end
19
+
20
+ def test_ul
21
+ assert_equal("<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n",h.ul(ITEMS))
22
+ end
23
+
24
+ def test_uls
25
+ 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",h.uls([ITEMS,ITEMS]))
26
+ end
27
+
28
+ def test_td
29
+ assert_equal("<td>a</td>\n",h.td('a'))
30
+ end
31
+
32
+ def test_tds
33
+ assert_equal("<td>a</td>\n<td>b</td>\n<td>c</td>\n",h.tds(CELLS))
34
+ end
35
+
36
+ def test_tr
37
+ assert_equal("<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n",h.tr(CELLS))
38
+ end
39
+
40
+ def test_trs
41
+ assert_equal("<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n<tr>\n<td>d</td>\n<td>e</td>\n<td>f</td>\n</tr>\n<tr>\n<td>g</td>\n<td>h</td>\n<td>i</td>\n</tr>\n",h.trs(ROWS))
42
+ end
43
+
44
+ def test_tbody
45
+ assert_equal("<tbody>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n<tr>\n<td>d</td>\n<td>e</td>\n<td>f</td>\n</tr>\n<tr>\n<td>g</td>\n<td>h</td>\n<td>i</td>\n</tr>\n</tbody>\n",h.tbody(ROWS))
46
+ end
47
+
48
+ def test_ths
49
+ assert_equal("<th>a</th>\n<th>b</th>\n<th>c</th>\n",h.ths(CELLS))
50
+ end
51
+
52
+ def test_thead(headers)
53
+ assert_equal("<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</thead>",h.thead(CELLS))
54
+ end
55
+
56
+ def test_tfoot
57
+ assert_equal("<tfoot>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n<\tr>\n</tfoot>\n",h.tfoot(CELLS))
58
+ end
59
+
60
+ def test_table_text
61
+ assert_equal("<table>\nfoo</table>\n",h.table(:text=>'foo'))
62
+ end
63
+
64
+ def test_table_text_and_attrs
65
+ assert_equal("<table id=\"foo\" class=\"bar\">\nfoo</table>\n",h.table(:id=>'foo',:class=>'bar',:text=>'foo'))
66
+ end
67
+
68
+ def test_tables
69
+ assert_equal("<table>\nfoo</table>\n<table>\nbar</table>\n",h.tables(:texts=>['foo','bar']))
70
+ end
71
+
72
+ def test_table_headers_body_footers
73
+ assert_equal(
74
+ "<table>\n"+
75
+ "<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</thead>\n"+
76
+ "<tbody>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n<tr>\n<td>d</td>\n<td>e</td>\n<td>f</td>\n</tr>\n<tr>\n<td>g</td>\n<td>h</td>\n<td>i</td>\n</tr>\n</tbody>\n"+
77
+ "<tfoot>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</tfoot>\n"+
78
+ "</table>\n",
79
+ h.table(:headers=>CELLS,:rows=>ROWS,:footers=>CELLS)
80
+ )
81
+ end
82
+
83
+ end
84
+
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webget-html_output
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - WebGet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-13 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: webget@webget.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/html_output.rb
26
+ has_rdoc: true
27
+ homepage: http://webget.com/
28
+ post_install_message:
29
+ rdoc_options: []
30
+
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ version:
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 1.2.0
49
+ signing_key:
50
+ specification_version: 2
51
+ summary: HTML output formating of items into tables, headers, rows, cells, lists, etc.
52
+ test_files:
53
+ - test/unit/html_output_test.rb