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
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/hawkins/post.rb', line 28
def create(args, options)
options["date"] ||= Time.now.to_s
options["editor"] ||= ENV['VISUAL'] || ENV['EDITOR'] || 'vi'
begin
date = Date.parse(options["date"])
rescue
Jekyll.logger.abort_with("Could not convert #{options['date']} into date format.")
end
if args.length != 1
Jekyll.logger.abort_with(
"Please provide an argument to use as the post title.\n
Remember to quote multiword strings.")
else
title = args[0]
end
slug = Jekyll::Utils.slugify(title)
site_opts = configuration_from_options(options)
site = Jekyll::Site.new(site_opts)
posts = site.in_source_dir('_posts')
filename = File.join(posts, "#{date.strftime('%Y-%m-%d')}-#{slug}.md")
unless File.exist?(posts)
Jekyll.logger.abort_with("#{posts} does not exist. Please create it.")
end
if File.exist?(filename)
Jekyll.logger.abort_with(
"#{filename} already exists. Cowardly refusing to overwrite it.")
end
content = <<-CONTENT
---
layout: post
title: #{title}
---
CONTENT
File.open(filename, 'w') do |f|
f.write(Jekyll::Utils.strip_heredoc(content))
end
Jekyll.logger.info("Wrote #{filename}")
case options["editor"]
when /g?vim/
editor_args = "+"
when /x?emacs/
editor_args = "+#{content.lines.count}"
else
editor_args = nil
end
exec(*[options["editor"], editor_args, filename].compact)
end
|