Class: Rote::Filters::TextFilter
- Inherits:
-
Object
- Object
- Rote::Filters::TextFilter
- Defined in:
- lib/rote/filters/base.rb
Overview
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.
Instance Attribute Summary collapse
-
#handler_blk ⇒ Object
Returns the value of attribute handler_blk.
-
#macros ⇒ Object
Returns the value of attribute macros.
Instance Method Summary collapse
- #filter(text, page) ⇒ Object
-
#initialize(&handler) ⇒ TextFilter
constructor
Create a new TextFilter.
Constructor Details
#initialize(&handler) ⇒ TextFilter
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" }
37 38 39 40 |
# File 'lib/rote/filters/base.rb', line 37 def initialize(&handler) @handler_blk = handler @macros = [] end |
Instance Attribute Details
#handler_blk ⇒ Object
Returns the value of attribute handler_blk.
30 31 32 |
# File 'lib/rote/filters/base.rb', line 30 def handler_blk @handler_blk end |
#macros ⇒ Object
Returns the value of attribute macros.
30 31 32 |
# File 'lib/rote/filters/base.rb', line 30 def macros @macros end |
Instance Method Details
#filter(text, page) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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" end 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] } end |