class Prawn::Markup::Processor

Processes known HTML tags. Unknown tags are ignored.

Attributes

bottom_margin[R]
options[R]
pdf[R]
stack[R]
text_buffer[R]

Public Class Methods

known_elements() click to toggle source
# File lib/prawn/markup/processor.rb, line 10
def known_elements
  @@known_elments ||= []
end
logger() click to toggle source
# File lib/prawn/markup/processor.rb, line 14
def logger
  @@logger
end
logger=(logger) click to toggle source
# File lib/prawn/markup/processor.rb, line 18
def logger=(logger)
  @@logger = logger
end
new(pdf, options = {}) click to toggle source
Calls superclass method
# File lib/prawn/markup/processor.rb, line 41
def initialize(pdf, options = {})
  super()
  @pdf = pdf
  @options = options
end

Public Instance Methods

characters(string) click to toggle source
# File lib/prawn/markup/processor.rb, line 65
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) click to toggle source
# File lib/prawn/markup/processor.rb, line 60
def end_element(name)
  send("end_#{name}") if respond_to?("end_#{name}", true)
  stack.pop
end
error(string) click to toggle source
# File lib/prawn/markup/processor.rb, line 70
def error(string)
  logger.info("SAX parsing error: #{string.strip}") if logger
end
parse(html) click to toggle source
# File lib/prawn/markup/processor.rb, line 47
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 = []) click to toggle source
# File lib/prawn/markup/processor.rb, line 55
def start_element(name, attrs = [])
  stack.push(name: name, attrs: attrs.to_h)
  send("start_#{name}") if known_element?(name) && respond_to?("start_#{name}", true)
end
warning(string) click to toggle source
# File lib/prawn/markup/processor.rb, line 74
def warning(string)
  logger.info("SAX parsing warning: #{string.strip}") if logger
end

Private Instance Methods

append_text(string) click to toggle source
# File lib/prawn/markup/processor.rb, line 91
def append_text(string)
  text_buffer.concat(string)
end
buffered_text?() click to toggle source
# File lib/prawn/markup/processor.rb, line 95
def buffered_text?
  !text_buffer.strip.empty?
end
current_attrs() click to toggle source
# File lib/prawn/markup/processor.rb, line 113
def current_attrs
  stack.last[:attrs]
end
dig_options(*keys) click to toggle source
# File lib/prawn/markup/processor.rb, line 145
def dig_options(*keys)
  keys.inject(options) { |opts, key| opts ? opts[key] : nil }
end
dump_text() click to toggle source
# File lib/prawn/markup/processor.rb, line 99
def dump_text
  text = process_text(text_buffer.dup)
  text_buffer.clear
  text
end
inside_container?() click to toggle source
# File lib/prawn/markup/processor.rb, line 109
def inside_container?
  false
end
known_element?(name) click to toggle source
# File lib/prawn/markup/processor.rb, line 87
def known_element?(name)
  self.class.known_elements.include?(name)
end
logger() click to toggle source
# File lib/prawn/markup/processor.rb, line 149
def logger
  self.class.logger
end
placeholder_value(keys, *args) click to toggle source
# File lib/prawn/markup/processor.rb, line 134
def placeholder_value(keys, *args)
  placeholder = dig_options(*keys)
  return if placeholder.nil?

  if placeholder.respond_to?(:call)
    placeholder.call(*args)
  else
    placeholder.to_s
  end
end
process_text(text) click to toggle source
# File lib/prawn/markup/processor.rb, line 117
def process_text(text)
  if options[:text] && options[:text][:preprocessor]
    options[:text][:preprocessor].call(text)
  else
    text
  end
end
put_bottom_margin(value) click to toggle source
# File lib/prawn/markup/processor.rb, line 105
def put_bottom_margin(value)
  @bottom_margin = value
end
reset() click to toggle source
# File lib/prawn/markup/processor.rb, line 82
def reset
  @stack = []
  @text_buffer = +''
end
style_properties() click to toggle source
# File lib/prawn/markup/processor.rb, line 125
def style_properties
  style = current_attrs['style']
  if style
    style.split(';').map { |p| p.split(':', 2).map(&:strip) }.to_h
  else
    {}
  end
end