Module: HTMLOutput

Defined in:
lib/html_output.rb

Overview

HTML Output

Author

Joel Parker Henderson, [email protected]

Copyright

Copyright © 2006-2008 Joel Parker Henderson

License

CreativeCommons License, Non-commercial Share Alike

License

LGPL, GNU Lesser General Public License

Instance Method Summary collapse

Instance Method Details

#li(item) ⇒ Object

Return:

<li>item</li>


258
259
260
# File 'lib/html_output.rb', line 258

def li(item)
  "<li>" + item.to_s + "</li>\n"
end

#lis(items) ⇒ Object

Return:

<li>item[0]</li>
<li>item[1]</li>
<li>item[2]</li>


268
269
270
# File 'lib/html_output.rb', line 268

def lis(items)
  items.map{|x| li(x)}.join
end

#table(ops = {}) ⇒ Object

Options:

:text : the complete text of the table, e.g. <table>text</table>
:headers
:footers
:rows
:class is the table's css class, e.g. :class=>'sortable' 
:id is the table's css id, e.g. :id=>'mytable'

Return:

<table id="foo" class="bar">
<thead>
<tr>
<th>header[0]</th>
<th>header[1]</th>
<th>header[2]</th>
</tr>
</thead>
<tbody>
<tr>
<td>row[0][0]</td>
<td>row[0][1]</td>
<td>row[0][2]</td>
</tr>
<tr>
<td>row[1][0]</td>
<td>row[1][1]</td>
<td>row[1][2]</td>
</tr>
<tr>
<td>row[2][0]</td>
<td>row[2][1]</td>
<td>row[2][2]</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>
<th>footer[0]</th>
<th>footer[1]</th>
<th>footer[2]</th>
</tr>
</tfoot>
</table>

Special cases

If headers or row or footers are nil, then this skips them.

If footers==[] then this method will use a footers array with blanks that is the same length as the headers array.



63
64
65
66
67
68
69
70
71
72
# File 'lib/html_output.rb', line 63

def table(ops={})
  text=ops[:text]
  if !text
   headers=ops[:headers]
   footers=(defined?(ops[:footers]) ? ((ops[:footers]==true) ? Array.new(headers.size,'') : ops[:footers]) : false)
   rows=ops[:rows]
   text=((headers ? thead(headers) : '') + (rows ? tbody(rows) : '') + (footers ? tfoot(footers) : ''))
  end
  return "<table#{attrs_to_string(ops,[:id,:class])}>\n" + text + "</table>\n"
end

#tables(ops = {}) ⇒ Object

Options:

:texts

Return:

<table>
texts[0]
</table>
<table>
texts[0]
</table>
...


87
88
89
# File 'lib/html_output.rb', line 87

def tables(ops={})
  ops[:texts].map{|text| table(ops.merge(:text=>text))}.join
end

#tbody(rows) ⇒ Object

Return:

<tbody>
<tr>
<td>row[0][0]</td>
<td>row[0][1]</td>
<td>row[0][2]</td>
</tr>
<tr>
<td>row[1][0]</td>
<td>row[1][1]</td>
<td>row[1][2]</td>
</tr>
<tr>
<td>row[2][0]</td>
<td>row[2][1]</td>
<td>row[2][2]</td>
</tr>
</tbody>


141
142
143
# File 'lib/html_output.rb', line 141

def tbody(rows)
  "<tbody>\n" + trs(rows) + "</tbody>\n"
end

#td(cell) ⇒ Object

Return:

<td>cell</td>


188
189
190
# File 'lib/html_output.rb', line 188

def td(cell)
  "<td>" + cell.to_s + "</td>\n"
end

#tds(cells) ⇒ Object

Return:

<td>cells[0]</td>
<td>cells[1]</td>
<td>cells[2]</td>


198
199
200
# File 'lib/html_output.rb', line 198

def tds(cells)
  cells.map{|x| td(x)}.join
end

#tfoot(footers) ⇒ Object

Return:

<tfoot>
<th>
<th>footer[0]</th>
<th>footer[1]</th>
<th>footer[2]</th>
</tfoot>


211
212
213
# File 'lib/html_output.rb', line 211

def tfoot(footers)
  "<tfoot>\n" + tr(ths(footers)) + "</tfoot>\n"
end

#th(header) ⇒ Object

Return:

<th>header</th>


107
108
109
# File 'lib/html_output.rb', line 107

def th(header)
  "<th>" + header.to_s + "</th>\n"
end

#thead(headers) ⇒ Object

Return:

<thead>
<th>header[0]</th>
<th>header[1]</th>
<th>header[2]</th>
</thead>


99
100
101
# File 'lib/html_output.rb', line 99

def thead(headers)
  "<thead>\n" + tr(ths(headers)) + "</thead>\n"
end

#ths(headers) ⇒ Object

Return:

<th>header[0]</th>
<th>header[1]</th>
<th>header[2]</th>


117
118
119
# File 'lib/html_output.rb', line 117

def ths(headers)
  headers.map{|x| th(x)}.join
end

#tr(row) ⇒ Object

Return:

<tr>
row
</tr>

Return when row is enumerable:

<tr>
<td>row[0]</td>
<td>row[1]</td>
<td>row[2]</td>
</tr>


158
159
160
# File 'lib/html_output.rb', line 158

def tr(row)
  "<tr>\n" + (row.is_a?(String) ? row : row.is_a?(Enumerable) ? tds(row) : row.to_s) + "</tr>\n"
end

#trs(rows) ⇒ Object

Return:

<tr>
<td>row[0][0]</td>
<td>row[0][1]</td>
<td>row[0][2]</td>
</tr>
<tr>
<td>row[1][0]</td>
<td>row[1][1]</td>
<td>row[1][2]</td>
</tr>
<tr>
<td>row[2][0]</td>
<td>row[2][1]</td>
<td>row[2][2]</td>
</tr>


180
181
182
# File 'lib/html_output.rb', line 180

def trs(rows)
  rows.map{|x| tr(x)}.join
end

#ul(list) ⇒ Object

Return:

<ul>
list
</ul>

Return when list is enumerable:

<ul>
<li>list[1]</li>
<li>list[2]</li>
<li>list[3]</li>
</ul>


228
229
230
# File 'lib/html_output.rb', line 228

def ul(list)
  "<ul>\n" + (list.is_a?(String) ? list : list.is_a?(Enumerable) ? lis(list) : list.to_s) + "</ul>\n"
end

#uls(lists) ⇒ Object

Return:

<ul>
<li>list[0][1]</li>
<li>list[0][2]</li>
<li>list[0][3]</li>
</ul>
<ul>
<li>list[1][1]</li>
<li>list[1][2]</li>
<li>list[1][3]</li>
</ul>
<ul>
<li>list[2][1]</li>
<li>list[2][2]</li>
<li>list[2][3]</li>
</ul>


250
251
252
# File 'lib/html_output.rb', line 250

def uls(lists)
 lists.map{|x| ul(x)}.join
end