Class: Prawn::Markup::Processor

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Includes:
Blocks, Headings, Images, 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, Lists, Tables, Text Classes: Error

Constant Summary

Constants included from Images

Images::ALLOWED_IMAGE_TYPES

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 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.



37
38
39
40
# File 'lib/prawn/markup/processor.rb', line 37

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

Class Method Details

.known_elementsObject



8
9
10
# File 'lib/prawn/markup/processor.rb', line 8

def known_elements
  @@known_elments ||= []
end

.loggerObject



12
13
14
# File 'lib/prawn/markup/processor.rb', line 12

def logger
  @@logger
end

.logger=(logger) ⇒ Object



16
17
18
# File 'lib/prawn/markup/processor.rb', line 16

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

Instance Method Details

#characters(string) ⇒ Object



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

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



57
58
59
60
# File 'lib/prawn/markup/processor.rb', line 57

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

#error(string) ⇒ Object



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

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

#parse(html) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/prawn/markup/processor.rb', line 42

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



50
51
52
53
54
55
# File 'lib/prawn/markup/processor.rb', line 50

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



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

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