Sha256: 48fededba5ebd12f30122ec76db8c56d69922b32ed0b5b21b773ed28d7975f6e

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

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

module Genit

  # Replace each fragment in a page.
  class Fragment

    # Public: Constructor.
    #
    # file        - Full String filename of the page.
    # working_dir - The String working directory, where live the project.
    def initialize file, working_dir
      @page = HtmlDocument.open_fragment file
      @working_dir = working_dir
      HtmlDocument.genit_tags_from(@page).each do |tag|
        case tag['class']
          when 'fragment'
            @file = tag['file']
            error "Incomplete #{tag}" unless tag.key?('file')
            unless File.exists?(File.join(@working_dir, FRAGMENTS_DIR, @file))
              error "No such file #{tag}"
            end
            replace_fragment 
        end
      end
    end

    # Public: Get the page in html format.
    #
    # Returns the html code of the page as a String.
    def to_html
      @page.to_html
    end

    private

      def replace_fragment
        builder = Builder.new(@page)
        @page = builder.replace(css_rule, content)
      end

      def css_rule
        "genit.fragment[file='#{@file}']"
      end

      def content
        full_path = File.join(@working_dir, FRAGMENTS_DIR, @file)
        HtmlDocument.open_as_string(full_path)
      end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
genit-2.1 lib/genit/documents/fragment.rb
genit-2.0 lib/genit/documents/fragment.rb