class Rote::Filters::MacroFilter
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.
Attributes
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.
An array of macro names supported by this filter. This can be used to selectively disable individual macro support.
Public Class Methods
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.
# File lib/rote/filters/base.rb 106 def initialize(names = [], code_re = MACRO_RE, &block) 107 @names = (names || []).map { |n| n.to_s } 108 @block = block 109 @code_re = code_re 110 end
Public Instance Methods
# File lib/rote/filters/base.rb 112 def filter(text,page) 113 text.gsub(@code_re) { handler($1,$2,$3,$&) || $& } 114 end
You may override this method if you want to completely override the standard macro dispatch.
# File lib/rote/filters/base.rb 118 def handler(macro,args,body,all) 119 if @names.include?(macro) then 120 @block[macro,args,body,all] 121 elsif respond_to?(meth = "macro_#{macro}") then 122 self.send(meth,args,body,all) 123 else 124 nil 125 end 126 end