Class: SpreadsheetBuilder::HtmlParser
- Inherits:
-
Object
- Object
- SpreadsheetBuilder::HtmlParser
- Defined in:
- lib/spreadsheet_builder/html_parser.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Class Method Summary collapse
Instance Method Summary collapse
- #build(force_level = :none) ⇒ Object
- #css ⇒ Object
-
#initialize(html, *css_paths) ⇒ HtmlParser
constructor
A new instance of HtmlParser.
-
#to_data(force_level = :none) ⇒ Object
TODO clean this up.
Constructor Details
#initialize(html, *css_paths) ⇒ HtmlParser
Returns a new instance of HtmlParser.
17 18 19 20 21 22 |
# File 'lib/spreadsheet_builder/html_parser.rb', line 17 def initialize(html, *css_paths) # TODO merge inline style tags into CssParser @css_load_paths = css_paths @html = html @doc = Nokogiri::HTML(@html) end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
16 17 18 |
# File 'lib/spreadsheet_builder/html_parser.rb', line 16 def doc @doc end |
Class Method Details
.from_erb(file) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/spreadsheet_builder/html_parser.rb', line 8 def self.from_erb(file) html = File.read(file) template = ERB.new(html) html = template.result new(html) end |
.from_slim(file, options = {}, context = self, &block) ⇒ Object
3 4 5 6 |
# File 'lib/spreadsheet_builder/html_parser.rb', line 3 def self.from_slim(file, = {}, context = self, &block) html = Slim::Template.new(file, ).render(context, &block) new(html) end |
Instance Method Details
#build(force_level = :none) ⇒ Object
24 25 26 |
# File 'lib/spreadsheet_builder/html_parser.rb', line 24 def build(force_level = :none) SpreadsheetBuilder.from_data(to_data(force_level)) end |
#css ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/spreadsheet_builder/html_parser.rb', line 28 def css return @css if @css @doc.css('link[rel=stylesheet]').map { |l| href = l["href"].sub(/^\/+/, '') # This will have to be updated later with a host @css_load_paths << "#{href}" } #@css = SpreadsheetBuilder::CssParser.new(@css_load_paths) # Figure out the best way to load this @css = SpreadsheetBuilder::CssParser.new([]) end |
#to_data(force_level = :none) ⇒ Object
TODO clean this up
42 43 44 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/spreadsheet_builder/html_parser.rb', line 42 def to_data(force_level = :none) cells = [] merges = [] col_widths = {} row_heights = {} css.reset(force_level) tb = doc.css('table').first # ignoring specified formats for anything other than table tr td/th tb_format = css.format_from_node(tb) row = 0 doc.css('tr').each do |tr| tr_format = tb_format.merge(@css.format_from_node(tr)) increment = true tr.css('td, th').each_with_index do |td, col| # TODO Do we really need rowheight and colwidth now that there # is css parsing? rowheight = td.attributes["rowheight"] colwidth = td.attributes["colwidth"] rowspan = td.attributes["rowspan"] colspan = td.attributes["colspan"] rowheight &&= rowheight.value.to_i colwidth &&= colwidth.value.to_i rowspan &&= rowspan.value.to_i colspan &&= colspan.value.to_i add_td_to_cells(row, col, td, tr_format, cells) if colspan (1..colspan-1).each {|t| add_td_to_cells(row, col+t, td, tr_format, cells) } end if rowspan (1..rowspan-2).each {|t| add_td_to_cells(row+t, col, td, tr_format, cells) } increment = false end if colspan || rowspan merges << [ row, col, row + (rowspan || 2)-2, col + (colspan || 1)-1 ] end end row += 1 if increment end puts cells.inspect { cells: cells, merges: { 0 => merges } } end |