Class: Webgen::PathHandler::PageUtils::Node

Inherits:
Base::Node show all
Defined in:
lib/webgen/path_handler/page_utils.rb

Overview

Custom Node class that provides easy access to the blocks of the parsed page file and methods for rendering a block.

Direct Known Subclasses

Feed::Node, Sitemap::Node, TestHelper::RenderNode

Instance Attribute Summary

Attributes inherited from Node

#acn, #alcn, #children, #cn, #dest_path, #lang, #lcn, #level, #meta_info, #node_info, #parent, #tree

Instance Method Summary collapse

Methods inherited from Base::Node

#content, #link_to, #route_to, #url

Methods inherited from Node

#=~, #[], #initialize, #inspect, #is_ancestor_of?, #is_directory?, #is_file?, #is_fragment?, #is_root?, #link_to, #proxy_node, #resolve, #route_to, #to_s, #versions

Constructor Details

This class inherits a constructor from Webgen::Node

Instance Method Details

#blocksObject

Return the blocks (see PageUtils#parse_as_page!) for this node.



22
23
24
# File 'lib/webgen/path_handler/page_utils.rb', line 22

def blocks
  node_info[:blocks]
end

#render_block(name, context, pipeline = nil) ⇒ Object

Render the block name of this node using the provided Context object.

Uses the content processors specified for the block via the blocks meta information key if the pipeline parameter is not set.

Returns the given context with the rendered content.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/webgen/path_handler/page_utils.rb', line 32

def render_block(name, context, pipeline = nil)
  unless blocks.has_key?(name)
    raise Webgen::RenderError.new("No block named '#{name}' found", nil, context.dest_node.alcn, alcn)
  end

  content_processor = context.website.ext.content_processor
  context.website.ext.item_tracker.add(context.dest_node, :node_content, self)

  context.content = blocks[name].dup
  context[:block_name] = name
  pipeline ||= if (t = self['blocks']) && (t = t[name] || t['defaults']) && t.key?('pipeline')
                 t['pipeline']
               else
                 []
               end
  content_processor.normalize_pipeline(pipeline).each do |processor|
    content_processor.call(processor, context)
  end
  context[:block_name] = nil
  context
end

#template_chain(lang = @lang) ⇒ Object

Return the template chain for this node.

When invoked directly, the lang parameter should not be used. This parameter is necessary for the recursive invocation of the method so that the correct templates are used. Consider the following path hierarchy:

/default.en.template
/default.de.template
/custom.template
/index.de.page                  template: custom.template
/index.en.page                  template: custom.template

The template chains for index.en.page and index.de.page are therefore

/default.en.template → /custom.template
/default.de.template → /custom.template

This means that the /custom.template needs to reference different templates depending on the language.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/webgen/path_handler/page_utils.rb', line 73

def template_chain(lang = @lang)
  cached_template = (tree.website.cache.volatile[[alcn, :templates]] ||= {})
  if cached_template.has_key?(lang)
    template_node = cached_template[lang]
  elsif self['template'].kind_of?(String)
    template_node = resolve(self['template'], lang, true)
    if template_node.nil?
      tree.website.logger.warn do
        ["Template '#{self['template']}' for <#{self}> not found, using default template!",
         'Fix the value of the meta information \'template\' for <#{self}>']
      end
      template_node = default_template(parent, lang)
    end
    cached_template[lang] = template_node
  elsif meta_info.has_key?('template') && self['template'].nil?
    template_node = cached_template[lang] = nil
  else
    tree.website.logger.debug { "Using default template in language '#{lang}' for <#{self}>" }
    template_node = default_template(parent, lang)
    if template_node == self && !parent.is_root?
      template_node = default_template(parent.parent, lang)
    end
    cached_template[lang] = template_node
  end

  if template_node.nil?
    []
  else
    (template_node == self ? [] : template_node.template_chain(lang) + [template_node])
  end
end