Class: Weblog
- Inherits:
-
Object
show all
- Defined in:
- lib/weblog.rb,
lib/weblog/post.rb,
lib/weblog/index.rb,
lib/weblog/month.rb,
lib/weblog/helpers.rb,
lib/weblog/snippet.rb
Defined Under Namespace
Modules: Helpers
Classes: Index, Month, Post, Snippet
Constant Summary
collapse
- DIRECTORIES =
%w(
public/javascripts
public/stylesheets
draft
)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(cwd, options = {}) ⇒ Weblog
Returns a new instance of Weblog.
46
47
48
49
50
51
52
|
# File 'lib/weblog.rb', line 46
def initialize(cwd, options={})
@options = options
unless @path = self.class.find_working_directory(cwd)
puts "[!] Please use this tool from somewhere within the working directory"
end
_load_metadata
end
|
Instance Attribute Details
#path ⇒ Object
74
75
76
|
# File 'lib/weblog.rb', line 74
def path
@path
end
|
#title ⇒ Object
74
75
76
|
# File 'lib/weblog.rb', line 74
def title
@title
end
|
Class Method Details
.find_working_directory(cwd) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/weblog.rb', line 33
def self.find_working_directory(cwd)
candidate = cwd
while(candidate)
directory = File.join(candidate, 'public')
if File.exist?(directory)
return candidate
else
parts = candidate.split('/')
candidate = parts.empty? ? false : parts[0..-2].join('/')
end
end; nil
end
|
.generate(options) ⇒ Object
167
168
169
170
171
|
# File 'lib/weblog.rb', line 167
def self.generate(options)
weblog = new(Dir.pwd, options)
weblog.generate
weblog
end
|
.init(options) ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/weblog.rb', line 159
def self.init(options)
DIRECTORIES.each { |path| FileUtils.mkdir_p(path) }
unless File.exist?('templates')
FileUtils.cp_r(template_path, 'templates')
FileUtils.rm_rf('templates/.svn')
end
end
|
.preview(options) ⇒ Object
185
186
187
188
189
|
# File 'lib/weblog.rb', line 185
def self.preview(options)
weblog = new(Dir.pwd, options)
weblog.preview
weblog
end
|
.print_info(options) ⇒ Object
179
180
181
182
183
|
# File 'lib/weblog.rb', line 179
def self.print_info(options)
weblog = new(Dir.pwd, options)
weblog.print_info
weblog
end
|
.publish(options, *directories) ⇒ Object
173
174
175
176
177
|
# File 'lib/weblog.rb', line 173
def self.publish(options, *directories)
weblog = new(Dir.pwd, options)
weblog.publish(*directories)
weblog
end
|
.source_root ⇒ Object
25
26
27
|
# File 'lib/weblog.rb', line 25
def self.source_root
File.expand_path('../../', __FILE__)
end
|
.template_path ⇒ Object
29
30
31
|
# File 'lib/weblog.rb', line 29
def self.template_path
File.join(source_root, 'templates')
end
|
Instance Method Details
66
67
68
69
70
|
# File 'lib/weblog.rb', line 66
def _load_metadata
JSON.parse(File.read(metadata_filename)).each do |key, value|
self.send("#{key}=", value)
end if File.exist?(metadata_filename)
end
|
#author ⇒ Object
116
117
118
|
# File 'lib/weblog.rb', line 116
def author
git.config('user.name')
end
|
#drafts ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/weblog.rb', line 98
def drafts
if @drafts.nil?
paths = Set.new
base_path = File.join(@path, 'draft')
path_length = base_path.split('/').length
Dir.glob(File.join(base_path, '**/post.*')).each do |file|
paths << file.split('/')[path_length..-2].join('/')
end
@drafts = paths.map do |path|
Weblog::Post.new(self, path, :draft => true)
end
end; @drafts
end
|
#generate ⇒ Object
126
127
128
129
130
|
# File 'lib/weblog.rb', line 126
def generate
posts.each { |post| post.generate }
months.each { |month| month.generate }
index.generate
end
|
#git ⇒ Object
54
55
56
|
# File 'lib/weblog.rb', line 54
def git
@git ||= Git.open(@path)
end
|
#index ⇒ Object
122
123
124
|
# File 'lib/weblog.rb', line 122
def index
Weblog::Index.new(self)
end
|
62
63
64
|
# File 'lib/weblog.rb', line 62
def metadata_filename
File.join(path, 'config.json')
end
|
#months ⇒ Object
80
81
82
|
# File 'lib/weblog.rb', line 80
def months
Month.all(self)
end
|
#posts ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/weblog.rb', line 84
def posts
if @posts.nil?
paths = Set.new
base_path = File.join(@path, 'public')
path_length = base_path.split('/').length
Dir.glob(File.join(base_path, '**/post.*')).each do |file|
paths << file.split('/')[path_length..-2].join('/')
end
@posts = paths.map do |path|
Weblog::Post.new(self, path)
end
end; @posts
end
|
#preview ⇒ Object
155
156
157
|
# File 'lib/weblog.rb', line 155
def preview
drafts.each { |draft| draft.generate }
end
|
#print_info ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/weblog.rb', line 143
def print_info
months.each do |month|
puts "#{month.path}: #{month.size} #{month.size == 1 ? 'post' : 'posts'}"
end
puts [
"Directory: #{@path}",
"Title: #{title}",
"Author: #{author}",
"Total posts: #{size} #{size == 1 ? 'post' : 'posts'}"
].join("\n")
end
|
#publish(*directories) ⇒ Object
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/weblog.rb', line 132
def publish(*directories)
year = Time.now.year.to_s
month = "%02d" % Time.now.month
directories.each do |directory|
name = directory.split('/').last
destination = File.join(@path, 'public', year, month, name)
FileUtils.mkdir_p(File.dirname(destination))
FileUtils.mv(directory, destination)
end
end
|
#size ⇒ Object
112
113
114
|
# File 'lib/weblog.rb', line 112
def size
posts.size
end
|
#verbose? ⇒ Boolean
58
59
60
|
# File 'lib/weblog.rb', line 58
def verbose?
@options[:verbose]
end
|