forked from Rostlab/biojs.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newpost.rb
executable file
·42 lines (36 loc) · 935 Bytes
/
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
#!/usr/bin/ruby
# Create new jekyll post and opens in vim
# $ ruby _new.rb This is the title
# adapted from https://gist.github.com/brantfaircloth/608362#file-newpost-rb
# The arguments form the title
unless ARGV[0]
raise "Please provide a post title."
end
# Create a URL slug from the title
def slugify(title)
str = title.dup
str.gsub!(/[^a-zA-Z0-9 ]/,"")
str.gsub!(/[ ]+/," ")
str.gsub!(/ /,"-")
str.downcase!
str
end
# Create parameters
root = File.dirname(__FILE__)
title = ARGV.join(' ')
slug = slugify(title)
prefix = Time.now.strftime("%Y-%m-%d")
datetime = Time.now.strftime("%Y-%m-%d %H:%M:%S")
file = "#{prefix}-#{slug}.markdown"
path = File.join(root, "_posts/#{file}")
text = <<-eos
---
title: "#{title}"
layout: post
date: #{datetime}
---
eos
# Create a new file and open it in vim
File.open(path, 'w') { |f| f.write(text) }
system("git", "add", path)
system("vim", "+6", path)