Class: Plate::Post
Overview
A model for each blog post
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Page
#body, #content, #file, #layout, #meta, #partials, #raw, #site
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare two posts, by date.
-
#category ⇒ Object
Returns the category for this blog post.
-
#category_for_url ⇒ Object
Category for this post, formatted to be URL-friendly.
-
#date ⇒ Object
Returns the date for this post, either from the filename or meta hash.
- #day ⇒ Object
-
#file_path ⇒ Object
The full file path of where this file will be written to.
- #inspect ⇒ Object
- #month ⇒ Object
-
#permalink(cache_buster = false) ⇒ Object
Return the [relative] path for this post.
-
#permalink_template ⇒ Object
The template to use when generating the permalink.
-
#slug ⇒ Object
Returns the URL slug to use for this blog post.
-
#tags ⇒ Object
Utility method to sanitize tags output.
- #year ⇒ Object
Methods inherited from Page
#==, #basename, #directory, #downgrade?, #engines, #extension, #extensions, #file?, #file_name, #format_extension, #id, #initialize, #keywords, #load!, #loaded?, #path, #relative_file, #reload!, #rendered_body, #rendered_content, #title_for_url, #to_s, #url, #write!
Methods included from Callbacks
Constructor Details
This class inherits a constructor from Plate::Page
Instance Method Details
#<=>(other) ⇒ Object
Compare two posts, by date.
130 131 132 |
# File 'lib/plate/post.rb', line 130 def <=>(other) self.date <=> other.date end |
#category ⇒ Object
Returns the category for this blog post. If no category is given in the meta information, then the value for config is used.
If no default category has been given, this will default to “Posts”
8 9 10 |
# File 'lib/plate/post.rb', line 8 def category default = self.[:category] || self.site.default_category end |
#category_for_url ⇒ Object
Category for this post, formatted to be URL-friendly
13 14 15 |
# File 'lib/plate/post.rb', line 13 def category_for_url self.category.to_s.dasherize.parameterize end |
#date ⇒ Object
Returns the date for this post, either from the filename or meta hash. If both are provided, the meta information takes precedence.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/plate/post.rb', line 19 def date result = nil if self.[:date] result = self.[:date].to_s elsif self.basename =~ /^(\d{4}-\d{2}-\d{2})-/ result = $1.to_s end begin return Time.parse(result) rescue Exception => e self.site.log(" ** Problem reading date for file #{relative_file} (#{e.}). Post skipped.") end raise NoPostDateProvided end |
#day ⇒ Object
37 38 39 |
# File 'lib/plate/post.rb', line 37 def day date.strftime('%d') end |
#file_path ⇒ Object
The full file path of where this file will be written to. (Relative to site root)
42 43 44 |
# File 'lib/plate/post.rb', line 42 def file_path "#{permalink}/index.html" end |
#inspect ⇒ Object
46 47 48 |
# File 'lib/plate/post.rb', line 46 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} name=#{name.to_s.inspect} date=#{date.to_s}>" end |
#month ⇒ Object
50 51 52 |
# File 'lib/plate/post.rb', line 50 def month date.strftime('%m') end |
#permalink(cache_buster = false) ⇒ Object
Return the [relative] path for this post. Uses the permalink_template
variable as the method for converting post data into a URL.
The permalink_template can be set in the global config named ‘permalink’.
Available options are:
-
‘date` - The date of this post, formatted as YYYY-MM-DD
-
‘title` - The title of this post, formatted for URL
-
‘slug` - The filename slug
-
‘year` - The 4-digit year of this post
-
‘month` - The 2-digit month for this post
-
‘day` - The 2-digit day of month for this post
-
‘category` - The category for this post
All values are formatted to be URL-safe. (No spaces, underscores or weird characters.)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/plate/post.rb', line 70 def permalink(cache_buster = false) return @permalink if @permalink and !cache_buster date = self.date # All of these variables can be put into a URL permalink_attributes = { "date" => date.strftime('%Y-%m-%d'), "slug" => slug, "title" => title_for_url, "year" => year, "month" => month, "day" => day, "category" => category_for_url } # Copy the permalink template as a starting point result = permalink_template.clone # Replace all variables from the attributes into the template permalink_attributes.each { |key, value| result.gsub!(/:#{Regexp.escape(key)}/, value) } # Remove any double slashes result.gsub!(/\/\//, '/') # Remove file extensions, and cleanup URL result = result.split('/').reject{ |segment| segment =~ /^\.+$/ }.join('/') @permalink = result end |
#permalink_template ⇒ Object
The template to use when generating the permalink.
102 103 104 |
# File 'lib/plate/post.rb', line 102 def permalink_template self.site.[:permalink] || '/:category/:year/:month/:slug' end |
#slug ⇒ Object
Returns the URL slug to use for this blog post.
This will convert the file name from a format like:
2012-01-01-post-name.md
To simply:
post-name
115 116 117 118 |
# File 'lib/plate/post.rb', line 115 def slug name = self.basename.to_s.downcase.gsub(/^(\d{4}-\d{2}-\d{2})-/, '').split('.')[0] name.dasherize.parameterize end |
#tags ⇒ Object
Utility method to sanitize tags output. Tags are returned as an array.
121 122 123 |
# File 'lib/plate/post.rb', line 121 def @tags ||= (Array === self.[:tags] ? self.[:tags] : self.[:tags].to_s.strip.split(',')).collect { |s| s.to_s.strip }.sort end |
#year ⇒ Object
125 126 127 |
# File 'lib/plate/post.rb', line 125 def year date.strftime('%Y') end |