From ffa93c22f193cc42978e4d2db73ffee573b417b7 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 20:03:57 +0200 Subject: [PATCH 1/9] Refactor Post#to_liquid --- lib/jekyll/post.rb | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index a7afa800..75e8dbee 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -96,6 +96,23 @@ module Jekyll end end + # Public: the Post title, from the YAML Front-Matter or from the slug + # + # Returns the post title + def title + self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' ') + end + + # Public: the path to the post relative to the site source, + # from the YAML Front-Matter or from a combination of + # the directory it's in, "_posts", and the name of the + # post file + # + # Returns the path to the file relative to the site source + def path + self.data['path'] || File.join(@dir, '_posts', @name).sub(/\A\//, '') + end + # Compares Post objects. First compares the Post date. If the dates are # equal, it compares the Post slugs. # @@ -262,18 +279,7 @@ module Jekyll # # Returns the representative Hash. def to_liquid - self.data.deep_merge({ - "title" => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '), - "url" => self.url, - "date" => self.date, - "id" => self.id, - "categories" => self.categories, - "next" => self.next, - "previous" => self.previous, - "tags" => self.tags, - "content" => self.content, - "excerpt" => self.excerpt, - "path" => self.data['path'] || File.join(@dir, '_posts', @name).sub(/\A\//, '') }) + self.data.deep_merge(methods_as_hash_for_liquid) end # Returns the shorthand String identifier of this Post. @@ -302,6 +308,17 @@ module Jekyll protected + # Protected: Fetch a Hash of specified attributes which has a corresponding + # method: title, url, date, id, categories, next, previous, tags, + # content, excerpt, and path + # + # Returns a hash of the attributes and their values + def methods_as_hash_for_liquid + Hash[%w[title url date id categories next previous tags content excerpt path].map { |attribute| + [attribute, send(attribute.to_sym)] + }] + end + # Internal: Extract excerpt from the content # # By default excerpt is your first paragraph of a post: everything before From 09c1c01d073744f549ee1f806e379faf14a5b805 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 20:05:52 +0200 Subject: [PATCH 2/9] Refactor Post#related_posts to build the index in a different method --- lib/jekyll/post.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 75e8dbee..3eab2b04 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -229,16 +229,7 @@ module Jekyll return [] unless posts.size > 1 if self.site.lsi - self.class.lsi ||= begin - puts "Starting the classifier..." - lsi = Classifier::LSI.new(:auto_rebuild => false) - $stdout.print(" Populating LSI... ");$stdout.flush - posts.each { |x| $stdout.print(".");$stdout.flush;lsi.add_item(x) } - $stdout.print("\n Rebuilding LSI index... ") - lsi.build_index - puts "" - lsi - end + build_index related = self.class.lsi.find_related(self.content, 11) related - [self] @@ -247,6 +238,19 @@ module Jekyll end end + def build_index + self.class.lsi ||= begin + puts "Starting the classifier..." + lsi = Classifier::LSI.new(:auto_rebuild => false) + $stdout.print(" Populating LSI... "); $stdout.flush + posts.each { |x| $stdout.print("."); $stdout.flush; lsi.add_item(x) } + $stdout.print("\n Rebuilding LSI index... ") + lsi.build_index + puts "" + lsi + end + end + # Add any necessary layouts to this post. # # layouts - A Hash of {"name" => "layout"}. From 266a52b161f358715706d46094893f90a333e9f8 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 20:09:18 +0200 Subject: [PATCH 3/9] The call to Post#read_yaml was in an unnecessary begin...rescue block. --- lib/jekyll/post.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 3eab2b04..1ebb7f94 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -39,11 +39,7 @@ module Jekyll self.categories = dir.downcase.split('/').reject { |x| x.empty? } self.process(name) - begin - self.read_yaml(@base, name) - rescue Exception => msg - raise FatalException.new("#{msg} in #{@base}/#{name}") - end + self.read_yaml(@base, name) # If we've added a date and time to the YAML, use that instead of the # filename date. Means we'll sort correctly. From 482986e3467457ced56c814ce91fd04f387551ac Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 20:21:17 +0200 Subject: [PATCH 4/9] Refactored setting of Post.published flag --- lib/jekyll/post.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 1ebb7f94..4e337e96 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -41,18 +41,11 @@ module Jekyll self.process(name) self.read_yaml(@base, name) - # If we've added a date and time to the YAML, use that instead of the - # filename date. Means we'll sort correctly. if self.data.has_key?('date') - # ensure Time via to_s and reparse self.date = Time.parse(self.data["date"].to_s) end - if self.data.has_key?('published') && self.data['published'] == false - self.published = false - else - self.published = true - end + self.published = self.is_published self.tags = self.data.pluralized_array("tag", "tags") @@ -64,6 +57,14 @@ module Jekyll self.categories.flatten! end + def is_published + if self.data.has_key?('published') && self.data['published'] == false + false + else + true + end + end + # Get the full path to the directory containing the post files def containing_dir(source, dir) return File.join(source, dir, '_posts') From 6d9845a58b14a0dfba1120193bcd86ec9aaa8f97 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 20:23:58 +0200 Subject: [PATCH 5/9] Refactor Post category instantiation --- lib/jekyll/post.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 4e337e96..46d64d3a 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -47,14 +47,8 @@ module Jekyll self.published = self.is_published - self.tags = self.data.pluralized_array("tag", "tags") - - if self.categories.empty? - self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.downcase} - end - - self.tags.flatten! - self.categories.flatten! + self.setup_categories + self.setup_tags end def is_published @@ -65,6 +59,18 @@ module Jekyll end end + def setup_categories + if self.categories.empty? + self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.downcase} + end + self.categories.flatten! + end + + def setup_tags + self.tags = self.data.pluralized_array("tag", "tags") + self.tags.flatten! + end + # Get the full path to the directory containing the post files def containing_dir(source, dir) return File.join(source, dir, '_posts') From 422a4bd5cdd4b1932c963600fe960d9ae3491113 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 20:25:07 +0200 Subject: [PATCH 6/9] Renamed tag- and category-instantiation methods --- lib/jekyll/post.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 46d64d3a..d62c8e51 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -47,8 +47,8 @@ module Jekyll self.published = self.is_published - self.setup_categories - self.setup_tags + self.populate_categories + self.populate_tags end def is_published @@ -59,14 +59,14 @@ module Jekyll end end - def setup_categories + def populate_categories if self.categories.empty? self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.downcase} end self.categories.flatten! end - def setup_tags + def populate_tags self.tags = self.data.pluralized_array("tag", "tags") self.tags.flatten! end From f7841d17139738627ea7f08575def787e2856d48 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 20:59:54 +0200 Subject: [PATCH 7/9] Remove unnecessary self.tags.flatten! in favour of a one-line sol'n. --- lib/jekyll/post.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index d62c8e51..f75c6186 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -67,8 +67,7 @@ module Jekyll end def populate_tags - self.tags = self.data.pluralized_array("tag", "tags") - self.tags.flatten! + self.tags = self.data.pluralized_array("tag", "tags").flatten end # Get the full path to the directory containing the post files From fcbab9a3ca8c8bfd5b381806dc856b955e6c50e6 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 26 Apr 2013 21:00:47 +0200 Subject: [PATCH 8/9] Post#is_published ~> Post#published? --- lib/jekyll/post.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index f75c6186..0b2d6330 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -45,13 +45,13 @@ module Jekyll self.date = Time.parse(self.data["date"].to_s) end - self.published = self.is_published + self.published = self.published? self.populate_categories self.populate_tags end - def is_published + def published? if self.data.has_key?('published') && self.data['published'] == false false else From f7fde70f5f1c31c7c115a8d6183e02a5ed32c4fd Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 28 Apr 2013 15:21:17 +0200 Subject: [PATCH 9/9] =?UTF-8?q?'The=20Best=20of=20Both=20Worlds,'=20=C3=A0?= =?UTF-8?q?=20la=20@chad's=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/jekyll/post.rb | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 0b2d6330..9824ef02 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -10,6 +10,21 @@ module Jekyll # Valid post name regex. MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/ + # Attributes for Liquid templates + ATTRIBUTES_FOR_LIQUID = %w[ + title + url + date + id + categories + next + previous + tags + content + excerpt + path + ] + # Post name validator. Post filenames must be like: # 2008-11-05-my-awesome-post.textile # @@ -285,7 +300,10 @@ module Jekyll # # Returns the representative Hash. def to_liquid - self.data.deep_merge(methods_as_hash_for_liquid) + further_data = Hash[ATTRIBUTES_FOR_LIQUID.map { |attribute| + [attribute, send(attribute)] + }] + data.deep_merge(further_data) end # Returns the shorthand String identifier of this Post. @@ -314,17 +332,6 @@ module Jekyll protected - # Protected: Fetch a Hash of specified attributes which has a corresponding - # method: title, url, date, id, categories, next, previous, tags, - # content, excerpt, and path - # - # Returns a hash of the attributes and their values - def methods_as_hash_for_liquid - Hash[%w[title url date id categories next previous tags content excerpt path].map { |attribute| - [attribute, send(attribute.to_sym)] - }] - end - # Internal: Extract excerpt from the content # # By default excerpt is your first paragraph of a post: everything before