Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lib/jekyll/commands/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Commands
class Publish < Command
def self.init_with_program(prog)
prog.command(:publish) do |c|
c.syntax 'publish NAME'
c.syntax 'publish DRAFT_PATH'
c.description 'Moves a draft into the _posts directory and sets the date'

c.option 'date', '-d DATE', '--date DATE', 'Specify the post date'
Expand All @@ -15,16 +15,18 @@ def self.init_with_program(prog)
end

def self.process(args, options = {})
raise ArgumentError.new('You must specify a name.') if args.empty?
raise ArgumentError.new('You must specify a draft path.') if args.empty?

date = options["date"].nil? ? Date.today : Date.parse(options["date"])
file = args.shift
draft_path = args.shift
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you validate that this exists?


raise ArgumentError.new("There was no draft found at '#{draft_path}'.") unless File.exist? draft_path

post_path = post_name(date, file)
FileUtils.mv(draft_name(file), post_path)
post_path = post_name(date, draft_name(draft_path))
FileUtils.mv(draft_path, post_path)

puts "Draft #{file} was published to ./#{post_path}"
end
puts "Draft #{draft_path} was published to ./#{post_path}"
end

# Internal: Gets the filename of the post to be created
#
Expand All @@ -33,8 +35,8 @@ def self.post_name(date, name)
"_posts/#{date.strftime('%Y-%m-%d')}-#{name}"
end

def self.draft_name(name)
"_drafts/#{name}"
def self.draft_name(path)
File.basename(path)
end

end
Expand Down