diff --git a/.gitignore b/.gitignore index 2e2251a..b740f93 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ assets assets/* public/* +source/blog/tag/* diff --git a/Rakefile b/Rakefile index bdb8534..a462c35 100644 --- a/Rakefile +++ b/Rakefile @@ -1,12 +1,40 @@ +$: << File.expand_path(File.join(File.dirname(__FILE__), "lib")) + +require 'fileutils' # # Config # -$server = "jimeh.me" -$user = "jimeh" -$path = "jimeh.me/www" +SERVER = "jimeh.me" +USER = "jimeh" +REMOTE_PATH = "jimeh.me/www" +RSYNC_TARGET = "#{USER}@#{SERVER}:#{REMOTE_PATH}" + +TAGS_DIR = "source/blog/tag" + +SITE_SRC = "source/site" +SITE_DEST = "public" + +BLOG_SRC = "source/blog" +BLOG_DEST = "public/blog" + +# +# Jekyll init +# + +namespace :jekyll do + task :initialize do + gem "jekyll" + require "jekyll" + require "jekyll/tags/post" + require "jekyll/tags/related" + @options = Jekyll.configuration("auto" => false, "source" => BLOG_SRC, "destination" => BLOG_DEST) + @blog = Jekyll::Site.new(@options) + @blog.read_posts("") + end +end # # Build tasks @@ -24,16 +52,31 @@ desc "Build site, excluding JavaScript libs." task :build => "build:default" namespace :build do - task :default do + task :default => ["jekyll:initialize", "build:tags"] do system "jekyll ./source/site ./public" - system "jekyll ./source/blog ./public/blog" + @blog.process end + + desc "Build tags" + task :tags => "jekyll:initialize" do + FileUtils.rm_rf(TAGS_DIR) + FileUtils.mkdir_p(TAGS_DIR) + tags = @blog.categories + tags.each do |tag, posts| + File.open(File.join(TAGS_DIR, tag + ".html"), "w") do |file| + generate_index posts, file, "title" => "#{tag}" + end + end + end + desc "Compress JavaScript libs specified in the Jimfile." task :js do system "jim compress" end + desc "Build site and compress JavaScript libs" task :all => ["build", "build:js"] + end desc "Sync assets into public folder for local testing." @@ -78,24 +121,24 @@ task :deploy => "deploy:default" namespace :deploy do task :default do - rsync "public/", "#{$user}@#{$server}:#{$path}" + rsync "public/", RSYNC_TARGET end desc "Deploy assets folder to remote server." task :assets do - rsync "assets/", "#{$user}@#{$server}:#{$path}" + rsync "assets/", RSYNC_TARGET end desc "Deploy both public and assets folders to remote server." task :all => "build:all" do - rsync ["public/", "assets/"], "#{$user}@#{$server}:#{$path}" + rsync ["public/", "assets/"], RSYNC_TARGET end desc "Deploy all via rsync removing remote files that don't exist locally." task :clean => "build:all" do - rsync ["public/", "assets/"], "#{$user}@#{$server}:#{$path}", ["--delete"] + rsync ["public/", "assets/"], RSYNC_TARGET, ["--delete"] end desc "Reset remote files completely via 'rm -rf' and redeploy everything via rsync." task :reset => "build:all" do - system "ssh #{$user}@#{$server} 'cd \"#{$path}\" && rm -rf ./* && rm -rf ./.*'" - rsync ["public/", "assets/"], "#{$user}@#{$server}:#{$path}" + system "ssh #{USER}@#{SERVER} 'cd \"#{REMOTE_PATH}\" && rm -rf ./* && rm -rf ./.*'" + rsync ["public/", "assets/"], RSYNC_TARGET end end @@ -114,3 +157,12 @@ def rsync(source, dest, options = []) system "rsync -vr #{options.join(" ")} #{source} #{dest}" end +def generate_index(posts, file, options = {}) + file.puts YAML.dump(options.merge({"layout" => "tag-page", "robots" => "noindex"})) + file.puts "---" + posts = posts.sort{|x, y| x.date <=> y.date }.reverse! + output = posts.collect do |post| + %{{% post #{post.url} %}} + end.join("\n") + file.puts(output) +end diff --git a/lib/jekyll/tags/post.rb b/lib/jekyll/tags/post.rb new file mode 100644 index 0000000..759f26f --- /dev/null +++ b/lib/jekyll/tags/post.rb @@ -0,0 +1,37 @@ +module Tomafro + module Jekyll + module Tags + # {% post "2009/08/this-is-the-url" %} + + class PostTag < Liquid::Tag + def initialize(tag_name, url, tokens) + super + @url = url.strip + @file = 'post-div.html' + end + + def render(context) + context.stack do + post = context.registers[:site].posts.detect {|x| x.url == @url } + context.scopes.last['page'] = post + context.scopes.last['body'] = post.content + Dir.chdir(File.join(context.registers[:site].source, '_includes')) do + choices = Dir['**/*'].reject { |x| File.symlink?(x) } + if choices.include?(@file) + source = File.read(@file) + partial = Liquid::Template.parse(source) + context.stack do + partial.render(context) + end + else + "Included file '#{@file}' not found in _includes directory" + end + end + end + end + end + end + end +end + +Liquid::Template.register_tag('post', Tomafro::Jekyll::Tags::PostTag) diff --git a/lib/jekyll/tags/related.rb b/lib/jekyll/tags/related.rb new file mode 100644 index 0000000..1832042 --- /dev/null +++ b/lib/jekyll/tags/related.rb @@ -0,0 +1,51 @@ +module Tomafro + module Jekyll + module Tags + # {% post "2009/08/this-is-the-url" %} + + class RelatedTag < Liquid::Tag + def initialize(tag_name, url, tokens) + super + @url = url.strip + @file = 'related-posts.markdown' + end + + def render(context) + context.stack do + site = context.registers[:site] + post = site.posts.detect {|x| x.url == context['page']['url'] } + related = post.categories.inject([]) do |memo, tag| + memo << site.posts.select {|p| p.categories.include?(tag) } + end + related = (related.flatten.uniq - [post]) + + # top up related if less than 5 entries + + if related.size < 7 + missing = 7 - related.size + candidates = (site.posts - related - [post]) + related = related + candidates[0..missing] + end + + context.scopes.last['related'] = related + + Dir.chdir(File.join(context.registers[:site].source, '_includes')) do + choices = Dir['**/*'].reject { |x| File.symlink?(x) } + if choices.include?(@file) + source = File.read(@file) + partial = Liquid::Template.parse(source) + context.stack do + partial.render(context) + end + else + "Included file '#{@file}' not found in _includes directory" + end + end + end + end + end + end + end +end + +Liquid::Template.register_tag('related', Tomafro::Jekyll::Tags::RelatedTag) diff --git a/source/blog/_config.yml b/source/blog/_config.yml index cc79dd7..e104bf1 100644 --- a/source/blog/_config.yml +++ b/source/blog/_config.yml @@ -1,7 +1,7 @@ auto: false destination: ../../public/blog markdown: rdiscount -permalink: pretty +permalink: /:year/:month/:day/:title paginate: 8 pygments: true lsi: false \ No newline at end of file diff --git a/source/blog/_includes/paginator-empty.html b/source/blog/_includes/paginator-empty.html new file mode 100644 index 0000000..eee6ea4 --- /dev/null +++ b/source/blog/_includes/paginator-empty.html @@ -0,0 +1,4 @@ +
\ No newline at end of file diff --git a/source/blog/_includes/paginator.html b/source/blog/_includes/paginator.html new file mode 100644 index 0000000..a95a6eb --- /dev/null +++ b/source/blog/_includes/paginator.html @@ -0,0 +1,14 @@ ++ Comments +
+