Inserts additional links to CSS/JS files and other HTML head meta info directly before the HTML head end tag.
The data used by this content processor is taken from the Context object. Therefore this processor should be the last in the processing pipeline so that all other processors have been able to set the data.
The key :cp_head of context.persistent is used (the normal context.options won’t do because the data needs to be shared ‘backwards’ during the rendering) and it has to be a Hash with the following values:
An array of already resolved relative or absolute paths to Javascript files.
An array of Javascript fragments to be inserted directly into the head section.
An array of already resolved relative or absolute paths to CSS files.
An array of CSS fragments to be inserted directly into the head section.
A hash with key-value pairs from which meta tags are generated. The keys and the values will be properly escaped before insertion. The entries in the meta information meta of the content node are also used and take precedence over these entries.
Duplicate values will be removed from the above mentioned arrays before generating the output.
Insert the additional header information.
# File lib/webgen/contentprocessor/head.rb, line 36 36: def call(context) 37: require 'erb' 38: context.content.sub!(HTML_HEAD_END_RE) do |match| 39: result = '' 40: 41: # add content set programmatically 42: if context.persistent[:cp_head].kind_of?(Hash) 43: context.persistent[:cp_head][:js_file].uniq.each do |js_file| 44: result += "\n<script type=\"text/javascript\" src=\"#{js_file}\"></script>" 45: end if context.persistent[:cp_head][:js_file].kind_of?(Array) 46: 47: context.persistent[:cp_head][:js_inline].uniq.each do |content| 48: result += "\n<script type=\"text/javascript\">\n#{content}\n</script>" 49: end if context.persistent[:cp_head][:js_inline].kind_of?(Array) 50: 51: context.persistent[:cp_head][:css_file].uniq.each do |css_file| 52: result += "\n<link rel=\"stylesheet\" href=\"#{css_file}\" type=\"text/css\"/>" 53: end if context.persistent[:cp_head][:css_file].kind_of?(Array) 54: 55: context.persistent[:cp_head][:css_inline].uniq.each do |content| 56: result += "\n<style type=\"text/css\"><![CDATA[/\n#{content}\n]]></style>" 57: end if context.persistent[:cp_head][:css_inline].kind_of?(Array) 58: end 59: ((context.persistent[:cp_head] || {})[:meta] || {}).merge(context.node['meta'] || {}).each do |name, content| 60: result += "\n<meta name=\"#{ERB::Util.h(name)}\" content=\"#{ERB::Util.h(content)}\" />" 61: end 62: 63: # add links to other languages of same page 64: context.dest_node.tree.node_access[:acn][context.dest_node.acn]. 65: select {|n| n.alcn != context.dest_node.alcn}.each do |node| 66: context.dest_node.node_info[:used_meta_info_nodes] << node.alcn 67: result += "\n<link type=\"text/html\" rel=\"alternate\" hreflang=\"#{node.lang}\" " 68: result += "href=\"#{context.dest_node.route_to(node)}\" " 69: if node['title'] && !node['title'].empty? 70: result += "lang=\"#{node.lang}\" title=\"#{ERB::Util.h(node['title'])}\" " 71: end 72: result += "/>" 73: end 74: 75: link = Marshal.load(Marshal.dump(context.node['link'] || {})) 76: 77: handle_files = lambda do |files| 78: [files].flatten.compact.collect do |file| 79: if !Webgen::Node.url(file, false).absolute? 80: file = context.node.resolve(file, context.dest_node.lang) 81: if file 82: context.dest_node.node_info[:used_meta_info_nodes] << file.alcn 83: file = context.dest_node.route_to(file) 84: else 85: log(:error) { "Could not resolve path '#{file}' used in 'link' meta information in <#{context.node}>" } 86: context.dest_node.flag(:dirty) 87: end 88: end 89: file 90: end.compact 91: end 92: 93: # Add user defined javascript and CSS links 94: handle_files.call(link.delete('javascript')).each do |file| 95: result += "\n<script type=\"text/javascript\" src=\"#{file}\"></script>" 96: end 97: handle_files.call(link.delete('css')).each do |file| 98: result += "\n<link rel=\"stylesheet\" href=\"#{file}\" type=\"text/css\" />" 99: end 100: 101: # add generic links specified via the +link+ meta information 102: link.sort.each do |link_type, vals| 103: link_type = link_type.downcase 104: [vals].flatten.each do |val| 105: val = {'href' => val} if val.kind_of?(String) 106: val['rel'] ||= link_type 107: val = LINK_DOCUMENT_ATTRS.merge(val) if LINK_DOCUMENT_TYPES.include?(link_type) 108: if href = val.delete('href') 109: href = handle_files.call(href).first 110: else 111: log(:error) { "No link target specified for link type '#{link_type}' in 'link' meta information in <#{context.node}>" } 112: end 113: if href 114: s = "\n<link href=\"#{href}\" " 115: val.sort.each {|k,v| s += "#{k}=\"#{ERB::Util.h(v)}\" "} 116: result += s + "/>" 117: end 118: end 119: end 120: 121: result + match 122: end 123: context 124: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.