Merge pull request #1002 from mojombo/refactor-post

Refactor Jekyll::Post
This commit is contained in:
Parker Moore 2013-04-28 16:40:22 -07:00
commit 3237b39f21
1 changed files with 66 additions and 36 deletions

View File

@ -10,6 +10,21 @@ module Jekyll
# Valid post name regex. # Valid post name regex.
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/ 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: # Post name validator. Post filenames must be like:
# 2008-11-05-my-awesome-post.textile # 2008-11-05-my-awesome-post.textile
# #
@ -39,35 +54,37 @@ module Jekyll
self.categories = dir.downcase.split('/').reject { |x| x.empty? } self.categories = dir.downcase.split('/').reject { |x| x.empty? }
self.process(name) self.process(name)
begin
self.read_yaml(@base, name) self.read_yaml(@base, name)
rescue Exception => msg
raise FatalException.new("#{msg} in #{@base}/#{name}")
end
# 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') if self.data.has_key?('date')
# ensure Time via to_s and reparse
self.date = Time.parse(self.data["date"].to_s) self.date = Time.parse(self.data["date"].to_s)
end end
if self.data.has_key?('published') && self.data['published'] == false self.published = self.published?
self.published = false
else self.populate_categories
self.published = true self.populate_tags
end end
self.tags = self.data.pluralized_array("tag", "tags") def published?
if self.data.has_key?('published') && self.data['published'] == false
false
else
true
end
end
def populate_categories
if self.categories.empty? if self.categories.empty?
self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.downcase} self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.downcase}
end end
self.tags.flatten!
self.categories.flatten! self.categories.flatten!
end end
def populate_tags
self.tags = self.data.pluralized_array("tag", "tags").flatten
end
# Get the full path to the directory containing the post files # Get the full path to the directory containing the post files
def containing_dir(source, dir) def containing_dir(source, dir)
return File.join(source, dir, '_posts') return File.join(source, dir, '_posts')
@ -96,6 +113,23 @@ module Jekyll
end end
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 # Compares Post objects. First compares the Post date. If the dates are
# equal, it compares the Post slugs. # equal, it compares the Post slugs.
# #
@ -212,6 +246,16 @@ module Jekyll
return [] unless posts.size > 1 return [] unless posts.size > 1
if self.site.lsi if self.site.lsi
build_index
related = self.class.lsi.find_related(self.content, 11)
related - [self]
else
(posts - [self])[0..9]
end
end
def build_index
self.class.lsi ||= begin self.class.lsi ||= begin
puts "Starting the classifier..." puts "Starting the classifier..."
lsi = Classifier::LSI.new(:auto_rebuild => false) lsi = Classifier::LSI.new(:auto_rebuild => false)
@ -222,12 +266,6 @@ module Jekyll
puts "" puts ""
lsi lsi
end end
related = self.class.lsi.find_related(self.content, 11)
related - [self]
else
(posts - [self])[0..9]
end
end end
# Add any necessary layouts to this post. # Add any necessary layouts to this post.
@ -262,18 +300,10 @@ module Jekyll
# #
# Returns the representative Hash. # Returns the representative Hash.
def to_liquid def to_liquid
self.data.deep_merge({ further_data = Hash[ATTRIBUTES_FOR_LIQUID.map { |attribute|
"title" => self.data["title"] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '), [attribute, send(attribute)]
"url" => self.url, }]
"date" => self.date, data.deep_merge(further_data)
"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\//, '') })
end end
# Returns the shorthand String identifier of this Post. # Returns the shorthand String identifier of this Post.