class Serif::Draft

Attributes

autopublish[R]

Public Class Methods

all(site) click to toggle source
# File lib/serif/draft.rb, line 104
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/draft.rb, line 5
def self.dirname
  "_drafts"
end
exist?(site, slug) click to toggle source
# File lib/serif/draft.rb, line 100
def self.exist?(site, slug)
  all(site).any? { |d| d.slug == slug }
end
from_slug(site, slug) click to toggle source
# File lib/serif/draft.rb, line 109
def self.from_slug(site, slug)
  path = File.expand_path(File.join(site.directory, dirname, slug))
  new(site, path)
end
rename(site, original_slug, new_slug) click to toggle source
# File lib/serif/draft.rb, line 9
def self.rename(site, original_slug, new_slug)
  raise if File.exist?("#{site.directory}/#{dirname}/#{new_slug}")
  File.rename("#{site.directory}/#{dirname}/#{original_slug}", "#{site.directory}/#{dirname}/#{new_slug}")
end

Public Instance Methods

autopublish=(value) click to toggle source

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

# File lib/serif/draft.rb, line 64
def autopublish=(value)
  if value
    @source.headers[:publish] = "now"
  else
    @source.headers.delete(:publish)
  end

  headers_changed!
end
autopublish?() click to toggle source

Checks the value of the “publish” header, and returns true if the value is “now”, ignoring trailing and leading whitespace. Returns false, otherwise.

# File lib/serif/draft.rb, line 77
def autopublish?
  publish_header = headers[:publish]
  publish_header && publish_header.strip == "now"
end
delete!() click to toggle source
# File lib/serif/draft.rb, line 35
def delete!
  FileUtils.mkdir_p("#{site.directory}/_trash")
  File.rename(@path, File.expand_path("#{site.directory}/_trash/#{Time.now.to_i}-#{slug}"))
end
publish!() click to toggle source
# File lib/serif/draft.rb, line 40
def publish!
  publish_time = Time.now
  date = Time.now.strftime("%Y-%m-%d")
  filename = "#{date}-#{slug}"

  FileUtils.mkdir_p("#{site.directory}/#{Post.dirname}")
  full_published_path = File.expand_path("#{site.directory}/#{Post.dirname}/#{filename}")

  raise "conflict, post exists already" if File.exist?(full_published_path)

  set_publish_time(publish_time)

  @source.headers.delete(:publish) if autopublish?

  save

  File.rename(path, full_published_path)

  # update the path since the file has now changed
  @path = Post.new(site, full_published_path).path
end
to_liquid() click to toggle source
# File lib/serif/draft.rb, line 82
def to_liquid
  h = {
    "title" => title,
    "content" => content,
    "slug" => slug,
    "type" => "draft",
    "draft" => draft?,
    "published" => published?,
    "url" => url
  }

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

  h
end
url() click to toggle source

Returns the URL that would be used for this post if it were to be published now.

# File lib/serif/draft.rb, line 16
def url
  permalink_style = headers[:permalink] || site.config.permalink

  parts = {
    "title" => slug.to_s,
    "year" => Time.now.year.to_s,
    "month" => Time.now.month.to_s.rjust(2, "0"),
    "day" => Time.now.day.to_s.rjust(2, "0")
  }

  output = permalink_style

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

  output
end