Class: Dimples::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/dimples/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dimples/site.rb', line 15

def initialize(config)
  @source_paths = {}
  @output_paths = {}
  @templates = {}
  @categories = {}
  @archives = { year: {}, month: {}, day: {} }

  @pages = []
  @posts = []

  @latest_post = false

  @config = config

  @page_class = @config.class_override(:page) || Dimples::Page
  @post_class = @config.class_override(:post) || Dimples::Post

  @source_paths[:root] = File.expand_path(@config['source_path'])
  @output_paths[:site] = File.expand_path(@config['destination_path'])

  %w(pages posts public templates).each do |path|
    @source_paths[path.to_sym] = File.join(@source_paths[:root], path)
  end

  %w(archives posts categories).each do |path|
    output_path = File.join(@output_paths[:site], @config['paths'][path])
    @output_paths[path.to_sym] = output_path
  end
end

Instance Attribute Details

#archivesObject

Returns the value of attribute archives.



8
9
10
# File 'lib/dimples/site.rb', line 8

def archives
  @archives
end

#categoriesObject

Returns the value of attribute categories.



7
8
9
# File 'lib/dimples/site.rb', line 7

def categories
  @categories
end

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/dimples/site.rb', line 5

def config
  @config
end

#latest_postObject

Returns the value of attribute latest_post.



11
12
13
# File 'lib/dimples/site.rb', line 11

def latest_post
  @latest_post
end

#output_pathsObject

Returns the value of attribute output_paths.



4
5
6
# File 'lib/dimples/site.rb', line 4

def output_paths
  @output_paths
end

#page_classObject

Returns the value of attribute page_class.



12
13
14
# File 'lib/dimples/site.rb', line 12

def page_class
  @page_class
end

#pagesObject

Returns the value of attribute pages.



9
10
11
# File 'lib/dimples/site.rb', line 9

def pages
  @pages
end

#post_classObject

Returns the value of attribute post_class.



13
14
15
# File 'lib/dimples/site.rb', line 13

def post_class
  @post_class
end

#postsObject

Returns the value of attribute posts.



10
11
12
# File 'lib/dimples/site.rb', line 10

def posts
  @posts
end

#source_pathsObject

Returns the value of attribute source_paths.



3
4
5
# File 'lib/dimples/site.rb', line 3

def source_paths
  @source_paths
end

#templatesObject

Returns the value of attribute templates.



6
7
8
# File 'lib/dimples/site.rb', line 6

def templates
  @templates
end

Instance Method Details

#archive_day(year, month, day) ⇒ Object



127
128
129
# File 'lib/dimples/site.rb', line 127

def archive_day(year, month, day)
  @archives[:day]["#{year}/#{month}/#{day}"] ||= []
end

#archive_month(year, month) ⇒ Object



123
124
125
# File 'lib/dimples/site.rb', line 123

def archive_month(year, month)
  @archives[:month]["#{year}/#{month}"] ||= []
end

#archive_year(year) ⇒ Object



119
120
121
# File 'lib/dimples/site.rb', line 119

def archive_year(year)
  @archives[:year][year] ||= []
end

#build_pagination(index, page_count, item_count, path) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/dimples/site.rb', line 275

def build_pagination(index, page_count, item_count, path)
  pagination = {
    page: index,
    pages: page_count,
    post_count: item_count,
    path: path
  }

  if (pagination[:page] - 1) > 0
    pagination[:previous_page] = pagination[:page] - 1
  end

  if (pagination[:page] + 1) <= pagination[:pages]
    pagination[:next_page] = pagination[:page] + 1
  end

  if pagination[:previous_page]
    pagination[:previous_page_url] = pagination[:path]

    if pagination[:previous_page] != 1
      page_string = "page#{pagination[:previous_page]}"
      pagination[:previous_page_url] += page_string
    end
  end

  if pagination[:next_page]
    page_string = "#{pagination[:path]}page#{pagination[:next_page]}"
    pagination[:next_page_url] = page_string
  end

  pagination
end

#copy_assetsObject



241
242
243
244
245
246
247
248
# File 'lib/dimples/site.rb', line 241

def copy_assets
  if Dir.exist?(@source_paths[:public])
    path = File.join(@source_paths[:public], '.')
    FileUtils.cp_r(path, @output_paths[:site])
  end
rescue => e
  raise "Failed to copy site assets (#{e})"
end

#generateObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/dimples/site.rb', line 45

def generate
  prepare_output_directory
  scan_files
  generate_files
  copy_assets
rescue Errors::RenderingError => e
  puts "Error: Failed to render #{e.file}: #{e.message}"
rescue Errors::PublishingError => e
  puts "Error: Failed to publish #{e.file}: #{e.message}"
end

#generate_archivesObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/dimples/site.rb', line 192

def generate_archives
  %w(year month day).each do |date_type|
    if @config['generation']["#{date_type}_archives"]
      layout = @config['layouts']["#{date_type}_archives"]

      @archives[date_type.to_sym].each_value do |posts|
        title = posts[0].date.strftime(@config['date_formats'][date_type])
        paths = [@output_paths[:archives], posts[0].year]
        dates = { year: posts[0].year }

        case date_type
        when 'month'
          paths << posts[0].month
          dates[:month] = posts[0].month
        when 'day'
          paths.concat([posts[0].month, posts[0].day])
          dates.merge(month: posts[0].month, day: posts[0].day)
        end

        paginate(posts: posts, title: title, paths: paths, layout: layout, context: dates)
      end
    end
  end
end

#generate_categoriesObject



177
178
179
180
181
# File 'lib/dimples/site.rb', line 177

def generate_categories
  @categories.each do |slug, posts|
    generate_category(slug, posts)
  end
end

#generate_category(slug, posts) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/dimples/site.rb', line 183

