-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_post.rb
executable file
·52 lines (43 loc) · 1.5 KB
/
create_post.rb
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env ruby
# Script to create a blog post using a template. It takes one input parameter
# which is the title of the blog post
# e.g. command:
# $ ./create_post.rb "helper script to create new posts using jekyll"
#
# Author:Usman Ismail
# Some constants
TEMPLATE = "_template.markdown"
TARGET_DIR = "_posts"
# Check Argument count
if ARGV.length != 5
abort "Usage: create_post.rb TITLE AUTHOR PERMALINK CATEGORY_1,CATEGORY_2 TAG_1,TAG_2"
end
# Get the title which was passed as an argument
title = ARGV[0]
authors = ARGV[1].dup
authors.gsub!(',', "\n- ");
permalink = ARGV[2]
categories = ARGV[3].dup
categories.gsub!(',', "\n- ");
tags = ARGV[4].dup
tags.gsub!(',', "\n- ");
# Get the filename and remove some unsupported characters
# TODO probably need to make list of disallowed characters more complete
filename = title.gsub(' ','-')
filename = filename.gsub(':','-')
filename = filename.gsub(';','-')
filename = "#{ Time.now.strftime('%Y-%m-%d') }-#{filename}.markdown"
filepath = File.join(TARGET_DIR, filename)
# Create a copy of the template with the title replaced
new_post = File.read(TEMPLATE)
new_post.gsub!('TITLE', title);
new_post.gsub!('PERMALINK', permalink);
new_post.gsub!('DATE', "#{ Time.now.strftime('%Y-%m-%d %H:%M:%S') }");
new_post.gsub!('AUTHORS', authors);
new_post.gsub!('CATEGORIES', categories);
new_post.gsub!('TAGS', tags);
# Write out the file to the target directory
new_post_file = File.open(filepath, 'w')
new_post_file.puts new_post
new_post_file.close
puts "created => #{filepath}"