forked from mgutz/nanoc3_blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
51 lines (41 loc) · 1.12 KB
/
Rakefile
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
require 'nanoc3/tasks'
require 'fileutils'
namespace :create do
desc "Creates a new article"
task :article do
$KCODE = 'UTF8'
require 'active_support/core_ext'
require 'active_support/multibyte'
@ymd = Time.now.to_s(:db).split(' ')[0]
if !ENV['title']
$stderr.puts "\t[error] Missing title argument.\n\tusage: rake create:article title='article title'"
exit 1
end
title = ENV['title'].capitalize
path, filename, full_path = calc_path(title)
if File.exists?(full_path)
$stderr.puts "\t[error] Exists #{full_path}"
exit 1
end
template = <<TEMPLATE
---
created_at: #{@ymd}
excerpt:
kind: article
publish: true
tags: [misc]
title: "#{title.titleize}"
---
TODO: Add content to `#{full_path}.`
TEMPLATE
FileUtils.mkdir_p(path) if !File.exists?(path)
File.open(full_path, 'w') { |f| f.write(template) }
$stdout.puts "\t[ok] Edit #{full_path}"
end
def calc_path(title)
year, month_day = @ymd.split('-', 2)
path = "content/" + year + "/"
filename = month_day + "-" + title.parameterize('_') + ".cr"
[path, filename, path + filename]
end
end