Class: PrawnHtml::HtmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn_html/html_parser.rb

Constant Summary collapse

REGEXP_STYLES =
/\s*([^{\s]+)\s*{\s*([^}]*?)\s*}/m.freeze

Instance Method Summary collapse

Constructor Details

#initialize(renderer, ignore_content_tags: %i[script style])) ⇒ HtmlParser

Init the HtmlParser

Parameters:

  • renderer (DocumentRenderer)

    document renderer

  • ignore_content_tags (Array) (defaults to: %i[script style]))

    array of tags (symbols) to skip their contents while preparing the PDF document



13
14
15
16
17
18
19
# File 'lib/prawn_html/html_parser.rb', line 13

def initialize(renderer, ignore_content_tags: %i[script style])
  @processing = false
  @ignore = false
  @ignore_content_tags = 
  @renderer = renderer
  @raw_styles = {}
end

Instance Method Details

#parse_styles(text_styles) ⇒ Object

Parses CSS styles

Parameters:

  • text_styles (String)

    The CSS styles to evaluate



36
37
38
# File 'lib/prawn_html/html_parser.rb', line 36

def parse_styles(text_styles)
  @raw_styles = text_styles.scan(REGEXP_STYLES).to_h
end

#process(html) ⇒ Object

Processes HTML and renders it

Parameters:

  • html (String)

    The HTML content to process



24
25
26
27
28
29
30
31
# File 'lib/prawn_html/html_parser.rb', line 24

def process(html)
  @styles = {}
  @processing = !html.include?('<body')
  @document = Oga.parse_html(html)
  process_styles # apply previously loaded styles
  traverse_nodes(document.children)
  renderer.flush
end