Sha256: 9374e18afbcb3ad8f944dfd2b7729ea7a97036d3067c2442781a1f0801daae79

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

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

module Genit

  # A Genit tag that represents a variable.
  # For example, in the template you have: <genit var="page_title"/>,
  # and in the page you have: <genit var="page_title">My Title</genit>.
  class HereTag < Tag
  
    # Public: Constructor.
    #
    # working_dir - The String working directory, where live the project.
    # template    - The Nokogiri::XML::Document into which we process the tag.
    # filename    - The String name of the page
    # tag         - The tag to process as a Nokogiri::XML::Element
    def initialize working_dir, template, filename, tag
      super working_dir, template, filename, tag
    end

    # Public: Replace a variable in the template. The variable content is found
    # in the tag in the page.
    #
    # Returns the template as a Nokogiri::XML::Document
    def process
      replace_tag_into_template! get_css_rule, get_variable_value
      @template
    end

    private

      def get_css_rule
        var_name = @tag['here']
        "genit[here='#{var_name}']"
      end

      def get_variable_value
        file = File.join(@working_dir, PAGES_DIR, @filename)
        doc = HtmlDocument.open_fragment file
        html_or_nothing_from(doc.at_css("genit[what='#{@tag['here']}']"))
      end

      def html_or_nothing_from element
        if element.nil?
          warning "here without what #{@tag}"
          ""
        else
          element.inner_html
        end
      end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
genit-2.1 lib/genit/tags/here_tag.rb
genit-2.0 lib/genit/tags/here_tag.rb