Class: Sitepress::Compiler

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/sitepress/compiler.rb

Overview

Compile all resources from a Sitepress site into static pages.

Instance Method Summary collapse

Constructor Details

#initialize(site:, stdout: $stdout) ⇒ Compiler

Returns a new instance of Compiler.


9
10
11
12
# File 'lib/sitepress/compiler.rb', line 9

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

Instance Method Details

#compile(target_path:) ⇒ Object

Iterates through all pages and writes them to disk


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sitepress/compiler.rb', line 15

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