module Webgen::ContentProcessor::HtmlHead
General information¶ ↑
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.
Use the methods defined on the special Webgen::Context::HtmlHead::Proxy
object which can be accessed via Webgen::Context#html_head
to provide values.
Internal details¶ ↑
The key :cp_html_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:
- :js_file
-
An array of already resolved relative or absolute paths to Javascript files.
- :js_inline
-
An array of Javascript fragments to be inserted directly into the head section.
- :css_file
-
An array of already resolved relative or absolute paths to CSS files.
- :css_inline
-
An array of CSS fragments to be inserted directly into the head section.
- :meta
-
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.
Public Class Methods
Insert the additional header information.
# File lib/webgen/content_processor/html_head.rb 40 def self.call(context) 41 context.content.sub!(HTML_HEAD_END_RE) do |match| 42 result = '' 43 result << tags_from_context_data(context) 44 result << links_to_translations(context) 45 result << links_from_link_meta_info(context) 46 result << match 47 end 48 context 49 end
Return a string containing HTML link tags to the links from the 'link' meta information.
# File lib/webgen/content_processor/html_head.rb 120 def self.links_from_link_meta_info(context) 121 link_mi = Marshal.load(Marshal.dump(context.content_node['link'] || {})) 122 result = '' 123 124 # Add user defined javascript and CSS links 125 resolve_paths(context, link_mi.delete('javascript')).each do |file| 126 result += "\n<script type=\"text/javascript\" src=\"#{file}\"></script>" 127 end 128 resolve_paths(context, link_mi.delete('css')).each do |file| 129 result += "\n<link rel=\"stylesheet\" href=\"#{file}\" type=\"text/css\" />" 130 end 131 132 # add generic links 133 link_mi.sort.each do |link_type, vals| 134 link_type = link_type.downcase 135 [vals].flatten.each do |val| 136 val = {'href' => val} if val.kind_of?(String) 137 val['rel'] ||= link_type 138 val = LINK_DOCUMENT_ATTRS.merge(val) if LINK_DOCUMENT_TYPES.include?(link_type) 139 href = val.delete('href') 140 href = resolve_paths(context, href).first if href 141 if href 142 result << "\n<link href=\"#{href}\" " 143 val.sort.each {|k,v| result << "#{k}=\"#{ERB::Util.h(v)}\" "} 144 result << "/>" 145 else 146 context.website.logger.error do 147 "No link target specified for link type '#{link_type}' in 'link' meta information in <#{context.content_node}>" 148 end 149 end 150 end 151 end 152 153 result 154 end
Return a string containing HTML link tags to translations of the destination node.
# File lib/webgen/content_processor/html_head.rb 87 def self.links_to_translations(context) 88 context.website.tree.translations(context.dest_node).map do |node| 89 next '' if node.alcn == context.dest_node.alcn 90 context.website.ext.item_tracker.add(context.dest_node, :node_meta_info, node) 91 92 result = "\n<link type=\"text/html\" rel=\"alternate\" hreflang=\"#{node.lang}\" " 93 result << "href=\"#{context.dest_node.route_to(node)}\" " 94 if node['title'] && !node['title'].empty? 95 result << "lang=\"#{node.lang}\" title=\"#{ERB::Util.h(node['title'])}\" " 96 end 97 result << "/>" 98 end.join('') 99 end
Yield the values of the specified array.
# File lib/webgen/content_processor/html_head.rb 82 def self.process_data_array(context, array_name, &block) 83 (context.persistent[:cp_html_head][array_name] || []).uniq.each(&block) 84 end
Given an array of path names, add tracker information for the resolved nodes (reference node is the content node) and return the relative paths to them.
# File lib/webgen/content_processor/html_head.rb 103 def self.resolve_paths(context, paths) 104 [paths].flatten.compact.collect do |path| 105 next path if Webgen::Path.url(path, false).absolute? 106 node = context.content_node.resolve(path, context.dest_node.lang, true) 107 if node 108 context.website.ext.item_tracker.add(context.dest_node, :node_meta_info, node) 109 context.dest_node.route_to(node) 110 else 111 nil 112 end 113 end.compact 114 end