Class | Webgen::ContentProcessor::Blocks |
In: |
lib/webgen/contentprocessor/blocks.rb
|
Parent: | Object |
Replaces special xml tags with the rendered content of a node.
BLOCK_RE | = | /<webgen:block\s*?((?:\s\w+=('|")[^'"]+\2)+)\s*\/>/ |
BLOCK_ATTR_RE | = | /(\w+)=('|")([^'"]+)\2/ |
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 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:
# 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