Class: Serif::Draft

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

Instance Attribute Summary collapse

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

Instance Attribute Details

#autopublishObject

Returns the value of attribute autopublish.



3
4
5
# File 'lib/serif/draft.rb', line 3

def autopublish
  @autopublish
end

Class Method Details

.all(site) ⇒ Object



104
105
106
107
# 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

.dirnameObject



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

def self.dirname
  "_drafts"
end

.exist?(site, slug) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/serif/draft.rb', line 100

def self.exist?(site, slug)
  all(site).any? { |d| d.slug == slug }
end

.from_slug(site, slug) ⇒ Object



109
110
111
112
# 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) ⇒ Object



9
10
11
12
# 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

Instance Method Details

#autopublish?Boolean

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

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/serif/draft.rb', line 77

def autopublish?
  publish_header = headers[:publish]
  publish_header && publish_header.strip == "now"
end

#delete!Object



35
36
37
38
# 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!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_liquidObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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

#urlObject

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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