Class: Plate::Post

Inherits:
Page
  • Object
show all
Defined in:
lib/plate/post.rb

Overview

A model for each blog post

Direct Known Subclasses

Draft

Instance Attribute Summary

Attributes inherited from Page

#body, #content, #file, #layout, #meta, #partials, #raw, #site

Instance Method Summary collapse

Methods inherited from Page

#==, #base_path, #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

included

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

#categoryObject

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.meta[:category] || self.site.default_category
end

#category_for_urlObject

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

#dateObject

Returns the date for this post, either from the filename or meta hash. If both are provided, the meta information takes precedence.

Raises:



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.meta[:date]
    result = self.meta[: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.message}). Post skipped.")
  end

  raise NoPostDateProvided
end

#dayObject



37
38
39
# File 'lib/plate/post.rb', line 37

def day
  date.strftime('%d')
end

#file_pathObject

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

#inspectObject



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

#monthObject



50
51
52
# File 'lib/plate/post.rb', line 50

def month
  date.strftime('%m')
end

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

The template to use when generating the permalink.



102
103
104
# File 'lib/plate/post.rb', line 102

def permalink_template
  self.site.options[:permalink] || '/:category/:year/:month/:slug'
end

#slugObject

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

#tagsObject

Utility method to sanitize tags output. Tags are returned as an array.



121
122
123
# File 'lib/plate/post.rb', line 121

def tags
  @tags ||= (Array === self.meta[:tags] ? self.meta[:tags] : self.meta[:tags].to_s.strip.split(',')).collect { |s| s.to_s.strip }.sort
end

#yearObject



125
126
127
# File 'lib/plate/post.rb', line 125

def year
  date.strftime('%Y')
end