All Files
(50.0%
covered at
0.71
hits/line)
7 files in total.
110 relevant lines.
55 lines covered and
55 lines missed
-
# -*- coding: utf-8 -*-
-
=begin rdoc
-
Please see README
-
=end
-
-
1
['lists','misc','tables'].map{|x|
-
3
require File.dirname(__FILE__) + "/sixarm_ruby_html/#{x}.rb"
-
}
-
-
# -*- coding: utf-8 -*-
-
-
-
1
module HTML
-
-
# Return:
-
# <ul>
-
# list
-
# </ul>
-
#
-
# Return when list is enumerable:
-
# <ul>
-
# <li>list[1]</li>
-
# <li>list[2]</li>
-
# <li>list[3]</li>
-
# </ul>
-
-
1
def ul(list)
-
"<ul>\n" + (list.is_a?(String) ? list : list.is_a?(Enumerable) ? lis(list) : list.to_s) + "</ul>\n"
-
end
-
-
-
# 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>
-
-
1
def uls(lists)
-
lists.map{|x| ul(x)}.join
-
end
-
-
-
# Return:
-
# <li>item</li>
-
-
1
def li(item)
-
"<li>" + item.to_s + "</li>\n"
-
end
-
-
-
# Return:
-
# <li>item[0]</li>
-
# <li>item[1]</li>
-
# <li>item[2]</li>
-
-
1
def lis(items)
-
items.map{|x| li(x)}.join
-
end
-
-
-
end
-
# -*- coding: utf-8 -*-
-
-
-
1
module HTML
-
-
# Return the text wrapped in a comment.
-
#
-
# ==Example
-
# comment('foo') => "<!--foo-->"
-
-
1
def comment(text)
-
"<!--#{text}-->"
-
end
-
-
# 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>"
-
#
-
-
1
def wrap(text,tag)
-
t=tag
-
t.sub!(/^</,'')
-
t.sub!(/>$/,'')
-
open=t
-
shut=t.split.first
-
"<#{open}>#{text}</#{shut}>"
-
end
-
-
-
# 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"'
-
-
1
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
-
-
end
-
# -*- coding: utf-8 -*-
-
-
1
module HTML
-
-
-
# 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.
-
-
1
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
-
-
-
# Options:
-
# :texts
-
#
-
# Return:
-
# <table>
-
# texts[0]
-
# </table>
-
# <table>
-
# texts[0]
-
# </table>
-
# ...
-
-
1
def tables(ops={})
-
ops[:texts].map{|text| table(ops.merge(:text=>text))}.join
-
end
-
-
-
# Return:
-
# <thead>
-
# <th>header[0]</th>
-
# <th>header[1]</th>
-
# <th>header[2]</th>
-
# </thead>
-
-
1
def thead(headers)
-
"<thead>\n" + tr(ths(headers)) + "</thead>\n"
-
end
-
-
-
# Return:
-
# <th>header</th>
-
-
1
def th(header)
-
"<th>" + header.to_s + "</th>\n"
-
end
-
-
-
# Return:
-
# <th>header[0]</th>
-
# <th>header[1]</th>
-
# <th>header[2]</th>
-
-
1
def ths(headers)
-
headers.map{|x| th(x)}.join
-
end
-
-
-
# 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>
-
-
1
def tbody(rows)
-
"<tbody>\n" + trs(rows) + "</tbody>\n"
-
end
-
-
-
# Return:
-
# <tr>
-
# row
-
# </tr>
-
#
-
# Return when row is enumerable:
-
# <tr>
-
# <td>row[0]</td>
-
# <td>row[1]</td>
-
# <td>row[2]</td>
-
# </tr>
-
-
1
def tr(row)
-
"<tr>\n" + (row.is_a?(String) ? row : row.is_a?(Enumerable) ? tds(row) : row.to_s) + "</tr>\n"
-
end
-
-
-
# 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>
-
-
1
def trs(rows)
-
rows.map{|x| tr(x)}.join
-
end
-
-
-
# Return:
-
# <td>cell</td>
-
-
1
def td(cell)
-
"<td>" + cell.to_s + "</td>\n"
-
end
-
-
-
# Return:
-
# <td>cells[0]</td>
-
# <td>cells[1]</td>
-
# <td>cells[2]</td>
-
-
1
def tds(cells)
-
cells.map{|x| td(x)}.join
-
end
-
-
-
# Return:
-
# <tfoot>
-
# <th>
-
# <th>footer[0]</th>
-
# <th>footer[1]</th>
-
# <th>footer[2]</th>
-
# </tfoot>
-
-
1
def tfoot(footers)
-
"<tfoot>\n" + tr(ths(footers)) + "</tfoot>\n"
-
end
-
-
-
end
-
# -*- coding: utf-8 -*-
-
1
require 'minitest/autorun'
-
1
require 'sixarm_ruby_html'
-
-
1
describe HTML do
-
-
1
include HTML
-
-
1
before do
-
ITEM ||= 'a'
-
ITEMS ||= ['a','b','c']
-
end
-
-
1
it "#li" do
-
li(ITEM).must_equal "<li>a</li>\n"
-
end
-
-
1
it "#lis" do
-
lis(ITEMS).must_equal "<li>a</li>\n<li>b</li>\n<li>c</li>\n"
-
end
-
-
1
it "#ul" do
-
ul(ITEMS).must_equal "<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n</ul>\n"
-
end
-
-
1
it "#uls" do
-
uls([ITEMS,ITEMS]).must_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"
-
end
-
-
end
-
-
-
-
# -*- coding: utf-8 -*-
-
1
require 'minitest/autorun'
-
1
require 'sixarm_ruby_html'
-
-
1
describe HTML do
-
-
1
include HTML
-
-
1
it "#comment" do
-
comment('a').must_equal "<!--a-->"
-
end
-
-
1
it "#wrap" do
-
wrap('a','<b>').must_equal "<b>a</b>"
-
wrap('a','b').must_equal "<b>a</b>"
-
wrap('a','b c d e').must_equal "<b c d e>a</b>"
-
end
-
-
end
-
-
-
-
# -*- coding: utf-8 -*-
-
1
require 'minitest/autorun'
-
1
require 'sixarm_ruby_html'
-
-
1
describe HTML do
-
-
1
include HTML
-
-
1
before do
-
CELLS ||= ['a','b','c']
-
ROWS ||= [['a','b','c'],['d','e','f'],['g','h','i']]
-
end
-
-
1
it "#td" do
-
td('a').must_equal "<td>a</td>\n"
-
end
-
-
1
it "#tds" do
-
tds(CELLS).must_equal "<td>a</td>\n<td>b</td>\n<td>c</td>\n"
-
end
-
-
1
it "#tr" do
-
tr(CELLS).must_equal "<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n"
-
end
-
-
1
it "#trs" do
-
trs(ROWS).must_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"
-
end
-
-
1
it "#tbody" do
-
tbody(ROWS).must_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"
-
end
-
-
1
it "#ths" do
-
ths(CELLS).must_equal "<th>a</th>\n<th>b</th>\n<th>c</th>\n"
-
end
-
-
1
it "#thead" do
-
thead(CELLS).must_equal "<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</thead>\n"
-
end
-
-
1
it "#tfoot" do
-
tfoot(CELLS).must_equal "<tfoot>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</tfoot>\n"
-
end
-
-
1
it "#table_text" do
-
table(:text=>'foo').must_equal "<table>\nfoo</table>\n"
-
end
-
-
1
it "#table_text_and_attrs" do
-
table(:id=>'foo',:class=>'bar',:text=>'foo').must_equal "<table id=\"foo\" class=\"bar\">\nfoo</table>\n"
-
end
-
-
1
it "#tables" do
-
tables(:texts=>['foo','bar']).must_equal "<table>\nfoo</table>\n<table>\nbar</table>\n"
-
end
-
-
1
it "#table_headers_body_footers" do
-
table(:headers=>CELLS,:rows=>ROWS,:footers=>CELLS).must_equal \
-
"<table>\n"+
-
"<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</thead>\n"+
-
"<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"+
-
"<tfoot>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</tfoot>\n"+
-
"</table>\n"
-
end
-
-
end
-
-
-