-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathThorfile
39 lines (34 loc) · 1 KB
/
Thorfile
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
require "fileutils"
require "shellwords"
class Blog < Thor
TEMPLATE = (<<-TEXT).gsub(/^ +/, '')
---
layout: post
title: TITLE
author: YOUR NAME
author_url: https://sinatra.github.com/
publish_date: #{Time.now.strftime('%A, %B %d, %Y')}
---
POST CONTENT HERE
TEXT
desc "new", "Create a new blog post and open in EDITOR"
def new(title=nil)
abort("usage: thor blog:new 'Post Title'") if title.nil?
post = TEMPLATE.sub('TITLE', title)
date = Time.now.strftime('%Y-%m-%d')
file = "_posts/#{date}-#{title.downcase.gsub(/[!.,;:+=-]/, '').gsub(/\W+/, '-')}.markdown"
File.open(file, 'wb') { |f| f.write(post) }
editor = ENV['VISUAL'] || ENV['EDITOR']
if !editor
abort("Either set $VISUAL or $EDITOR")
else
commands = Shellwords.shellwords(editor)
commands << file
success = system(*commands)
if !success
abort("Could not run '#{editor} #{file}', exit code: #{$?.exitstatus}")
end
end
end
end
# vim: ft=ruby