Class: Prawn::Markup::Processor

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Includes:
Blocks, Headings, Images, Inputs, Lists, Tables, Text
Defined in:
lib/prawn/markup/processor.rb

Overview

Processes known HTML tags. Unknown tags are ignored.

Defined Under Namespace

Modules: Blocks, Headings, Images, Inputs, Lists, Tables, Text Classes: Error

Constant Summary

Constants included from Images

Images::ALLOWED_IMAGE_TYPES

Constants included from Inputs

Inputs::DEFAULT_CHECKABLE_CHARS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Text

#end_a, #end_b, #end_i, #end_strikethrough, #end_sub, #end_sup, #end_u, prepended, #start_a, #start_b, #start_i, #start_strikethrough, #start_sub, #start_sup, #start_u

Methods included from Blocks

#end_div, #end_document, #end_p, prepended, #start_br, #start_div, #start_hr, #start_p

Methods included from Headings

#end_heading, prepended, #start_heading

Methods included from Images

prepended, #start_iframe, #start_img

Methods included from Inputs

prepended, #start_input

Methods included from Tables

#end_table, #end_td, prepended, #start_img, #start_table, #start_td, #start_th, #start_tr

Methods included from Lists

#end_li, #end_list, prepended, #start_img, #start_li, #start_list, #start_ol, #start_ul

Constructor Details

#initialize(pdf, options = {}) ⇒ Processor

Returns a new instance of Processor.



41
42
43
44
# File 'lib/prawn/markup/processor.rb', line 41

def initialize(pdf, options = {})
  @pdf = pdf
  @options = options
end

Class Method Details

.known_elementsObject



10
11
12
# File 'lib/prawn/markup/processor.rb', line 10

def known_elements
  @@known_elments ||= []
end

.loggerObject



14
15
16
# File 'lib/prawn/markup/processor.rb', line 14

def logger
  @@logger
end

.logger=(logger) ⇒ Object



18
19
20
# File 'lib/prawn/markup/processor.rb', line 18

def logger=(logger)
  @@logger = logger
end

Instance Method Details

#characters(string) ⇒ Object



66
67
68
69
# File 'lib/prawn/markup/processor.rb', line 66

def characters(string)
  # entities will be replaced again later by inline_format
  append_text(string.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;'))
end

#end_element(name) ⇒ Object



61
62
63
64
# File 'lib/prawn/markup/processor.rb', line 61

def end_element(name)
  send("end_#{name}") if respond_to?("end_#{name}", true)
  stack.pop
end

#error(string) ⇒ Object



71
72
73
# File 'lib/prawn/markup/processor.rb', line 71

def error(string)
  logger.info('SAX parsing error: ' + string.strip) if logger
end

#parse(html) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/prawn/markup/processor.rb', line 46

def parse(html)
  return if html.to_s.strip.empty?

  reset
  html = Prawn::Markup::Normalizer.new(html).normalize
  Nokogiri::HTML::SAX::Parser.new(self).parse(html) { |ctx| ctx.recovery = true }
end

#start_element(name, attrs = []) ⇒ Object



54
55
56
57
58
59
# File 'lib/prawn/markup/processor.rb', line 54

def start_element(name, attrs = [])
  stack.push(name: name, attrs: Hash[attrs])
  if self.class.known_elements.include?(name)
    send("start_#{name}") if respond_to?("start_#{name}", true)
  end
end

#warning(string) ⇒ Object



75
76
77
# File 'lib/prawn/markup/processor.rb', line 75

def warning(string)
  logger.info('SAX parsing warning: ' + string.strip) if logger
end