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.



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

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.



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

def inline_eol_sign
  @inline_eol_sign
end

#linenoObject (readonly)

Returns the value of attribute lineno.



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

def lineno
  @lineno
end

#next_linesObject (readonly)

Returns the value of attribute next_lines.



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

def next_lines
  @next_lines
end

#traitsObject (readonly)

Returns the value of attribute traits.



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

def traits
  @traits
end

Instance Method Details

#check(re) ⇒ Object



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

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

#colnoObject



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

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

#eat_matched?(str) ⇒ Boolean

check which works only once

Returns:

  • (Boolean)


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

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

#eof?Boolean

Returns:

  • (Boolean)


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

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

#eol?Boolean

Returns:

  • (Boolean)


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

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

#fail!(text) ⇒ Object

basic services



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

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

#inline_eol?(exclude = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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)
    ) # FIXME: ugly, but no idea of prettier solution
end

#inspectObject



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

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

#matchedObject



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

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

#matched?(re) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/infoboxer/parser/context.rb', line 135

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

#matched_inline?(re) ⇒ Boolean

state inspection

Returns:

  • (Boolean)


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

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

#next!Object

lines navigation



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

def next!
  shift(+1)
end

#pop_eol_signObject



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

def pop_eol_sign
  @inline_eol_sign = nil
end

#prev!Object



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

def prev!
  shift(-1)
end

#push_eol_sign(re) ⇒ Object



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

def push_eol_sign(re)
  @inline_eol_sign = re
end

#restObject Also known as: current



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

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

#scan(re) ⇒ Object

scanning



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

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

#scan_continued_until(re, leave_pattern = false) ⇒ Object



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

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



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

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

#skip(re) ⇒ Object



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

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