lib/nanoc/base/compiler.rb in nanoc-2.0.4 vs lib/nanoc/base/compiler.rb in nanoc-2.1
- old
+ new
@@ -1,55 +1,67 @@
module Nanoc
+
+ # Nanoc::Compiler is responsible for compiling a site's page and asset
+ # representations.
class Compiler
attr_reader :stack
+ # Creates a new compiler for the given site.
def initialize(site)
- @site = site
+ @site = site
+ @stack = []
end
- def run(page=nil)
- # Give feedback
- log(:high, "Compiling #{page.nil? ? 'site' : 'page'}...")
- time_before = Time.now
+ # 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.
+ 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
- # Get the data we need
+ # Load data
@site.load_data
- eval(@site.code, $nanoc_binding)
# Create output directory if necessary
FileUtils.mkdir_p(@site.config[:output_dir])
- # Compile
+ # Initialize
@stack = []
- pages = (page.nil? ? @site.pages : [ page ])
- pages.each do |current_page|
- begin
- current_page.compile
- rescue => exception
- handle_exception(exception, current_page, !page.nil?)
- end
- end
- # Give feedback
- log(:high, "No pages were modified.") unless pages.any? { |page| page.modified? }
- log(:high, "#{page.nil? ? 'Site' : 'Pages'} compiled in #{format('%.2f', Time.now - time_before)}s.")
- end
+ # Get pages and asset reps
+ objects = @site.pages + @site.assets if objects.nil?
+ reps = objects.map { |o| o.reps }.flatten
- def handle_exception(exception, page, single_page)
- raise exception if single_page
-
- log(:high, "ERROR: An exception occured while compiling page #{page.path}.", $stderr)
- log(:high, "", $stderr)
- log(:high, "If you think this is a bug in nanoc, please do report it at", $stderr)
- log(:high, "<http://nanoc.stoneship.org/trac/newticket> -- thanks!", $stderr)
- log(:high, "", $stderr)
- log(:high, 'Message:', $stderr)
- log(:high, ' ' + exception.message, $stderr)
- log(:high, 'Backtrace:', $stderr)
- log(:high, exception.backtrace.map { |t| ' - ' + t }.join("\n"), $stderr)
-
- exit(1)
+ # Compile everything
+ reps.each do |rep|
+ if rep.is_a?(Nanoc::PageRep)
+ rep.compile(also_layout, even_when_not_outdated, from_scratch)
+ else
+ rep.compile(even_when_not_outdated, from_scratch)
+ end
+ end
end
end
end