Sha256: ccf79a3a4cf6762ed8b9c4f6a23f61c9758baea2d2017541b1eef3e754772fbe

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 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}" if @file.nil?
            error "No such file #{tag}" unless File.exists?(File.join(@working_dir, 'fragments', @file))
            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', @file)
      HtmlDocument.open_as_string(full_path)
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
genit-1.0.1 lib/genit/documents/fragment.rb
genit-1.0 lib/genit/documents/fragment.rb