Skip to content

Commit 8100f7e

Browse files
authored
Merge pull request #42 from mloberg/feature/site-source-fix
Allow Jekyll Source Directory
2 parents 8e1db4b + d467f20 commit 8100f7e

File tree

15 files changed

+250
-25
lines changed

15 files changed

+250
-25
lines changed

lib/jekyll-compose/arg_parser.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
class Jekyll::Compose::ArgParser
2-
attr_reader :args, :options
2+
attr_reader :args, :options, :config
33
def initialize(args, options)
44
@args = args
55
@options = options
6+
@config = Jekyll.configuration(options)
67
end
78

89
def validate!
910
raise ArgumentError.new('You must specify a name.') if args.empty?
1011
end
1112

1213
def type
13-
type = options["extension"] || Jekyll::Compose::DEFAULT_TYPE
14+
options["extension"] || Jekyll::Compose::DEFAULT_TYPE
1415
end
1516

1617
def layout
17-
layout = options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT
18+
options["layout"] || Jekyll::Compose::DEFAULT_LAYOUT
1819
end
1920

2021
def title
@@ -24,4 +25,8 @@ def title
2425
def force?
2526
!!options["force"]
2627
end
28+
29+
def source
30+
config['source'].gsub(/^#{Regexp.quote(Dir.pwd)}/, '')
31+
end
2732
end

lib/jekyll-compose/file_creator.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Jekyll
22
module Compose
33
class FileCreator
4-
attr_reader :file, :force
5-
def initialize(fileInfo, force = false)
4+
attr_reader :file, :force, :root
5+
def initialize(fileInfo, force = false, root = nil)
66
@file = fileInfo
77
@force = force
8+
@root = root
89
end
910

1011
def create!
@@ -16,20 +17,25 @@ def create!
1617
private
1718

1819
def validate_should_write!
19-
raise ArgumentError.new("A #{file.resource_type} already exists at #{file.path}") if File.exist?(file.path) and !force
20+
raise ArgumentError.new("A #{file.resource_type} already exists at #{file_path}") if File.exist?(file_path) and !force
2021
end
2122

2223
def ensure_directory_exists
23-
dir = File.dirname file.path
24+
dir = File.dirname file_path
2425
Dir.mkdir(dir) unless Dir.exist?(dir)
2526
end
2627

2728
def write_file
28-
File.open(file.path, "w") do |f|
29+
File.open(file_path, "w") do |f|
2930
f.puts(file.content)
3031
end
3132

32-
puts "New #{file.resource_type} created at #{file.path}."
33+
puts "New #{file.resource_type} created at #{file_path}."
34+
end
35+
36+
def file_path
37+
return file.path if root.nil? or root.empty?
38+
return File.join(root, file.path)
3339
end
3440
end
3541
end

lib/jekyll-compose/file_mover.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module Jekyll
22
module Compose
33
class FileMover
4-
attr_reader :movement
5-
def initialize(movement)
4+
attr_reader :movement, :root
5+
def initialize(movement, root = nil)
66
@movement = movement
7+
@root = root
78
end
89

910
def resource_type
@@ -17,17 +18,31 @@ def move
1718
end
1819

1920
def validate_source
20-
raise ArgumentError.new("There was no #{resource_type} found at '#{movement.from}'.") unless File.exist? movement.from
21+
raise ArgumentError.new("There was no #{resource_type} found at '#{from}'.") unless File.exist? from
2122
end
2223

2324
def ensure_directory_exists
24-
dir = File.dirname movement.to
25+
dir = File.dirname to
2526
Dir.mkdir(dir) unless Dir.exist?(dir)
2627
end
2728

2829
def move_file
29-
FileUtils.mv(movement.from, movement.to)
30-
puts "#{resource_type.capitalize} #{movement.from} was moved to #{movement.to}"
30+
FileUtils.mv(from, to)
31+
puts "#{resource_type.capitalize} #{from} was moved to #{to}"
32+
end
33+
34+
private
35+
def from
36+
movement.from
37+
end
38+
39+
def to
40+
file_path(movement.to)
41+
end
42+
43+
def file_path(path)
44+
return path if root.nil? or root.empty?
45+
return File.join(root, path)
3146
end
3247
end
3348
end

lib/jekyll-compose/movement_arg_parser.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Jekyll
22
module Compose
33
class MovementArgParser
4-
attr_reader :args, :options
4+
attr_reader :args, :options, :config
55
def initialize(args, options)
66
@args = args
77
@options = options
8+
@config = Jekyll.configuration(options)
89
end
910

1011
def validate!
@@ -14,6 +15,10 @@ def validate!
1415
def path
1516
args.join ' '
1617
end
18+
19+
def source
20+
source = config['source'].gsub(/^#{Regexp.quote(Dir.pwd)}/, '')
21+
end
1722
end
1823
end
1924
end

lib/jekyll/commands/draft.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def self.options
1616
[
1717
['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
1818
['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the draft layout"],
19-
['force', '-f', '--force', 'Overwrite a draft if it already exists']
19+
['force', '-f', '--force', 'Overwrite a draft if it already exists'],
20+
['config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'],
21+
['source', '-s', '--source SOURCE', 'Custom source directory'],
2022
]
2123
end
2224

@@ -27,7 +29,7 @@ def self.process(args = [], options = {})
2729

2830
draft = DraftFileInfo.new params
2931

30-
Compose::FileCreator.new(draft, params.force?).create!
32+
Compose::FileCreator.new(draft, params.force?, params.source).create!
3133
end
3234

3335
class DraftFileInfo < Compose::FileInfo

lib/jekyll/commands/page.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def self.options
1616
[
1717
['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
1818
['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the page layout"],
19-
['force', '-f', '--force', 'Overwrite a page if it already exists']
19+
['force', '-f', '--force', 'Overwrite a page if it already exists'],
20+
['config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'],
21+
['source', '-s', '--source SOURCE', 'Custom source directory'],
2022
]
2123
end
2224

@@ -26,7 +28,7 @@ def self.process(args = [], options = {})
2628

2729
page = PageFileInfo.new params
2830

29-
Compose::FileCreator.new(page, params.force?).create!
31+
Compose::FileCreator.new(page, params.force?, params.source).create!
3032
end
3133

3234
class PageArgParser < Compose::ArgParser

lib/jekyll/commands/post.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def self.options
1717
['extension', '-x EXTENSION', '--extension EXTENSION', 'Specify the file extension'],
1818
['layout', '-l LAYOUT', '--layout LAYOUT', "Specify the post layout"],
1919
['force', '-f', '--force', 'Overwrite a post if it already exists'],
20-
['date', '-d DATE', '--date DATE', 'Specify the post date']
20+
['date', '-d DATE', '--date DATE', 'Specify the post date'],
21+
['config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'],
22+
['source', '-s', '--source SOURCE', 'Custom source directory'],
2123
]
2224
end
2325

@@ -27,7 +29,7 @@ def self.process(args = [], options = {})
2729

2830
post = PostFileInfo.new params
2931

30-
Compose::FileCreator.new(post, params.force?).create!
32+
Compose::FileCreator.new(post, params.force?, params.source).create!
3133
end
3234

3335

lib/jekyll/commands/publish.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def self.init_with_program(prog)
77
c.description 'Moves a draft into the _posts directory and sets the date'
88

99
c.option 'date', '-d DATE', '--date DATE', 'Specify the post date'
10+
c.option 'config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'
11+
c.option 'source', '-s', '--source SOURCE', 'Custom source directory'
1012

1113
c.action do |args, options|
1214
Jekyll::Commands::Publish.process(args, options)
@@ -20,7 +22,7 @@ def self.process(args = [], options = {})
2022

2123
movement = DraftMovementInfo.new params
2224

23-
mover = DraftMover.new movement
25+
mover = DraftMover.new movement, params.source
2426
mover.move
2527
end
2628

lib/jekyll/commands/unpublish.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ def self.init_with_program(prog)
66
c.syntax 'unpublish POST_PATH'
77
c.description 'Moves a post back into the _drafts directory'
88

9+
c.option 'config', '--config CONFIG_FILE[,CONFIG_FILE2,...]', Array, 'Custom configuration file'
10+
c.option 'source', '-s', '--source SOURCE', 'Custom source directory'
11+
912
c.action do |args, options|
1013
process(args, options)
1114
end
@@ -18,7 +21,7 @@ def self.process(args = [], options = {})
1821

1922
movement = PostMovementInfo.new params
2023

21-
mover = PostMover.new movement
24+
mover = PostMover.new movement, params.source
2225
mover.move
2326
end
2427

spec/draft_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,37 @@
7373
expect(File.read(path)).to match(/layout: post/)
7474
end
7575
end
76+
77+
context 'when a configuration file exists' do
78+
let(:config) { source_dir('_config.yml') }
79+
let(:drafts_dir) { Pathname.new source_dir(File.join('site', '_drafts')) }
80+
81+
before(:each) do
82+
File.open(config, 'w') do |f|
83+
f.write(%{
84+
source: site
85+
})
86+
end
87+
end
88+
89+
after(:each) do
90+
FileUtils.rm(config)
91+
end
92+
93+
it 'should use source directory set by config' do
94+
expect(path).not_to exist
95+
capture_stdout { described_class.process(args) }
96+
expect(path).to exist
97+
end
98+
end
99+
100+
context 'when source option is set' do
101+
let(:drafts_dir) { Pathname.new source_dir(File.join('site', '_drafts')) }
102+
103+
it 'should use source directory set by command line option' do
104+
expect(path).not_to exist
105+
capture_stdout { described_class.process(args, 'source' => 'site') }
106+
expect(path).to exist
107+
end
108+
end
76109
end

0 commit comments

Comments
 (0)