diff --git a/README.md b/README.md index 99450e3..d9cc600 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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 diff --git a/lib/jekyll-compose/arg_parser.rb b/lib/jekyll-compose/arg_parser.rb index 3b338ce..fb45ae6 100644 --- a/lib/jekyll-compose/arg_parser.rb +++ b/lib/jekyll-compose/arg_parser.rb @@ -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)}/*!, "") diff --git a/lib/jekyll/commands/post.rb b/lib/jekyll/commands/post.rb index ea5e58e..8d8074b 100644 --- a/lib/jekyll/commands/post.rb +++ b/lib/jekyll/commands/post.rb @@ -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 @@ -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 = {}) diff --git a/lib/jekyll/commands/publish.rb b/lib/jekyll/commands/publish.rb index c6e9aab..2298cd9 100644 --- a/lib/jekyll/commands/publish.rb +++ b/lib/jekyll/commands/publish.rb @@ -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 @@ -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 diff --git a/spec/publish_spec.rb b/spec/publish_spec.rb index fab1678..a5e75dc 100644 --- a/spec/publish_spec.rb +++ b/spec/publish_spec.rb @@ -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}"] } @@ -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 @@ -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 @@ -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