Skip to content

Commit 942d46e

Browse files
ashmarolijekyllbot
authored andcommitted
Add support for collections_dir configuration (#74)
Merge pull request 74
1 parent 1c07f92 commit 942d46e

File tree

7 files changed

+88
-17
lines changed

7 files changed

+88
-17
lines changed

lib/jekyll-compose/arg_parser.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def force?
3333
end
3434

3535
def source
36-
config["source"].gsub(%r!^#{Regexp.quote(Dir.pwd)}!, "")
36+
File.join(config["source"], config["collections_dir"])
37+
.gsub(%r!^#{Regexp.quote(Dir.pwd)}/*!, "")
3738
end
3839
end
3940
end

lib/jekyll-compose/file_creator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def validate_should_write!
3030

3131
def ensure_directory_exists
3232
dir = File.dirname file_path
33-
Dir.mkdir(dir) unless Dir.exist?(dir)
33+
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
3434
end
3535

3636
def write_file

lib/jekyll-compose/movement_arg_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def validate!
88
end
99

1010
def path
11-
args.join " "
11+
File.join(source, args.join(" ")).sub(%r!\A/!, "")
1212
end
1313
end
1414
end

spec/draft_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@
139139
end
140140
end
141141
end
142+
143+
context "and collections_dir is set" do
144+
let(:collections_dir) { "my_collections" }
145+
let(:drafts_dir) { Pathname.new source_dir("site", collections_dir, "_drafts") }
146+
let(:config_data) do
147+
%(
148+
source: site
149+
collections_dir: #{collections_dir}
150+
)
151+
end
152+
153+
it "should create drafts at the correct location" do
154+
expect(path).not_to exist
155+
capture_stdout { described_class.process(args) }
156+
expect(path).to exist
157+
end
158+
159+
it "should write a helpful message when successful" do
160+
output = capture_stdout { described_class.process(args) }
161+
generated_path = File.join("site", collections_dir, "_drafts", "a-test-post.md").cyan
162+
expect(output).to include("New draft created at #{generated_path}")
163+
end
164+
end
142165
end
143166

144167
context "when source option is set" do

spec/post_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,29 @@
150150
end
151151
end
152152
end
153+
154+
context "and collections_dir is set" do
155+
let(:collections_dir) { "my_collections" }
156+
let(:posts_dir) { Pathname.new source_dir("site", collections_dir, "_posts") }
157+
let(:config_data) do
158+
%(
159+
source: site
160+
collections_dir: #{collections_dir}
161+
)
162+
end
163+
164+
it "should create posts at the correct location" do
165+
expect(path).not_to exist
166+
capture_stdout { described_class.process(args) }
167+
expect(path).to exist
168+
end
169+
170+
it "should write a helpful message when successful" do
171+
output = capture_stdout { described_class.process(args) }
172+
generated_path = File.join("site", collections_dir, "_posts", filename).cyan
173+
expect(output).to include("New post created at #{generated_path}")
174+
end
175+
end
153176
end
154177

155178
context "when source option is set" do

spec/publish_spec.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@
102102
let(:config) { source_dir("_config.yml") }
103103
let(:drafts_dir) { Pathname.new source_dir("site", "_drafts") }
104104
let(:posts_dir) { Pathname.new source_dir("site", "_posts") }
105-
106-
let(:args) { ["site/_drafts/#{draft_to_publish}"] }
105+
let(:config_data) do
106+
%(
107+
source: site
108+
)
109+
end
107110

108111
before(:each) do
109112
File.open(config, "w") do |f|
110-
f.write(%(
111-
source: site
112-
))
113+
f.write(config_data)
113114
end
114115
end
115116

@@ -129,8 +130,6 @@
129130
let(:drafts_dir) { Pathname.new source_dir("site", "_drafts") }
130131
let(:posts_dir) { Pathname.new source_dir("site", "_posts") }
131132

132-
let(:args) { ["site/_drafts/#{draft_to_publish}"] }
133-
134133
it "should use source directory set by command line option" do
135134
expect(post_path).not_to exist
136135
expect(draft_path).to exist

spec/unpublish_spec.rb

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,15 @@
8787
let(:config) { source_dir("_config.yml") }
8888
let(:drafts_dir) { Pathname.new(source_dir("site", "_drafts")) }
8989
let(:posts_dir) { Pathname.new(source_dir("site", "_posts")) }
90-
91-
let(:args) { ["site/_posts/#{post_filename}"] }
90+
let(:config_data) do
91+
%(
92+
source: site
93+
)
94+
end
9295

9396
before(:each) do
9497
File.open(config, "w") do |f|
95-
f.write(%(
96-
source: site
97-
))
98+
f.write(config_data)
9899
end
99100
end
100101

@@ -109,14 +110,38 @@
109110
expect(post_path).not_to exist
110111
expect(draft_path).to exist
111112
end
113+
114+
context "and collections_dir is set" do
115+
let(:collections_dir) { "my_collections" }
116+
let(:drafts_dir) { Pathname.new(source_dir("site", collections_dir, "_drafts")) }
117+
let(:posts_dir) { Pathname.new(source_dir("site", collections_dir, "_posts")) }
118+
let(:config_data) do
119+
%(
120+
source: site
121+
collections_dir: #{collections_dir}
122+
)
123+
end
124+
125+
it "should move posts to the correct location" do
126+
expect(post_path).to exist
127+
expect(draft_path).not_to exist
128+
capture_stdout { described_class.process(args) }
129+
expect(draft_path).to exist
130+
end
131+
132+
it "should write a helpful message when successful" do
133+
output = capture_stdout { described_class.process(args) }
134+
post_filepath = File.join("site", collections_dir, "_posts", post_filename)
135+
draft_filepath = File.join("site", collections_dir, "_drafts", post_name)
136+
expect(output).to include("Post #{post_filepath} was moved to #{draft_filepath}")
137+
end
138+
end
112139
end
113140

114141
context "when source option is set" do
115142
let(:drafts_dir) { Pathname.new(source_dir("site", "_drafts")) }
116143
let(:posts_dir) { Pathname.new(source_dir("site", "_posts")) }
117144

118-
let(:args) { ["site/_posts/#{post_filename}"] }
119-
120145
it "should use source directory set by command line option" do
121146
expect(post_path).to exist
122147
expect(draft_path).not_to exist

0 commit comments

Comments
 (0)