Class: Marko::Markup::MacroProcessor

Inherits:
Object
  • Object
show all
Extended by:
Pluggable
Defined in:
lib/marko/markup/macro.rb

Overview

Macro processor

Examples:

processor = MacroProcessor.new
processor << Toc.new
processor << Todo.new
processor.('bla bla @@todo, bla bla @@list')
# => 'bla bla <substitute @@todo>, bla bla..'

Instance Method Summary collapse

Methods included from Pluggable

plug

Constructor Details

#initializeMacroProcessor

Returns a new instance of MacroProcessor.



150
151
152
153
154
155
156
157
# File 'lib/marko/markup/macro.rb', line 150

def initialize
  @macros = {}
  self.<<(MList.new)
  self.<<(MTree.new)
  self.<<(MLink.new)
  self.<<(MTodo.new)
  self.<<(MSkip.new)
end

Instance Method Details

#<<(macro) ⇒ Object



159
160
161
162
163
# File 'lib/marko/markup/macro.rb', line 159

def <<(macro)
  MustbeMacro.(macro)
  @macros[macro.pattern] = macro
  macro
end

#process(text, obj) ⇒ Object Also known as: call



165
166
167
168
169
170
# File 'lib/marko/markup/macro.rb', line 165

def process(text, obj)
  fail 'No macro registered' unless @macros.any?
  String.new(text).tap {|str|
    @macros.values.each{|m| m.gsub!(str, obj) }
  }
end