Class: Infoboxer::Parser::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/infoboxer/parser/context.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, traits = nil) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
17
# File 'lib/infoboxer/parser/context.rb', line 9

def initialize(text, traits = nil)
  @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

Instance Attribute Details

#inline_eol_signObject (readonly)

Returns the value of attribute inline_eol_sign.



98
99
100
# File 'lib/infoboxer/parser/context.rb', line 98

def inline_eol_sign
  @inline_eol_sign
end

#linenoObject (readonly)

Returns the value of attribute lineno.



6
7
8
# File 'lib/infoboxer/parser/context.rb', line 6

def lineno
  @lineno
end

#next_linesObject (readonly)

Returns the value of attribute next_lines.



19
20
21
# File 'lib/infoboxer/parser/context.rb', line 19

def next_lines
  @next_lines
end

#traitsObject (readonly)

Returns the value of attribute traits.



7
8
9
# File 'lib/infoboxer/parser/context.rb', line 7

def traits
  @traits
end

Instance Method Details

#check(re) ⇒ Object



68
69
70
71
72
73
# File 'lib/infoboxer/parser/context.rb', line 68

def check(re)
  res = @scanner.check(re)
  @matched = nil
  @rest = nil
  res
end

#colnoObject



21
22
23
# File 'lib/infoboxer/parser/context.rb', line 21

def colno
  @scanner && @scanner.pos || 0
end

#eat_matched?(str) ⇒ Boolean

check which works only once

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/infoboxer/parser/context.rb', line 30

def eat_matched?(str)
  return false unless matched == str
  @matched = 'DUMMY'
  true
end

#eof?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/infoboxer/parser/context.rb', line 51

def eof?
  !next_lines || # we are after the file end
    next_lines.empty? && eol?
end

#eol?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/infoboxer/parser/context.rb', line 144

def eol?
  !current || current.empty?
end

#fail!(text) ⇒ Object

basic services



149
150
151
# File 'lib/infoboxer/parser/context.rb', line 149

def fail!(text)
  fail(ParsingError, "#{text} at line #{@lineno}:\n\t#{current}")
end

#inline_eol?(exclude = nil) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'lib/infoboxer/parser/context.rb', line 100

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 || Regexp.last_match(1) !~ exclude)
    ) # FIXME: ugly, but no idea of prettier solution
end

#inspectObject



56
57
58
# File 'lib/infoboxer/parser/context.rb', line 56

def inspect
  "#<Context(line #{lineno} of #{@lines.count}: #{current})>"
end

#matchedObject



25
26
27
# File 'lib/infoboxer/parser/context.rb', line 25

def matched
  @matched ||= @scanner && @scanner.matched
end

#matched?(re) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/infoboxer/parser/context.rb', line 140

def matched?(re)
  re && matched =~ re
end

#matched_inline?(re) ⇒ Boolean

state inspection

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
# File 'lib/infoboxer/parser/context.rb', line 130

def matched_inline?(re)
  if re.nil?
    matched.empty? && eol?
  elsif re.inspect.start_with?('/^') # was it REALLY at the beginning of the line?..
    @scanner.pos == matched.length && matched =~ re
  else
    matched =~ re
  end
end

#next!Object

lines navigation



43
44
45
# File 'lib/infoboxer/parser/context.rb', line 43

def next!
  shift(+1)
end

#pop_eol_signObject



94
95
96
# File 'lib/infoboxer/parser/context.rb', line 94

def pop_eol_sign
  @inline_eol_sign = nil
end

#prev!Object



47
48
49
# File 'lib/infoboxer/parser/context.rb', line 47

def prev!
  shift(-1)
end

#push_eol_sign(re) ⇒ Object



90
91
92
# File 'lib/infoboxer/parser/context.rb', line 90

def push_eol_sign(re)
  @inline_eol_sign = re
end

#restObject Also known as: current



36
37
38
# File 'lib/infoboxer/parser/context.rb', line 36

def rest
  @rest ||= @scanner && @scanner.rest
end

#scan(re) ⇒ Object

scanning



61
62
63
64
65
66
# File 'lib/infoboxer/parser/context.rb', line 61

def scan(re)
  res = @scanner.scan(re)
  @matched = nil
  @rest = nil
  res
end

#scan_continued_until(re, leave_pattern = false) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/infoboxer/parser/context.rb', line 109

def scan_continued_until(re, leave_pattern = false)
  res = ''

  loop do
    chunk = _scan_until(re)
    case matched
    when re
      res << chunk
      break
    when nil
      res << rest << "\n"
      next!
      eof? && fail!("Unfinished scan: #{re} not found")
    end
  end

  res[/#{re}\Z/] = '' unless leave_pattern
  res
end

#scan_until(re, leave_pattern = false) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/infoboxer/parser/context.rb', line 82

def scan_until(re, leave_pattern = false)
  guard_eof!

  res = _scan_until(re)
  res[matched] = '' if res && !leave_pattern
  res
end

#skip(re) ⇒ Object



75
76
77
78
79
80
# File 'lib/infoboxer/parser/context.rb', line 75

def skip(re)
  res = @scanner.skip(re)
  @matched = nil
  @rest = nil
  res
end

#unscan_matched!Object



153
154
155
156
157
# File 'lib/infoboxer/parser/context.rb', line 153

def unscan_matched!
  return unless @matched
  @scanner.pos -= @matched.size
  @rest = nil
end