Skip to content

Commit ef25d41

Browse files
Gavin D. Howardjekyllbot
authored andcommitted
Add/Remove Dates When Publishing/Unpublishing (#92)
Merge pull request 92
1 parent 6e114b9 commit ef25d41

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

lib/jekyll-compose/file_mover.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def move
2222
return unless valid_source? && valid_destination?
2323

2424
ensure_directory_exists
25+
update_front_matter
2526
move_file
2627
end
2728

@@ -43,6 +44,20 @@ def move_file
4344
Jekyll.logger.info "#{resource_type_from.capitalize} #{from} was moved to #{to}"
4445
end
4546

47+
def update_front_matter
48+
content = File.read(from)
49+
if content =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
50+
content = $POSTMATCH
51+
match = Regexp.last_match[1] if Regexp.last_match
52+
data = movement.front_matter(Psych.safe_load(match))
53+
File.write(from, "#{Psych.dump(data)}---\n#{content}")
54+
end
55+
rescue Psych::SyntaxError => e
56+
Jekyll.logger.warn e
57+
rescue StandardError => e
58+
Jekyll.logger.warn e
59+
end
60+
4661
private
4762

4863
def valid_source?

lib/jekyll/commands/publish.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def resource_type
3737
end
3838

3939
def date
40-
options["date"].nil? ? Date.today : Date.parse(options["date"])
40+
options["date"].nil? ? Time.now : Date.parse(options["date"])
4141
end
4242

4343
def name
@@ -59,6 +59,11 @@ def to
5959
date_stamp = params.date.strftime Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT
6060
"_posts/#{date_stamp}-#{params.name}"
6161
end
62+
63+
def front_matter(data)
64+
data["date"] ||= params.date.strftime("%Y-%m-%d %H:%M %z")
65+
data
66+
end
6267
end
6368

6469
class DraftMover < Compose::FileMover

lib/jekyll/commands/unpublish.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def from
5353
def to
5454
"_drafts/#{params.name}"
5555
end
56+
57+
def front_matter(data)
58+
data.reject { |key, _value| key == "date" }
59+
end
5660
end
5761

5862
class PostMover < Compose::FileMover

spec/publish_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
let(:drafts_dir) { Pathname.new source_dir("_drafts") }
55
let(:posts_dir) { Pathname.new source_dir("_posts") }
66
let(:draft_to_publish) { "a-test-post.md" }
7+
let(:timestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT) }
78
let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) }
89
let(:post_filename) { "#{datestamp}-#{draft_to_publish}" }
910
let(:args) { ["_drafts/#{draft_to_publish}"] }
@@ -19,7 +20,7 @@
1920
before(:each) do
2021
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
2122
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
22-
FileUtils.touch draft_path
23+
File.write(draft_path, "---\nlayout: post\n---\n")
2324
end
2425

2526
after(:each) do
@@ -34,13 +35,17 @@
3435
expect(draft_path).to exist
3536
capture_stdout { described_class.process(args) }
3637
expect(post_path).to exist
38+
expect(draft_path).not_to exist
39+
expect(File.read(post_path)).to include("date: #{timestamp}")
3740
end
3841

3942
it "publishes with a specified date" do
4043
path = posts_dir.join "2012-03-04-#{draft_to_publish}"
4144
expect(path).not_to exist
4245
capture_stdout { described_class.process(args, "date"=>"2012-3-4") }
4346
expect(path).to exist
47+
expect(draft_path).not_to exist
48+
expect(File.read(path)).to include("date: 2012-03-04")
4449
end
4550

4651
it "writes a helpful message on success" do
@@ -53,6 +58,8 @@
5358
path = posts_dir.join "2012-03-04-a-test-post.md"
5459
capture_stdout { described_class.process(args, "date" => "2012-3-4") }
5560
expect(path).to exist
61+
expect(draft_path).not_to exist
62+
expect(File.read(path)).to include("date: 2012-03-04")
5663
end
5764

5865
it "creates the posts folder if necessary" do
@@ -94,6 +101,7 @@
94101
expect(output).to_not include("A post already exists at _posts/#{post_filename}")
95102
expect(draft_path).not_to exist
96103
expect(post_path).to exist
104+
expect(File.read(post_path)).to include("date: #{timestamp}")
97105
end
98106
end
99107

spec/unpublish_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
let(:drafts_dir) { Pathname.new(source_dir("_drafts")) }
55
let(:posts_dir) { Pathname.new(source_dir("_posts")) }
66
let(:post_name) { "a-test-post.md" }
7-
let(:post_filename) { "2012-03-04-#{post_name}" }
7+
let(:timestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT) }
8+
let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) }
9+
let(:post_filename) { "#{datestamp}-#{post_name}" }
810
let(:post_path) { posts_dir.join post_filename }
911
let(:draft_path) { drafts_dir.join post_name }
1012

@@ -18,7 +20,7 @@
1820
before(:each) do
1921
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
2022
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
21-
FileUtils.touch post_path
23+
File.write(post_path, "---\nlayout: post\ndate: #{timestamp}\n---\n")
2224
end
2325

2426
after(:each) do
@@ -32,6 +34,7 @@
3234
capture_stdout { described_class.process(args) }
3335
expect(post_path).not_to exist
3436
expect(draft_path).to exist
37+
expect(File.read(draft_path)).not_to include("date: #{timestamp}")
3538
end
3639

3740
it "writes a helpful message on success" do
@@ -77,6 +80,7 @@
7780
expect(output).to_not include("A draft already exists at _drafts/#{post_name}")
7881
expect(draft_path).to exist
7982
expect(post_path).not_to exist
83+
expect(File.read(draft_path)).not_to include("date: #{timestamp}")
8084
end
8185
end
8286

0 commit comments

Comments
 (0)