Class: Infoboxer::Parser::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, traits = nil) ⇒ Context

Returns a new instance of Context.



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

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

#linenoObject (readonly)

Returns the value of attribute lineno.



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

def lineno
  @lineno
end

#next_linesObject (readonly)

Returns the value of attribute next_lines.



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

def next_lines
  @next_lines
end

#traitsObject (readonly)

Returns the value of attribute traits.



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

def traits
  @traits
end

Instance Method Details

#check(re) ⇒ Object



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

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

#colnoObject



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

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

#eat_matched?(str) ⇒ Boolean

check which works only once

Returns:

  • (Boolean)


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

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

#eof?Boolean

Returns:

  • (Boolean)


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

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

#eol?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/infoboxer/parser/context.rb', line 125

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

#fail!(text) ⇒ Object

basic services



130
131
132
# File 'lib/infoboxer/parser/context.rb', line 130

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

#inline_eol?(exclude = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def inline_eol?(exclude = nil)
  # not using StringScanner#check, as it will change #matched value
  eol? ||
    (current =~ %r[^(</ref>|}})] &&
      (!exclude || $1 !~ exclude)) # FIXME: ugly, but no idea of prettier solution
end

#inspectObject



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

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

#matchedObject



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

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

#matched?(re) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/infoboxer/parser/context.rb', line 121

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

#matched_inline?(re) ⇒ Boolean

state inspection

Returns:

  • (Boolean)


117
118
119
# File 'lib/infoboxer/parser/context.rb', line 117

def matched_inline?(re)
  re.nil? ? (matched.empty? && eol?) : matched =~ re
end

#next!Object

lines navigation



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

def next!
  shift(+1)
end

#prev!Object



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

def prev!
  shift(-1)
end

#restObject Also known as: current



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

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

#scan(re) ⇒ Object

scanning



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

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

#scan_continued_until(re, leave_pattern = false) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/infoboxer/parser/context.rb', line 96

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



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

def scan_until(re, leave_pattern = false)
  guard_eof!
  
  res = _scan_until(re)
  res[matched] = '' if res && !leave_pattern
  res
end

#skip(re) ⇒ Object



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

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