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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, root_path:, stdout: $stdout, raise_exception_on_error: true) ⇒ Compiler

Returns a new instance of Compiler.



17
18
19
20
21
22
# File 'lib/sitepress/compiler.rb', line 17

def initialize(site:, root_path:, stdout: $stdout, raise_exception_on_error: true)
  @site = site
  @stdout = stdout
  @root_path = Pathname.new(root_path)
  @raise_exception_on_error = raise_exception_on_error
end

Instance Attribute Details

#raise_exception_on_errorObject

If a resource can’t render, it will raise an exception and stop the compiler. Sometimes its useful to turn off errors so you can get through a full compilation and see how many errors you encounter along the way. To do that, you’d set ‘raise_exception_on_error` to `false` and the compile will get through all the resources.



15
16
17
# File 'lib/sitepress/compiler.rb', line 15

def raise_exception_on_error
  @raise_exception_on_error
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



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

def root_path
  @root_path
end

#siteObject (readonly)

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

#compileObject

Iterates through all pages and writes them to disk



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sitepress/compiler.rb', line 25

def compile
  status "Building #{site.root_path.expand_path} to #{root_path.expand_path}"
  resources.each do |resource, path|
    if resource.renderable?
      status "Rendering #{path}"
      File.open(path.expand_path, "w"){ |f| f.write render resource }
    else
      status "Copying #{path}"
      cp resource.asset.path, path.expand_path
    end
  rescue
    status "Error building #{resource.inspect}"
    raise if raise_exception_on_error
  end
  status "Successful build to #{root_path.expand_path}"
end