class Serum::Post

Constants

MATCHER

Valid post name regex.

Attributes

content[RW]
data[RW]
date[RW]
dir[RW]
ext[RW]
name[R]
output[RW]
published[RW]
site[RW]
slug[RW]

Public Class Methods

new(site, source, dir, name) click to toggle source

Initialize this Post instance.

site - The Site. base - The String path to the dir containing the post file. name - The String filename of the post file.

Returns the new Post.

# File lib/serum/post.rb, line 36
def initialize(site, source, dir, name)
  @site = site
  @base = source
  @dir = dir
  @name = name

  self.process(name)
  begin
    self.read_yaml(@base, name)
  rescue Exception => msg
    raise FatalException.new("#{msg} in #{@base}/#{name}")
  end

  # 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

  if self.data.has_key?('published') && self.data['published'] == false
    self.published = false
  else
    self.published = true
  end

  if self.data.has_key?('slug')
    self.slug = self.data['slug']
  end
end
valid?(name) click to toggle source

Post name validator. Post filenames must be like: 2008-11-05-my-awesome-post.textile

Returns true if valid, false if not.

# File lib/serum/post.rb, line 19
def self.valid?(name)
  name =~ MATCHER
end

Public Instance Methods

<=>(other) click to toggle source

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.

Returns -1, 0, 1

# File lib/serum/post.rb, line 96
def <=>(other)
  cmp = self.date <=> other.date
  if 0 == cmp
   cmp = self.slug <=> other.slug
  end
  return cmp
end
id() click to toggle source

The UID for this post (useful in feeds). e.g. /2008/11/05/my-awesome-post

Returns the String UID.

# File lib/serum/post.rb, line 145
def id
  File.join(self.dir, self.slug)
end
inspect() click to toggle source

Returns the shorthand String identifier of this Post.

# File lib/serum/post.rb, line 150
def inspect
  "<Post: #{self.id}>"
end
method_missing(meth, *args) click to toggle source
Calls superclass method
# File lib/serum/post.rb, line 173
def method_missing(meth, *args)
  if self.data.has_key?(meth.to_s) && args.empty?
    self.data[meth.to_s]
  else
    super
  end
end
next() click to toggle source
# File lib/serum/post.rb, line 154
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
previous() click to toggle source
# File lib/serum/post.rb, line 164
def previous
  pos = self.site.posts.index(self)
  if pos && pos > 0
    self.site.posts[pos-1]
  else
    nil
  end
end
process(name) click to toggle source

Extract information from the post filename.

name - The String filename of the post file.

Returns nothing.

# File lib/serum/post.rb, line 109
def process(name)
  m, cats, date, slug, ext = *name.match(MATCHER)
  self.date = Time.parse(date)
  self.slug = slug
  self.ext = ext
rescue ArgumentError
  raise FatalException.new("Post #{name} does not have a valid date.")
end
read_yaml(base, name) click to toggle source

Read the YAML frontmatter.

base - The String path to the dir containing the file. name - The String filename of the file.

Returns nothing.

# File lib/serum/post.rb, line 73
def read_yaml(base, name)
  begin
    content = File.read(File.join(base, name))

    if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
      self.data = YAML.safe_load($1)
      self.content = $' # everything after the last match
    end
  rescue => e
    puts "Error reading file #{File.join(base, name)}: #{e.message}"
  rescue SyntaxError => e
    puts "YAML Exception reading #{File.join(base, name)}: #{e.message}"
  end

  self.data ||= {}
end
url() click to toggle source

The generated relative url of this post. e.g. /2008/11/05/my-awesome-post.html

Returns the String URL.

# File lib/serum/post.rb, line 130
def url
  return @url if @url

  url = "#{self.site.baseurl}#{self.id}"

  # sanitize url
  @url = url.split('/').reject{ |part| part =~ /^\.+$/ }.join('/')
  @url += "/" if url =~ /\/$/
  @url
end