class Serif::Post

Public Class Methods

all(site) click to toggle source
# File lib/serif/post.rb, line 60
def self.all(site)
  files = Dir[File.join(site.directory, dirname, "*")].select { |f| File.file?(f) }.map { |f| File.expand_path(f) }
  files.map { |f| new(site, f) }
end
dirname() click to toggle source
# File lib/serif/post.rb, line 5
def self.dirname
  "_posts"
end
from_basename(site, filename) click to toggle source
# File lib/serif/post.rb, line 65
def self.from_basename(site, filename)
  all(site).find { |p| p.basename == filename }
end

Public Instance Methods

autoupdate=(value) click to toggle source

if the assigned value is truthy, the “update” header is set to “now”, otherwise the header is removed.

# File lib/serif/post.rb, line 32
def autoupdate=(value)
  if value
    @source.headers[:update] = "now"
  else
    @source.headers.delete(:update)
  end

  headers_changed!
end
autoupdate?() click to toggle source

returns true if the post has been marked as needing a new updated timestamp header.

this is based on the presence of an “update: now” header.

# File lib/serif/post.rb, line 46
def autoupdate?
  update_header = headers[:update]
  update_header && update_header.strip == "now"
end
to_liquid() click to toggle source
# File lib/serif/post.rb, line 69
def to_liquid
  h = {
    "title" => title,
    "created" => created,
    "updated" => updated,
    "content" => content,
    "slug" => slug,
    "url" => url,
    "type" => "post",
    "draft" => draft?,
    "published" => published?,
    "basename" => basename
  }

  headers.each do |key, value|
    h[key.to_s] = value
  end

  h
end
update!() click to toggle source

Updates the updated timestamp and saves the contents.

If there is an “update” header (see autoupdate?), it is deleted.

# File lib/serif/post.rb, line 54
def update!
  @source.headers.delete(:update)
  set_updated_time(Time.now)
  save
end
url() click to toggle source
# File lib/serif/post.rb, line 9
def url
  permalink_style = headers[:permalink] || site.config.permalink

  filename_parts = File.basename(path).split("-")

  parts = {
    "title" => slug,
    "year" => filename_parts[0],
    "month" => filename_parts[1],
    "day" => filename_parts[2]
  }

  output = permalink_style

  parts.each do |placeholder, value|
    output = output.gsub(Regexp.quote(":" + placeholder), value)
  end

  output
end