How to Maintain Drafts for Octopress 2

Last updated: Mar 15, 2018

jekyll, octopress

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

  1. Create a directory cd source && mkdir _drafts
  2. (see below) Create a draft using rake new_draft[some title]
  3. (see below) Preview drafts and posts rake preview
  4. (when done with a draft) ‘mv path_to_draft path_to_post’ (prepend an appropriate date to the filename manually)
  5. (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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
drafts_dir = "_drafts"    # directory

task :new_draft, :title do |t, args|
  if args.title
    title = args.title
  else
    title = get_stdin("Enter a title for your post: ")
  end
  raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
  mkdir_p "#{source_dir}/#{drafts_dir}"
  filename = "#{source_dir}/#{drafts_dir}/#{title.to_url}.#{new_post_ext}"
  if File.exist?(filename)
    abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
  end
  puts "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 "---"
  end
end

Step 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
desc "preview without draft the site in a web browser"
task :preview do
  raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.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" unless File.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) rescue Errno::ESRCH }
    exit 0
  }

  [jekyllPid, compassPid, rackupPid].each { |pid| Process.wait(pid) }
end

Step 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
desc "Default deploy task"
task :deploy do
  # Check if preview posts exist, which should not be published
  if File.exists?(".preview-mode")
    puts "## Found posts in preview mode, regenerating files ..."
    File.delete(".preview-mode")
    Rake::Task[:generate].execute
  end
  Rake::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}"
    if file_ext == ".markdown" or file_ext == ".md"
        file_base = File.basename(item, file_ext)
        puts "removing a draft: #{public_dir}/#{file_base}"
        rm_rf("#{public_dir}/#{file_base}")
    end
  end
  Rake::Task["#{deploy_default}"].execute
end
comments powered by Disqus