Class: Rote::Filters::MacroFilter
- Inherits:
-
Object
- Object
- Rote::Filters::MacroFilter
- Defined in:
- lib/rote/filters/base.rb
Overview
Baseclass from which Rote filters can be derived if you want some help with macro replacement.
There are three ways to make a macro filter:
-
Subclass this class, and provide
macro_name
methods wherename
is the macro name. These methods receive args (args, body, raw_macro) -
Create an instance of this class with a block taking up to four arguments (name,args,body,raw_macro)
-
Subclass this class and override the
handler
method to process all macros.
Instance Attribute Summary collapse
-
#handler_blk ⇒ Object
Block that will be called for each supported macro in the filtered text.
-
#names ⇒ Object
An array of macro names supported by this filter.
Instance Method Summary collapse
- #filter(text, page) ⇒ Object
-
#handler(macro, args, body, all) ⇒ Object
You may override this method if you want to completely override the standard macro dispatch.
-
#initialize(names = [], code_re = MACRO_RE, &block) ⇒ MacroFilter
constructor
Create a new macro filter.
Constructor Details
#initialize(names = [], code_re = MACRO_RE, &block) ⇒ MacroFilter
Create a new macro filter. If a three-arg block is passed, it will be called for each macro with a name that exists in the macros
array. Otherwise, macros will be sought as methods (e.g. macro_code
). If an array of names isn’t passed, a search such methods will be used to populate the names array.
106 107 108 109 110 |
# File 'lib/rote/filters/base.rb', line 106 def initialize(names = [], code_re = MACRO_RE, &block) @names = (names || []).map { |n| n.to_s } @block = block @code_re = code_re end |
Instance Attribute Details
#handler_blk ⇒ Object
Block that will be called for each supported macro in the filtered text. Like:
{ |macro, args, body, raw_macro| "replacement" }
The presence of a block precludes the use of any macro_xxxx
methods on the subclass.
98 99 100 |
# File 'lib/rote/filters/base.rb', line 98 def handler_blk @handler_blk end |
#names ⇒ Object
An array of macro names supported by this filter. This can be used to selectively disable individual macro support.
89 90 91 |
# File 'lib/rote/filters/base.rb', line 89 def names @names end |
Instance Method Details
#filter(text, page) ⇒ Object
112 113 114 |
# File 'lib/rote/filters/base.rb', line 112 def filter(text,page) text.gsub(@code_re) { handler($1,$2,$3,$&) || $& } end |
#handler(macro, args, body, all) ⇒ Object
You may override this method if you want to completely override the standard macro dispatch.
118 119 120 121 122 123 124 125 126 |
# File 'lib/rote/filters/base.rb', line 118 def handler(macro,args,body,all) if @names.include?(macro) then @block[macro,args,body,all] elsif respond_to?(meth = "macro_#{macro}") then self.send(meth,args,body,all) else nil end end |