Jon Canady

 

Jekyll Helper Scripts

I have a couple of helper scripts that I couldn't use Jekyll without:

deploy

Shamelessly stolen from The Pug Automatic, it's pretty quick (and all on one line, despite what word-wrapping occurs here):

1 jekyll --no-auto && rsync -avz --delete _site/ blog:/pub/jcanady/joncanady.com/

Run jekyll -- I keep auto-regeneration turned on in my script, so disable that -- then rsync the site directory up to my blog's host. blog is an ssh alias. I like to have my blog's repository hosted on Github, and for various reasons Github can't connect to my blog's server, so this is nearly as good.

newpost

This one is a lifesaver. I can't remember where I found the original ruby script, but after sprinkling a little optparse over it, it became an indispensable utility; just drop it somewhere on your $PATH (~/bin/ is nice).

 1 #!/usr/bin/env ruby
 2 
 3 ###
 4 # newpost -- creates a new Jekyll post
 5 # Change the blog_dir variable below, and you should be set.
 6 # For maximum win, put in ~/bin/ (and add that to your $PATH)
 7 #
 8 # Jon Canady, 2010. 
 9 # Covered by the WTFPL: http://sam.zoy.org/wtfpl/
10 ###
11 
12 # omit the trailing slash, please
13 blog_dir = "/Users/jonc/Sites/joncanady-blog" 
14 
15 
16 require 'optparse'
17 
18 options = {}
19 
20 optparse = OptionParser.new do |opts|
21 
22   # banner
23   opts.banner = "Usage: newpost [options] \"title of post\""
24 
25   options[:draft] = false
26   opts.on('-d', '--draft', 'Create post as a draft') do 
27     options[:draft] = true
28   end
29 
30   options[:format] = :markdown
31   opts.on('-f', '--format FORMAT', 'Post is in [markdown|textile] (default markdown)') do |format|
32     valid_formats = %w(markdown textile)
33     puts opts && exit unless valid_formats.include?(format)
34     options[:format] = format
35   end
36 
37   opts.on( '-h', '--help', 'Display this screen' ) do
38     puts opts
39     exit
40   end
41 
42 end
43 
44 
45 
46 begin
47 optparse.parse!
48 rescue OptionParser::InvalidOption => e
49   puts e
50   puts o
51   exit 1
52 end
53  
54 if ARGV[0].nil?
55   optparse.display
56   exit 1
57 end
58 
59 puts "Generating post..."
60 
61 date_prefix = Time.now.strftime("%Y-%m-%d")
62 postname = ARGV[0].strip.downcase.gsub(/ /, '-')
63 
64 post_dir = options[:draft] ? '_drafts' : '_posts'
65 
66 post = "#{blog_dir}/#{post_dir}/#{date_prefix}-#{postname}.#{options[:format]}"
67  
68 header = <<-END
69 ---
70 layout: post
71 title: #{ARGV[0]}
72 ---
73  
74 END
75  
76 File.open(post, 'w') do |f|
77   f << header
78 end
79  
80 system("mate", "-a", post)

I bundle both of these with my blog's source; the latest versions can always be found in my tasks/ directory.