Class: Nanoc3::Compiler

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

Overview

Responsible for compiling a site’s item representations.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Compiler) initialize(site)

Creates a new compiler fo the given site

Parameters:



36
37
38
39
40
41
42
43
44
# File 'lib/nanoc3/base/compiler.rb', line 36

def initialize(site)
  @site = site

  @stack = []

  @item_compilation_rules  = []
  @item_routing_rules      = []
  @layout_filter_mapping   = OrderedHash.new
end

Instance Attribute Details

- (Array<Nanoc3::Rule>) item_compilation_rules (readonly)

The list of compilation rules that will be used to compile items. This array will be filled by Site#load_data.

Returns:



19
20
21
# File 'lib/nanoc3/base/compiler.rb', line 19

def item_compilation_rules
  @item_compilation_rules
end

- (Array<Nanoc3::Rule>) item_routing_rules (readonly)

The list of routing rules that will be used to give all items a path. This array will be filled by Site#load_data.

Returns:



25
26
27
# File 'lib/nanoc3/base/compiler.rb', line 25

def item_routing_rules
  @item_routing_rules
end

- (Hash) layout_filter_mapping (readonly)

The hash containing layout-to-filter mapping rules. This hash is ordered: iterating over the hash will happen in insertion order.

Returns:

  • (Hash)

    The layout-to-filter mapping rules



31
32
33
# File 'lib/nanoc3/base/compiler.rb', line 31

def layout_filter_mapping
  @layout_filter_mapping
end

- (Array) stack (readonly)

The compilation stack. When the compiler begins compiling a rep or a layout, it will be placed on the stack; when it is done compiling the rep or layout, it will be removed from the stack.

Returns:

  • (Array)

    The compilation stack



13
14
15
# File 'lib/nanoc3/base/compiler.rb', line 13

def stack
  @stack
end

Instance Method Details

- (Nanoc3::Rule?) compilation_rule_for(rep)

Finds the first matching compilation rule for the given item representation.

or nil if no rules have been found

Parameters:

Returns:

  • (Nanoc3::Rule, nil)

    The compilation rule for the given item rep,



99
100
101
102
103
# File 'lib/nanoc3/base/compiler.rb', line 99

def compilation_rule_for(rep)
  @item_compilation_rules.find do |rule|
    rule.applicable_to?(rep.item) && rule.rep_name == rep.name
  end
end

- (Array?) filter_for_layout(layout)

Finds the filter name and arguments to use for the given layout.

arguments for the given layout.

Parameters:

  • layout (Nanoc3::Layout)

    The layout for which to fetch the filter.

Returns:

  • (Array, nil)

    A tuple containing the filter name and the filter



123
124
125
126
127
128
# File 'lib/nanoc3/base/compiler.rb', line 123

def filter_for_layout(layout)
  @layout_filter_mapping.each_pair do |layout_identifier, filter_name_and_args|
    return filter_name_and_args if layout.identifier =~ layout_identifier
  end
  nil
end

- (Nanoc3::Rule?) routing_rule_for(rep)

Finds the first matching routing rule for the given item representation.

nil if no rules have been found

Parameters:

Returns:

  • (Nanoc3::Rule, nil)

    The routing rule for the given item rep, or



111
112
113
114
115
# File 'lib/nanoc3/base/compiler.rb', line 111

def routing_rule_for(rep)
  @item_routing_rules.find do |rule|
    rule.applicable_to?(rep.item) && rule.rep_name == rep.name
  end
end

- (void) run(item = nil, params = {})

This method returns an undefined value.

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

its dependencies. Pass nil if the entire site should be compiled.

compiled even if it is not outdated, false if not

Parameters:

  • item (Nanoc3::Item) (defaults to: nil)

    The item that should be compiled, along with

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :force (Boolean) — default: false

    true if the rep should be



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nanoc3/base/compiler.rb', line 56

def run(item=nil, params={})
  # Create output directory if necessary
  FileUtils.mkdir_p(@site.config[:output_dir])

  # Load dependencies
  dependency_tracker.load_graph

  # Get items and reps to compile
  if item
    items = [ item ] + dependency_tracker.successors_of(item)
    items.uniq!
  else
    items = @site.items
  end
  reps = items.map { |i| i.reps }.flatten

  # Prepare dependencies
  if params.has_key?(:force) && params[:force]
    reps.each { |r| r.force_outdated = true }
  else
    dependency_tracker.propagate_outdatedness
  end
  forget_dependencies_if_outdated(items)

  # Compile reps
  dependency_tracker.start
  compile_reps(reps)
  dependency_tracker.stop

  # Cleanup
  FileUtils.rm_rf(Nanoc3::Filter::TMP_BINARY_ITEMS_DIR)

  # Store dependencies
  dependency_tracker.store_graph
end