Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/Remove Dates When Publishing/Unpublishing #92

Merged
merged 7 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/jekyll-compose/file_mover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def move
return unless valid_source? && valid_destination?

ensure_directory_exists
update_front_matter
move_file
end

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

def update_front_matter
content = File.read(from)
if content =~ %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m
DirtyF marked this conversation as resolved.
Show resolved Hide resolved
content = $POSTMATCH
match = Regexp.last_match[1] if Regexp.last_match
data = movement.front_matter(Psych.safe_load(match))
File.write(from, "#{Psych.dump(data)}---\n#{content}")
end
rescue SyntaxError => e
DirtyF marked this conversation as resolved.
Show resolved Hide resolved
Jekyll.logger e
DirtyF marked this conversation as resolved.
Show resolved Hide resolved
rescue StandardError => e
Jekyll.logger e
DirtyF marked this conversation as resolved.
Show resolved Hide resolved
end

private

def valid_source?
Expand Down
6 changes: 5 additions & 1 deletion lib/jekyll/commands/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def resource_type
end

def date
options["date"].nil? ? Date.today : Date.parse(options["date"])
options["date"].nil? ? Time.now : Date.parse(options["date"])
end

def name
Expand All @@ -59,6 +59,10 @@ def to
date_stamp = params.date.strftime Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT
"_posts/#{date_stamp}-#{params.name}"
end

def front_matter(data)
data.merge("date" => params.date.strftime("%Y-%m-%d %H:%M %z"))
DirtyF marked this conversation as resolved.
Show resolved Hide resolved
end
end

class DraftMover < Compose::FileMover
Expand Down
4 changes: 4 additions & 0 deletions lib/jekyll/commands/unpublish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def from
def to
"_drafts/#{params.name}"
end

def front_matter(data)
data.reject { |x| x == "date" }
gavinhoward marked this conversation as resolved.
Show resolved Hide resolved
end
end

class PostMover < Compose::FileMover
Expand Down
10 changes: 9 additions & 1 deletion spec/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:drafts_dir) { Pathname.new source_dir("_drafts") }
let(:posts_dir) { Pathname.new source_dir("_posts") }
let(:draft_to_publish) { "a-test-post.md" }
let(:timestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT) }
let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) }
let(:post_filename) { "#{datestamp}-#{draft_to_publish}" }
let(:args) { ["_drafts/#{draft_to_publish}"] }
Expand All @@ -19,7 +20,7 @@
before(:each) do
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
FileUtils.touch draft_path
File.write(draft_path, "---\nlayout: post\n---\n")
end

after(:each) do
Expand All @@ -34,13 +35,17 @@
expect(draft_path).to exist
capture_stdout { described_class.process(args) }
expect(post_path).to exist
expect(draft_path).not_to exist
expect(File.read(post_path)).to include("date: #{timestamp}")
end

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

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

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

Expand Down
8 changes: 6 additions & 2 deletions spec/unpublish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
let(:drafts_dir) { Pathname.new(source_dir("_drafts")) }
let(:posts_dir) { Pathname.new(source_dir("_posts")) }
let(:post_name) { "a-test-post.md" }
let(:post_filename) { "2012-03-04-#{post_name}" }
let(:timestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT) }
let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) }
let(:post_filename) { "#{datestamp}-#{post_name}" }
let(:post_path) { posts_dir.join post_filename }
let(:draft_path) { drafts_dir.join post_name }

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

after(:each) do
Expand All @@ -32,6 +34,7 @@
capture_stdout { described_class.process(args) }
expect(post_path).not_to exist
expect(draft_path).to exist
expect(File.read(draft_path)).not_to include("date: #{timestamp}")
end

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

Expand Down