Class: Tartan::WikiRule

Inherits:
Hash show all
Defined in:
lib/tartan/wiki_rule.rb

Instance Method Summary collapse

Methods inherited from Hash

#deep_merge, #symbolize

Constructor Details

#initialize(matcher = {}) ⇒ WikiRule

Returns a new instance of WikiRule.



6
7
8
# File 'lib/tartan/wiki_rule.rb', line 6

def initialize(matcher={})
  matcher.each { |(a, v)| self[a] = v }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol) ⇒ Object

define methods to access elements of matcher hash



15
16
17
18
# File 'lib/tartan/wiki_rule.rb', line 15

def method_missing(method_symbol)
  self.class.send(:define_method, method_symbol) {self[method_symbol]}
  self[method_symbol]
end

Instance Method Details

#block(parse_type, text, match_result, wikiparser) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/tartan/wiki_rule.rb', line 130

def block(parse_type, text, match_result, wikiparser)
    return unless match_result

    mid = text

    start_strip = ""
    end_strip = ""
  
    if subparse_match_group
      subparse_index = match_result.sym2index(subparse_match_group)

      last_match = match_result.last_match_index
      start_strip = match_result.pre_sub_match(subparse_index)
      end_strip = match_result.post_sub_match(subparse_index)
      mid = match_result[subparse_index]
    end

    if strip.is_a?(Hash) and strip[:match] and strip[:replace]
      mid = mid.gsub(strip_match, strip_replace)
    end

    # need to evaluate the start_text before the execution of the
    # sub-parse in case any of the wiki helper functions depend on
    # ordering of the text
    start_text = start_mark(parse_type)? 
                  wikiparser.substitute(start_mark(parse_type), 
                                         match_result): 
                  ""
    if subparse_context
        mid = wikiparser.parse_by_context(parse_type, subparse_context, mid)
    end
  
    mid = start_text + mid
  
    if end_mark(parse_type)
      mid += wikiparser.substitute(end_mark(parse_type), match_result)
    end

    if ! has_key?(parse_type)
      mid = start_strip + mid + end_strip
    
      if unstrip
        mid.gsub!(unstrip_match, unstrip_replace)
      end
    end
  
    mid
end

#default_ruleObject



46
47
48
# File 'lib/tartan/wiki_rule.rb', line 46

def default_rule
  self[:default]
end

#end_mark(type) ⇒ Object



56
57
58
59
60
# File 'lib/tartan/wiki_rule.rb', line 56

def end_mark(type)
  return false if self[type].nil? or self[type].class == TrueClass

  self[type][:end_mark]
end

#handle_match(parse_type, parsed, to_be_parsed, match_result, wikiparser) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/tartan/wiki_rule.rb', line 179

def handle_match(parse_type, parsed, to_be_parsed, match_result, wikiparser)
    debug_opt = wikiparser.get_var('debug') 
    if $DEBUG || debug || true == debug_opt || debug_opt == title
        puts "\n%s matches [[%s<<\n%s\n>>%s]]" %
             [title, match_result.pre_match, match_result, 
                match_result.post_match] 
    end

    wikiparser.set_vars(vars_to_set, match_result)
  
    mid = match_result[0]
  
    if replace_regex(parse_type)
      mid = replace(parse_type, match_result, wikiparser)
    elsif true == self[parse_type] or
       start_mark(parse_type) or
       end_mark(parse_type) or
       subparse
     
      mid = block(parse_type, mid, match_result, wikiparser)
    end
  
    if shelve && mid
        mid = wikiparser.shelve(mid)
    end

    if "" != match_result.pre_match
      parsed.add_text(false, match_result.pre_match)
    end
  
    parsed.add_text(true, mid) if "" != mid
  
    if "" != match_result.post_match
      to_be_parsed.add_back_text(false, match_result.post_match)
    end
  
    return parsed, to_be_parsed
end

#has_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'lib/tartan/wiki_rule.rb', line 116

def has_type?(type)
    if String == type.class
        type = type.to_sym
    end

    return self[type]
end

#matchObject



20
21
22
# File 'lib/tartan/wiki_rule.rb', line 20

