Skip to content

Commit 5a4b687

Browse files
committed
Added draft command
1 parent 223e9c9 commit 5a4b687

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

lib/jekyll/commands/draft.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module Jekyll
2+
module Commands
3+
class Draft < Command
4+
def self.init_with_program(prog)
5+
prog.command(:draft) do |c|
6+
c.syntax 'draft NAME'
7+
c.description 'Creates a new draft post with the given NAME'
8+
9+
c.option 'type', '-t TYPE', '--type TYPE', 'Specify the content type'
10+
c.option 'layout', '-l LAYOUT', '--layout LAYOUT', 'Specify the post layout'
11+
c.option 'force', '-f', '--force', 'Overwrite a draft if it already exists'
12+
13+
c.action do |args, options|
14+
Jekyll::Commands::Draft.process(args, options)
15+
end
16+
end
17+
end
18+
19+
def self.process(args, options = {})
20+
raise ArgumentError.new('You must specify a name.') if args.empty?
21+
22+
type = options["type"].nil? ? "markdown" : options["type"]
23+
layout = options["layout"].nil? ? "post" : options["layout"]
24+
25+
title = args.shift
26+
name = title.gsub(' ', '-').downcase
27+
28+
draft_path = draft_name(name, type)
29+
30+
raise ArgumentError.new("A draft already exists at ./#{draft_path}") if File.exist?(draft_path) and !options["force"]
31+
32+
File.open(draft_path, "w") do |f|
33+
f.puts(front_matter(layout, title))
34+
end
35+
36+
puts "New draft created at ./#{draft_path}.\n"
37+
end
38+
# Internal: Gets the filename of the draft to be created
39+
#
40+
# Returns the filename of the draft, as a String
41+
def self.draft_name(name, ext='markdown')
42+
"_drafts/#{name}.#{ext}"
43+
end
44+
45+
def self.front_matter(layout, title)
46+
"---
47+
layout: #{layout}
48+
title: #{title}
49+
---"
50+
end
51+
end
52+
end
53+
end

test/test_draft_command.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require 'helper'
2+
require 'jekyll/commands/draft'
3+
4+
class TestDraftCommand < Test::Unit::TestCase
5+
6+
context 'when no flags are given' do
7+
setup do
8+
@name = 'A test post'
9+
@args = [@name]
10+
@drafts_dir = '_drafts'
11+
@path = File.join(@drafts_dir, 'a-test-post.markdown')
12+
stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS }
13+
@site = Site.new(Jekyll.configuration)
14+
FileUtils.mkdir @drafts_dir
15+
end
16+
17+
teardown do
18+
FileUtils.rm_r @drafts_dir
19+
end
20+
21+
should 'create a new draft post' do
22+
assert !File.exist?(@path)
23+
capture_stdout { Jekyll::Commands::Draft.process(@args) }
24+
assert File.exist?(@path)
25+
end
26+
27+
should 'write a success message' do
28+
output = capture_stdout { Jekyll::Commands::Draft.process(@args) }
29+
success_message = "New draft created at ./#{@path}.\n"
30+
assert_equal success_message, output
31+
end
32+
end
33+
34+
context 'when the draft already exists' do
35+
setup do
36+
@name = 'An existing draft'
37+
@args = [@name]
38+
@drafts_dir = '_drafts'
39+
@path = File.join(@drafts_dir, 'an-existing-draft.markdown')
40+
stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS }
41+
@site = Site.new(Jekyll.configuration)
42+
FileUtils.mkdir @drafts_dir
43+
FileUtils.touch @path
44+
end
45+
46+
teardown do
47+
FileUtils.rm_r @drafts_dir
48+
end
49+
50+
should 'raise an ArgumentError' do
51+
exception = assert_raise ArgumentError do
52+
capture_stdout { Jekyll::Commands::Draft.process(@args) }
53+
end
54+
assert_equal "A draft already exists at ./#{@path}", exception.message
55+
end
56+
57+
should 'overwrite the existing draft if --force is given' do
58+
capture_stdout { Jekyll::Commands::Draft.process(@args, { "force" => true }) }
59+
assert File.readlines(@path).grep(/layout: post/).any?
60+
end
61+
62+
end
63+
64+
context 'when no args are given' do
65+
setup do
66+
@empty_args = []
67+
end
68+
69+
should 'raise an ArgumentError' do
70+
exception = assert_raise ArgumentError do
71+
Jekyll::Commands::Draft.process(@empty_args)
72+
end
73+
assert_equal 'You must specify a name.', exception.message
74+
end
75+
end
76+
77+
end

0 commit comments

Comments
 (0)