Class: Weblog::Post
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
#format_date, #format_date_and_time
Constructor Details
#initialize(weblog, path, options = {}) ⇒ Post
Returns a new instance of Post.
5
6
7
8
9
10
11
|
# File 'lib/weblog/post.rb', line 5
def initialize(weblog, path, options={})
@weblog = weblog
@path = path
@post = self
@options = options
_load_metadata
end
|
Instance Attribute Details
#author ⇒ Object
76
77
78
|
# File 'lib/weblog/post.rb', line 76
def author
@author ||= _author
end
|
#path ⇒ Object
Returns the value of attribute path.
3
4
5
|
# File 'lib/weblog/post.rb', line 3
def path
@path
end
|
Instance Method Details
#_author ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/weblog/post.rb', line 68
def _author
if commit = first_commit
first_commit.author.name
else
@weblog.author
end
end
|
37
38
39
40
41
|
# File 'lib/weblog/post.rb', line 37
def _load_metadata
JSON.parse(File.read(metadata_filename)).each do |key, value|
self.send("#{key}=", value)
end if File.exist?(metadata_filename)
end
|
#_published_at ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/weblog/post.rb', line 83
def _published_at
if commit = first_commit
commit.date
else
Time.now
end
end
|
#_updated_at ⇒ Object
-
The updated date:
-
When it’s not in the git repository: nil
-
When it has modifications: current date and time
-
When it has no modifications: last commit date
99
100
101
102
103
104
105
106
107
|
# File 'lib/weblog/post.rb', line 99
def _updated_at
if modified?
Time.now
elsif commit = last_commit
commit.date
else
nil
end
end
|
#blob ⇒ Object
45
46
47
48
49
|
# File 'lib/weblog/post.rb', line 45
def blob
if File.exist?(index_filename)
@blob ||= @weblog.git.gblob(index_filename)
end
end
|
#content ⇒ Object
133
134
135
|
# File 'lib/weblog/post.rb', line 133
def content
snippet.content
end
|
#draft? ⇒ Boolean
15
16
17
|
# File 'lib/weblog/post.rb', line 15
def draft?
@options[:draft] == true
end
|
#first_commit ⇒ Object
55
56
57
|
# File 'lib/weblog/post.rb', line 55
def first_commit
@first_commit ||= blob.log(10_000).map{|l|l}.last if blob
end
|
#generate ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/weblog/post.rb', line 155
def generate
FileUtils.mkdir_p(File.dirname(index_filename))
File.open(index_filename, 'w') do |file|
file.write(render)
end
puts "Generated #{path}" if @weblog.verbose?
end
|
#index_filename ⇒ Object
151
152
153
|
# File 'lib/weblog/post.rb', line 151
def index_filename
File.join(@weblog.path, public_directory, @path, 'index.html')
end
|
#javascripts ⇒ Object
119
120
121
122
123
|
# File 'lib/weblog/post.rb', line 119
def javascripts
Dir.glob(File.join(@weblog.path, public_directory, path, '*.js')).map do |javascript_path|
File.join(path, File.basename(javascript_path))
end
end
|
#last_commit ⇒ Object
51
52
53
|
# File 'lib/weblog/post.rb', line 51
def last_commit
@last_commit ||= blob.log(1).first if blob
end
|
23
24
25
|
# File 'lib/weblog/post.rb', line 23
def metadata_filename
File.join(@weblog.path, public_directory, @path, 'post.json')
end
|
#modified? ⇒ Boolean
59
60
61
62
63
|
# File 'lib/weblog/post.rb', line 59
def modified?
@modified ||= @weblog.git.status.changed.any? do |filename, status|
snippet.filename.end_with?(filename)
end
end
|
#public_directory ⇒ Object
19
20
21
|
# File 'lib/weblog/post.rb', line 19
def public_directory
draft? ? 'draft' : 'public'
end
|
#published_at ⇒ Object
91
92
93
|
# File 'lib/weblog/post.rb', line 91
def published_at
@published_at ||= _published_at
end
|
#published_at=(published_at) ⇒ Object
29
30
31
|
# File 'lib/weblog/post.rb', line 29
def published_at=(published_at)
@published_at = Time.parse(published_at)
end
|
#render ⇒ Object
145
146
147
148
149
|
# File 'lib/weblog/post.rb', line 145
def render
Erubis::EscapedEruby.new(
File.read(template_filename)
).result(binding)
end
|
#snippet ⇒ Object
125
126
127
|
# File 'lib/weblog/post.rb', line 125
def snippet
@snippet ||= Snippet.new(@weblog, self, File.join(public_directory, path))
end
|
#stylesheets ⇒ Object
113
114
115
116
117
|
# File 'lib/weblog/post.rb', line 113
def stylesheets
Dir.glob(File.join(@weblog.path, public_directory, path, '*.css')).map do |stylesheet_path|
File.join(path, File.basename(stylesheet_path))
end
end
|
#template_filename ⇒ Object
141
142
143
|
# File 'lib/weblog/post.rb', line 141
def template_filename
File.join(@weblog.path, "templates/post.html.erb")
end
|
#title ⇒ Object
129
130
131
|
# File 'lib/weblog/post.rb', line 129
def title
snippet.title.strip
end
|
#updated_at ⇒ Object
109
110
111
|
# File 'lib/weblog/post.rb', line 109
def updated_at
@updated_at ||= _updated_at
end
|
#updated_at=(updated_at) ⇒ Object
33
34
35
|
# File 'lib/weblog/post.rb', line 33
def updated_at=(updated_at)
@updated_at = Time.parse(updated_at)
end
|