def generate_category(slug, posts)
  name = @config['category_names'][slug] || slug.capitalize
  paths = [@output_paths[:categories], slug]
  layout = @config['layouts']['category']
  context = { category: slug }

  paginate(posts: posts, title: name, paths: paths, layout: layout, context: context)
end

#generate_category_feedsObject



232
233
234
235
236
237
238
239
# File 'lib/dimples/site.rb', line 232

def generate_category_feeds
  @categories.each do |slug, posts|
    path = File.join(@output_paths[:categories], slug)
    posts = posts[0..@config['pagination']['per_page'] - 1]

    generate_feed(path, posts: posts, category: slug)
  end
end

#generate_feed(path, options) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/dimples/site.rb', line 217

def generate_feed(path, options)
  feed = @page_class.new(self)

  feed.filename = 'feed'
  feed.extension = 'atom'
  feed.layout = 'feed'

  feed.write(feed.output_path(path), options)
end

#generate_filesObject



140
141
142
143
144
145
146
147
148
# File 'lib/dimples/site.rb', line 140

def generate_files
  generate_posts
  generate_pages
  generate_archives

  generate_categories if @config['generation']['categories']
  generate_posts_feed if @config['generation']['feed']
  generate_category_feeds if @config['generation']['category_feeds']
end

#generate_page(page) ⇒ Object



173
174
175
# File 'lib/dimples/site.rb', line 173

def generate_page(page)
  page.write(page.output_path(@output_paths[:site]))
end

#generate_pagesObject



167
168
169
170
171
# File 'lib/dimples/site.rb', line 167

def generate_pages
  @pages.each do |page|
    generate_page(page)
  end
end

#generate_post(post) ⇒ Object



163
164
165
# File 'lib/dimples/site.rb', line 163

def generate_post(post)
  post.write(post.output_path(@output_paths[:posts]))
end

#generate_postsObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dimples/site.rb', line 150

def generate_posts
  @posts.each do |post|
    generate_post(post)
  end

  if @config['generation']['paginated_posts']
    paths = [@output_paths[:archives]]
    layout = @config['layouts']['posts']

    paginate(posts: @posts, paths: paths, layout: layout)
  end
end

#generate_posts_feedObject



227
228
229
230
# File 'lib/dimples/site.rb', line 227

def generate_posts_feed
  posts = @posts[0..@config['pagination']['per_page'] - 1]
  generate_feed(@output_paths[:site], posts: posts)
end

#paginate(posts:, title: nil, paths:, layout: false, context: {}) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dimples/site.rb', line 250

def paginate(posts:, title: nil, paths:, layout: false, context: {})
  raise "'#{layout}' template not found" unless @templates.key?(layout)

  per_page = @config['pagination']['per_page']
  page_count = (posts.length.to_f / per_page.to_i).ceil

  page_path = paths[0].gsub(@output_paths[:site], '') + '/'
  page_path += paths[1..-1].join('/') + '/' if paths.length > 1

  (1..page_count).each do |index|
    page = @page_class.new(self)

    page.layout = layout
    page.title = title || @templates[layout].title

    pagination = build_pagination(index, page_count, posts.count, page_path)
    output_path = File.join(paths, index != 1 ? "page#{index}" : '')

    context[:posts] = posts.slice((index - 1) * per_page, per_page)
    context[:pagination] = pagination

    page.write(page.output_path(output_path), context)
  end
end

#prepare_output_directoryObject



56
57
58
59
60
61
62
63
64
# File 'lib/dimples/site.rb', line 56

def prepare_output_directory
  if Dir.exist?(@output_paths[:site])
    FileUtils.remove_dir(@output_paths[:site])
  end

  Dir.mkdir(@output_paths[:site])
rescue => e
  raise "Failed to prepare the site directory (#{e})"
end

#prepare_page(page) ⇒ Object



134
135
# File 'lib/dimples/site.rb', line 134

def prepare_page(page)
end

#prepare_post(post) ⇒ Object



137
138
# File 'lib/dimples/site.rb', line 137

def prepare_post(post)
end

#prepare_template(template) ⇒ Object



131
132
# File 'lib/dimples/site.rb', line 131

def prepare_template(template)
end

#scan_filesObject



66
67
68
69
70
# File 'lib/dimples/site.rb', line 66

def scan_files
  scan_templates
  scan_pages
  scan_posts
end

#scan_pagesObject



80
81
82
83
84
85
86
# File 'lib/dimples/site.rb', line 80

def scan_pages
  Dir.glob(File.join(@source_paths[:pages], '**', '*.*')).each do |path|
    page = @page_class.new(self, path)
    prepare_page(page)
    @pages << page
  end
end

#scan_postsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dimples/site.rb', line 88

def scan_posts
  Dir.glob(File.join(@source_paths[:posts], '*.*')).reverse_each do |path|
    post = @post_class.new(self, path)
    prepare_post(post)

    next if post.draft

    post.categories.each do |slug|
      (@categories[slug] ||= []) << post
    end

    archive_year(post.year) << post
    archive_month(post.year, post.month) << post
    archive_day(post.year, post.month, post.day) << post

    @posts << post
  end

  @posts.each_index do |index|
    if index - 1 >= 0
      @posts[index].next_post = @posts.fetch(index - 1, nil)
    end

    if index + 1 < @posts.count
      @posts[index].previous_post = @posts.fetch(index + 1, nil)
    end
  end

  @latest_post = @posts.first
end

#scan_templatesObject



72
73
74
75
76
77
78
# File 'lib/dimples/site.rb', line 72

def scan_templates
  Dir.glob(File.join(@source_paths[:templates], '**', '*.*')).each do |path|
    template = Dimples::Template.new(self, path)
    prepare_template(template)
    @templates[template.slug] = template
  end
end