Class: Rote::Filters::MacroFilter

Inherits:
Object
  • Object
show all
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 where name 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.

Direct Known Subclasses

Eval, Exec, Syntax

Instance Attribute Summary collapse

Instance Method Summary collapse

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_blkObject

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

#namesObject

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