lib/compiler.rb in nanoc-1.3.1 vs lib/compiler.rb in nanoc-1.4
- old
+ new
@@ -11,15 +11,15 @@
'.liquid' => :liquid
}
PAGE_DEFAULTS = {
:custom_path => nil,
+ :filename => 'index',
:extension => 'html',
:filters => [],
:is_draft => false,
- :layout => 'default',
- :order => 0
+ :layout => 'default'
}
def initialize
Nanoc.ensure_in_site
@@ -29,10 +29,13 @@
def run
# Require all Ruby source files in lib/
Dir['lib/*.rb'].each { |f| require f }
+ # Create output assets directory if necessary
+ FileUtils.mkdir_p(@config[:output_dir])
+
# Copy assets to output directory
copy_assets
# Compile all pages
pages = compile_pages(uncompiled_pages)
@@ -47,32 +50,31 @@
content = layouted_page(page, pages)
# Write page with layout
FileManager.create_file(path_for_page(page)) { content }
rescue => exception
- $stderr.puts "Exception occured while layouting page " +
- "'#{page[:_content_filename]}' in layout '#{page[:layout]}':" unless $quiet
- $stderr.puts exception unless $quiet
- $stderr.puts exception.backtrace.join("\n") unless $quiet
- exit
+ p = page[:_content_filename]
+ l = page[:layout]
+ handle_exception(exception, "layouting page '#{p}' in layout '#{l}'")
end
end
end
- private
+ private
# Copies the contents of the assets directory into the output directory
def copy_assets
- # Create output assets directory if necessary
- FileUtils.mkdir_p(@config[:output_dir])
-
# Remove existing assets
- Dir['assets/*'].each { |f| FileUtils.remove_entry_secure(f.sub('assets/', @config[:output_dir] + '/'), true) }
+ Dir['assets/*'].each do |f|
+ FileUtils.remove_entry_secure(f.sub('assets/', @config[:output_dir] + '/'), true)
+ end
# Copy assets
- FileUtils.cp_r(Dir['assets/*'], @config[:output_dir]) if File.directory?('assets') and !Dir['assets/*'].empty?
+ if File.directory?('assets') and !Dir['assets/*'].empty?
+ FileUtils.cp_r(Dir['assets/*'], @config[:output_dir])
+ end
end
# Returns a list of uncompiled pages
def uncompiled_pages
# Read all meta files
@@ -82,30 +84,22 @@
# Fix the path
page[:path] = filename.sub(/^content/, '').sub('meta.yaml', '')
# Get the content filename
- content_filenames = Dir[filename.sub('meta.yaml', File.basename(File.dirname(filename)) + '.*')].reject { |f| f =~ /~$/ }
+ content_filenames = Dir[filename.sub('meta.yaml', File.basename(File.dirname(filename)) + '.*')]
+ content_filenames.reject! { |f| f =~ /~$/ }
content_filenames += Dir["#{File.dirname(filename)}/index.*"] # fallback for nanoc 1.0
content_filenames.ensure_single('content files', File.dirname(filename))
page[:_content_filename] = content_filenames[0]
page
end
# Ignore drafts
pages.reject! { |page| page[:is_draft] }
- # Sort pages by order and by path
- pages.sort! do |x,y|
- if x[:order].to_i == y[:order].to_i
- x[:path] <=> y[:path]
- else
- x[:order].to_i <=> y[:order].to_i
- end
- end
-
pages
end
# Returns the layout for the given page
def layout_for_page(a_page)
@@ -121,69 +115,49 @@
end
# Returns the path for the given page
def path_for_page(a_page)
if a_page[:custom_path].nil?
- @config[:output_dir] + a_page[:path] + 'index.' + a_page[:extension]
+ @config[:output_dir] + a_page[:path] + a_page[:filename] + '.' + a_page[:extension]
else
@config[:output_dir] + a_page[:custom_path]
end
end
# Compiles the given pages and returns the compiled pages
- def compile_pages(a_pages)
+ def compile_pages(pages)
# Create page objects from given pages
- given_pages = []
- a_pages.each { |page| given_pages << Page.new(page) }
+ given_pages = []
+ pages.each { |page| given_pages << Page.new(page) }
- # Create arrays for two kinds of pages
- compiled_pages = []
- uncompiled_pages = []
- given_pages.each { |page| uncompiled_pages << page }
+ # Compile pages
+ Page.compile(given_pages)
- # Compile all pages
- given_pages.each do |page|
- # Read page
- content = File.read(page._content_filename)
-
- # Compile page
- begin
- content = content.filter(page.filters, :assigns => { :page => page, :pages => uncompiled_pages + compiled_pages })
- rescue Exception => exception
- $stderr.puts "Exception occured while compiling page " +
- "'#{page[:_content_filename]}':" unless $quiet
- $stderr.puts exception unless $quiet
- $stderr.puts exception.backtrace.join("\n") unless $quiet
- exit
- end
-
- # Move from uncompiled to compiled
- uncompiled_pages -= [ page ]
- compiled_pages += [ page ]
-
- # Create compiled page
- page[:content] = content
- end
-
- # Return all pages
- compiled_pages
+ given_pages
end
# Layouts the page
def layouted_page(a_page, a_pages)
- context = { :page => a_page.merge(:_content_filename => nil), :pages => a_pages }
+ # Find layout
layout = layout_for_page(a_page)
- content = nil
+ # Build params
+ assigns = { :page => a_page.merge(:_content_filename => nil), :pages => a_pages }
+ params = { :assigns => assigns }
+ params[:haml_options] = (a_page[:haml_options] || {}).symbolize_keys
+
+ # Layout
case layout[:type]
when :eruby
- content = layout[:content].eruby(context)
+ content = layout[:content].eruby(params)
when :haml
- content = layout[:content].haml(context)
+ content = layout[:content].haml(params)
when :markaby
- content = layout[:content].markaby(context)
+ content = layout[:content].markaby(params)
when :liquid
- content = layout[:content].liquid(context)
+ content = layout[:content].liquid(params)
+ else
+ content = nil
end
content
end