class WikiThat::Parser
Parsers are disposable objects for translate a Mediawiki document to an Element
tree @author Bryan T. Meyers
Attributes
All of the errors generated while parsing
The output of the translation to HTML
All of the errors generated while parsing
Public Class Methods
Create a new WikiThat::Parser
@param [String] doc the MediaWiki document @param [String] base_url the base URL for relative links @param [String] default_namespace the default namespace for relative links @param [String] sub_url the sub URL for relative links @param [String] media_base the base URL for media sources
@returns [WikiThat::Parser] a newly configured Parser
# File lib/wiki-that/parser/parser.rb, line 57 def initialize(doc, base_url, default_namespace, sub_url, media_base) @lexer = WikiThat::Lexer.new(doc) @base_url = base_url ? base_url.strip : base_url @default_namespace = default_namespace ? default_namespace.strip : default_namespace @index = 0 @sub_url = sub_url ? sub_url.strip : sub_url @media_base = media_base ? media_base.strip : media_base @errors = {} @warnings = {} @tokens = [] @result = Element.new(:root) @line = 1 end
Public Instance Methods
Move the parser tape forward @param [Integer] dist how many steps to move forward, default 1
# File lib/wiki-that/parser/parser.rb, line 124 def advance(dist = 1) @index += dist end
Get the character at the pointer @return [String] the character being pointed at
# File lib/wiki-that/parser/parser.rb, line 154 def current @tokens[@index] end
Check if the end of the tape has been reached @returns [Boolean] True if at or beyond the end
# File lib/wiki-that/parser/parser.rb, line 196 def end? @tokens.nil? or @index >= @tokens.length end
Append Error to the error list @param [String] error the error to append
# File lib/wiki-that/parser/parser.rb, line 132 def error(error) unless @errors[@line] @errors[@line] = [] end @errors[@line].push(error) end
Determine if the current character matches any types in a list @param [Array] types a list of types to check @return [Boolean] True iff the current token type is in the list
# File lib/wiki-that/parser/parser.rb, line 163 def match?(types) if end? return false end types.each do |type| if current.type == type return true end end false end
Determine if the current token does not match any types in a list @param [Array] types a list of types to check @return [Boolean] True iff the current token type is not in the list
# File lib/wiki-that/parser/parser.rb, line 180 def not_match?(types) if end? return true end types.each do |type| if current.type == type return false end end true end
Translate the MediaWiki document into an element tree
@returns [Element] the resulting root element
# File lib/wiki-that/parser/parser.rb, line 107 def parse @lexer.lex @tokens = @lexer.result until end? r = parse2 if r.is_a? Array @result.add_children(*r) else @result.add_child(r) end end end
Translate the current Tokens into sero or more elements @param [Boolean] table parsing a table? @returns [Object] the resulting element(s)
# File lib/wiki-that/parser/parser.rb, line 76 def parse2( table = false) if (table and match? [:table_end, :table_data, :table_header, :table_row]) or end? return [] end case current.type when :header_start parse_header when :rule parse_rule when :list_item parse_list when :comment, :tag_open, :tag_close parse_tag when :nowiki, :pre parse_nowiki when :table_start parse_table when :text, :break, :link_start, :format parse_text else warning "Skipping unexpected Token Type: #{current.type}" advance [] end end
Check if the parsing succeeded @returns [Boolean] True iff no errors
# File lib/wiki-that/parser/parser.rb, line 204 def success? @errors.length == 0 end
Append Warning to the warning list @param [String] warn the warning to append
# File lib/wiki-that/parser/parser.rb, line 143 def warning(warn) unless @warnings[@line] @warnings[@line] = [] end @warnings[@line].push(warn) end