lib/jekyll/post.rb in jekyll-1.1.2 vs lib/jekyll/post.rb in jekyll-1.2.0

- old
+ new

@@ -1,14 +1,10 @@ module Jekyll class Post include Comparable include Convertible - class << self - attr_accessor :lsi - end - # Valid post name regex. MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)(\.[^.]+)$/ EXCERPT_ATTRIBUTES_FOR_LIQUID = %w[ title @@ -107,34 +103,40 @@ # The post excerpt. This is either a custom excerpt # set in YAML front matter or the result of extract_excerpt. # # Returns excerpt string. def excerpt - if self.data.has_key? 'excerpt' - self.data['excerpt'] - else - self.extracted_excerpt.to_s - end + self.data.fetch('excerpt', self.extracted_excerpt.to_s) 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(' ') + self.data.fetch("title", self.titleized_slug) end + # Turns the post slug into a suitable title + def titleized_slug + 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\//, '') + self.data.fetch('path', self.relative_path.sub(/\A\//, '')) end + # The path to the post source file, relative to the site source + def relative_path + File.join(@dir, '_posts', @name) + 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. # @@ -193,40 +195,35 @@ 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 the String URL. + # Returns the String url. def url - return @url if @url + @url ||= URL.new({ + :template => template, + :placeholders => url_placeholders, + :permalink => permalink + }).to_s + end - url = if permalink - permalink - else - { - "year" => date.strftime("%Y"), - "month" => date.strftime("%m"), - "day" => date.strftime("%d"), - "title" => CGI.escape(slug), - "i_day" => date.strftime("%d").to_i.to_s, - "i_month" => date.strftime("%m").to_i.to_s, - "categories" => categories.map { |c| URI.escape(c.to_s) }.join('/'), - "short_month" => date.strftime("%b"), - "y_day" => date.strftime("%j"), - "output_ext" => self.output_ext - }.inject(template) { |result, token| - result.gsub(/:#{Regexp.escape token.first}/, token.last) - }.gsub(/\/\//, "/") - end - - # sanitize url - @url = url.split('/').reject{ |part| part =~ /^\.+$/ }.join('/') - @url += "/" if url =~ /\/$/ - @url.gsub!(/\A([^\/])/, '/\1') - @url + # Returns a hash of URL placeholder names (as symbols) mapping to the + # desired placeholder replacements. For details see "url.rb" + def url_placeholders + { + :year => date.strftime("%Y"), + :month => date.strftime("%m"), + :day => date.strftime("%d"), + :title => CGI.escape(slug), + :i_day => date.strftime("%d").to_i.to_s, + :i_month => date.strftime("%m").to_i.to_s, + :categories => (categories || []).map { |c| URI.escape(c.to_s) }.join('/'), + :short_month => date.strftime("%b"), + :y_day => date.strftime("%j"), + :output_ext => self.output_ext + } end # The UID for this post (useful in feeds). # e.g. /2008/11/05/my-awesome-post # @@ -253,11 +250,13 @@ payload = { "site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) }, "page" => self.to_liquid(EXCERPT_ATTRIBUTES_FOR_LIQUID) }.deep_merge(site_payload) - self.extracted_excerpt.do_layout(payload, {}) + if generate_excerpt? + self.extracted_excerpt.do_layout(payload, {}) + end do_layout(payload.merge({"page" => self.to_liquid}), layouts) end # Obtain destination path. @@ -266,24 +265,14 @@ # # Returns destination file path String. def destination(dest) # The url needs to be unescaped in order to preserve the correct filename path = File.join(dest, CGI.unescape(self.url)) - path = File.join(path, "index.html") if template[/\.html$/].nil? + path = File.join(path, "index.html") if path[/\.html$/].nil? path end - # Convert this post into a Hash for use in Liquid templates. - # - # Returns the representative Hash. - def to_liquid(attrs = ATTRIBUTES_FOR_LIQUID) - further_data = Hash[attrs.map { |attribute| - [attribute, send(attribute)] - }] - data.deep_merge(further_data) - end - # Returns the shorthand String identifier of this Post. def inspect "<Post: #{self.id}>" end @@ -307,9 +296,17 @@ end protected def extract_excerpt - Jekyll::Excerpt.new(self) + if generate_excerpt? + Jekyll::Excerpt.new(self) + else + "" + end + end + + def generate_excerpt? + !(site.config['excerpt_separator'].to_s.empty?) end end end