Skip to content

Instantly share code, notes, and snippets.

@al3x
Created April 22, 2009 23:50
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save al3x/100171 to your computer and use it in GitHub Desktop.
Save al3x/100171 to your computer and use it in GitHub Desktop.
Handy new post script for Jekyll blogs or similar
#!/usr/bin/env ruby
unless ARGV[0]
puts 'Usage: newpost "the post title"'
exit(-1)
end
date_prefix = Time.now.strftime("%Y-%m-%d")
postname = ARGV[0].strip.downcase.gsub(/ /, '-')
post = "/Users/al3x/src/al3x.github.com/_posts/#{date_prefix}-#{postname}.textile"
header = <<-END
---
layout: post
title: "#{ARGV[0]}"
---
h1. {{ page.title }}
END
File.open(post, 'w') do |f|
f << header
end
system("mate", "-a", post)
@joshlawrence
Copy link

joshlawrence commented Feb 20, 2018

I wanted to generalize your script to open the _posts directory in any path, and open whatever editor is defined in their env. This is what I came up with; please note that I'm not a ruby developer at all, so some of this may be wrong. Hope it helps!

#!/usr/bin/env ruby

require "pathname"

unless ARGV[0]
  puts 'Usage: newpost "the post title"'
  exit(-1)
end

date_prefix = Time.now.strftime("%Y-%m-%d")
postname = ARGV[0].strip.downcase.gsub(/ /, '-')
post = Pathname(__FILE__).dirname + "_posts" + "#{date_prefix}-#{postname}.md"

header = <<-END
---
layout: default
title: "#{ARGV[0]}"
---

# {{ page.title }}

END

File.open(post, 'w') do |f|
  f << header
end

system("#{ENV['EDITOR']}, post)

EDIT: nevermind, I screwed the editor part, I'll work on that and post another update.

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