Skip to content

Commit 8dbf1e8

Browse files
committed
Refactor publish command
1 parent 8f68701 commit 8dbf1e8

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

lib/jekyll/commands/post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def self.process(args = [], options = {})
3333

3434
class PostArgParser < Compose::ArgParser
3535
def date
36-
date = options["date"].nil? ? Time.now : DateTime.parse(options["date"])
36+
options["date"].nil? ? Time.now : DateTime.parse(options["date"])
3737
end
3838
end
3939

lib/jekyll/commands/publish.rb

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,84 @@ def self.init_with_program(prog)
1515
end
1616

1717
def self.process(args = [], options = {})
18+
params = PublishArgParser.new args, options
19+
params.validate!
20+
21+
movement = DraftMovementInfo.new params
22+
23+
mover = DraftMover.new movement
24+
mover.move
25+
end
26+
27+
end
28+
29+
class PublishArgParser
30+
attr_reader :args, :options
31+
def initialize(args, options)
32+
@args = args
33+
@options = options
34+
end
35+
36+
def validate!
1837
raise ArgumentError.new('You must specify a draft path.') if args.empty?
38+
end
1939

20-
date = options["date"].nil? ? Date.today : Date.parse(options["date"])
21-
draft_path = args.shift
40+
def date
41+
options["date"].nil? ? Date.today : Date.parse(options["date"])
42+
end
2243

23-
raise ArgumentError.new("There was no draft found at '#{draft_path}'.") unless File.exist? draft_path
44+
def draft_path
45+
args.join ' '
46+
end
2447

25-
Dir.mkdir("_posts") unless Dir.exist?("_posts")
26-
post_path = post_name(date, draft_name(draft_path))
27-
FileUtils.mv(draft_path, post_path)
48+
def draft_name
49+
File.basename draft_path
50+
end
51+
end
2852

29-
puts "Draft #{draft_path} was published to #{post_path}"
53+
class DraftMovementInfo
54+
attr_reader :params
55+
def initialize(params)
56+
@params = params
3057
end
3158

32-
# Internal: Gets the filename of the post to be created
33-
#
34-
# Returns the filename of the post, as a String
35-
def self.post_name(date, name)
36-
"_posts/#{date.strftime('%Y-%m-%d')}-#{name}"
59+
def from
60+
params.draft_path
3761
end
3862

39-
def self.draft_name(path)
40-
File.basename(path)
63+
def to
64+
"_posts/#{_date_stamp}-#{params.draft_name}"
4165
end
4266

67+
def _date_stamp
68+
params.date.strftime '%Y-%m-%d'
69+
end
70+
end
71+
72+
class DraftMover
73+
attr_reader :movement
74+
def initialize(movement)
75+
@movement = movement
76+
end
77+
78+
def move
79+
validate_source
80+
ensure_directory_exists
81+
move_file
82+
end
83+
84+
def validate_source
85+
raise ArgumentError.new("There was no draft found at '#{movement.from}'.") unless File.exist? movement.from
86+
end
87+
88+
def ensure_directory_exists
89+
Dir.mkdir("_posts") unless Dir.exist?("_posts")
90+
end
91+
92+
def move_file
93+
FileUtils.mv(movement.from, movement.to)
94+
puts "Draft #{movement.from} was published to #{movement.to}"
95+
end
4396
end
4497
end
4598
end

0 commit comments

Comments
 (0)