Module: HTML
- Defined in:
- lib/sixarm_ruby_html/misc.rb,
lib/sixarm_ruby_html/lists.rb,
lib/sixarm_ruby_html/tables.rb
Instance Method Summary (collapse)
-
- (Object) attrs_to_string(ops, keys = nil)
There's likely a better more-standard way to do this.
-
- (Object) comment(text)
Return the text wrapped in a comment.
-
- (Object) li(item)
Return:.
-
- (Object) lis(items)
Return: .
-
- (Object) table(ops = {})
Options:.
-
- (Object) tables(ops = {})
Options:.
-
- (Object) tbody(rows)
Return:.
-
- (Object) td(cell)
Return:.
-
- (Object) tds(cells)
Return:.
-
- (Object) tfoot(footers)
Return:.
-
- (Object) th(header)
Return:.
-
- (Object) thead(headers)
Return:.
-
- (Object) ths(headers)
Return:.
-
- (Object) tr(row)
Return:.
-
- (Object) trs(rows)
Return:.
-
- (Object) ul(list)
Return:.
-
- (Object) uls(lists)
Return:.
-
- (Object) wrap(text, tag)
Return the text wrapped in a tag pair.
Instance Method Details
- (Object) attrs_to_string(ops, keys = nil)
There's likely a better more-standard way to do this.
This method is only used by the #table method.
Return a string of the attributes suitable for HTML
Example
hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
attrs(hash) => ' foo="bar" goo="car" hoo="dar"'
Example with selected keys
hash={:foo'=>'bar',:goo'=>'car',;hoo=>'dar'}
attrs(hash,[:foo,:hoo]) => ' foo="bar" hoo="dar"'
51 52 53 54 55 |
# File 'lib/sixarm_ruby_html/misc.rb', line 51 def attrs_to_string(ops,keys=nil) return '' if !ops or ops=={} keys||=ops.keys return keys.inject(''){|s,k| s = (ops[k] ? s+=" #{k}=\"#{ops[k]}\"" : s)} end |
- (Object) comment(text)
Return the text wrapped in a comment.
Example
comment('foo') => "<!--foo-->"
11 12 13 |
# File 'lib/sixarm_ruby_html/misc.rb', line 11 def comment(text) "<!--#{text}-->" end |
- (Object) li(item)
Return:
<li>item</li>
48 49 50 |
# File 'lib/sixarm_ruby_html/lists.rb', line 48 def li(item) "<li>" + item.to_s + "</li>\n" end |
- (Object) lis(items)
Return:
<li>item[0]</li>
<li>item[1]</li>
<li>item[2]</li>
58 59 60 |
# File 'lib/sixarm_ruby_html/lists.rb', line 58 def lis(items) items.map{|x| li(x)}.join end |
- (Object) table(ops = {})
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'
Example
headers = ['a','b','c']
footers = ['x','y','z']
rows=[['d','e,'f'],['g','h,'i'],['j','k','l']]
table(:id=>'foo', :class=>'bar', :headers=>headers, :footers=>footers, :rows=>rows)
Return HTML
<table id="foo" class="bar">
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
</thead>
<tbody>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>h</td>
<td>i</td>
</tr>
<tr>
<td>j</td>
<td>k</td>
<td>l</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>x</th>
<th>y</th>
<th>z</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.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sixarm_ruby_html/tables.rb', line 62 def table(ops={}) text=ops[:text] if !text headers=ops[:headers] =(defined?(ops[:footers]) ? ((ops[:footers]==true) ? Array.new(headers.size,'') : ops[:footers]) : false) rows=ops[:rows] text=((headers ? thead(headers) : '') + (rows ? tbody(rows) : '') + ( ? tfoot() : '')) end return "<table#{attrs_to_string(ops,[:id,:class])}>\n" + text + "</table>\n" end |
- (Object) tables(ops = {})
Options:
:texts
Return:
<table>
texts[0]
</table>
<table>
texts[0]
</table>
...
86 87 88 |
# File 'lib/sixarm_ruby_html/tables.rb', line 86 def tables(ops={}) ops[:texts].map{|text| table(ops.merge(:text=>text))}.join end |
- (Object) tbody(rows)
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>
140 141 142 |
# File 'lib/sixarm_ruby_html/tables.rb', line 140 def tbody(rows) "<tbody>\n" + trs(rows) + "</tbody>\n" end |
- (Object) td(cell)
Return:
<td>cell</td>
187 188 189 |
# File 'lib/sixarm_ruby_html/tables.rb', line 187 def td(cell) "<td>" + cell.to_s + "</td>\n" end |
- (Object) tds(cells)
Return:
<td>cells[0]</td>
<td>cells[1]</td>
<td>cells[2]</td>
197 198 199 |
# File 'lib/sixarm_ruby_html/tables.rb', line 197 def tds(cells) cells.map{|x| td(x)}.join end |
- (Object) tfoot(footers)
Return:
<tfoot>
<th>
<th>footer[0]</th>
<th>footer[1]</th>
<th>footer[2]</th>
</tfoot>
210 211 212 |
# File 'lib/sixarm_ruby_html/tables.rb', line 210 def tfoot() "<tfoot>\n" + tr(ths()) + "</tfoot>\n" end |
- (Object) th(header)
Return:
<th>header</th>
106 107 108 |
# File 'lib/sixarm_ruby_html/tables.rb', line 106 def th(header) "<th>" + header.to_s + "</th>\n" end |
- (Object) thead(headers)
Return:
<thead>
<th>header[0]</th>
<th>header[1]</th>
<th>header[2]</th>
</thead>
98 99 100 |
# File 'lib/sixarm_ruby_html/tables.rb', line 98 def thead(headers) "<thead>\n" + tr(ths(headers)) + "</thead>\n" end |
- (Object) ths(headers)
Return:
<th>header[0]</th>
<th>header[1]</th>
<th>header[2]</th>
116 117 118 |
# File 'lib/sixarm_ruby_html/tables.rb', line 116 def ths(headers) headers.map{|x| th(x)}.join end |
- (Object) tr(row)
Return:
<tr>
row
</tr>
Return when row is enumerable:
<tr>
<td>row[0]</td>
<td>row[1]</td>
<td>row[2]</td>
</tr>
157 158 159 |
# File 'lib/sixarm_ruby_html/tables.rb', line 157 def tr(row) "<tr>\n" + (row.is_a?(String) ? row : row.is_a?(Enumerable) ? tds(row) : row.to_s) + "</tr>\n" end |
- (Object) trs(rows)
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>
179 180 181 |
# File 'lib/sixarm_ruby_html/tables.rb', line 179 def trs(rows) rows.map{|x| tr(x)}.join end |
- (Object) ul(list)
Return:
<ul>
list
</ul>
Return when list is enumerable:
<ul>
<li>list[1]</li>
<li>list[2]</li>
<li>list[3]</li>
</ul>
18 19 20 |
# File 'lib/sixarm_ruby_html/lists.rb', line 18 def ul(list) "<ul>\n" + (list.is_a?(String) ? list : list.is_a?(Enumerable) ? lis(list) : list.to_s) + "</ul>\n" end |
- (Object) uls(lists)
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>
40 41 42 |
# File 'lib/sixarm_ruby_html/lists.rb', line 40 def uls(lists) lists.map{|x| ul(x)}.join end |
- (Object) wrap(text, tag)
Return the text wrapped in a tag pair.
Example
wrap('foo','<bar>') => "<bar>foo</bar>"
Example: you can omit the tag angle brackets
wrap('foo','bar') => "<bar>foo</bar>"
Example: you can use arbitrary tag attributes
wrap('foo','<bar x="1" y="2">') => "<bar x=1 y=2>foo</bar>"
27 28 29 30 31 32 33 34 |
# File 'lib/sixarm_ruby_html/misc.rb', line 27 def wrap(text,tag) t=tag t.sub!(/^</,'') t.sub!(/>$/,'') open=t shut=t.split.first "<#{open}>#{text}</#{shut}>" end |