Class: TextFilterPlugin::Macro

Inherits:
TextFilterPlugin show all
Defined in:
lib/text_filter_plugin.rb

Direct Known Subclasses

MacroPost, MacroPre

Class Method Summary collapse

Methods inherited from TextFilterPlugin

available_filter_types, available_filters, component_name, config_value, default_config, default_helper_module!, filter_map, filter_type, help_text, inherited, logger, macro_filters, reloadable?, sanitize, short_name

Methods included from PublifyPlugins

#plugin_description, #plugin_display_name, #plugin_public_action, #plugin_public_actions

Class 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” }



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/text_filter_plugin.rb', line 105

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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/text_filter_plugin.rb', line 121

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 = new_text.gsub(regex2) do |_match|
    macrofilter(attributes_parse(Regexp.last_match[1].to_s), Regexp.last_match[2].to_s)
  end

  new_text
end