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.
document | [R] | |
document | [R] | |
state | [R] | |
state | [R] | |
tags | [R] | |
tags | [R] |
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:
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.
# 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:
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.
# 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
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.
# 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.
# 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.
# 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.
# 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