Skip to content

Commit 540a8ba

Browse files
Mehonoshinjekyllbot
authored andcommitted
Autoopen newly generated files in selected editor (#64)
Merge pull request 64
1 parent 52bebd2 commit 540a8ba

File tree

6 files changed

+97
-9
lines changed

6 files changed

+97
-9
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ Unpublish your post using:
5050

5151
$ bundle exec jekyll unpublish _posts/2014-01-24-my-new-draft.md
5252

53+
## Configuration
54+
55+
To customize the default plugin configuration edit the `jekyll_compose` section within your jekyll config file.
56+
57+
```
58+
jekyll_compose:
59+
auto_open: true
60+
```
61+
62+
and make sure that you have `EDITOR` or `JEKYLL_EDITOR` environment variable set.
63+
64+
The latter one will override default `EDITOR` value.
65+
66+
It will open a newly generated post in your selected editor.
67+
5368
## Contributing
5469

5570
1. Fork it ( http://github.com/jekyll/jekyll-compose/fork )

lib/jekyll-compose.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "jekyll-compose/file_creator"
77
require "jekyll-compose/file_mover"
88
require "jekyll-compose/file_info"
9+
require "jekyll-compose/file_editor"
910

1011
module Jekyll
1112
module Compose

lib/jekyll-compose/file_creator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def create!
1616
write_file
1717
end
1818

19+
def file_path
20+
return file.path if root.nil? || root.empty?
21+
return File.join(root, file.path)
22+
end
23+
1924
private
2025

2126
def validate_should_write!
@@ -34,11 +39,6 @@ def write_file
3439

3540
puts "New #{file.resource_type} created at #{file_path}."
3641
end
37-
38-
def file_path
39-
return file.path if root.nil? || root.empty?
40-
return File.join(root, file.path)
41-
end
4242
end
4343
end
4444
end

lib/jekyll-compose/file_editor.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
#
3+
# This class is aimed to open the created file in the selected editor.
4+
# To use this feature specify at Jekyll config:
5+
#
6+
# ```
7+
# jekyll_compose:
8+
# auto_open: true
9+
# ```
10+
#
11+
# And make sure, that you have JEKYLL_EDITOR or EDITOR environment variables set up.
12+
# This will allow to open the file in your default editor automatically.
13+
14+
module Jekyll
15+
module Compose
16+
class FileEditor
17+
class << self
18+
def open_editor(filepath)
19+
run_editor(post_editor, File.expand_path(filepath)) if post_editor
20+
end
21+
22+
def run_editor(editor_name, filepath)
23+
system("#{editor_name} #{filepath}")
24+
end
25+
26+
def post_editor
27+
return unless auto_open?
28+
ENV['JEKYLL_EDITOR'] || ENV['EDITOR']
29+
end
30+
31+
def auto_open?
32+
jekyll_compose_config && jekyll_compose_config['auto_open']
33+
end
34+
35+
def jekyll_compose_config
36+
@jekyll_compose_config ||= Jekyll.configuration['jekyll_compose']
37+
end
38+
end
39+
end
40+
end
41+
end
42+

lib/jekyll/commands/post.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def self.process(args = [], options = {})
3131

3232
post = PostFileInfo.new params
3333

34-
Compose::FileCreator.new(post, params.force?, params.source).create!
34+
file_creator = Compose::FileCreator.new(post, params.force?, params.source)
35+
file_creator.create!
36+
Compose::FileEditor.open_editor(file_creator.file_path)
3537
end
3638

3739
class PostArgParser < Compose::ArgParser

spec/post_spec.rb

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
before(:each) do
1818
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
19+
allow(Jekyll::Compose::FileEditor).to receive(:system)
1920
end
2021

2122
after(:each) do
@@ -90,12 +91,13 @@
9091
context "when a configuration file exists" do
9192
let(:config) { source_dir("_config.yml") }
9293
let(:posts_dir) { Pathname.new source_dir("site", "_posts") }
94+
let(:config_data) { %(
95+
source: site
96+
) }
9397

9498
before(:each) do
9599
File.open(config, "w") do |f|
96-
f.write(%(
97-
source: site
98-
))
100+
f.write(config_data)
99101
end
100102
end
101103

@@ -108,6 +110,32 @@
108110
capture_stdout { described_class.process(args) }
109111
expect(path).to exist
110112
end
113+
114+
context 'auto_open editor is set' do
115+
let(:posts_dir) { Pathname.new source_dir("_posts") }
116+
let(:config_data) { %(
117+
jekyll_compose:
118+
auto_open: true
119+
)}
120+
121+
context 'env variable EDITOR is set up' do
122+
before { ENV['EDITOR'] = 'vim' }
123+
124+
it 'opens post in default editor' do
125+
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with('vim', path.to_s)
126+
capture_stdout { described_class.process(args) }
127+
end
128+
129+
context 'env variable JEKYLL_EDITOR is set up' do
130+
before { ENV['JEKYLL_EDITOR'] = 'nano' }
131+
132+
it 'opens post in jekyll editor' do
133+
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with('nano', path.to_s)
134+
capture_stdout { described_class.process(args) }
135+
end
136+
end
137+
end
138+
end
111139
end
112140

113141
context "when source option is set" do

0 commit comments

Comments
 (0)