From 058f97baeb0d270e853461d9a670e306c404ee72 Mon Sep 17 00:00:00 2001 From: alzeih Date: Fri, 12 Jan 2018 22:30:53 +1300 Subject: [PATCH] Formatting of dates and times in a DRY manner --- lib/jekyll-compose.rb | 2 ++ lib/jekyll/commands/post.rb | 4 ++-- lib/jekyll/commands/publish.rb | 2 +- spec/post_spec.rb | 6 +++--- spec/publish_spec.rb | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/jekyll-compose.rb b/lib/jekyll-compose.rb index b2a36e0..8fe628f 100644 --- a/lib/jekyll-compose.rb +++ b/lib/jekyll-compose.rb @@ -12,6 +12,8 @@ module Compose DEFAULT_TYPE = "md".freeze DEFAULT_LAYOUT = "post".freeze DEFAULT_LAYOUT_PAGE = "page".freeze + DEFAULT_DATESTAMP_FORMAT = "%Y-%m-%d".freeze + DEFAULT_TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M %z".freeze end end diff --git a/lib/jekyll/commands/post.rb b/lib/jekyll/commands/post.rb index 12a371e..5a34164 100644 --- a/lib/jekyll/commands/post.rb +++ b/lib/jekyll/commands/post.rb @@ -54,11 +54,11 @@ def file_name end def _date_stamp - @params.date.strftime "%Y-%m-%d" + @params.date.strftime Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT end def _time_stamp - @params.date.strftime("%Y-%m-%d %H:%M %z") + @params.date.strftime Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT end def content(custom_front_matter = {}) diff --git a/lib/jekyll/commands/publish.rb b/lib/jekyll/commands/publish.rb index 2e6fda4..af97750 100644 --- a/lib/jekyll/commands/publish.rb +++ b/lib/jekyll/commands/publish.rb @@ -54,7 +54,7 @@ def from end def to - date_stamp = params.date.strftime "%Y-%m-%d" + date_stamp = params.date.strftime Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT "_posts/#{date_stamp}-#{params.name}" end end diff --git a/spec/post_spec.rb b/spec/post_spec.rb index 19216e0..0eda741 100644 --- a/spec/post_spec.rb +++ b/spec/post_spec.rb @@ -4,8 +4,8 @@ let(:name) { "A test post" } let(:args) { [name] } let(:posts_dir) { Pathname.new source_dir("_posts") } - let(:datestamp) { Time.now.strftime("%Y-%m-%d") } - let(:timestamp) { Time.now.strftime("%Y-%m-%d %H:%M %z") } + let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) } + let(:timestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT) } let(:filename) { "#{datestamp}-a-test-post.md" } let(:path) { posts_dir.join(filename) } @@ -67,7 +67,7 @@ context "when the post already exists" do let(:name) { "An existing post" } - let(:filename) { "#{Time.now.strftime("%Y-%m-%d")}-an-existing-post.md" } + let(:filename) { "#{Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT)}-an-existing-post.md" } before(:each) do FileUtils.touch path diff --git a/spec/publish_spec.rb b/spec/publish_spec.rb index acf0ce8..b147edc 100644 --- a/spec/publish_spec.rb +++ b/spec/publish_spec.rb @@ -4,7 +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(:datestamp) { Time.now.strftime("%Y-%m-%d") } + let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) } let(:post_filename) { "#{datestamp}-#{draft_to_publish}" } let(:args) { ["_drafts/#{draft_to_publish}"] }