Skip to content

Commit 660ec9a

Browse files
committed
Added post command
1 parent b2b18ba commit 660ec9a

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

lib/jekyll/commands/post.rb

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

test/test_post_command.rb

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

0 commit comments

Comments
 (0)