Class: MarkdownExpander::Expander
- Inherits:
-
Object
- Object
- MarkdownExpander::Expander
- Defined in:
- lib/markdown-expander.rb
Defined Under Namespace
Classes: Expression, LoopStart, Node
Constant Summary collapse
- LOOP_START_MATCH =
/{{\s*([a-zA-Z_]*)\s+in\s+([a-zA-Z_.]*)\s*}}/- LOOP_END_MATCH =
/{{\s*end\s*}}/- EXPRESSION_MATCH =
/{{\s*([a-zA-Z_.]+)\s*}}/
Instance Method Summary collapse
- #evaluate_nodes(root, scope) ⇒ Object
-
#initialize(template) ⇒ Expander
constructor
A new instance of Expander.
- #render(options) ⇒ Object
Constructor Details
#initialize(template) ⇒ Expander
Returns a new instance of Expander.
9 10 11 |
# File 'lib/markdown-expander.rb', line 9 def initialize template @template = template end |
Instance Method Details
#evaluate_nodes(root, scope) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/markdown-expander.rb', line 43 def evaluate_nodes root, scope lines = [] root.children.each_with_index do |child, index| if child.value.class == Expression lines << child.value.evaluate(scope) elsif child.value.class == LoopStart name = child.value.name.to_sym parts = child.value.looper.split(".") parts.each do |part| scope = scope[part.to_sym] end scope.each do |item| lines << evaluate_nodes(child, {name => item}) end else lines << child.value end end lines.join("") end |
#render(options) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/markdown-expander.rb', line 13 def render root = Node.new(nil, nil) node = root @template.each_line do |line| if line =~ LOOP_START_MATCH new_node = Node.new(node, LoopStart.new($1, $2)) node.children << new_node node = new_node elsif line =~ LOOP_END_MATCH node = node.parent else loop do if line =~ EXPRESSION_MATCH before_match = $` after_match = $' node.children << Node.new(node, before_match) node.children << Node.new(node, Expression.new($1)) line = after_match else node.children << Node.new(node, line) break end end end end evaluate_nodes(root, ) end |