Module: Infoboxer::Parser::Util

Included in:
Infoboxer::Parser
Defined in:
lib/infoboxer/parser/util.rb

Constant Summary collapse

FORMATTING =
%r((
  '''''|'''|''  |     # bold, italic, bold italic
  \[\[          |     # link
  {{            |     # template
  \[[a-z]+://   |     # external link
  <nowiki[^>]*> |     # nowiki
  <ref[^>]*>    |     # reference
  <gallery[^>]*>|     # gallery
  <math>        |     # math
  <                   # HTML tag
))x
INLINE_EOL =
%r[(?=   # if we have ahead... (not scanned, just checked
  </ref>        |     # <ref> closed
  }}
)]x
INLINE_EOL_BRACK =
%r[(?=   # if we have ahead... (not scanned, just checked
  </ref>        |     # <ref> closed
  }}            |     # or template closed
  (?<!\])\](?!\])     # or ext.link closed,
                      # the madness with look-ahead/behind means
                      # "match single bracket but not double"
)]x
INLINE_EOL_BRACK2 =

FIXME: ok, NOW it's officially ridiculous

%r[(?=   # if we have ahead... (not scanned, just checked
  </ref>        |     # <ref> closed
  }}            |     # or template closed
  \]\]                # or int.link closed
)]x

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reObject (readonly)

Returns the value of attribute re.



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

def re
  @re
end

Instance Method Details

#guarded_loopObject



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

def guarded_loop
  loop do
    pos_before = @context.lineno, @context.colno
    yield
    pos_after = @context.lineno, @context.colno
    pos_after == pos_before and
      @context.fail!("Infinite loop on position #{pos_after.last}")
  end
end

#make_regexpsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/infoboxer/parser/util.rb', line 40

def make_regexps
  {
    file_namespace: /(#{@context.traits.file_namespace.join('|')}):/,
    formatting: FORMATTING,
    inline_until_cache: Hash.new { |h, r|
      h[r] = Regexp.union(*[r, FORMATTING, /$/].compact.uniq)
    },
    short_inline_until_cache: Hash.new { |h, r|
      h[r] = Regexp.union(*[r, INLINE_EOL, FORMATTING, /$/].compact.uniq)
    },
    short_inline_until_cache_brackets: Hash.new { |h, r|
      h[r] = Regexp.union(*[r, INLINE_EOL_BRACK, FORMATTING, /$/].compact.uniq)
    },
    short_inline_until_cache_brackets2: Hash.new { |h, r|
      h[r] = Regexp.union(*[r, INLINE_EOL_BRACK2, FORMATTING, /$/].compact.uniq)
    }
  }
end

#parse_params(str) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/infoboxer/parser/util.rb', line 59

def parse_params(str)
  return {} unless str

  scan = StringScanner.new(str)
  params = {}
  loop do
    scan.skip(/\s*/)
    name = scan.scan(/[^ \t=]+/) or break
    scan.skip(/\s*/)
    if scan.peek(1) == '='
      scan.skip(/=\s*/)
      q = scan.scan(/['"]/)
      value = q ? scan.scan_until(/#{q}|$/).sub(q, '') : scan.scan_until(/\s|$/)
      params[name.to_sym] = value
    else
      params[name.to_sym] = name
    end
  end
  params
end