Class: ODFReport::Parser::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/odf-report/parser/default.rb

Overview

Default HTML parser

sample HTML

<p> first paragraph </p> <p> second <strong>paragraph</strong> </p> <blockquote>

<p> first <em>quote paragraph</em> </p>
<p> first quote paragraph </p>
<p> first quote paragraph </p>

</blockquote> <p> third <strong>paragraph</strong> </p>

<p style=“margin: 100px”> fourth paragraph </p> <p style=“margin: 120px”> fifth paragraph </p> <p> sixth <strong>paragraph</strong> </p>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, template_node) ⇒ Default

Returns a new instance of Default.



28
29
30
31
32
33
34
# File 'lib/odf-report/parser/default.rb', line 28

def initialize(text, template_node)
  @text = text
  @paragraphs = []
  @template_node = template_node

  parse
end

Instance Attribute Details

#paragraphsObject

Returns the value of attribute paragraphs.



26
27
28
# File 'lib/odf-report/parser/default.rb', line 26

def paragraphs
  @paragraphs
end

Instance Method Details

#add_paragraph(text, style) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/odf-report/parser/default.rb', line 49

def add_paragraph(text, style)

  node = @template_node.dup

  node['text:style-name'] = style if style
  node.children = text

  @paragraphs << node
end

#parseObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/odf-report/parser/default.rb', line 36

def parse

  xml = @template_node.parse(@text)

  xml.css("p", "h1", "h2").each do |p|

    style = check_style(p)
    text = parse_formatting(p.inner_html)

    add_paragraph(text, style)
  end
end