Sha256: aa98d617fda3e18b6a834df6428dde8b869582fdecc0f2a4695ee7458c8a5e58

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

# -*- encoding: utf-8 -*-

require 'webgen/error'

module Webgen
  class Context

    # Provides methods for adding data for Webgen::ContentProcessor::HtmlHead.
    module HtmlHead

      # Proxy object for working with the data structure needed by
      # Webgen::ContentProcessor::HtmlHead.
      class Proxy

        def initialize(context) #:nodoc:
          @context = context
        end

        # Set the meta tag to the provided value.
        #
        # Note that some meta information keys may not be specified multiple times!
        def meta(name, content)
          (cp_hash[:meta] ||= {})[name] = content
        end

        # Add a link to the given file in the HTML head section.
        #
        # The type can either be :css for CSS files or :js for javascript files. The path to the
        # file is resolved using the "relocatable" tag (see Webgen::Tag::Relocatable).
        def link_file(type, file)
          type_check!(type)
          (cp_hash["#{type}_file".intern] ||= []) << @context.tag('relocatable', file)
        end

        # Add inline CSS or JS fragments to the HTML head section.
        #
        # The type can either be :css for a CSS fragment or :js for a javascript fragment.
        def inline_fragment(type, content)
          type_check!(type)
          (cp_hash["#{type}_inline".intern] ||= []) << content
        end

        def type_check!(type) #:nodoc:
          if ![:css, :js].include?(type)
            raise Webgen::RenderError.new("Type must either be :css or :js, not #{type}",
                                          "context.html_head", @context.dest_node, @context.ref_node)
          end
        end
        private :type_check!

        def cp_hash #:nodoc:
          @context.persistent[:cp_html_head] ||= {}
        end
        private :cp_hash

      end

      # Return the Proxy object for adding data to the context for
      # Webgen::ContentProcessor::HtmlHead.
      def html_head
        Proxy.new(self)
      end

    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
webgen-1.5.2 lib/webgen/context/html_head.rb
webgen-1.5.1 lib/webgen/context/html_head.rb
webgen-1.5.0 lib/webgen/context/html_head.rb
webgen-1.4.1 lib/webgen/context/html_head.rb
webgen-1.4.0 lib/webgen/context/html_head.rb
webgen-1.3.0 lib/webgen/context/html_head.rb
webgen-1.2.1 lib/webgen/context/html_head.rb
webgen-1.2.0 lib/webgen/context/html_head.rb
webgen-1.1.0 lib/webgen/context/html_head.rb
webgen-1.0.0 lib/webgen/context/html_head.rb
webgen-1.0.0.beta3 lib/webgen/context/html_head.rb