Class: Flowcation::Layout

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Substitutions

#substitute

Constructor Details

#initialize(doc, path, blocks, substitutions, format) ⇒ Layout

Returns a new instance of Layout.



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

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.from_config(options = {}) ⇒ Object



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

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

Instance Method Details

#contentObject



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

def content
  doc = @doc.dup
  substitute(doc)
  blocks.each do |block|
    block_doc = doc.at_xpath(block.xpath)
    raise BlockNotFoundException.build(xpath: block.xpath, path: self.path) unless block_doc
    value = @format.sub("::name::", block.name)
    case block.type
    when 'content'
      doc.at_xpath(block.xpath).content = value
    when 'replace'
      doc.at_xpath(block.xpath).replace Nokogiri::XML::Text.new(value, doc.document)
    when 'replace_collection'
      doc.at_xpath(block.xpath).replace Nokogiri::XML::Text.new(value, doc.document)
      doc.xpath(block.xpath).each do |node|
        node.remove if node.is_a? Nokogiri::XML::Element
      end
    end
  end
  Render.sanitize(doc.to_html(encoding: 'UTF-8'))
end