class TextFilterPlugin::Macro

Public Class Methods

attributes_parse(string) click to toggle source

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

# File lib/text_filter_plugin.rb, line 109
def self.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) click to toggle source
# File lib/text_filter_plugin.rb, line 125
def self.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