Class: Prawn::Format::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/format/parser.rb

Overview

The Parser class is used by the formatting subsystem to take the raw tokens from the Lexer class and wrap them in “instructions”, which are then used by the LayoutBuilder to determine how each token should be rendered.

The parser also ensures that tags are opened and closed consistently. It is not forgiving at all–if you forget to close a tag, the parser will raise an exception (TagError).

It will also raise an exception if a tag is encountered with no style definition for it.

Defined Under Namespace

Classes: TagError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, text, options = {}) ⇒ Parser

Creates a new parser associated with the given document, and which will parse the given text. The options may include either of two optional keys:

  • :tags is used to specify the hash of tags and their associated styles. Any tag not specified here will not be recognized by the parser, and will cause an error if it is encountered in text.

  • :styles is used to specify the mapping of style classes to their definitions. The keys should be symbols, and the values should be hashes. The values have the same format as for the :tags map.

  • :style is the default style for any text not otherwise wrapped by tags.

Example:

parser = Parser.new(@pdf, "<b class='ruby'>hello</b>",
    :tags => { :b => { :font_weight => :bold } },
    :styles => { :ruby => { :color => "red" } },
    :style => { :font_family => "Times-Roman" })

See Format::State for a description of the supported style options.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/prawn/format/parser.rb', line 54

def initialize(document, text, options={})
  @document = document
  @lexer = Lexer.new(text)
  @tags = options[:tags] || {}
  @styles = options[:styles] || {}

  @state = State.new(document, :style => options[:style])
  @lexer.verbatim = (@state.white_space == :pre)

  @action = :start

  @saved = []
  @tag_stack = []
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



29
30
31
# File 'lib/prawn/format/parser.rb', line 29

def document
  @document
end

#stateObject (readonly)

Returns the value of attribute state.



31
32
33
# File 'lib/prawn/format/parser.rb', line 31

def state
  @state
end

#tagsObject (readonly)

Returns the value of attribute tags.



30
31
32
# File 'lib/prawn/format/parser.rb', line 30

def tags
  @tags
end

Instance Method Details

#eos?Boolean

Returns true if the end of the stream has been reached. Subsequent calls to peek or next will return nil.

Returns:

  • (Boolean)


104
105
106
# File 'lib/prawn/format/parser.rb', line 104

def eos?
  peek.nil?
end

#nextObject

Returns the next instruction from the stream. If there are no more instructions in the stream (e.g., the end has been encountered), this returns nil.



76
77
78
79
80
81
82
83
84
# File 'lib/prawn/format/parser.rb', line 76

def next
  return @saved.pop if @saved.any?

  case @action
  when :start then start_parse
  when :text  then text_parse
  else raise "BUG: unknown parser action: #{@action.inspect}"
  end
end

#peekObject

This is identical to next, except it does not consume the instruction. This means that peek returns the instruction that will be returned by the next call to next. It is useful for testing the next instruction in the stream without advancing the stream.



96
97
98
99
100
# File 'lib/prawn/format/parser.rb', line 96

def peek
  save = self.next
  push(save) if save
  return save
end

#push(instruction) ⇒ Object

“Ungets” the given instruction. This makes it so the next call to next will return instruction. This is useful for backtracking.



88
89
90
# File 'lib/prawn/format/parser.rb', line 88

def push(instruction)
  @saved.push(instruction)
end

#verbatim?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/prawn/format/parser.rb', line 69

def verbatim?
  @lexer.verbatim
end