Class Prawn::Format::Parser
In: lib/prawn/format/parser.rb
lib/prawn/format/parser.rb
Parent: Object

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.

Methods

eos?   eos?   new   new   next   next   peek   peek   push   push   verbatim?   verbatim?  

Classes and Modules

Class Prawn::Format::Parser::TagError

Attributes

document  [R] 
document  [R] 
state  [R] 
state  [R] 
tags  [R] 
tags  [R] 

Public Class methods

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.

[Source]

    # File lib/prawn/format/parser.rb, line 54
54:       def initialize(document, text, options={})
55:         @document = document
56:         @lexer = Lexer.new(text)
57:         @tags = options[:tags] || {}
58:         @styles = options[:styles] || {}
59: 
60:         @state = State.new(document, :style => options[:style])
61:         @lexer.verbatim = (@state.white_space == :pre)
62: 
63:         @action = :start
64: 
65:         @saved = []
66:         @tag_stack = []
67:       end

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.

[Source]

    # File lib/prawn/format/parser.rb, line 54
54:       def initialize(document, text, options={})
55:         @document = document
56:         @lexer = Lexer.new(text)
57:         @tags = options[:tags] || {}
58:         @styles = options[:styles] || {}
59: 
60:         @state = State.new(document, :style => options[:style])
61:         @lexer.verbatim = (@state.white_space == :pre)
62: 
63:         @action = :start
64: 
65:         @saved = []
66:         @tag_stack = []
67:       end

Public Instance methods

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

[Source]

     # File lib/prawn/format/parser.rb, line 104
104:       def eos?
105:         peek.nil?
106:       end

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

[Source]

     # File lib/prawn/format/parser.rb, line 104
104:       def eos?
105:         peek.nil?
106:       end

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.

[Source]

    # File lib/prawn/format/parser.rb, line 76
76:       def next
77:         return @saved.pop if @saved.any?
78: 
79:         case @action
80:         when :start then start_parse
81:         when :text  then text_parse
82:         else raise "BUG: unknown parser action: #{@action.inspect}"
83:         end
84:       end

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.

[Source]

    # File lib/prawn/format/parser.rb, line 76
76:       def next
77:         return @saved.pop if @saved.any?
78: 
79:         case @action
80:         when :start then start_parse
81:         when :text  then text_parse
82:         else raise "BUG: unknown parser action: #{@action.inspect}"
83:         end
84:       end

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.

[Source]

     # File lib/prawn/format/parser.rb, line 96
 96:       def peek
 97:         save = self.next
 98:         push(save) if save
 99:         return save
100:       end

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.

[Source]

     # File lib/prawn/format/parser.rb, line 96
 96:       def peek
 97:         save = self.next
 98:         push(save) if save
 99:         return save
100:       end

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

[Source]

    # File lib/prawn/format/parser.rb, line 88
88:       def push(instruction)
89:         @saved.push(instruction)
90:       end

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

[Source]

    # File lib/prawn/format/parser.rb, line 88
88:       def push(instruction)
89:         @saved.push(instruction)
90:       end

[Source]

    # File lib/prawn/format/parser.rb, line 69
69:       def verbatim?
70:         @lexer.verbatim
71:       end

[Source]

    # File lib/prawn/format/parser.rb, line 69
69:       def verbatim?
70:         @lexer.verbatim
71:       end

[Validate]