lib/infoboxer/parser/context.rb in infoboxer-0.2.7 vs lib/infoboxer/parser/context.rb in infoboxer-0.2.8

- old
+ new

@@ -1,18 +1,19 @@ # encoding: utf-8 + require 'strscan' module Infoboxer class Parser class Context attr_reader :lineno attr_reader :traits def initialize(text, traits = nil) - @lines = text. - gsub(/<!--.+?-->/m, ''). # FIXME: will also kill comments inside <nowiki> tag - split(/[\r\n]/) + @lines = text + .gsub(/<!--.+?-->/m, '') # FIXME: will also kill comments inside <nowiki> tag + .split(/[\r\n]/) @lineno = -1 @traits = traits || MediaWiki::Traits.default @scanner = StringScanner.new('') next! end @@ -80,11 +81,11 @@ res end def scan_until(re, leave_pattern = false) guard_eof! - + res = _scan_until(re) res[matched] = '' if res && !leave_pattern res end @@ -100,18 +101,18 @@ def inline_eol?(exclude = nil) # not using StringScanner#check, as it will change #matched value eol? || ( - (current =~ %r[^(</ref>|}})] || @inline_eol_sign && current =~ @inline_eol_sign) && - (!exclude || $1 !~ exclude) + (current =~ %r[^(</ref>|}})] || @inline_eol_sign && current =~ @inline_eol_sign) && + (!exclude || Regexp.last_match(1) !~ exclude) ) # FIXME: ugly, but no idea of prettier solution end def scan_continued_until(re, leave_pattern = false) res = '' - + loop do chunk = _scan_until(re) case matched when re res << chunk @@ -120,11 +121,11 @@ res << rest << "\n" next! eof? && fail!("Unfinished scan: #{re} not found") end end - + res[/#{re}\Z/] = '' unless leave_pattern res end # state inspection @@ -154,26 +155,24 @@ @rest = nil res end def guard_eof! - #eof? and fail!("End of input reached") - @scanner or fail!("End of input reached") + @scanner or fail!('End of input reached') end def shift(amount) @lineno += amount current = @lines[lineno] - @next_lines = @lines[(lineno+1)..-1] + @next_lines = @lines[(lineno + 1)..-1] if current @scanner.string = current @rest = current - @matched = nil else @scanner = nil @rest = nil - @matched = nil end + @matched = nil end end end end