Skip to content

Instantly share code, notes, and snippets.

@tnull
Forked from al3x/newpost.rb
Last active August 29, 2015 14:08
Show Gist options
  • Save tnull/34ebd2ea9efcf5209857 to your computer and use it in GitHub Desktop.
Save tnull/34ebd2ea9efcf5209857 to your computer and use it in GitHub Desktop.
Small ruby script to create a new jekyll post, open the editor, start 'jekyll serve' and open the localhost site in a browser.
#!/usr/bin/env ruby
unless ARGV[0]
puts 'Usage: newpost "the post title"'
exit(-1)
end
jekyll_site_prefix = "."
editor = "open -a Byword"
date_now = Time.now
date_prefix = date_now.strftime("%Y-%m-%d")
date_header = date_now.strftime("%Y-%m-%d %H:%M:%S")
post_title = ARGV.join(" ")
post_filename = date_prefix + "-" + post_title.strip.downcase.gsub(/ /, '-')
post_filepath = "#{jekyll_site_prefix}/_posts/#{post_filename}.md"
header = <<-END
---
layout: post
title: "#{post_title}"
date: #{date_header}
---
END
File.open(post_filepath, 'w') do |f|
f << header
end
system("touch #{post_filepath}")
system(editor + " #{post_filepath}")
system("jekyll serve -s #{jekyll_site_prefix}")
system("open http://localhost:4000")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment