Skip to content

Instantly share code, notes, and snippets.

@zanshin
Created August 24, 2012 02:31
Show Gist options
  • Save zanshin/3444886 to your computer and use it in GitHub Desktop.
Save zanshin/3444886 to your computer and use it in GitHub Desktop.
Ruby script to create new Octopress post, isolate it, and open it in an editor
#!/usr/bin/env ruby
#
# newpost.rb new-post-title
#
# Author: Mark Nichols, 8/2011
#
# This script automates the process of creating a new Octopress posting.
# 1. The Octopress rake new_post task is called passing in the posting name
# 2. The Octopress rake isolate taks is called to sequester all other postings in the _stash
# 3. The new file is opened in TextMate
#
# This script requires two parameters:
# 1. the sub-directory under ~/Project/octopress where the weblog is located, and
# 2. the title of the new posting to be created. The title needs to be double-quote delimited as it may contain spaces.
#
# This script expects the blogs to be siblings under the same parent directory. Futher, a symbolic link of `Rakefile.rake`
# that points to the provided `Rakefile` needs to be created.
#
# mhn 2012-01-12
require 'rake'
# where your Octopress Rakefile lives...
WEBLOG = ARGV[0]
BLOG_DIR = "/Users/mark/Projects/octopress/#{WEBLOG}"
POST_DIR = "#{BLOG_DIR}/source/_posts"
# build the rake new_post command
#new_title = '"' + ARGV[1] + '"'
new_title = ARGV[1]
rake_new_post = "new_post[#{new_title}]"
puts "Running: " + rake_new_post + " in "+ BLOG_DIR
# stuff to capture output
def capture_stdout
s = StringIO.new
oldstdout = $stdout
$stdout = s
yield
s.string
ensure
$stdout = oldstdout
end
Dir.chdir(BLOG_DIR) do
# setup rake and then create a new_post
Rake.application.init
Rake.application.rake_require("Rakefile", paths=["#{BLOG_DIR}"])
new_result = capture_stdout {Rake.application.invoke_task("#{rake_new_post}")}
puts new_result
# parse the new_post result into the file name, isolate it, and open it for editing
title_slug = new_result.split("/")
new_post = title_slug[2].strip
rake_isolate = "isolate[#{new_post}]"
puts "Running: " + rake_isolate
isolate_result = capture_stdout {Rake.application.invoke_task("#{rake_isolate}")}
puts "Posting created and isolated, opening in editor..."
exec "mate #{POST_DIR}/#{new_post}"
end
@irosenb
Copy link

irosenb commented Jul 4, 2013

How do you run this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment