Parent

Included Modules

Class Index [+]

Quicksearch

Webgen::ContentProcessor::Blocks

Replaces special xml tags with the rendered content of a node.

Constants

BLOCK_RE
BLOCK_ATTR_RE

Public Instance Methods

call(context) click to toggle source

Replace the webgen:block xml tags with the rendered content of the specified node.

    # File lib/webgen/contentprocessor/blocks.rb, line 14
14:     def call(context)
15:       context.content.gsub!(BLOCK_RE) do |match|
16:         attr = {}
17:         match_object = $~
18:         attr[:line_nr_proc] = lambda { match_object.pre_match.scan("\n").size + 1 }
19:         match.scan(BLOCK_ATTR_RE) {|name, sep, content| attr[name.to_sym] = content}
20:         render_block(context, attr)
21:       end
22:       context
23:     end
render_block(context, options) click to toggle source

Render a block of a page node and return the result.

The Webgen::Context object context is used as the render context and the options hash needs to hold all relevant information, that is:

:name (mandatory)

The name of the block that should be used.

:chain

The node chain used for rendering. If this is not specified, the node chain from the context is used. It needs to be a String in the format (A)LCN;(A)LCN;... or an array of nodes.

:node

Defines which node in the node chain should be used. Valid values are next (= default value; the next node in the node chain), first (the first node in the node chain with a block called name) or current (the currently rendered node, ignores the chain option).

:notfound

If this property is set to ignore, a missing block will not raise an error. It is unset by default, so missing blocks will raise errors.

    # File lib/webgen/contentprocessor/blocks.rb, line 43
43:     def render_block(context, options)
44:       if options[:chain].nil?
45:         used_chain = (context[:chain].length > 1 ? context[:chain][1..1] : context[:chain]).dup
46:       elsif options[:chain].kind_of?(Array)
47:         used_chain = options[:chain]
48:         dest_node = context.content_node
49:       else
50:         paths = options[:chain].split(';')
51:         used_chain = paths.collect do |path|
52:           temp_node = context.ref_node.resolve(path.strip, context.dest_node.lang)
53:           if temp_node.nil?
54:             raise Webgen::RenderError.new("Could not resolve <#{path.strip}>",
55:                                           self.class.name, context.dest_node,
56:                                           context.ref_node, (options[:line_nr_proc].call if options[:line_nr_proc]))
57:           end
58:           temp_node
59:         end.compact
60:         return '' if used_chain.length != paths.length
61:         dest_node = context.content_node
62:       end
63: 
64:       if options[:node] == 'first'
65:         used_chain.shift while used_chain.length > 0 && !used_chain.first.node_info[:page].blocks.has_key?(options[:name])
66:       elsif options[:node] == 'current'
67:         used_chain = context[:chain].dup
68:       end
69:       block_node = used_chain.first
70: 
71:       if !block_node || !block_node.node_info[:page].blocks.has_key?(options[:name])
72:         if options[:notfound] == 'ignore'
73:           return ''
74:         elsif block_node
75:           raise Webgen::RenderError.new("No block named '#{options[:name]}' found in <#{block_node}>",
76:                                         self.class.name, context.dest_node,
77:                                         context.ref_node, (options[:line_nr_proc].call if options[:line_nr_proc]))
78:         else
79:           raise Webgen::RenderError.new("No node in the render chain has a block named '#{options[:name]}'",
80:                                         self.class.name, context.dest_node,
81:                                         context.ref_node, (options[:line_nr_proc].call if options[:line_nr_proc]))
82:         end
83:       end
84: 
85:       context.dest_node.node_info[:used_nodes] << block_node.alcn
86:       tmp_context = block_node.node_info[:page].blocks[options[:name]].render(context.clone(:chain => used_chain, :dest_node => dest_node))
87:       tmp_context.content
88:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.