Module: TextFilterPlugin::MacroMethods

Included in:
MacroPost, MacroPre
Defined in:
lib/text_filter_plugin.rb

Instance Method Summary collapse

Instance Method Details

#attributes_parse(string) ⇒ Object

Utility function – hand it a XML string like <a href=“foo” title=“bar”> and it’ll give you back { “href” => “foo”, “title” => “bar” }



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/text_filter_plugin.rb', line 134

def attributes_parse(string)
  attributes = {}

  string.gsub(/([^ =]+="[^"]*")/) do |match|
    key, value = match.split("=", 2)
    attributes[key] = value.delete('"')
  end

  string.gsub(/([^ =]+='[^']*')/) do |match|
    key, value = match.split("=", 2)
    attributes[key] = value.delete("'")
  end

  attributes
end

#filtertext(text) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/text_filter_plugin.rb', line 150

def filtertext(text)
  regex1 = %r{<publify:#{short_name}(?:[ \t][^>]*)?/>}
  regex2 = %r{<publify:#{short_name}([ \t][^>]*)?>(.*?)</publify:#{short_name}>}m

  new_text = text.gsub(regex1) do |match|
    macrofilter(attributes_parse(match))
  end

  new_text.gsub(regex2) do |_match|
    macrofilter(attributes_parse(Regexp.last_match[1].to_s), Regexp.last_match[2].to_s)
  end
end