Jekyllを便利に使うためのRakefile
結構前の話ですが、いまいち先の見えないOctopressに見切りをつけて、Jekyllに移行しました。何が悪かったのかわかりませんが、Octopress時代よりもページ生成が速くて快適です。以前作った並列化のコードもそのまま問題なく使えています。
ただ、移行に伴ってOctopress付属の便利なRakefileを使えなくなったので、似たようなものを書いてみました。あくまでも自分用なので、誰もが便利に使えるというものではないです。
require 'bundler'Bundler.requirerequire '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 endend
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 endend
task :rsync do config = Jekyll.configuration({}) system "rsync -avzr --delete #{config['destination']}/ #{config['rsync']['destination']}"end
利用には、parallel
とstringex
の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