Class: Comatose::TextFilters
- Inherits:
-
Object
- Object
- Comatose::TextFilters
- Defined in:
- lib/comatose/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 = TextFilters.default_processor) ⇒ Object
Process the text with using the specified context.
Constructor Details
#initialize(name, title) ⇒ TextFilters
Returns a new instance of TextFilters.
16 17 18 19 |
# File 'lib/comatose/text_filters.rb', line 16 def initialize(name, title) @name = name @title = title end |
Class Attribute Details
.registered_filters ⇒ Object (readonly)
Returns the value of attribute registered_filters.
48 49 50 |
# File 'lib/comatose/text_filters.rb', line 48 def registered_filters @registered_filters end |
.registered_titles ⇒ Object (readonly)
Returns the value of attribute registered_titles.
49 50 51 |
# File 'lib/comatose/text_filters.rb', line 49 def registered_titles @registered_titles end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/comatose/text_filters.rb', line 12 def name @name end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
13 14 15 |
# File 'lib/comatose/text_filters.rb', line 13 def title @title end |
Class Method Details
.[](name) ⇒ Object
78 79 80 |
# File 'lib/comatose/text_filters.rb', line 78 def [](name) get_filter(name) end |
.all ⇒ Object
83 84 85 |
# File 'lib/comatose/text_filters.rb', line 83 def all registered_filters end |
.all_titles ⇒ Object
88 89 90 |
# File 'lib/comatose/text_filters.rb', line 88 def all_titles registered_titles.keys end |
.define(name, title, &block) ⇒ Object
Use this to create and register your TextFilters
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/comatose/text_filters.rb', line 52 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 #Comatose.logger.debug "Filter '#{name}' was not included: #{$!}" unless TextFilters.logger.nil? rescue #Comatose.logger.debug "Filter '#{name}' was not included: #{$!}" unless TextFilters.logger.nil? end end |
.get_filter(name) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/comatose/text_filters.rb', line 70 def get_filter(name) #Comatose.logger.debug "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
98 99 100 |
# File 'lib/comatose/text_filters.rb', line 98 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
93 94 95 |
# File 'lib/comatose/text_filters.rb', line 93 def render_text(text, name=nil) get_filter(name).render_text(text) end |
.transform(text, context = nil, name = nil, processor = nil) ⇒ Object
Call
104 105 106 107 108 109 110 |
# File 'lib/comatose/text_filters.rb', line 104 def transform(text, context=nil, name=nil, processor=nil) #Comatose.logger.debug "transform with text #{text}" #Comatose.logger.debug "transform with context #{context}" #Comatose.logger.debug "transform with name #{name}" #Comatose.logger.debug "transform with processor #{processor}" 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
24 25 26 |
# File 'lib/comatose/text_filters.rb', line 24 def create_link(title, url) %Q|<a href="#{url}">#{title}</a>| end |
#process_text(text, context, processor = TextFilters.default_processor) ⇒ Object
Process the text with using the specified context
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/comatose/text_filters.rb', line 30 def process_text(text, context, processor=TextFilters.default_processor) processed_text = nil #Comatose.logger.debug "text before process: #{text}" case when processor == :erb processed_text = process_with_erb(text, context) when processor == :liquid processed_text = process_with_liquid(text, context) else raise "Unknown Text Processor '#{processor.to_s}'" end #Comatose.logger.debug "text after process: #{processed_text}" return processed_text end |