Class: Flowcation::Template

Inherits:
Object
  • Object
show all
Includes:
Registry, Substitutions
Defined in:
lib/flowcation/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Substitutions

#substitute

Constructor Details

#initialize(doc, layout, path, content_for_blocks, substitutions, format) ⇒ Template

Returns a new instance of Template.



6
7
8
9
10
11
12
13
14
15
# File 'lib/flowcation/template.rb', line 6

def initialize(doc, layout, path, content_for_blocks, substitutions, format)
  @doc = doc
  @path = path
  @layout_name = layout
  @substitutions = substitutions
  @format = format
  content_for_blocks.each do |name, options|
    register_block Flowcation::Block.new(name, options)
  end
end

Instance Attribute Details

#layout_nameObject (readonly)

Returns the value of attribute layout_name.



5
6
7
# File 'lib/flowcation/template.rb', line 5

def layout_name
  @layout_name
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/flowcation/template.rb', line 5

def path
  @path
end

Class Method Details

.from_config(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/flowcation/template.rb', line 38

def self.from_config(options={})
  new \
    Nokogiri::HTML(File.new(options['file'])).xpath("//body"), 
    options['layout'], 
    options['path'], 
    options['content_for'], 
    options['substitutions'], 
    options['format']
end

Instance Method Details

#contentObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flowcation/template.rb', line 17

def content
  doc = @doc.dup
  substitute(doc)
  erb = ""
  blocks.each do |block|
    block_doc = doc.at_xpath(block.xpath)
    raise BlockNotFoundException.build(xpath: block.xpath, path: self.path) unless block_doc
    content = case block.type 
      when 'content'
        doc.at_xpath(block.xpath).inner_html
      when 'replace_collection'
        doc.xpath(block.xpath).to_html
    end
    
    erb << @format.
      sub("::name::", block.name).
      sub("::content::", content)
  end
  Render.sanitize(erb)
end