Class: TableData::Presenters::HTML

Inherits:
TableData::Presenter show all
Defined in:
lib/tabledata/presenters/html.rb

Instance Attribute Summary

Attributes inherited from TableData::Presenter

#table

Instance Method Summary collapse

Methods inherited from TableData::Presenter

#initialize, present

Constructor Details

This class inherits a constructor from TableData::Presenter

Instance Method Details

#html_footObject



21
22
23
24
25
26
27
28
29
# File 'lib/tabledata/presenters/html.rb', line 21

def html_foot
  <<-EOHTML

</tbody>
    </table>
  </body>
</html>
  EOHTML
end

#html_headObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tabledata/presenters/html.rb', line 9

def html_head
  <<-EOHTML
<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8'>
  </head>
  <body>
    <table>
  EOHTML
end

#html_table_headerObject



31
32
33
34
35
36
37
38
39
# File 'lib/tabledata/presenters/html.rb', line 31

def html_table_header
  if @table.headers?
    "      <thead>\n        <tr>\n"+
      @table.headers.map { |cell|"          <th>#{CGI.escapeHTML(cell)}</th>" }.join("\n")+
      "\n        </tr>\n      </thead>\n"
  else
    ''
  end
end

#string(options = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/tabledata/presenters/html.rb', line 41

def string(options=nil)
  html_head+
    html_table_header+
    "      </body>\n"+
    @table.body.map { |row|
      "        <tr>\n"+row.map { |cell| "          <td>#{CGI.escapeHTML(cell)}</td>" }.join("\n")+"\n        </tr>"
    }.join("\n")+
    html_foot
end

#write(path, options = nil) ⇒ Object



51
52
53
# File 'lib/tabledata/presenters/html.rb', line 51

def write(path, options=nil)
  File.write(path, string, encoding: 'utf-8')
end