From 7e52ddd873c4df2b6e4cb85ab14926c86d4fbdab Mon Sep 17 00:00:00 2001 From: Brian Hedberg Date: Fri, 14 Oct 2016 12:31:11 -0500 Subject: [PATCH 1/4] added changes from mnot/jekyll-archives --- lib/jekyll-archives.rb | 18 +++++++++--------- lib/jekyll-archives/archive.rb | 28 ++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 94e59a9..157459e 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -6,14 +6,6 @@ module Archives autoload :Archive, 'jekyll-archives/archive' autoload :VERSION, 'jekyll-archives/version' - if (Jekyll.const_defined? :Hooks) - Jekyll::Hooks.register :site, :after_reset do |site| - # We need to disable incremental regen for Archives to generate with the - # correct content - site.regenerator.instance_variable_set(:@disabled, true) - end - end - class Archives < Jekyll::Generator safe true @@ -92,6 +84,14 @@ def enabled?(archive) end end + # Write archives to their destination + def write + @archives.each do |archive| + archive.write(@site.dest) if archive.regenerate? + archive.add_dependencies + end + end + def tags @site.post_attr_hash('tags') end @@ -105,7 +105,7 @@ def years hash = Hash.new { |h, key| h[key] = [] } # In Jekyll 3, Collection#each should be called on the #docs array directly. - if Jekyll::VERSION >= '3.0.0' + if Jekyll::VERSION >= '3.0.0' @posts.docs.each { |p| hash[p.date.strftime("%Y")] << p } else @posts.each { |p| hash[p.date.strftime("%Y")] << p } diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index c87ea89..49689d8 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -13,6 +13,7 @@ class Archive < Jekyll::Page name path url + permalink ).freeze # Initialize a new Archive page @@ -30,8 +31,8 @@ def initialize(site, title, type, posts) @config = site.config['jekyll-archives'] # Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb) - if title.to_s.length - @slug = Utils.slugify(title.to_s) + if title.is_a? String + @slug = Utils.slugify(title) end # Use ".html" for file extension and url for path @@ -86,6 +87,10 @@ def url raise ArgumentError.new "Template \"#{template}\" provided is invalid." end + def permalink + data && data.is_a?(Hash) && data['permalink'] + end + # Produce a title object suitable for Liquid based on type of archive. # # Returns a String (for tag and category archives) and nil for @@ -106,6 +111,25 @@ def date end end + # Add dependencies for incremental mode + def add_dependencies + if defined? site.regenerator + archive_path = site.in_dest_dir(relative_path) + site.regenerator.add(archive_path) + @posts.each do |post| + site.regenerator.add_dependency(archive_path, post.path) + end + end + end + + def regenerate? + if defined? site.regenerator + site.regenerator.regenerate?(self) + else + true + end + end + # Obtain the write path relative to the destination directory # # Returns the destination relative path String. From 0659b7d95568872e891ac64b35c05d67695edd7c Mon Sep 17 00:00:00 2001 From: Brian Hedberg Date: Fri, 14 Oct 2016 12:31:11 -0500 Subject: [PATCH 2/4] added changes from mnot/jekyll-archives --- lib/jekyll-archives.rb | 44 +++++++++++++++++++++++++++++----- lib/jekyll-archives/archive.rb | 28 ++-------------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 157459e..47f4526 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -6,6 +6,14 @@ module Archives autoload :Archive, 'jekyll-archives/archive' autoload :VERSION, 'jekyll-archives/version' + if (Jekyll.const_defined? :Hooks) + Jekyll::Hooks.register :site, :after_reset do |site| + # We need to disable incremental regen for Archives to generate with the + # correct content + site.regenerator.instance_variable_set(:@disabled, true) + end + end + class Archives < Jekyll::Generator safe true @@ -84,16 +92,40 @@ def enabled?(archive) end end - # Write archives to their destination - def write - @archives.each do |archive| - archive.write(@site.dest) if archive.regenerate? - archive.add_dependencies + # Helper method for tags + # Receives an array of strings + # Returns an array of strings without dashes + def remove_dashes(tags) + cleaned_tags = [] + tags.each do |tag| + cleaned_tags << tag.gsub(/-/, ' ').squeeze end + cleaned_tags end + # Helper method for tags + # Receives a post, and an external hash + # Assigns posts associated with particular tags to the provided hash. + def post_attr_tags(post, hash) + post.data['tags'] ||= [] + post.data['tags'] = remove_dashes(post.data['tags']) + post.data['tags'].each { |t| hash[t] << post } if post.data['tags'] + end + + # Custom `post_attr_hash` method for tags def tags - @site.post_attr_hash('tags') + hash = Hash.new { |h, key| h[key] = [] } + + # In Jekyll 3, Collection#each should be called on the #docs array directly. + if Jekyll::VERSION >= '3.0.0' + @posts.docs.each do |p| + post_attr_tags(p, hash) + end + else + @posts.each { |p| post_attr_tags(p, hash) } + end + hash.values.each { |posts| posts.sort!.reverse! } + hash end def categories diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index 49689d8..c87ea89 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -13,7 +13,6 @@ class Archive < Jekyll::Page name path url - permalink ).freeze # Initialize a new Archive page @@ -31,8 +30,8 @@ def initialize(site, title, type, posts) @config = site.config['jekyll-archives'] # Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb) - if title.is_a? String - @slug = Utils.slugify(title) + if title.to_s.length + @slug = Utils.slugify(title.to_s) end # Use ".html" for file extension and url for path @@ -87,10 +86,6 @@ def url raise ArgumentError.new "Template \"#{template}\" provided is invalid." end - def permalink - data && data.is_a?(Hash) && data['permalink'] - end - # Produce a title object suitable for Liquid based on type of archive. # # Returns a String (for tag and category archives) and nil for @@ -111,25 +106,6 @@ def date end end - # Add dependencies for incremental mode - def add_dependencies - if defined? site.regenerator - archive_path = site.in_dest_dir(relative_path) - site.regenerator.add(archive_path) - @posts.each do |post| - site.regenerator.add_dependency(archive_path, post.path) - end - end - end - - def regenerate? - if defined? site.regenerator - site.regenerator.regenerate?(self) - else - true - end - end - # Obtain the write path relative to the destination directory # # Returns the destination relative path String. From 64e68f24d7e2f1c338043cbde7f880d0b4080f0d Mon Sep 17 00:00:00 2001 From: Brian Hedberg Date: Wed, 9 Nov 2016 09:12:39 -0600 Subject: [PATCH 3/4] Add binding --- lib/jekyll-archives.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 47f4526..cb6c458 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -98,7 +98,7 @@ def enabled?(archive) def remove_dashes(tags) cleaned_tags = [] tags.each do |tag| - cleaned_tags << tag.gsub(/-/, ' ').squeeze + cleaned_tags << tag.gsub(/-/, ' ') end cleaned_tags end From cb7c31246afa19a7bba71f7af2b998ef010771bb Mon Sep 17 00:00:00 2001 From: Brian Hedberg Date: Wed, 9 Nov 2016 15:39:41 -0600 Subject: [PATCH 4/4] add tests for tags --- .../source/_posts/2016-11-09-post-with-dashed-tags.md | 8 ++++++++ .../source/_posts/2016-11-09-post-with-spaced-tags.md | 8 ++++++++ test/test_jekyll_archives.rb | 11 ++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/source/_posts/2016-11-09-post-with-dashed-tags.md create mode 100644 test/source/_posts/2016-11-09-post-with-spaced-tags.md diff --git a/test/source/_posts/2016-11-09-post-with-dashed-tags.md b/test/source/_posts/2016-11-09-post-with-dashed-tags.md new file mode 100644 index 0000000..8e07514 --- /dev/null +++ b/test/source/_posts/2016-11-09-post-with-dashed-tags.md @@ -0,0 +1,8 @@ +--- +title: Post with dashes +tags: + - words-with-dashes + - words-with-spaces +--- + +Tags in this post have text delimited by a dash. diff --git a/test/source/_posts/2016-11-09-post-with-spaced-tags.md b/test/source/_posts/2016-11-09-post-with-spaced-tags.md new file mode 100644 index 0000000..de5d7e0 --- /dev/null +++ b/test/source/_posts/2016-11-09-post-with-spaced-tags.md @@ -0,0 +1,8 @@ +--- +title: Post with spaces +tags: + - words with spaces + - words with dashes +--- + +Tags in this post have text delimited by a space. diff --git a/test/test_jekyll_archives.rb b/test/test_jekyll_archives.rb index 01eabda..3046711 100644 --- a/test/test_jekyll_archives.rb +++ b/test/test_jekyll_archives.rb @@ -16,18 +16,21 @@ class TestJekyllArchives < Minitest::Test @archives.generate(@site) assert archive_exists? @site, "2014/index.html" assert archive_exists? @site, "2013/index.html" + assert archive_exists? @site, "2016/index.html" end should "generate archive pages by month" do @archives.generate(@site) assert archive_exists? @site, "2014/08/index.html" assert archive_exists? @site, "2014/03/index.html" + assert archive_exists? @site, "2016/11/index.html" end should "generate archive pages by day" do @archives.generate(@site) assert archive_exists? @site, "2014/08/17/index.html" assert archive_exists? @site, "2013/08/16/index.html" + assert archive_exists? @site, "2016/11/09/index.html" end should "generate archive pages by tag" do @@ -35,6 +38,8 @@ class TestJekyllArchives < Minitest::Test assert archive_exists? @site, "tag/test-tag/index.html" assert archive_exists? @site, "tag/tagged/index.html" assert archive_exists? @site, "tag/new/index.html" + assert archive_exists? @site, "tag/words-with-dashes/index.html" + assert archive_exists? @site, "tag/words-with-spaces/index.html" end should "generate archive pages by category" do @@ -120,7 +125,7 @@ class TestJekyllArchives < Minitest::Test end should "populate the {{ site.archives }} tag in Liquid" do - assert_equal 12, read_file("length.html").to_i + assert_equal 17, read_file("length.html").to_i end end @@ -175,6 +180,10 @@ class TestJekyllArchives < Minitest::Test @day_archive = @archives.detect {|a| a.type == "day"} end + should "populate the {{ site.tags }} with tags that do not contain dashes" do + !@tag_archive.title.include? '-' + end + should "populate the title field in case of category or tag" do assert @tag_archive.title.is_a? String assert @category_archive.title.is_a? String