Skip to content

Instantly share code, notes, and snippets.

@bouchard
Created March 8, 2012 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouchard/2000658 to your computer and use it in GitHub Desktop.
Save bouchard/2000658 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# A few helpful tips about the Rules file:
#
# * The string given to #compile and #route are matching patterns for
# identifiers--not for paths. Therefore, you can’t match on extension.
#
# * The order of rules is important: for each item, only the first matching
# rule is applied.
#
# * Item identifiers start and end with a slash (e.g. “/about/” for the file
# “content/about.html”). To select all children, grandchildren, … of an
# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
# because “*” matches zero or more characters.
require 'compass'
Compass.add_project_configuration 'compass/config.rb'
compile '/stylesheets/*/' do
filter :sass, Compass.sass_engine_options
filter :relativize_paths, :type => :css
end
compile '/' do
filter :erb
layout 'default'
end
compile '/archive/*' do
filter :erb
layout 'default'
end
compile '*' do
# item[:extension] returns 'html.erb' for multi-dotted filename
ext = item[:extension].nil? ? nil : item[:extension].split('.').last
if ['erb', 'md', 'markdown', 'xml', 'html'].include?(ext) || ext.nil?
filter :erb
filter :redcarpet2 if ['md', 'markdown'].include?(ext)
filter :rubypants
elsif ['ico'].include?(ext)
else
raise "Filter is not configured for #{item.identifier} in Rules file."
end
unless ['xml', 'ico'].include?(ext)
item[:kind] ||= 'post'
layout "_#{item[:kind]}"
snapshot :guts
layout 'default'
end
end
route '/favicon/' do
'/favicon.ico'
end
route '/atom/' do
'/atom.xml'
end
route '/stylesheets/*/' do
item.identifier.chop + '.css'
end
route '*' do
route_path(item)
end
layout '*', :erb
preprocess do
def paginate_posts
posts_to_paginate = sorted_posts
# Slice off the first :page_size posts since we include these in the index.
posts_to_paginate.slice!(-(@config[:page_size])..-1)
grouped_posts = []
until posts_to_paginate.empty?
grouped_posts << posts_to_paginate.slice!(0..(@config[:page_size] - 1))
end
# first = 1
# last = [grouped_posts.first.length, @config[:page_size]].min + 1
grouped_posts.each_with_index do |subposts, i|
@items << Nanoc3::Item.new(
"<%= render 'archive_page', :identifiers => #{subposts.map(&:identifier)}, :last_page => #{i + 1 == grouped_posts.length}, :i => #{i} %>",
{ :title => "Archive (Posts #{subposts.first[:created_at]} to #{subposts.last[:created_at]})" },
"/archive/#{i + 1}/"
)
# first += subposts.length + 1
# last += subposts.length + 1
end
end
def normalize_dates
require 'chronic'
sorted_posts.each do |i|
i[:created_at] = Chronic.parse(i[:created_at].to_s)
end
end
def collate_links_on_homepage
return unless latest_links.length > 0
ll = latest_links
k = @items.index{ |i| i.identifier == ll.first.identifier }
created_at = @items[k][:created_at] || Time.now
@items << Nanoc3::Item.new(
"<%= render '_latest_links' %>",
{
:title => 'Latest Links',
:created_at => created_at,
:kind => 'latest_links'
},
"/posts/latest_links/"
)
ll.collect{ |l| item_by_identifier(l.identifier)[:in_latest_links] = true }
# @items.delete_if{ |item| ll.map(&:identifier).include?(item.identifier) }
end
copy_assets
@items.delete_if { |item| [false, 'false'].include?(item[:published]) }
normalize_dates
collate_links_on_homepage
paginate_posts
end
Captain! We’ve been hit!
=== MESSAGE:
Nanoc::Errors::NoSuchSnapshot: The “<Nanoc::ItemRep name=default binary=false raw_path=output/posts/2012-03-08-advice-blah-blah/index.html item.identifier=/posts/2012-03-08-advice_blah_blah/>” item rep does not have a snapshot “:guts”
=== COMPILATION STACK:
(empty)
=== BACKTRACE:
0. /Users/Crash/nanoc/lib/nanoc/base/result_data/item_rep.rb:254:in `compiled_content'
1. /Users/Crash/nanoc/lib/nanoc/base/source_data/item.rb:135:in `compiled_content'
2. item / (rep default):2:in `block in get_binding'
3. item / (rep default):1:in `each'
4. item / (rep default):1:in `get_binding'
5. /Users/Crash/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/erb.rb:838:in `eval'
6. /Users/Crash/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/erb.rb:838:in `result'
7. /Users/Crash/nanoc/lib/nanoc/filters/erb.rb:31:in `run'
8. /Users/Crash/nanoc/lib/nanoc/base/result_data/item_rep.rb:344:in `filter'
9. /Users/Crash/nanoc/lib/nanoc/base/compilation/item_rep_proxy.rb:47:in `filter'
10. /Users/Crash/nanoc/lib/nanoc/base/compilation/rule_context.rb:54:in `filter'
11. ./Rules:25:in `block in load'
12. /Users/Crash/nanoc/lib/nanoc/base/compilation/rule.rb:68:in `instance_eval'
13. /Users/Crash/nanoc/lib/nanoc/base/compilation/rule.rb:68:in `apply_to'
14. /Users/Crash/nanoc/lib/nanoc/base/compilation/compiler.rb:379:in `compile_rep'
15. /Users/Crash/nanoc/lib/nanoc/base/compilation/compiler.rb:332:in `block in compile_reps'
16. /Users/Crash/nanoc/lib/nanoc/base/compilation/compiler.rb:325:in `loop'
17. /Users/Crash/nanoc/lib/nanoc/base/compilation/compiler.rb:325:in `compile_reps'
18. /Users/Crash/nanoc/lib/nanoc/base/compilation/compiler.rb:85:in `run'
19. /Users/Crash/nanoc/lib/nanoc/base/source_data/site.rb:57:in `compile'
20. /Users/Crash/nanoc/lib/nanoc/cli/commands/compile.rb:57:in `run'
21. /Users/Crash/nanoc/lib/nanoc/cli/command_runner.rb:14:in `block in call'
22. /Users/Crash/nanoc/lib/nanoc/cli/error_handler.rb:42:in `handle_while'
23. /Users/Crash/nanoc/lib/nanoc/cli/error_handler.rb:23:in `handle_while'
24. /Users/Crash/nanoc/lib/nanoc/cli/command_runner.rb:13:in `call'
25. /Users/Crash/.rvm/gems/ruby-1.9.3-p125/gems/cri-2.2.0/lib/cri/command_dsl.rb:175:in `block in runner'
26. /Users/Crash/.rvm/gems/ruby-1.9.3-p125/gems/cri-2.2.0/lib/cri/command.rb:291:in `call'
27. /Users/Crash/.rvm/gems/ruby-1.9.3-p125/gems/cri-2.2.0/lib/cri/command.rb:291:in `run_this'
28. /Users/Crash/.rvm/gems/ruby-1.9.3-p125/gems/cri-2.2.0/lib/cri/command.rb:244:in `run'
29. /Users/Crash/.rvm/gems/ruby-1.9.3-p125/gems/cri-2.2.0/lib/cri/command.rb:257:in `run'
30. /Users/Crash/nanoc/lib/nanoc/cli.rb:44:in `block in run'
31. /Users/Crash/nanoc/lib/nanoc/cli/error_handler.rb:42:in `handle_while'
32. /Users/Crash/nanoc/lib/nanoc/cli/error_handler.rb:23:in `handle_while'
33. /Users/Crash/nanoc/lib/nanoc/cli.rb:41:in `run'
34. /Users/Crash/nanoc/bin/nanoc:12:in `<top (required)>'
35. /Users/Crash/.rvm/gems/ruby-1.9.3-p125/bin/nanoc:19:in `load'
36. /Users/Crash/.rvm/gems/ruby-1.9.3-p125/bin/nanoc:19:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment