Module: Infoboxer::Parser::Template

Includes:
Tree
Included in:
Infoboxer::Parser
Defined in:
lib/infoboxer/parser/template.rb

Instance Method Summary collapse

Instance Method Details

#sanitize_value(nodes) ⇒ Object



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

def sanitize_value(nodes)
  nodes.pop if (nodes.last.is_a?(Pre) || nodes.last.is_a?(Text)) && nodes.last.text =~ /^\s*$/ # FIXME: dirty!
  nodes
end

#templateObject

NB: here we are not distingish templates like {{Infobox|variable}} and "magic words" like {{formatnum:123}} Just calling all of them "templates". This behaviour will change in future, I presume More about magic words: https://www.mediawiki.org/wiki/Help:Magic_words



13
14
15
16
17
18
19
20
21
22
# File 'lib/infoboxer/parser/template.rb', line 13

def template
  name = @context.scan_continued_until(/\||:|}}/) or
    @context.fail!('Template name not found')

  log "Parsing template #{name}"

  name.strip!
  vars = @context.eat_matched?('}}') ? Nodes[] : template_vars
  @context.traits.templates.find(name).new(name, vars)
end

#template_varsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/infoboxer/parser/template.rb', line 24

def template_vars
  log 'Parsing template variables'

  num = 1
  res = Nodes[]

  guarded_loop do
    @context.next! while @context.eol?
    if @context.check(/\s*([^=}|<]+)\s*=\s*/)
      name = @context.scan(/\s*([^=]+)/).strip
      @context.skip(/\s*=\s*/)
    else
      name = num
      num += 1
    end
    log "Variable #{name} found"

    value = sanitize_value(long_inline(/\||}}/))

    # it was just empty line otherwise
    res << Var.new(name.to_s, value) unless value.empty? && name.is_a?(Numeric)

    log 'Variable value found'

    break if @context.eat_matched?('}}')

    @context.eof? and @context.fail!("Unexpected break of template variables: #{res}")
  end
  res
end