Merge pull request #1002 from mojombo/refactor-post
Refactor Jekyll::Post
This commit is contained in:
commit
3237b39f21
|
@ -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
|
||||
#
|
||||
|
@ -39,35 +54,37 @@ 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.
|
||||
if self.data.has_key?('date')
|
||||
# ensure Time via to_s and reparse
|
||||
self.date = Time.parse(self.data["date"].to_s)
|
||||
end
|
||||
|
||||
self.published = self.published?
|
||||
|
||||
self.populate_categories
|
||||
self.populate_tags
|
||||
end
|
||||
|
||||
def published?
|
||||
if self.data.has_key?('published') && self.data['published'] == false
|
||||
self.published = false
|
||||
false
|
||||
else
|
||||
self.published = true
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
self.tags = self.data.pluralized_array("tag", "tags")
|
||||
|
||||
def populate_categories
|
||||
if self.categories.empty?
|
||||
self.categories = self.data.pluralized_array('category', 'categories').map {|c| c.downcase}
|
||||
end
|
||||
|
||||
self.tags.flatten!
|
||||
self.categories.flatten!
|
||||
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
|
||||
def containing_dir(source, dir)
|
||||
return File.join(source, dir, '_posts')
|
||||
|
@ -96,6 +113,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.
|
||||
#
|
||||
|
@ -212,16 +246,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]
|
||||
|
@ -230,6 +255,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"}.
|
||||
|
@ -262,18 +300,10 @@ 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\//, '') })
|
||||
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.
|
||||
|
|
Loading…
Reference in New Issue