Module: Entable::HtmlBuilder
- Defined in:
- lib/entable/html_builder.rb
Defined Under Namespace
Classes: ContentDefinitionError
Instance Method Summary collapse
- #build_interpreter(spreadsheet_config) ⇒ Object
- #build_line_interpreter(columns, attrs) ⇒ Object
- #build_title_rows(title_rows, attribute_rows) ⇒ Object
- #parse_column_content(str, errors) ⇒ Object
- #parse_column_definitions(conf) ⇒ Object
Instance Method Details
#build_interpreter(spreadsheet_config) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/entable/html_builder.rb', line 75 def build_interpreter spreadsheet_config title_rows, content_rows, attribute_rows, transformer, wrapper = parse_column_definitions(spreadsheet_config) titles = build_title_rows title_rows, attribute_rows titles = to_utf8 "\n#{titles}\n" code = <<CODE include Entable::XlsExport def to_xls items, *args #{transformer ? "items = Entable::Transformer.apply_transform(items, :#{transformer})" : ""} #{wrapper ? "items = Entable::Wrapper.apply_wrapper :#{wrapper}, items, *args" : ""} #{ "#{xls_html_prologue}#{titles}".inspect } + content(items) + #{xls_html_epilogue.inspect} end def lines item "#{content_rows.zip(attribute_rows).map { |row, attrs| "<tr>#{build_line_interpreter row, attrs}</tr>\\n" }.join }" end def content(items) items.map { |item| lines(item) }.join end CODE Class.new do; class_eval code; end.new end |
#build_line_interpreter(columns, attrs) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/entable/html_builder.rb', line 68 def build_line_interpreter columns, attrs columns.zip(attrs).map { |cell, attr| colspan_attr = attr["colspan"] ? " colspan=#{attr["colspan"]}" : "" "<td#{colspan_attr}>\#{to_utf8(#{cell.inspect})}</td>" }.join(' ').gsub("%{", '#{') end |
#build_title_rows(title_rows, attribute_rows) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/entable/html_builder.rb', line 60 def build_title_rows title_rows, attribute_rows titles = title_rows.zip(attribute_rows).map { |tr, attrs| "<tr>" + tr.zip(attrs).map { |t, attr| colspan_attr = attr["colspan"] ? " colspan=#{attr["colspan"]}" : "" "<td#{colspan_attr}>#{quote_for_xls(to_utf8 t)}</td>"}.join(" ") + "</tr>" }.join("\n") end |
#parse_column_content(str, errors) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/entable/html_builder.rb', line 4 def parse_column_content str, errors error = false str = str.strip.gsub(/%\{[^}]*\}/) { |match| attr = match.gsub(/^%\{/, '').gsub(/\}$/, '').gsub(/-/, '_').strip if attr.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/) "%{item.#{attr}}" else errors << "prohibited attribute #{match}" error = true end } raise ContentDefinitionError.new if error str end |
#parse_column_definitions(conf) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/entable/html_builder.rb', line 21 def parse_column_definitions conf title_rows = [] content_rows = [] attribute_rows = [] errors = [] raise "no config: #{conf.inspect}" unless conf.is_a?(Hash) rows = conf["multi-row"] if rows.nil? one_row = conf["columns"] rows = one_row ? [one_row] : [] end rows.each do |columns| titles = [] contents = [] attrs = [] columns.each do |column_def| titles << column_def["title"] attrs << (column_def["attributes"] || { }) begin contents << parse_column_content(column_def["content"], errors) rescue ContentDefinitionError => e end end title_rows << titles content_rows << contents attribute_rows << attrs end raise errors.join("\n") unless errors.empty? preprocess = conf["preprocess"] || { } [title_rows, content_rows, attribute_rows, preprocess["transform"], preprocess["wrap"]] end |