Module: Infoboxer::Parser::Util

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

Constant Summary collapse

FORMATTING =
/(
  '{2,5}        |     # bold, italic
  \[\[          |     # link
  {{            |     # template
  \[[a-z]+:\/\/ |     # external link
  <nowiki[^>]*> |     # reference
  <ref[^>]*>    |     # nowiki
  <                   # 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.



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

def re
  @re
end

Instance Method Details

#guarded_loopObject



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

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



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

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



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

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(/['"]/)
      if q
        value = scan.scan_until(/#{q}|$/).sub(q, '')
      else
        value = scan.scan_until(/\s|$/)
      end
      params[name.to_sym] = value
    else
      params[name.to_sym] = name
    end
  end
  params
end