Sha256: ae5ba7e940b68ff62170d51337592ff9fb82aea5ef55e487921081207e1a3912

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

require "pathname"
require "fileutils"

module Sitepress
  # Compile all resources from a Sitepress site into static pages.
  class Compiler
    include FileUtils

    def initialize(site:, stdout: $stdout)
      @site = site
      @stdout = stdout
    end

    # Iterates through all pages and writes them to disk
    def compile(target_path:)
      target_path = Pathname.new(target_path)
      mkdir_p target_path
      root = Pathname.new("/")
      cache_resources = @site.cache_resources
      @stdout.puts "Compiling #{@site.root_path.expand_path}"
      begin
        @site.cache_resources = true
        @site.resources.each do |resource|
          derooted = Pathname.new(resource.request_path).relative_path_from(root)
          path = target_path.join(derooted)
          mkdir_p path.dirname
          @stdout.puts "  #{path}"
          File.open(path.expand_path, "w"){ |f| f.write render(resource) }
        end
        @stdout.puts "Successful compilation to #{target_path.expand_path}"
      ensure
        @site.cache_resources = cache_resources
      end
    end

    private
    def render(resource)
      RenderingContext.new(resource: resource, site: @site).render
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sitepress-cli-1.0.1 lib/sitepress/compiler.rb
sitepress-cli-1.0.0 lib/sitepress/compiler.rb
sitepress-cli-0.1.29 lib/sitepress/compiler.rb
sitepress-cli-0.1.28 lib/sitepress/compiler.rb
sitepress-cli-0.1.27 lib/sitepress/compiler.rb
sitepress-cli-0.1.26 lib/sitepress/compiler.rb
sitepress-cli-0.1.25 lib/sitepress/compiler.rb