class Rote::Filters::TextFilter

Baseclass from which Rote filters can be derived if they want to process text without macros. This class replaces macro tags/bodies with simple placeholders, containing only characters [a-z0-9] before passing it the text to the block. On return, macro markers are replaced with the corresponding (numbered) original macro body.



Public Class Methods

new(&handler) click to toggle source

Create a new TextFilter. The supplied block will be called with the text to be rendered, with all macros replaced by plain-text macro markers:

{ |text, page| "replacement" }
# File lib/rote/filters/base.rb, line 37
def initialize(&handler)
  @handler_blk = handler
  @macros = []

Public Instance Methods

filter(text, page) click to toggle source
# File lib/rote/filters/base.rb, line 42
def filter(text, page)
  # we need to remove any macros to stop them being touched
  n = -1        
  tmp = text.gsub(MACRO_RE) do
    macros << $&
    # we need make the marker a 'paragraph'
    "\nxmxmxmacro#{n += 1}orcamxmxmx\n"
  tmp = handler(tmp,page)

  # Match the placeholder, including any (and greedily all) markup that's
  # been placed before or after it, and put the macro text back.
  tmp.gsub(PLACEHOLDER_RE) { macros[$1.to_i] }      

Protected Instance Methods

handler(tmp,page) click to toggle source

Calls the handler block. Subclasses may override this rather than use a block.

# File lib/rote/filters/base.rb, line 62
def handler(tmp,page)
  handler_blk[tmp,page] if handler_blk