-
Notifications
You must be signed in to change notification settings - Fork 0
/
newpost.rb
75 lines (61 loc) · 2.04 KB
/
newpost.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env ruby
# *********************************************
# Jekyll Post Generator Awesomeness
# by Cody Krieger (http://codykrieger.com)
# edited by Tao Zhang (http://ztpala.com)
# *********************************************
# Usage:
# % ./newpost.rb POST NAME
if ARGV.empty? or ARGV[0].downcase == "--help" or ARGV[0].downcase == "-h"
puts <<-USAGE
Usage:
% ./newpost.rb POST NAME
USAGE
exit (ARGV.empty? ? 1 : 0)
end
#from http://www.java2s.com/Code/Ruby/Time/Converttimetotimezone.htm
class Time
def convert_zone(to_zone)
original_zone = ENV["TZ"]
utc_time = dup.gmtime
ENV["TZ"] = to_zone
to_zone_time = utc_time.localtime
ENV["TZ"] = original_zone
return to_zone_time
end
end
class String
# from ruby on rails (https://github.com/rails/rails)
# activesupport/lib/active_support/inflector/transliterate.rb
def parameterize(sep = '-')
# replace accented chars with their ascii equivalents
parameterized_string = self.dup
# Turn unwanted chars into the separator
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep)
unless sep.nil? || sep.empty?
re_sep = Regexp.escape(sep)
# No more than one of the separator in a row.
parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
# Remove leading/trailing separator.
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
end
parameterized_string.downcase
end
end
TEMPLATE = "template.markdown"
POSTS_DIR = "_posts"
# Get the title and use it to derive the new filename
t = Time.now
t.convert_zone("US/Eastern")
title = ARGV.join(" ")
filename = "#{t.strftime('%Y-%m-%d')}-#{title.parameterize}.md"
filepath = File.join(POSTS_DIR, filename)
# Load in the template and set the title
post_text = File.read(TEMPLATE)
post_text.gsub!('%%TITLE%%', title)
post_text.gsub!('%%DATE%%', "#{t.strftime('%Y-%m-%d %H:%M:%S')}")
# Write out the post
post_file = File.open(filepath, 'w')
post_file.puts post_text
post_file.close
system("subl #{filepath}")