class Serif::ContentFile

Attributes

path[R]
site[R]
slug[R]

Public Class Methods

new(site, path = nil) click to toggle source
# File lib/serif/content_file.rb, line 11
def initialize(site, path = nil)
  @site = site
  @path = path

  if @path
    # we have to parse out the source first so that we get necessary
    # metadata like published vs. draft.
    load_source

    dirname = File.basename(File.dirname(@path))
    basename = File.basename(@path)
    @slug = draft? ? basename : basename.split("-")[3..-1].join("-")
  end
end

Public Instance Methods

basename() click to toggle source
# File lib/serif/content_file.rb, line 26
def basename
  File.basename(@path)
end
content(include_headers = false) click to toggle source
# File lib/serif/content_file.rb, line 72
def content(include_headers = false)
  include_headers ? "#{@source.headers.to_s}\n\n#{@source.to_s}" : @source.to_s
end
created() click to toggle source
# File lib/serif/content_file.rb, line 76
def created
  return nil if !@source
  headers[:created].utc
end
draft?() click to toggle source

Returns true if the file is in the directory for draft content, or has no saved path yet.

# File lib/serif/content_file.rb, line 56
def draft?
  return true if !path

  File.dirname(path) == File.join(site.directory, Draft.dirname)
end
headers() click to toggle source
# File lib/serif/content_file.rb, line 86
def headers
  return @cached_headers if @cached_headers

  return (@cached_headers = {}) unless @source

  headers = @source.headers
  converted_headers = {}

  headers.each do |header|
    key, value = header.key, header.value

    if key == :created || key == :updated
      value = Time.parse(value)
    end

    converted_headers[key] = value
  end

  @cached_headers = converted_headers
end
inspect() click to toggle source
# File lib/serif/content_file.rb, line 131
def inspect
  %Q{<#{self.class} #{headers.inspect}>}
end
published?() click to toggle source

Returns true if the file is in the directory for published posts, false otherwise.

If there is no path at all, returns false.

# File lib/serif/content_file.rb, line 66
def published?
  return false if !path

  File.dirname(path) == File.join(site.directory, Post.dirname)
end
save(markdown = nil) click to toggle source
# File lib/serif/content_file.rb, line 107
  def save(markdown = nil)
    markdown ||= content if @source

    save_path = path || "#{self.class.dirname}/#{@slug}"

    # TODO: when a draft is being saved, it will call set_publish_time
    # and then the #save call will execute this line, which will mean
    # there is a very, very slight difference (fraction of a second)
    # between the update time of a brand new published post and the
    # creation time.
    set_updated_time(Time.now)

    File.open(save_path, "w") do |f|
      f.puts %Q{#{@source.headers.to_s}

#{markdown}}.strip
    end

    # after every save, ensure we've re-loaded the saved content
    load_source

    true # always return true for now
  end
slug=(str) click to toggle source
# File lib/serif/content_file.rb, line 30
def slug=(str)
  @slug = str

  # if we're adding a slug and there's no path yet, then create the path.
  # this will run for new drafts

  @path ||= File.expand_path("#{site.directory}/#{self.class.dirname}/#{@slug}")
end
title() click to toggle source
# File lib/serif/content_file.rb, line 39
def title
  return nil if !@source
  headers[:title]
end
title=(new_title) click to toggle source
# File lib/serif/content_file.rb, line 44
def title=(new_title)
  if !@source
    @source = Redhead::String["title: #{new_title}\n\n"]
  else
    @source.headers[:title] = new_title
  end

  @cached_headers = nil
end
updated() click to toggle source
# File lib/serif/content_file.rb, line 81
def updated
  return nil if !@source
  (headers[:updated] || created).utc
end

Protected Instance Methods

headers_changed!() click to toggle source

Invalidates the cached headers entirely.

Any methods which alter headers should call this.

# File lib/serif/content_file.rb, line 150
def headers_changed!
  @cached_headers = nil
end
set_publish_time(time) click to toggle source
# File lib/serif/content_file.rb, line 137
def set_publish_time(time)
  @source.headers[:created] = time.xmlschema
  headers_changed!
end
set_updated_time(time) click to toggle source
# File lib/serif/content_file.rb, line 142
def set_updated_time(time)
  @source.headers[:updated] = time.xmlschema
  headers_changed!
end

Private Instance Methods

load_source() click to toggle source
# File lib/serif/content_file.rb, line 156
def load_source
  source = File.read(path).gsub(/\r?\n/, "\n")
  source.force_encoding("UTF-8")
  @source = Redhead::String[source]
  @cached_headers = nil
end