Parent

Class/Module Index [+]

Quicksearch

Nanoc::Compiler

Responsible for compiling a site’s item representations.

The compilation process makes use of notifications (see {Nanoc::NotificationCenter}) to track dependencies between items, layouts, etc. The following notifications are used:

Attributes

site[R]

@return [Nanoc::Site] The site this compiler belongs to

stack[R]

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.

@return [Array] The compilation stack

Public Class Methods

new(site) click to toggle source

Creates a new compiler fo the given site

@param [Nanoc::Site] site The site this compiler belongs to

# File lib/nanoc/base/compilation/compiler.rb, line 61
def initialize(site)
  @site = site

  @stack = []
end

Public Instance Methods

assigns_for(rep) click to toggle source

@param [Nanoc::ItemRep] rep The item representation for which the

assigns should be fetched

@return [Hash] The assigns that should be used in the next filter/layout

operation

@api private

# File lib/nanoc/base/compilation/compiler.rb, line 267
def assigns_for(rep)
  if rep.binary?
    content_or_filename_assigns = { :filename => rep.temporary_filenames[:last] }
  else
    content_or_filename_assigns = { :content => rep.content[:last] }
  end

  content_or_filename_assigns.merge({
    :item       => rep.item,
    :rep        => rep,
    :item_rep   => rep,
    :items      => site.items,
    :layouts    => site.layouts,
    :config     => site.config,
    :site       => site
  })
end
build_reps() click to toggle source

Creates the representations of all items as defined by the compilation rules.

@api private

# File lib/nanoc/base/compilation/compiler.rb, line 213
def build_reps
  items.each do |item|
    # Find matching rules
    matching_rules = rules_collection.item_compilation_rules_for(item)
    raise Nanoc::Errors::NoMatchingCompilationRuleFound.new(item) if matching_rules.empty?

    # Create reps
    rep_names = matching_rules.map { |r| r.rep_name }.uniq
    rep_names.each do |rep_name|
      item.reps << ItemRep.new(item, rep_name)
    end
  end
end
dependency_tracker() click to toggle source

Returns the dependency tracker for this site, creating it first if it does not yet exist.

@api private

@return [Nanoc::DependencyTracker] The dependency tracker for this site

# File lib/nanoc/base/compilation/compiler.rb, line 185
def dependency_tracker
  dt = Nanoc::DependencyTracker.new(@site.items + @site.layouts)
  dt.compiler = self
  dt
end
load() click to toggle source

Load the helper data that is used for compiling the site.

@api private

@return [void]

# File lib/nanoc/base/compilation/compiler.rb, line 111
def load
  return if @loaded || @loading
  @loading = true

  # Load site if necessary
  site.load

  # Preprocess
  rules_collection.load
  preprocess
  site.setup_child_parent_links
  build_reps
  route_reps

  # Load auxiliary stores
  stores.each { |s| s.load }

  @loaded = true
rescue => e
  unload
  raise e
ensure
  @loading = false
end
objects() click to toggle source

Returns all objects managed by the site (items, layouts, code snippets, site configuration and the rules).

@api private

# File lib/nanoc/base/compilation/compiler.rb, line 204
def objects
  site.items + site.layouts + site.code_snippets +
    [ site.config, rules_collection ]
end
outdatedness_checker() click to toggle source

@return [Nanoc::OutdatednessChecker] The outdatedness checker

# File lib/nanoc/base/compilation/compiler.rb, line 286
def outdatedness_checker
  Nanoc::OutdatednessChecker.new(
    :site => @site,
    :checksum_store => checksum_store,
    :dependency_tracker => dependency_tracker)
end
preprocess() click to toggle source

Runs the preprocessor.

@api private

# File lib/nanoc/base/compilation/compiler.rb, line 195
def preprocess
  return if rules_collection.preprocessor.nil?
  preprocessor_context.instance_eval(&rules_collection.preprocessor)
end
route_reps() click to toggle source

Determines the paths of all item representations.

@api private

# File lib/nanoc/base/compilation/compiler.rb, line 230
def route_reps
  reps.each do |rep|
    # Find matching rules
    rules = rules_collection.routing_rules_for(rep)
    raise Nanoc::Errors::NoMatchingRoutingRuleFound.new(rep) if rules[:last].nil?

    rules.each_pair do |snapshot, rule|
      # Get basic path by applying matching rule
      basic_path = rule.apply_to(rep, :compiler => self)
      next if basic_path.nil?
      if basic_path !~ %{^/}
        raise RuntimeError, "The path returned for the #{rep.inspect} item representation, “#{basic_path}”, does not start with a slash. Please ensure that all routing rules return a path that starts with a slash."
      end

      # Get raw path by prepending output directory
      rep.raw_paths[snapshot] = @site.config[:output_dir] + basic_path

      # Get normal path by stripping index filename
      rep.paths[snapshot] = basic_path
      @site.config[:index_filenames].each do |index_filename|
        if rep.paths[snapshot][-index_filename.length..-1] == index_filename
          # Strip and stop
          rep.paths[snapshot] = rep.paths[snapshot][0..-index_filename.length-1]
          break
        end
      end
    end
  end
end
rules_collection() click to toggle source

@return [Nanoc::RulesCollection] The collection of rules to be used

for compiling this site
# File lib/nanoc/base/compilation/compiler.rb, line 101
def rules_collection
  Nanoc::RulesCollection.new(self)
end
run(*args) click to toggle source

Compiles the site and writes out the compiled item representations.

Previous versions of nanoc (< 3.2) allowed passing items to compile, and had a “force” option to make the compiler recompile all pages, even when not outdated. These arguments and options are, as of nanoc 3.2, no longer used, and will simply be ignored when passed to {run}.

@overload run

@return [void]
# File lib/nanoc/base/compilation/compiler.rb, line 76
def run(*args)
  # Create output directory if necessary
  FileUtils.mkdir_p(@site.config[:output_dir])

  # Compile reps
  load
  @site.freeze

  # Determine which reps need to be recompiled
  forget_dependencies_if_outdated(items)

  dependency_tracker.start
  compile_reps(reps)
  dependency_tracker.stop
  store
ensure
  # Cleanup
  FileUtils.rm_rf(Nanoc::Filter::TMP_BINARY_ITEMS_DIR)
  FileUtils.rm_rf(Nanoc::ItemRep::TMP_TEXT_ITEMS_DIR)
end
store() click to toggle source

Store the modified helper data used for compiling the site.

@api private

@return [void]

# File lib/nanoc/base/compilation/compiler.rb, line 164
def store
  # Calculate rule memory
  (reps + layouts).each do |obj|
    rule_memory_store[obj] = rule_memory_calculator[obj]
  end

  # Calculate checksums
  self.objects.each do |obj|
    checksum_store[obj] = obj.checksum
  end

  # Store
  stores.each { |s| s.store }
end
unload() click to toggle source

Undoes the effects of {load}. Used when {load} raises an exception.

@api private

@return [void]

# File lib/nanoc/base/compilation/compiler.rb, line 141
def unload
  return if @unloading
  @unloading = true

  stores.each { |s| s.unload }

  @stack = []

  items.each { |item| item.reps.clear }
  site.teardown_child_parent_links
  rules_collection.unload

  site.unload

  @loaded = false
  @unloading = false
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.