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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require "stringex"
class Jekyll < Thor
    desc "new", "create a new post"
    method_option :editor, :default => "vim"
    def new(*title)
    title = title.join(" ")
    date = Time.now.strftime('%Y-%m-%d')
    filename = "_posts/#{date}-#{title.to_url}.md"

    if File.exist?(filename)
        abort("#{filename} already exists!")
    end

    puts "Creating new post: #{filename}"
    open(filename, 'w') do |post|
        post.puts "---"
        post.puts "layout: post"
        post.puts "title: \"#{title.gsub(/&/,'&amp;')}\""
        post.puts "tags: []"
        post.puts "category: general"
        post.puts "---"
    end

    system(options[:editor], filename)
    end
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