Skip to content

Instantly share code, notes, and snippets.

@irosenb
Forked from zanshin/newpost.rb
Last active December 19, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irosenb/5929675 to your computer and use it in GitHub Desktop.
Save irosenb/5929675 to your computer and use it in GitHub Desktop.
newpost for Mou.
#!/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 "open -a Mou #{POST_DIR}/#{new_post}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment