forked from jekyll/jekyll-compose
/
post.rb
78 lines (62 loc) · 2.32 KB
/
post.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
76
77
78
# frozen_string_literal: true
module Jekyll
module Commands
class Post < Command
def self.init_with_program(prog)
prog.command(:post) do |c|
c.syntax "post NAME"
c.description "Creates a new post with the given NAME"
options.each { |opt| c.option *opt }
c.action { |args, options| process args, options }
end
end
def self.options
[
["extension", "-x EXTENSION", "--extension EXTENSION", "Specify the file extension"],
["layout", "-l LAYOUT", "--layout LAYOUT", "Specify the post layout"],
["force", "-f", "--force", "Overwrite a post if it already exists"],
["date", "-d DATE", "--date DATE", "Specify the post date"],
["config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"],
["source", "-s", "--source SOURCE", "Custom source directory"],
]
end
def self.process(args = [], options = {})
config = configuration_from_options(options)
params = PostArgParser.new args, options, config
params.validate!
post = PostFileInfo.new params
file_creator = Compose::FileCreator.new(post, params.force?, params.source)
file_creator.create!
Compose::FileEditor.bootstrap(config)
Compose::FileEditor.open_editor(file_creator.file_path)
end
class PostArgParser < Compose::ArgParser
def date
options["date"].nil? ? Time.now : Date.parse(options["date"])
end
end
class PostFileInfo < Compose::FileInfo
def resource_type
"post"
end
def path
"_posts/#{file_name}"
end
def file_name
"#{_date_stamp}-#{super}"
end
def _date_stamp
@params.date.strftime Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT
end
def _time_stamp
@params.date.strftime Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT
end
def content(custom_front_matter = {})
default_front_matter = params.config.dig("jekyll_compose", "post_default_front_matter")
custom_front_matter.merge!(default_front_matter) if default_front_matter.is_a?(Hash)
super({ "date" => _time_stamp }.merge(custom_front_matter))
end
end
end
end
end