Class | Tags::DefaultTag |
In: |
lib/webgen/plugins/tags/tag_processor.rb
|
Parent: | Webgen::Plugin |
Base class for all tag plugins. The base class provides a default mechanism for retrieving configuration data from either the configuration file or the tag itself.
# File lib/webgen/plugins/tags/tag_processor.rb, line 153 153: def initialize( plugin_manager ) 154: super 155: @process_output = true 156: @cur_config = {} 157: @tags = [] 158: end
Register tag so that it gets processed by the current class.
# File lib/webgen/plugins/tags/tag_processor.rb, line 179 179: def self.register_tag( tag ) 180: (self.config.infos[:tags] ||= [] ) << tag 181: end
Set the parameter param as mandatory. The parameter default specifies, if this parameter should be the default mandatory parameter. If only a String is supplied in a tag, its value will be assigned to the default mandatory parameter. There should be only one default mandatory parameter.
# File lib/webgen/plugins/tags/tag_processor.rb, line 169 169: def self.set_mandatory( param, default = false ) 170: if self.config.params.nil? || !self.config.params.has_key?( param ) 171: $stderr.puts( "Cannot set parameter #{param} as mandatory as this parameter does not exist for #{self.name}" ) if $VERBOSE 172: else 173: self.config.params[param].mandatory = true 174: self.config.params[param].mandatory_default = default 175: end 176: end
Retrieves the parameter value for name. The value is taken from the current tag if the parameter is specified there or the default value set in register_config_value is used.
# File lib/webgen/plugins/tags/tag_processor.rb, line 222 222: def param( name, plugin = nil ) 223: if @cur_config.has_key?( name ) && plugin.nil? 224: return @cur_config[name] 225: else 226: super( name, plugin ) 227: end 228: end
Returns true if the output should be processed again
# File lib/webgen/plugins/tags/tag_processor.rb, line 161 161: def process_output? 162: @process_output 163: end
Default implementation for processing a tag. The parameter tag specifies the name of the tag which should be processed (useful for tag plugins which process different tags).
The node_chain parameter holds all relevant nodes. The first node in the chain is always the node in which the tag was found (a template )and the last node is the current node, i.e. the page node which triggered all this. The nodes between are other template nodes.
The method has to return the result of the tag processing and, optionally, a modified chain (as second result). The second value is currently only returned by the block tag.
Has to be overridden by subclasses!!!
# File lib/webgen/plugins/tags/tag_processor.rb, line 241 241: def process_tag( tag, node_chain ) 242: raise NotImplementedError 243: end
# File lib/webgen/plugins/tags/tag_processor.rb, line 184 184: def register_tag( tag ) 185: @tags << tag 186: end
Resets the tag configuration data.
# File lib/webgen/plugins/tags/tag_processor.rb, line 216 216: def reset_tag_config 217: @cur_config = {} 218: end
Set the configuration parameters for the next process_tag call. The configuration, if specified, is taken from the tag itself.
# File lib/webgen/plugins/tags/tag_processor.rb, line 195 195: def set_tag_config( config, node ) 196: @cur_config = {} 197: case config 198: when Hash 199: set_cur_config( config, node ) 200: 201: when String 202: set_default_mandatory_param( config, node ) 203: 204: when NilClass 205: 206: else 207: log(:error) { "Invalid parameter type (#{config.class}) for tag '#{self.class.plugin_name}' in <#{node.node_info[:src]}>" } 208: end 209: 210: unless all_mandatory_params_set? 211: log(:error) { "Not all mandatory parameters for tag '#{self.class.plugin_name}' in <#{node.node_info[:src]}> set" } 212: end 213: end
Check if all mandatory parameters have been set
# File lib/webgen/plugins/tags/tag_processor.rb, line 272 272: def all_mandatory_params_set? 273: params = self.class.config.params 274: ( params.nil? ? true : params.all? { |k,v| !v.mandatory || @cur_config.has_key?( k ) || !v.default.nil? } ) 275: end
Set the current configuration taking values from config which has to be a Hash.
# File lib/webgen/plugins/tags/tag_processor.rb, line 250 250: def set_cur_config( config, node ) 251: config.each do |key, value| 252: if self.class.config.params.has_key?( key ) 253: @cur_config[key] = value 254: log(:debug) { "Setting parameter '#{key}' to '#{value}' for tag '#{self.class.plugin_name}' in <#{node.node_info[:src]}>" } 255: else 256: log(:warn) { "Invalid parameter '#{key}' for tag '#{self.class.plugin_name}' in <#{node.node_info[:src]}>" } 257: end 258: end 259: end
Set the default mandatory parameter.
# File lib/webgen/plugins/tags/tag_processor.rb, line 262 262: def set_default_mandatory_param( value, node ) 263: param_name, param_value = self.class.config.params.find {|k,v| v.mandatory_default} unless self.class.config.params.nil? 264: if param_name.nil? 265: log(:error) { "No default mandatory parameter specified for tag '#{self.class.plugin_name}' but set in <#{node.node_info[:src]}>"} 266: else 267: @cur_config[param_name] = value 268: end 269: end