lib/infoboxer/parser/context.rb in infoboxer-0.3.3 vs lib/infoboxer/parser/context.rb in infoboxer-0.4.0

- old
+ new

@@ -1,42 +1,45 @@ +# frozen_string_literal: true + require 'strscan' module Infoboxer class Parser class Context # rubocop:disable Metrics/ClassLength attr_reader :lineno attr_reader :traits def initialize(text, traits = nil) @lines = text - .gsub(/<!--.+?-->/m, '') # FIXME: will also kill comments inside <nowiki> tag + .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 attr_reader :next_lines def colno - @scanner && @scanner.pos || 0 + @scanner&.pos || 0 end def matched - @matched ||= @scanner && @scanner.matched + @matched ||= @scanner&.matched end # check which works only once def eat_matched?(str) return false unless matched == str + @matched = 'DUMMY' true end def rest - @rest ||= @scanner && @scanner.rest + @rest ||= @scanner&.rest end alias_method :current, :rest # lines navigation @@ -105,11 +108,11 @@ (!exclude || Regexp.last_match(1) !~ exclude) ) # FIXME: ugly, but no idea of prettier solution end def scan_continued_until(re, leave_pattern = false) - res = '' + res = +'' loop do chunk = _scan_until(re) case matched when re @@ -150,10 +153,11 @@ fail(ParsingError, "#{text} at line #{@lineno}:\n\t#{current}") end def unscan_matched! return unless @matched + @scanner.pos -= @matched.size @rest = nil end private @@ -171,10 +175,10 @@ end def shift(amount) @lineno += amount current = @lines[lineno] - @next_lines = @lines[(lineno + 1)..-1] + @next_lines = @lines[(lineno + 1)..] if current @scanner.string = current @rest = current else @scanner = nil