The Lexer class is used by the formatting subsystem to scan a string and extract tokens from it. The tokens it looks for are either text, XML entities, or XML tags.
Note that the lexer only scans for a subset of XML–it is not a true XML scanner, and understands just enough to provide a basic markup language for use in formatting documents.
The subset includes only XML entities and tags–instructions, comments, and the like are not supported.
Create a new lexer that will scan the given text. The text must be UTF-8 encoded, and must consist of well-formed XML in the subset understand by the lexer.
# File lib/prawn/format/lexer.rb, line 30 def initialize(text) @scanner = StringScanner.new(text) @state = :start @verbatim = false end
Iterates over each token in the string, until the end of the string is reached. Each token is yielded. See next for a discussion of the available token types.
# File lib/prawn/format/lexer.rb, line 64 def each while (token = next_token) yield token end end
Returns the next token from the scanner. If the end of the string has been reached, this will return nil. Otherwise, the token itself is returned as a hash. The hash will always include a :type key, identifying the type of the token. It will be one of :text, :open, or :close.
For :text tokens, the hash will also contain a :text key, which will point to an array of strings. Each element of the array contains either word, whitespace, or some other character at which the line may be broken.
For :open tokens, the hash will contain a :tag key which identifies the name of the tag (as a symbol), and an :options key, which is another hash that contains the options that were given with the tag.
For :close tokens, the hash will contain only a :tag key.
# File lib/prawn/format/lexer.rb, line 53 def next if @state == :start && @scanner.eos? return nil else scan_next_token end end
Generated with the Darkfish Rdoc Generator 2.