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

call(context) click to toggle source

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
process_data_array(context, array_name, &block) click to toggle source

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
resolve_paths(context, paths) click to toggle source

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
tags_from_context_data(context) click to toggle source

Return a string containing the HTML tags corresponding to the information set in the given Context object and the values of the meta info key meta of the content node.

   # File lib/webgen/content_processor/html_head.rb
53 def self.tags_from_context_data(context)
54   result = ''
55   if context.persistent[:cp_html_head].kind_of?(Hash)
56     process_data_array(context, :js_file) do |js_file|
57       result += "\n<script type=\"text/javascript\" src=\"#{js_file}\"></script>"
58     end
59 
60     process_data_array(context, :js_inline) do |content|
61       result += "\n<script type=\"text/javascript\">//<![CDATA[\n#{content}\n//]]></script>"
62     end
63 
64     process_data_array(context, :css_file) do |css_file|
65       result += "\n<link rel=\"stylesheet\" href=\"#{css_file}\" type=\"text/css\"/>"
66     end
67 
68     process_data_array(context, :css_inline) do |content|
69       result += "\n<style type=\"text/css\">/*<![CDATA[/*/\n#{content}\n/*]]>*/</style>"
70     end
71   end
72   ((context.persistent[:cp_html_head] || {})[:meta] || {}).merge(context.content_node['meta'] || {}).each do |name, content|
73     result += "\n<meta name=\"#{ERB::Util.h(name)}\" content=\"#{ERB::Util.h(content)}\" />"
74   end
75   ((context.persistent[:cp_html_head] || {})[:meta_property] || {}).merge(context.content_node['meta_property'] || {}).each do |property, content|
76     result += "\n<meta property=\"#{ERB::Util.h(property)}\" content=\"#{ERB::Util.h(content)}\" />"
77   end
78   result
79 end