Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Refactor tags and categories #1639

Merged
merged 1 commit into from

4 participants

@maul-esel

Until now, Site maintained a list of tags and categories where each tag or category as associated with the posts that had the tag / belonged to the category. At the same time, Site#post_attr_hash produces such a hash.

So to avoid the duplication, this PR removes the special handling of tags and categories in favor of the more generic implementation in #post_attr_hash. In case any plugins use Site#tags and Site#categories (it's never used inside jekyll, except for the tests), I added dummy methods for them that just call #post_attr_hash.

@kelvinst

This is related to #670

But, since this doesn't hurt the actual behavior, I think it can be merged before that old PR.

@maul-esel

The travis failure is that old time issue again, not related to this PR. It actually builds fine.

@parkr parkr closed this
@parkr parkr reopened this
@parkr
Owner

Hey, would you be willing to rebase this?

@maul-esel maul-esel remove duplicate code for tags and categories
Previously the `Site#tags` and `Site#categories` actually had the
same structure as a hash returned by `Site#post_attr_hash()`, but
maintained separately. Remove this duplicated infrastructure. For
backwards-compatibility for plugins, provide aliases for them.
8c0e5d8
@maul-esel

No problem.

@parkr
Owner

You rock, thanks @maul-esel.

@mattr- Thoughts on this?

@mattr-
Owner

:+1:

@parkr parkr commented on the diff
lib/jekyll/site.rb
@@ -277,10 +272,18 @@ def post_attr_hash(post_attr)
# array of posts ) then sort each array in reverse order.
hash = Hash.new { |h, key| h[key] = [] }
posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
- hash.values.map { |sortme| sortme.sort! { |a, b| b <=> a } }
+ hash.values.each { |posts| posts.sort!.reverse! }
@parkr Owner
parkr added a note

Are posts sorted in the "correct" order still? Not sure if that's tested. It looks from the old code like it needs to be in reverse-chronological order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parkr parkr merged commit bc7aed2 into jekyll:master
@parkr parkr added this to the 2.0 milestone
@maul-esel maul-esel deleted the maul-esel:tags-and-categories branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 30, 2014
  1. @maul-esel

    remove duplicate code for tags and categories

    maul-esel authored
    Previously the `Site#tags` and `Site#categories` actually had the
    same structure as a hash returned by `Site#post_attr_hash()`, but
    maintained separately. Remove this duplicated infrastructure. For
    backwards-compatibility for plugins, provide aliases for them.
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 10 deletions.
  1. +11 −10 lib/jekyll/site.rb
View
21 lib/jekyll/site.rb
@@ -1,8 +1,8 @@
module Jekyll
class Site
attr_accessor :config, :layouts, :posts, :pages, :static_files,
- :categories, :exclude, :include, :source, :dest, :lsi, :highlighter,
- :permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts,
+ :exclude, :include, :source, :dest, :lsi, :highlighter,
+ :permalink_style, :time, :future, :safe, :plugins, :limit_posts,
:show_drafts, :keep_files, :baseurl, :data, :file_read_opts, :gems
attr_accessor :converters, :generators
@@ -50,8 +50,6 @@ def reset
self.posts = []
self.pages = []
self.static_files = []
- self.categories = Hash.new { |hash, key| hash[key] = [] }
- self.tags = Hash.new { |hash, key| hash[key] = [] }
self.data = {}
if limit_posts < 0
@@ -238,9 +236,6 @@ def render
[posts, pages].flatten.each do |page_or_post|
page_or_post.render(layouts, payload)
end
-
- categories.values.map { |ps| ps.sort! { |a, b| b <=> a } }
- tags.values.map { |ps| ps.sort! { |a, b| b <=> a } }
rescue Errno::ENOENT => e
# ignore missing layout dir
end
@@ -277,10 +272,18 @@ def post_attr_hash(post_attr)
# array of posts ) then sort each array in reverse order.
hash = Hash.new { |h, key| h[key] = [] }
posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
- hash.values.map { |sortme| sortme.sort! { |a, b| b <=> a } }
+ hash.values.each { |posts| posts.sort!.reverse! }
@parkr Owner
parkr added a note

Are posts sorted in the "correct" order still? Not sure if that's tested. It looks from the old code like it needs to be in reverse-chronological order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
hash
end
+ def tags
+ post_attr_hash('tags')
+ end
+
+ def categories
+ post_attr_hash('categories')
+ end
+
# Prepare site data for site payload. The method maintains backward compatibility
# if the key 'data' is already used in _config.yml.
#
@@ -376,8 +379,6 @@ def get_entries(dir, subfolder)
# Returns nothing
def aggregate_post_info(post)
posts << post
- post.categories.each { |c| categories[c] << post }
- post.tags.each { |c| tags[c] << post }
end
def relative_permalinks_deprecation_method
Something went wrong with that request. Please try again.