Drafting with Octopress 2 is a pain. At some point, the meta option published: false started blocking both preview (bad) and deployment (good). Jekyll supports jekyll build --drafts but somehow jekyll deploy deploys even drafts in Octopress 2. Below, I share some quick fixes I wrote to maintain drafts.
The problem
We need drafts to be previewable with rake preview and the drafts should not be deployed with rake deploy.
A revised workflow
Create a directory cd source && mkdir _drafts
(see below) Create a draft using rake new_draft[some title]
(see below) Preview drafts and posts rake preview
(when done with a draft) ‘mv path_to_draft path_to_post’ (prepend an appropriate date to the filename manually)
(see below) Deploy posts only with rake deploy (double check drafts are not included).
All you just need is modify Rakefile (Forgive my bad Ruby skills—I just needed them done fast).
Step 2
12345678910111213141516171819202122232425
drafts_dir="_drafts"# directorytask:new_draft,:titledo|t,args|ifargs.titletitle=args.titleelsetitle=get_stdin("Enter a title for your post: ")endraise"### You haven't set anything up yet. First run `rake install` to set up an Octopress theme."unlessFile.directory?(source_dir)mkdir_p"#{source_dir}/#{drafts_dir}"filename="#{source_dir}/#{drafts_dir}/#{title.to_url}.#{new_post_ext}"ifFile.exist?(filename)abort("rake aborted!")ifask("#{filename} already exists. Do you want to overwrite?",['y','n'])=='n'endputs"Creating new post: #{filename}"open(filename,'w')do|post|post.puts"---"post.puts"layout: post"post.puts"title: \"#{title.gsub(/&/,'&')}\""post.puts"date: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"post.puts"comments: true"post.puts"categories: "post.puts"---"endend
Step 3
1234567891011121314151617
desc"preview without draft the site in a web browser"task:previewdoraise"### You haven't set anything up yet. First run `rake install` to set up an Octopress theme."unlessFile.directory?(source_dir)puts"Starting to watch source with Jekyll and Compass. Starting Rack on port #{server_port}"system"compass compile --css-dir #{source_dir}/stylesheets"unlessFile.exist?("#{source_dir}/stylesheets/screen.css")#jekyllPid = Process.spawn({"OCTOPRESS_ENV"=>"preview"}, "jekyll build --watch")jekyllPid=Process.spawn({"OCTOPRESS_ENV"=>"preview"},"jekyll serve --drafts --trace")compassPid=Process.spawn("compass watch")rackupPid=Process.spawn("rackup --port #{server_port}")trap("INT"){[jekyllPid,compassPid,rackupPid].each{|pid|Process.kill(9,pid)rescueErrno::ESRCH}exit0}[jekyllPid,compassPid,rackupPid].each{|pid|Process.wait(pid)}end
Step 5
1234567891011121314151617181920212223
desc"Default deploy task"task:deploydo# Check if preview posts exist, which should not be publishedifFile.exists?(".preview-mode")puts"## Found posts in preview mode, regenerating files ..."File.delete(".preview-mode")Rake::Task[:generate].executeendRake::Task[:copydot].invoke(source_dir,public_dir)# exclude drafts in public/ before deployment# we compare each post in source/_drafts/ against one in public/Dir.foreach("#{source_dir}/#{drafts_dir}")do|item|file_ext=File.extname(item)puts"draft file candidate: #{file_ext}"iffile_ext==".markdown"orfile_ext==".md"file_base=File.basename(item,file_ext)puts"removing a draft: #{public_dir}/#{file_base}"rm_rf("#{public_dir}/#{file_base}")endendRake::Task["#{deploy_default}"].executeend