Module: HTML

Defined in:
lib/webget_ruby_html/misc.rb,
lib/webget_ruby_html/lists.rb,
lib/webget_ruby_html/tables.rb

Instance Method Summary collapse

Instance Method Details

#attrs_to_string(ops, keys = nil) ⇒ Object

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"'


50
51
52
53
54
# File 'lib/webget_ruby_html/misc.rb', line 50

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

#comment(text) ⇒ Object

Return the text wrapped in a comment.

Example

comment('foo') => "<!--foo-->"


10
11
12
# File 'lib/webget_ruby_html/misc.rb', line 10

def comment(text)
  "<!--#{text}-->"
end

#li(item) ⇒ Object

Return:

<li>item</li>


46
47
48
# File 'lib/webget_ruby_html/lists.rb', line 46

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>


56
57
58
# File 'lib/webget_ruby_html/lists.rb', line 56

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'

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/webget_ruby_html/tables.rb', line 62

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>
...


86
87
88
# File 'lib/webget_ruby_html/tables.rb', line 86

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>


140
141
142
# File 'lib/webget_ruby_html/tables.rb', line 140

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

#td(cell) ⇒ Object

Return:

<td>cell</td>


187
188
189
# File 'lib/webget_ruby_html/tables.rb', line 187

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>


197
198
199
# File 'lib/webget_ruby_html/tables.rb', line 197

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>


210
211
212
# File 'lib/webget_ruby_html/tables.rb', line 210

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

#th(header) ⇒ Object

Return:

<th>header</th>


106
107
108
# File 'lib/webget_ruby_html/tables.rb', line 106

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>


98
99
100
# File 'lib/webget_ruby_html/tables.rb', line 98

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>


116
117
118
# File 'lib/webget_ruby_html/tables.rb', line 116

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>


157
158
159
# File 'lib/webget_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

#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>


179
180
181
# File 'lib/webget_ruby_html/tables.rb', line 179

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>


16
17
18
# File 'lib/webget_ruby_html/lists.rb', line 16

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>


38
39
40
# File 'lib/webget_ruby_html/lists.rb', line 38

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

#wrap(text, tag) ⇒ Object

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>"


26
27
28
29
30
31
32
33
# File 'lib/webget_ruby_html/misc.rb', line 26

def wrap(text,tag)
  t=tag
  t.sub!(/^</,'')
  t.sub!(/>$/,'')
  open=t
  shut=t.split.first
  "<#{open}>#{text}</#{shut}>"
end