Class: Webgen::ContentProcessor::Head

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/webgen/contentprocessor/head.rb

Overview

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.options is used 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.

Constant Summary collapse

HTML_HEAD_END_RE =

:nodoc:

/<\/head\s*>/i

Instance Method Summary collapse

Methods included from Loggable

#log, #puts

Instance Method Details

#call(context) ⇒ Object

Insert the additional header information.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/webgen/contentprocessor/head.rb', line 30

def call(context)
  require 'erb'
  context.content.sub!(HTML_HEAD_END_RE) do |match|
    result = ''
    if context[:cp_head].kind_of?(Hash)
      context[:cp_head][:js_file].each do |js_file|
        result += "\n<script type=\"text/javascript\" src=\"#{js_file}\"></script>"
      end if context[:cp_head][:js_file].kind_of?(Array)

      context[:cp_head][:js_inline].each do |content|
        result += "\n<script type=\"text/javascript\">\n#{content}\n</script>"
      end if context[:cp_head][:js_inline].kind_of?(Array)

      context[:cp_head][:css_file].each do |css_file|
        result += "\n<link rel=\"stylesheet\" href=\"#{css_file}\" type=\"text/css\"/>"
      end if context[:cp_head][:css_file].kind_of?(Array)

      context[:cp_head][:css_inline].each do |content|
        result += "\n<style type=\"text/css\"><![CDATA[/\n#{content}\n]]></style>"
      end if context[:cp_head][:css_inline].kind_of?(Array)

      context[:cp_head][:meta].merge(context.node['meta'] || {}).each do |name, content|
        result += "\n<meta name=\"#{ERB::Util.h(name)}\" content=\"#{ERB::Util.h(content)}\" />"
      end if context[:cp_head][:meta].kind_of?(Hash)
    end
    result + match
  end
  context
end