Class: Serif::ContentFile

Inherits:
Object
  • Object
show all
Defined in:
lib/serif/content_file.rb

Direct Known Subclasses

Draft, Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, path = nil) ⇒ ContentFile

Returns a new instance of ContentFile.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/serif/content_file.rb', line 9

def path
  @path
end

#siteObject (readonly)

Returns the value of attribute site.



9
10
11
# File 'lib/serif/content_file.rb', line 9

def site
  @site
end

#slugObject

Returns the value of attribute slug.



9
10
11
# File 'lib/serif/content_file.rb', line 9

def slug
  @slug
end

Instance Method Details

#basenameObject



26
27
28
# File 'lib/serif/content_file.rb', line 26

def basename
  File.basename(@path)
end

#content(include_headers = false) ⇒ Object



72
73
74
# 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

#createdObject



76
77
78
79
# File 'lib/serif/content_file.rb', line 76

def created
  return nil if !@source
  headers[:created].utc
end

#draft?Boolean

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

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/serif/content_file.rb', line 56

def draft?
  return true if !path

  File.dirname(path) == File.join(site.directory, Draft.dirname)
end

#headersObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

#inspectObject



131
132
133
# File 'lib/serif/content_file.rb', line 131

def inspect
  %Q{<#{self.class} #{headers.inspect}>}
end

#published?Boolean

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

If there is no path at all, returns false.

Returns:

  • (Boolean)


66
67
68
69
70
# 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) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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

#titleObject



39
40
41
42
# File 'lib/serif/content_file.rb', line 39

def title
  return nil if !@source
  headers[:title]
end

#title=(new_title) ⇒ Object



44
45
46
47
48
49
50
51
52
# 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

#updatedObject



81
82
83
84
# File 'lib/serif/content_file.rb', line 81

def updated
  return nil if !@source
  (headers[:updated] || created).utc
end