Class: Nanoc2::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/base/compiler.rb

Overview

Nanoc2::Compiler is responsible for compiling a site’s page and asset representations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Compiler

Creates a new compiler for the given site.



10
11
12
13
# File 'lib/nanoc2/base/compiler.rb', line 10

def initialize(site)
  @site  = site
  @stack = []
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



7
8
9
# File 'lib/nanoc2/base/compiler.rb', line 7

def stack
  @stack
end

Instance Method Details

#run(objects = nil, params = {}) ⇒ Object

Compiles (part of) the site and writes out the compiled page and asset representations.

obj

The page or asset that should be compiled, along with their dependencies, or nil if the entire site should be compiled.

This method also accepts a few parameters:

:also_layout

true if the page rep should also be laid out and post-filtered, false if the page rep should only be pre-filtered. Only applicable to page reps, and not to asset reps. Defaults to true.

:even_when_not_outdated

true if the rep should be compiled even if it is not outdated, false if not. Defaults to false.

:from_scratch

true if all compilation stages (for page reps: pre-filter, layout, post-filter; for asset reps: filter) should be performed again even if they have already been performed, false otherwise. Defaults to false.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nanoc2/base/compiler.rb', line 37

def run(objects=nil, params={})
  # Parse params
  also_layout             = params[:also_layout]            || true
  even_when_not_outdated  = params[:even_when_not_outdated] || false
  from_scratch            = params[:from_scratch]           || false

  # Load data
  @site.load_data

  # Create output directory if necessary
  FileUtils.mkdir_p(@site.config[:output_dir])

  # Initialize
  @stack = []

  # Get pages and asset reps
  objects = @site.pages + @site.assets if objects.nil?
  reps = objects.map { |o| o.reps }.flatten

  # Compile everything
  reps.each do |rep|
    if rep.is_a?(Nanoc2::PageRep)
      rep.compile(also_layout, even_when_not_outdated, from_scratch)
    else
      rep.compile(even_when_not_outdated, from_scratch)
    end
  end
end