monolithic kernel

Jekyllを便利に使うためのRakefile

結構前の話ですが、いまいち先の見えないOctopressに見切りをつけて、Jekyllに移行しました。何が悪かったのかわかりませんが、Octopress時代よりもページ生成が速くて快適です。以前作った並列化のコードもそのまま問題なく使えています。

ただ、移行に伴ってOctopress付属の便利なRakefileを使えなくなったので、似たようなものを書いてみました。あくまでも自分用なので、誰もが便利に使えるというものではないです。

require 'bundler'
Bundler.require
require 'zlib'
task :new, :title do |t, args|
title = args.title or exit
config = Jekyll.configuration({})
open(File.join(config['source'], '_posts', "#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.markdown"), 'w') do |f|
f << <<-EOS
---
title: #{title}
date: #{Time.now.strftime('%Y-%m-%d %H:%M')}
---
EOS
end
end
task :build do
config = Jekyll.configuration({})
Jekyll::Commands::Build.process(config)
Parallel.each(Dir.glob(File.join(config['destination'], '**', '*.{css,html,js,xml}'))) do |f|
Zlib::GzipWriter.open("#{f}.gz") do |gz|
gz.puts open(f) {|f| f.read }
end
end
end
task :rsync do
config = Jekyll.configuration({})
system "rsync -avzr --delete #{config['destination']}/ #{config['rsync']['destination']}"
end

利用には、parallelstringexのgemが必要です。

このRakefileではnew build rsyncの3つのタスクを定義しています。

newは、新しくpostを作るタスクです。rake new[title]として使えるもので、Octopressのnew_postとだいたい同じです。

buildは、jelyll buildしたのちにHTMLやCSSなどをgzip圧縮したファイルを生成するタスクです。nginxのgzip_staticを利用することで、クライアントに応じてgzip圧縮前後のファイルを出し分けられます。

rsyncは、名前の通りrsyncを実行するタスクです。_config.ymlに以下のように書いておくことでデプロイできますが、それだけです。

rsync:
destination: host:/usr/share/nginx/html

Related articles