Class: Serif::Post

Inherits:
ContentFile show all
Defined in:
lib/serif/post.rb

Instance Attribute Summary

Attributes inherited from ContentFile

#path, #site, #slug

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ContentFile

#basename, #content, #created, #draft?, #headers, #initialize, #inspect, #published?, #save, #title, #title=, #updated

Constructor Details

This class inherits a constructor from Serif::ContentFile

Class Method Details

.all(site) ⇒ Object



60
61
62
63
# 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

.dirnameObject



5
6
7
# File 'lib/serif/post.rb', line 5

def self.dirname
  "_posts"
end

.from_basename(site, filename) ⇒ Object



65
66
67
# File 'lib/serif/post.rb', line 65

def self.from_basename(site, filename)
  all(site).find { |p| p.basename == filename }
end

Instance Method Details

#autoupdate=(value) ⇒ Object

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



32
33
34
35
36
37
38
39
40
# 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?Boolean

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.

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/serif/post.rb', line 46

def autoupdate?
  update_header = headers[:update]
  update_header && update_header.strip == "now"
end

#to_liquidObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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!Object

Updates the updated timestamp and saves the contents.

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



54
55
56
57
58
# File 'lib/serif/post.rb', line 54

def update!
  @source.headers.delete(:update)
  set_updated_time(Time.now)
  save
end

#urlObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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