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

Support a custom timestamp format in yaml frontmatter on posts #98

Merged
merged 5 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Create your new post using:

```sh
$ bundle exec jekyll post "My New Post"
# or specify a custom format for the date attribute in the yaml front matter
$ bundle exec jekyll post "My New Post" --timestamp-format "%Y-%m-%d %H:%M:%S %z"
```

```sh
Expand Down Expand Up @@ -85,6 +87,8 @@ Publish your draft using:
```sh
# or specify a specific date on which to publish it
$ bundle exec jekyll publish _drafts/my-new-draft.md --date 2014-01-24
# or specify a custom format for the date attribute in the yaml front matter
$ bundle exec jekyll publish _drafts/my-new-draft.md --timestamp-format "%Y-%m-%d %H:%M:%S %z"
```

Unpublish your post using:
Expand Down Expand Up @@ -141,7 +145,7 @@ jekyll_compose:
```

This will also auto add:
- The creation timestamp under the `date` attribure.
- The creation timestamp under the `date` attribute.
- The title attribute under the `title` attribute


Expand Down
4 changes: 4 additions & 0 deletions lib/jekyll-compose/arg_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def force?
!!options["force"]
end

def timestamp_format
options["timestamp_format"] || Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT
end

def source
File.join(config["source"], config["collections_dir"])
.gsub(%r!^#{Regexp.quote(Dir.pwd)}/*!, "")
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/commands/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def self.options
["force", "-f", "--force", "Overwrite a post if it already exists"],
["date", "-d DATE", "--date DATE", "Specify the post date"],
["config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"],
["timestamp_format", "--timestamp-format FORMAT", "Custom timestamp format"],
]
end

Expand Down Expand Up @@ -62,7 +63,7 @@ def _date_stamp
end

def _time_stamp
@params.date.strftime Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT
@params.date.strftime @params.timestamp_format
end

def content(custom_front_matter = {})
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/commands/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def self.options
["date", "-d DATE", "--date DATE", "Specify the post date"],
["config", "--config CONFIG_FILE[,CONFIG_FILE2,...]", Array, "Custom configuration file"],
["force", "-f", "--force", "Overwrite a post if it already exists"],
["timestamp_format", "--timestamp-format FORMAT", "Custom timestamp format"],
]
end

Expand Down Expand Up @@ -64,7 +65,7 @@ def to
end

def front_matter(data)
data["date"] ||= params.date.strftime("%Y-%m-%d %H:%M %z")
data["date"] ||= params.date.strftime(params.timestamp_format)
data
end
end
Expand Down
66 changes: 57 additions & 9 deletions spec/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
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(:timestamp_format) { Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT }
let(:date) { Time.now }
let(:timestamp) { date.strftime(timestamp_format) }
let(:datestamp) { date.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) }
let(:post_filename) { "#{datestamp}-#{draft_to_publish}" }
let(:args) { ["_drafts/#{draft_to_publish}"] }

Expand Down Expand Up @@ -39,13 +41,30 @@
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")
context "when date option is set" do
let(:date) { Date.parse("2012-3-4") }

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

context "and timestamp format is set" do
let(:timestamp_format) { "%Y-%m-%d %H:%M:%S" }

it "published with a specified date in a given format" do
expect(post_path).not_to exist
expect(draft_path).to exist
capture_stdout { described_class.process(args, "date" => "2012-3-4", "timestamp_format" => timestamp_format) }
expect(post_path).to exist
expect(draft_path).not_to exist
expect(File.read(post_path)).to include("date: '2012-03-04 00:00:00'")
end
end
end

it "writes a helpful message on success" do
Expand Down Expand Up @@ -130,6 +149,7 @@
expect(draft_path).to exist
capture_stdout { described_class.process(args) }
expect(post_path).to exist
expect(draft_path).not_to exist
end
end

Expand All @@ -142,6 +162,34 @@
expect(draft_path).to exist
capture_stdout { described_class.process(args, "source" => "site") }
expect(post_path).to exist
expect(draft_path).not_to exist
end
end

context "when timestamp format option is set" do
context "to a custom value" do
let(:timestamp_format) { "%Y-%m-%d %H:%M:%S" }

it "should use timestamp format set by command line option" do
expect(post_path).not_to exist
expect(draft_path).to exist
capture_stdout { described_class.process(args, "timestamp_format" => timestamp_format) }
expect(post_path).to exist
expect(draft_path).not_to exist
expect(File.read(post_path)).to include("date: '#{timestamp}'")
end
end
context "to the default value" do
let(:timestamp_format) { Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT }

it "should use timestamp format set by command line option" do
expect(post_path).not_to exist
expect(draft_path).to exist
capture_stdout { described_class.process(args, "timestamp_format" => timestamp_format) }
expect(post_path).to exist
expect(draft_path).not_to exist
expect(File.read(post_path)).to include("date: #{timestamp}")
end
end
end
end