lib/jekyll/post.rb in jekyll-1.0.0.rc1 vs lib/jekyll/post.rb in jekyll-1.0.0
- old
+ new
@@ -8,10 +8,25 @@
end
# 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
#
# Returns true if valid, false if not.
def self.valid?(name)
@@ -37,39 +52,41 @@
@base = self.containing_dir(source, dir)
@name = name
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')
end
@@ -94,10 +111,27 @@
else
self.extracted_excerpt
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.
#
# other - The other Post we are comparing to.
#
@@ -192,10 +226,11 @@
end
# sanitize url
@url = url.split('/').reject{ |part| part =~ /^\.+$/ }.join('/')
@url += "/" if url =~ /\/$/
+ @url.gsub!(/\A([^\/])/, '/\1')
@url
end
# The UID for this post (useful in feeds).
# e.g. /2008/11/05/my-awesome-post
@@ -210,28 +245,32 @@
# Returns an Array of related Posts.
def related_posts(posts)
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]
else
(posts - [self])[0..9]
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"}.
# site_payload - The site payload hash.
#
@@ -256,38 +295,17 @@
path = File.join(dest, CGI.unescape(self.url))
path = File.join(path, "index.html") if template[/\.html$/].nil?
path
end
- # Write the generated post file to the destination directory.
- #
- # dest - The String path to the destination dir.
- #
- # Returns nothing.
- def write(dest)
- path = destination(dest)
- FileUtils.mkdir_p(File.dirname(path))
- File.open(path, 'w') do |f|
- f.write(self.output)
- end
- end
-
# Convert this post into a Hash for use in Liquid templates.
#
# 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.
def inspect
"<Post: #{self.id}>"