Class: TextFilters
- Inherits:
-
Object
- Object
- TextFilters
- Defined in:
- lib/text_filters.rb
Class Attribute Summary collapse
-
.registered_filters ⇒ Object
readonly
Returns the value of attribute registered_filters.
-
.registered_titles ⇒ Object
readonly
Returns the value of attribute registered_titles.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
- .[](name) ⇒ Object
- .all ⇒ Object
- .all_titles ⇒ Object
-
.define(name, title, &block) ⇒ Object
Use this to create and register your TextFilters.
- .get_filter(name) ⇒ Object
- .process_text(text, context = nil, name = nil, processor = nil) ⇒ Object
- .render_text(text, name = nil) ⇒ Object
-
.transform(text, context = nil, name = nil, processor = nil) ⇒ Object
Call.
Instance Method Summary collapse
-
#create_link(title, url) ⇒ Object
The default create_link method…
-
#initialize(name, title) ⇒ TextFilters
constructor
A new instance of TextFilters.
-
#process_text(text, context, processor = nil) ⇒ Object
Process the text with using the specified context.
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_filters ⇒ Object (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_titles ⇒ Object (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
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/text_filters.rb', line 12 def name @name end |
#title ⇒ Object (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
65 66 67 |
# File 'lib/text_filters.rb', line 65 def [](name) get_filter(name) end |
.all ⇒ Object
69 70 71 |
# File 'lib/text_filters.rb', line 69 def all registered_filters end |
.all_titles ⇒ Object
73 74 75 |
# File 'lib/text_filters.rb', line 73 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 62 63 |
# 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 filter = registered_filters[name] raise "No filter found for '#{name}' in registered_titles: #{registered_titles.inspect} or registered_filters: #{registered_filters.inspect}" unless filter filter end |
.process_text(text, context = nil, name = nil, processor = nil) ⇒ Object
81 82 83 |
# File 'lib/text_filters.rb', line 81 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
77 78 79 |
# File 'lib/text_filters.rb', line 77 def render_text(text, name=nil) get_filter(name).render_text(text) end |
.transform(text, context = nil, name = nil, processor = nil) ⇒ Object
Call
86 87 88 |
# File 'lib/text_filters.rb', line 86 def transform(text, context=nil, name=nil, processor=nil) render_text( process_text(text, context, name, processor), name ) end |
Instance Method Details
#create_link(title, url) ⇒ Object
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 |