A test post with Thor

I found this little tool that helps create a post for github-pages with Jekyll from the command line. It creates the file with the appropriate URL-friendly name and it opens your choice editor so you can start writing your content immediately.

Usage

Make sure you have the thor and stringex gems, then create a jekyll.thor file in the root directory of your github-pages project with the following content:

 1 require "stringex"
 2 class Jekyll < Thor
 3     desc "new", "create a new post"
 4     method_option :editor, :default => "vim"
 5     def new(*title)
 6     title = title.join(" ")
 7     date = Time.now.strftime('%Y-%m-%d')
 8     filename = "_posts/#{date}-#{title.to_url}.md"
 9 
10     if File.exist?(filename)
11         abort("#{filename} already exists!")
12     end
13 
14     puts "Creating new post: #{filename}"
15     open(filename, 'w') do |post|
16         post.puts "---"
17         post.puts "layout: post"
18         post.puts "title: \"#{title.gsub(/&/,'&amp;')}\""
19         post.puts "tags: []"
20         post.puts "category: general"
21         post.puts "---"
22     end
23 
24     system(options[:editor], filename)
25     end
26 end

I modified it so it uses vim as the default editor, the tags are a one-liner list and there is a default category.

Update (Mar 20, 2014): of course I didn’t say how to use it, forgot it, and now had to came back to the same link and read it again. Here it is for a more immediate reference:

thor jekyll:new The title of the new post

Published: March 17, 2014

blog comments powered by Disqus