class Docstache::Renderer
Constants
- BLOCK_REGEX
Public Class Methods
new(xml, data)
click to toggle source
# File lib/docstache/renderer.rb, line 5 def initialize(xml, data) @content = xml @data = DataScope.new(data) end
Public Instance Methods
render()
click to toggle source
# File lib/docstache/renderer.rb, line 10 def render find_and_expand_blocks replace_tags(@content, @data) return @content end
Private Instance Methods
expand_and_replace_block(block)
click to toggle source
# File lib/docstache/renderer.rb, line 29 def expand_and_replace_block(block) case block.type when :conditional case condition = @data.get(block.name) when Array condition = !condition.empty? else condition = !!condition end condition = !condition if block.inverted unless condition block.content_elements.each(&:unlink) end when :loop set = @data.get(block.name, condition: block.condition) content = set.map { |item| data = DataScope.new(item, @data) elements = block.content_elements.map(&:clone) replace_tags(Nokogiri::XML::NodeSet.new(@content, elements), data) } content.each do |els| el = els[0] els[1..-1].each do |next_el| el.after(next_el) el = next_el end block.closing_element.before(els[0]) end block.content_elements.each(&:unlink) end block.opening_element.unlink block.closing_element.unlink end
find_and_expand_blocks()
click to toggle source
# File lib/docstache/renderer.rb, line 18 def find_and_expand_blocks blocks = @content.text.scan(BLOCK_REGEX) found_blocks = blocks.uniq.map { |block| inverted = block[0] == "^" Block.find_all(name: block[1], elements: @content.elements, data: @data, inverted: inverted, condition: block[2]) }.flatten found_blocks.each do |block| expand_and_replace_block(block) end end
text(obj)
click to toggle source
# File lib/docstache/renderer.rb, line 76 def text(obj) "#{obj}" end