lib/jekyll/post.rb in mojombo-jekyll-0.5.0 vs lib/jekyll/post.rb in mojombo-jekyll-0.5.1
- old
+ new
@@ -16,14 +16,17 @@
# Returns <Bool>
def self.valid?(name)
name =~ MATCHER
end
- attr_accessor :site
- attr_accessor :date, :slug, :ext, :categories, :topics, :published
- attr_accessor :data, :content, :output
+ attr_accessor :site, :date, :slug, :ext, :topics, :published, :data, :content, :output
+ attr_writer :categories
+ def categories
+ @categories ||= []
+ end
+
# Initialize this Post instance.
# +site+ is the Site
# +base+ is the String path to the dir containing the post file
# +name+ is the String filename of the post file
# +categories+ is an Array of Strings for the categories for this post
@@ -86,20 +89,11 @@
# permalink is absent, set to the default date
# e.g. "/2008/11/05/" if the permalink style is :date, otherwise nothing
#
# Returns <String>
def dir
- if permalink
- permalink.to_s.split("/")[0..-2].join("/") + '/'
- else
- prefix = self.categories.empty? ? '' : '/' + self.categories.join('/')
- if [:date, :pretty].include?(self.site.permalink_style)
- prefix + date.strftime("/%Y/%m/%d/")
- else
- prefix + '/'
- end
- end
+ File.dirname(url)
end
# The full path and filename of the post.
# Defined in the YAML of the post body
# (Optional)
@@ -107,25 +101,47 @@
# Returns <String>
def permalink
self.data && self.data['permalink']
end
+ def template
+ case self.site.permalink_style
+ when :pretty
+ "/:categories/:year/:month/:day/:title"
+ when :none
+ "/:categories/:title.html"
+ when :date
+ "/:categories/:year/:month/:day/:title.html"
+ else
+ self.site.permalink_style.to_s
+ end
+ end
+
# The generated relative url of this post
# e.g. /2008/11/05/my-awesome-post.html
#
# Returns <String>
def url
- ext = self.site.permalink_style == :pretty ? '' : '.html'
- permalink || self.id + ext
+ return permalink if permalink
+
+ @url ||= {
+ "year" => date.strftime("%Y"),
+ "month" => date.strftime("%m"),
+ "day" => date.strftime("%d"),
+ "title" => CGI.escape(slug),
+ "categories" => categories.sort.join('/')
+ }.inject(template) { |result, token|
+ result.gsub(/:#{token.first}/, token.last)
+ }.gsub(/\/\//, "/")
end
# The UID for this post (useful in feeds)
# e.g. /2008/11/05/my-awesome-post
#
# Returns <String>
def id
- self.dir + self.slug
+ File.join(self.dir, self.slug)
end
# Calculate related posts.
#
# Returns [<Post>]
@@ -170,13 +186,14 @@
#
# Returns nothing
def write(dest)
FileUtils.mkdir_p(File.join(dest, dir))
- path = File.join(dest, self.url)
+ # The url needs to be unescaped in order to preserve the correct filename
+ path = File.join(dest, CGI.unescape(self.url))
- if self.site.permalink_style == :pretty
+ if template[/\.html$/].nil?
FileUtils.mkdir_p(path)
path = File.join(path, "index.html")
end
File.open(path, 'w') do |f|
@@ -192,14 +209,35 @@
"url" => self.url,
"date" => self.date,
"id" => self.id,
"topics" => self.topics,
"categories" => self.categories,
+ "next" => self.next,
+ "previous" => self.previous,
"content" => self.content }.deep_merge(self.data)
end
def inspect
"<Post: #{self.id}>"
+ end
+
+ def next
+ pos = self.site.posts.index(self)
+
+ if pos && pos < self.site.posts.length-1
+ self.site.posts[pos+1]
+ else
+ nil
+ end
+ end
+
+ def previous
+ pos = self.site.posts.index(self)
+ if pos && pos > 0
+ self.site.posts[pos-1]
+ else
+ nil
+ end
end
end
end