def match
  self[:match] ? self[:match].to_regexp : nil
end

#matcherObject



10
11
12
# File 'lib/tartan/wiki_rule.rb', line 10

def matcher
  self
end

#parse_element(parse_type, to_be_parsed, wikiparser) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/tartan/wiki_rule.rb', line 218

def parse_element(parse_type, to_be_parsed, wikiparser)
    parsed = ParsedTextList.new

    position = 0
    while element = to_be_parsed.shift
        if element.claimed? then parsed.add(element); next ; end

        match_result = match.match(element)

        unless match_result then parsed.add(element); next; end

        if !position_matches?(parsed, to_be_parsed, match_result)
            parsed.add(element)
            next
        end
      
        parsed, to_be_parsed = handle_match(parse_type, parsed, 
                                            to_be_parsed, 
                                            match_result, wikiparser)
    end

    parsed
end

#placed(position) ⇒ Object



42
43
44
# File 'lib/tartan/wiki_rule.rb', line 42

def placed(position)
  self.delete(position)
end

#position_matches?(parsed, to_be_parsed, match_result) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tartan/wiki_rule.rb', line 98

def position_matches?(parsed, to_be_parsed, match_result)
    return true if !target_position

    pos = parsed.length

    return true if target_position == pos
    return true if :first == target_position && 0 == pos

    if 0 == to_be_parsed.length && match_result.post_match.empty?
        return true if :last == target_position
        return true if :all == target_position && 0 == parsed.length
    else
        return true if :not_last == target_position
    end

    return false
end

#replace(parse_type, match_result, wikiparser) ⇒ Object



124
125
126
127
128
# File 'lib/tartan/wiki_rule.rb', line 124

def replace(parse_type, match_result, wikiparser)
    return unless match_result
  
    wikiparser.substitute(replace_regex(parse_type), match_result)
end

#replace_regex(type) ⇒ Object



62
63
64
65
66
# File 'lib/tartan/wiki_rule.rb', line 62

def replace_regex(type)
  return false if self[type].nil? or self[type].class == TrueClass

  self[type][:replace]
end

#start_mark(type) ⇒ Object



50
51
52
53
54
# File 'lib/tartan/wiki_rule.rb', line 50

def start_mark(type)
  return false if self[type].nil? or self[type].class == TrueClass

  self[type][:start_mark]
end

#strip_matchObject



24
25
26
27
# File 'lib/tartan/wiki_rule.rb', line 24

def strip_match
  strip = self[:strip]
  (strip and strip[:match]) ? strip[:match].to_regexp : nil
end

#strip_replaceObject



34
35
36
# File 'lib/tartan/wiki_rule.rb', line 34

def strip_replace
  self[:strip][:replace]
end

#subparse_contextObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tartan/wiki_rule.rb', line 76

def subparse_context
  return nil if ! self.has_key?(:subparse)

  if self[:subparse].is_a?(Symbol) or self[:subparse].is_a?(String)
    return self[:subparse]
  elsif self[:subparse].has_key?(:context)
    return self[:subparse][:context]
  end

  return nil
end

#subparse_match_groupObject



88
89
90
91
92
93
94
95
96
# File 'lib/tartan/wiki_rule.rb', line 88

def subparse_match_group
  if has_key?(:subparse) and 
     self[:subparse].is_a?(Hash) and 
     self[:subparse].has_key?(:match_group)
    self[:subparse][:match_group]
  else
    nil
  end
end

#target_positionObject



68
69
70
# File 'lib/tartan/wiki_rule.rb', line 68

def target_position
  self[:position]
end

#unstrip_matchObject



29
30
31
32
# File 'lib/tartan/wiki_rule.rb', line 29

def unstrip_match
  unstrip = self[:unstrip]
  (unstrip and unstrip[:match]) ? unstrip[:match].to_regexp : nil
end

#unstrip_replaceObject



38
39
40
# File 'lib/tartan/wiki_rule.rb', line 38

def unstrip_replace
  self[:unstrip][:replace]
end

#vars_to_setObject



72
73
74
# File 'lib/tartan/wiki_rule.rb', line 72

def vars_to_set
    self[:set]
end