class Webgen::PathHandler::PageUtils::Node

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

Public Instance Methods

blocks() click to toggle source

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

   # File lib/webgen/path_handler/page_utils.rb
21 def blocks
22   node_info[:blocks]
23 end
render_block(name, context, pipeline = nil) click to toggle source

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.

   # File lib/webgen/path_handler/page_utils.rb
31 def render_block(name, context, pipeline = nil)
32   unless blocks.has_key?(name)
33     raise Webgen::RenderError.new("No block named '#{name}' found", nil, context.dest_node.alcn, alcn)
34   end
35 
36   content_processor = context.website.ext.content_processor
37   context.website.ext.item_tracker.add(context.dest_node, :node_content, self)
38 
39   context.content = blocks[name].dup
40   context[:block_name] = name
41   pipeline ||= if (t = self['blocks']) && (t = t[name] || t['defaults']) && t.key?('pipeline')
42                  t['pipeline']
43                else
44                  []
45                end
46   content_processor.normalize_pipeline(pipeline).each do |processor|
47     content_processor.call(processor, context)
48   end
49   context[:block_name] = nil
50   context
51 end
template_chain(lang = @lang) click to toggle source

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.

    # File lib/webgen/path_handler/page_utils.rb
 72 def template_chain(lang = @lang)
 73   cached_template = (tree.website.cache.volatile[[alcn, :templates]] ||= {})
 74   if cached_template.has_key?(lang)
 75     template_node = cached_template[lang]
 76   elsif self['template'].kind_of?(String)
 77     template_node = resolve(self['template'], lang, true)
 78     if template_node.nil?
 79       tree.website.logger.warn do
 80         ["Template '#{self['template']}' for <#{self}> not found, using default template!",
 81          'Fix the value of the meta information \'template\' for <#{self}>']
 82       end
 83       template_node = default_template(parent, lang)
 84     end
 85     cached_template[lang] = template_node
 86   elsif meta_info.has_key?('template') && self['template'].nil?
 87     template_node = cached_template[lang] = nil
 88   else
 89     tree.website.logger.debug { "Using default template in language '#{lang}' for <#{self}>" }
 90     template_node = default_template(parent, lang)
 91     if template_node == self && !parent.is_root?
 92       template_node = default_template(parent.parent, lang)
 93     end
 94     cached_template[lang] = template_node
 95   end
 96 
 97   if template_node.nil?
 98     []
 99   else
100     (template_node == self ? [] : template_node.template_chain(lang) + [template_node])
101   end
102 end

Protected Instance Methods

default_template(dir, lang) click to toggle source

Return the default template for the directory node dir and language lang.

If the template node is not found, the parent directories are searched.

    # File lib/webgen/path_handler/page_utils.rb
107 def default_template(dir, lang)
108   default_template_name = tree.website.config['path_handler.default_template']
109   template = dir.resolve(default_template_name, lang)
110   if template.nil?
111     if dir.is_root?
112       tree.website.logger.warn do
113         ["Default template '#{default_template_name}' not found in root directory!",
114          'Provide a </#{default_template_name}> to fix this warning.']
115       end
116     else
117       template = default_template(dir.parent, lang)
118     end
119   end
120   template
121 end