Class: TextFilters

Inherits:
Object
  • Object
show all
Defined in:
lib/text_filters.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, title) ⇒ TextFilters

Returns a new instance of TextFilters.



14
15
16
17
# File 'lib/text_filters.rb', line 14

def initialize(name, title)
  @name = name
  @title = title
end

Class Attribute Details

.registered_filtersObject (readonly)

Returns the value of attribute registered_filters.



36
37
38
# File 'lib/text_filters.rb', line 36

def registered_filters
  @registered_filters
end

.registered_titlesObject (readonly)

Returns the value of attribute registered_titles.



37
38
39
# File 'lib/text_filters.rb', line 37

def registered_titles
  @registered_titles
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/text_filters.rb', line 12

def name
  @name
end

#titleObject (readonly)

Returns the value of attribute title.



13
14
15
# File 'lib/text_filters.rb', line 13

def title
  @title
end

Class Method Details

.[](name) ⇒ Object



63
64
65
# File 'lib/text_filters.rb', line 63

def [](name)
  get_filter(name)
end

.allObject



67
68
69
# File 'lib/text_filters.rb', line 67

def all
  registered_filters
end

.all_titlesObject



71
72
73
# File 'lib/text_filters.rb', line 71

def all_titles
  registered_titles.keys
end

.define(name, title, &block) ⇒ Object

Use this to create and register your TextFilters



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/text_filters.rb', line 40

def define(name, title, &block)
  begin
    p = new(name, title)
    p.instance_eval(&block)
    if p.respond_to? :render_text
      registered_titles[title] = name
      registered_filters[name] = p
    else
      raise "#render_text isn't implemented in this class"
    end
  rescue LoadError
    TextFilters.logger.debug "Filter '#{name}' was not included: #{$!}" unless TextFilters.logger.nil?
  rescue
    TextFilters.logger.debug "Filter '#{name}' was not included: #{$!}" unless TextFilters.logger.nil?
  end
end

.get_filter(name) ⇒ Object



57
58
59
60
61
# File 'lib/text_filters.rb', line 57

def get_filter(name)
  name = TextFilters.default_filter if name.nil?
  name = registered_titles[name] if name.is_a? String
  registered_filters[name]
end

.process_text(text, context = nil, name = nil, processor = nil) ⇒ Object



79
80
81
# File 'lib/text_filters.rb', line 79

def process_text(text, context=nil, name=nil, processor=nil)
  get_filter(name).process_text(text, context, processor)
end

.render_text(text, name = nil) ⇒ Object



75
76
77
# File 'lib/text_filters.rb', line 75

def render_text(text, name=nil)
  get_filter(name).render_text(text)
end

.transform(text, context = nil, name = nil, processor = nil) ⇒ Object

Call



84
85
86
# File 'lib/text_filters.rb', line 84

def transform(text, context=nil, name=nil, processor=nil)
  render_text( process_text(text, context, name, processor), name )
end

Instance Method Details

The default create_link method… Override for your specific filter, if needed, in the #define block



21
22
23
# File 'lib/text_filters.rb', line 21

def create_link(title, url)
  %Q|<a href="#{url}">#{title}</a>|
end

#process_text(text, context, processor = nil) ⇒ Object

Process the text with using the specified context



26
27
28
29
30
31
32
# File 'lib/text_filters.rb', line 26

def process_text(text, context, processor=nil)
  case (processor || TextFilters.default_processor)
  when :erb then process_with_erb(text, context)
  when :liquid then process_with_liquid(text, context)
  else raise "Unknown Text Processor '#{processor.to_s}'"
  end
end