Class | Tags::TagProcessor |
In: |
lib/webgen/plugins/tags/tag_processor.rb
|
Parent: | Webgen::Plugin |
This class is used for processing tags. When a content string is parsed and a tag is encountered, the registered plugin for the tag is called. If no plugin for a tag is registered but a default plugin is, the default plugin is called. Otherwise an error is raised.
The default plugin can be defined by using the special key :default.
Processes the given content using the nodes in chain which should be an array of nodes. The first node is the main template (from which the content was retrieved, the ref_node), then comes the sub template, the sub sub template and so on until the last node which is the current node (the node) that is the reason for the whole processing.
After having processed all nodes, the method returns the result as string, ie. the rendered content.
# File lib/webgen/plugins/tags/tag_processor.rb, line 46 46: def process( content, chain ) 47: if !content.kind_of?( String ) 48: log(:warn) { "The content in <#{chain.first.node_info[:src]}> is not a string, but a #{content.class.name}" } 49: content = content.to_s 50: end 51: 52: return replace_tags( content, chain.first ) do |tag, tag_data| 53: log(:debug) { "Replacing tag #{tag} with data '#{tag_data}' in <#{chain.first.full_path}>" } 54: 55: result = '' 56: processor = processor_for_tag( tag ) 57: if !processor.nil? 58: begin 59: processor.set_tag_config( YAML::load( "--- #{tag_data}" ), chain.first ) 60: rescue ArgumentError => e 61: log(:error) { "Could not parse the data '#{tag_data}' for tag #{tag} in <#{node.nod_info[:src]}>: #{e.message}" } 62: end 63: result, tag_chain = processor.process_tag( tag, chain ) 64: processor.reset_tag_config 65: 66: result = process( result, tag_chain || chain ) if processor.process_output? 67: end 68: 69: result 70: end 71: end
Returns the tag processor for tag or nil if tag is unknown.
# File lib/webgen/plugins/tags/tag_processor.rb, line 114 114: def processor_for_tag( tag ) 115: tags = registered_tags 116: if tags.has_key?( tag ) 117: tags[tag] 118: elsif tags.has_key?( :default ) 119: tags[:default] 120: else 121: log(:error) { "No tag processor for tag '#{tag}' found" } 122: end 123: end
Returns a hash of the registered tag plugins with the tag name as key.
# File lib/webgen/plugins/tags/tag_processor.rb, line 126 126: def registered_tags 127: if !defined?( @registered_tags_cache ) || @cached_plugins_hash != @plugin_manager.plugins.keys.hash 128: @registered_tags_cache = {} 129: @plugin_manager.plugins.each do |name, plugin| 130: if plugin.kind_of?( DefaultTag ) 131: #TOOD write log message if duplicate tag name 132: plugin.tags.each {|tag| @registered_tags_cache[tag] = plugin } 133: end 134: end 135: @cached_plugins_hash = @plugin_manager.plugins.keys.hash 136: end 137: @registered_tags_cache 138: end
Scans the content for tags. If a tag is found, the block is called which needs to return the value for the given tag. The changed content is returned.
# File lib/webgen/plugins/tags/tag_processor.rb, line 79 79: def replace_tags( content, node ) # :yields: tag, data 80: offset = 0; 81: content = content.dup 82: while index = content.index( /(\\*)\{(\w+):/, offset ) 83: bracketCount = 1; 84: length = $1.length + 1; 85: tag = $2 86: content[(index + length)..-1].each_byte do |char| 87: length += 1 88: bracketCount += 1 if char == ?{ 89: bracketCount -= 1 if char == ?} 90: break if bracketCount == 0 91: end 92: 93: if bracketCount > 0 94: log(:error) { "Unbalanced curly brackets in <#{node.node_info[:src]}>!!!" } 95: newContent = content[index, length] 96: else 97: newContent = "\\"* ( $1.length / 2 ) 98: if $1.length % 2 == 1 99: newContent += content[index + $1.length, length - $1.length] 100: else 101: tagHeaderLength = $1.length + tag.length + 2 102: realContent = content[index + tagHeaderLength, length - tagHeaderLength - 1].lstrip 103: newContent += yield( tag, realContent ).to_s 104: end 105: content[index, length] = newContent 106: end 107: offset = index + newContent.length 108: end 109: content 110: end