Module: BatchKit::Helpers::Html
- Included in:
- Defined in:
- lib/batch-kit/helpers/html.rb
Overview
Defines a number of methods to help with generating simple HTML documents
Instance Method Summary collapse
-
#add_table_cells(body, row, cols, cell_type = :td) ⇒ Object
Adds a row of cells to a table.
- #create_head_tag(opts = {}) ⇒ Object
-
#create_html_document(body_text = nil, opts = {}) {|body| ... } ⇒ Object
Creates a new HTML document with a pre-defined set of styles.
-
#create_html_table(body, data, *cols) ⇒ Object
Creates an HTML table from
data
. - #create_style_tag(opts = {}) ⇒ Object
Instance Method Details
#add_table_cells(body, row, cols, cell_type = :td) ⇒ Object
Adds a row of cells to a table.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/batch-kit/helpers/html.rb', line 137 def add_table_cells(body, row, cols, cell_type = :td) cols.each_with_index do |col, i| cls = col[:class] show = col.fetch(:show, true) prefix = col.fetch(:prefix, '') suffix = col.fetch(:suffix, '') next if !show val = case when col[:value] col[:value].call(row) when row.is_a?(Array) row[i] when row.is_a?(Hash) row[col[:name]] when row.respond_to?(col[:name]) row.send(col[:name]) when row.respond_to?(:[]) row[col[:name]] else row end case val when Numeric val = val.with_commas cls = 'right' unless cls when Date, Time, DateTime val = val.strftime('%H:%M:%S') cls = 'right' unless cls end td = %Q{<#{cell_type}#{cls ? " class='#{cls}'" : ''}>#{prefix}#{val}#{suffix}</#{cell_type}>} body << td end end |
#create_head_tag(opts = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/batch-kit/helpers/html.rb', line 34 def create_head_tag(opts = {}) head_tag = <<-EOS.gsub(/^\w+\n$/, '') <head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"> #{opts[:title] ? "<title>#{opts[:title]}</title>" : ''} #{create_style_tag(opts)} </head> EOS end |
#create_html_document(body_text = nil, opts = {}) {|body| ... } ⇒ Object
Creates a new HTML document with a pre-defined set of styles
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/batch-kit/helpers/html.rb', line 13 def create_html_document(body_text = nil, opts = {}) if body_text.is_a?(Hash) opts = body_text body_text = nil end hdr = <<-EOS.gsub(/\s{20}/, '') <html> #{create_head_tag(opts)} <body> EOS body = [hdr] body << body_text if body_text yield body if block_given? body << <<-EOS.gsub(/\s{20}/, '') </body> </html> EOS end |
#create_html_table(body, data, *cols) ⇒ Object
Creates an HTML table from data
.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/batch-kit/helpers/html.rb', line 98 def create_html_table(body, data, *cols) cols.map!{ |col| col.is_a?(Symbol) || col.is_a?(String) ? {name: col.intern} : col } body << "<table>" body << "<thead><tr>" add_table_cells(body, cols.map{ |col| (col[:label] || col[:name]).to_s.titleize }, cols.map{ |col| {show: col.fetch(:show, true)} }, :th) body << "</tr></thead>" body << "<tbody>" data.each do |row| body << "<tr>" add_table_cells(body, row, cols) body << "</tr>" end body << "</tbody>" body << "</table>" end |
#create_style_tag(opts = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/batch-kit/helpers/html.rb', line 45 def create_style_tag(opts = {}) font = opts.fetch(:font, 'Calibri') style_tag = <<-EOS <style> @font-face {font-family: #{font};} h1 {font-family: #{font}; font-size: 16pt;} h2 {font-family: #{font}; font-size: 14pt; margin: 1em 0em .2em;} h3 {font-family: #{font}; font-size: 12pt; margin: 1em 0em .2em;} body {font-family: #{font}; font-size: 11pt;} p {margin: .2em 0em;} table {font-family: #{font}; font-size: 10pt; line-height: 12pt; border-collapse: collapse;} th {background-color: #00205B; color: white; font-size: 11pt; font-weight: bold; text-align: left; border: 1px solid #DDDDFF; padding: 1px 5px;} td {border: 1px solid #DDDDFF; padding: 1px 5px;} .summary {font-size: 13pt;} .red {background-color: white; color: #FF0000;} .amber {background-color: white; color: #FFA500;} .green {background-color: white; color: #33A000;} .blue {background-color: white; color: #0000A0;} .bold {font-weight: bold;} .center {text-align: center;} .right {text-align: right;} .separator {width: 200px; border-bottom: 1px gray solid;} </style> </head> <body> EOS end |