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
# 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

  @page_class = Dimples::Page
  @post_class = Dimples::Post

  @config = Dimples::Configuration.new(config)

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

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

  @output_paths[:posts] = File.join(@output_paths[:site], @config['paths']['posts'])
end

Instance Attribute Details

#archives ⇒ Object

Returns the value of attribute archives.



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

def archives
  @archives
end

#categories ⇒ Object

Returns the value of attribute categories.



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

def categories
  @categories
end

#config ⇒ Object

Returns the value of attribute config.



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

def config
  @config
end

#latest_post ⇒ Object

Returns the value of attribute latest_post.



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

def latest_post
  @latest_post
end

#output_paths ⇒ Object

Returns the value of attribute output_paths.



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

def output_paths
  @output_paths
end

#page_class ⇒ Object

Returns the value of attribute page_class.



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

def page_class
  @page_class
end

#pages ⇒ Object

Returns the value of attribute pages.



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

def pages
  @pages
end

#post_class ⇒ Object

Returns the value of attribute post_class.



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

def post_class
  @post_class
end

#posts ⇒ Object

Returns the value of attribute posts.



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

def posts
  @posts
end

#source_paths ⇒ Object

Returns the value of attribute source_paths.



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

def source_paths
  @source_paths
end

#templates ⇒ Object

Returns the value of attribute templates.



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

def templates
  @templates
end

Instance Method Details

#copy_assets ⇒ Object



192
193
194
195
196
197
198
# File 'lib/dimples/site.rb', line 192

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

#generate ⇒ Object



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

def generate
  prepare_site
  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}"
rescue => e
  puts "Error: #{e}"
end

#generate_archives ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dimples/site.rb', line 154

def generate_archives
  %w[year month day].each do |date_type|

    if @config['generation']["#{date_type}_archives"]

      template = @config['layouts'][date_type] || @config['layouts']['archives'] || @config['layouts']['posts']
      archives[date_type.to_sym].each_pair do |date_title, posts|

        paths = [@output_paths[:posts], posts[0].year]
        paths << posts[0].month if date_type =~ /month|day/
        paths << posts[0].day if date_type == 'day'

        paginate(posts: posts, title: date_title, paths: paths, layout: template)
      end
    end
  end
end

#generate_categories ⇒ Object



148
149
150
151
152
# File 'lib/dimples/site.rb', line 148

def generate_categories
  @categories.each_value do |category|
    paginate(posts: category.posts, title: category.name, paths: [@output_paths[:posts], category.slug], layout: @config['layouts']['category'])
  end
end

#generate_category_feeds ⇒ Object



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

def generate_category_feeds
  @categories.each_value do |category|
    generate_feed(File.join(@output_paths[:posts], category.slug), {posts: category.posts[0..@config['pagination']['per_page'] - 1], category: category.slug})
  end
end

#generate_feed(path, options) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/dimples/site.rb', line 172

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

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

  feed.write(path, options)
end

#generate_files ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/dimples/site.rb', line 122

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_pages ⇒ Object



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

def generate_pages
  @pages.each do |page|
    page.write(@output_paths[:site])
  end
end

#generate_posts ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/dimples/site.rb', line 132

def generate_posts
  @posts.each do |post|
    post.write(@output_paths[:posts])
  end

  if @config['generation']['paginated_posts']
    paginate(posts: @posts, paths: [@output_paths[:posts]], layout: @config['layouts']['posts'])
  end
end

#generate_posts_feed ⇒ Object



182
183
184
# File 'lib/dimples/site.rb', line 182

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

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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/dimples/site.rb', line 209

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

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

  for index in 1..page_count
    page = @page_class.new(self)

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

    pagination = {
      page: index,
      pages: page_count,
      post_count: posts.length,
      path: pagination_url(index, paths)
    }

    pagination[:previous_page] = pagination[:page] - 1 if (pagination[:page] - 1) > 0
    pagination[:next_page] = pagination[:page] + 1 if (pagination[:page] + 1) <= pagination[:pages]

    if pagination[:previous_page]
      pagination[:previous_page_url] = pagination[:path]
      pagination[:previous_page_url] += "page" + pagination[:previous_page].to_s if pagination[:previous_page] != 1
    end

    pagination[:next_page_url] = pagination[:path] + "page" + pagination[:next_page].to_s if pagination[:next_page]

    output_path = File.join(paths, index != 1 ? "page#{index}" : '')

    page.write(output_path, {posts: posts.slice((index - 1) * per_page, per_page), pagination: pagination})
  end
end

#pagination_url(page, paths) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/dimples/site.rb', line 200

def pagination_url(page, paths)
  path = '/'

  path += File.split(paths[0])[-1] + "/" if paths[0] != @output_paths[:site]
  path += paths[1..-1].join('/') + "/" if paths.length > 1

  path
end

#prepare_site ⇒ Object



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

def prepare_site
  begin
    FileUtils.remove_dir(@output_paths[:site]) if Dir.exist?(@output_paths[:site])
    Dir.mkdir(@output_paths[:site])
  rescue => e
    raise "Failed to prepare the site directory (#{e})"
  end
end

#scan_categories ⇒ Object



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

def scan_categories
  Dir.glob(File.join(@source_paths[:categories], '*.*')).each do |path|
    slug = File.basename(path, File.extname(path))
    category = Dimples::Category.new(slug, path)

    @categories[category.slug] = category
  end
end

#scan_files ⇒ Object



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

def scan_files
  scan_templates
  scan_categories
  scan_pages
  scan_posts
end

#scan_pages ⇒ Object



88
89
90
91
92
# File 'lib/dimples/site.rb', line 88

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

#scan_posts ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dimples/site.rb', line 94

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

    post.categories.each do |slug|
      @categories[slug] ||= Dimples::Category.new(slug)
      @categories[slug].posts << post
    end

    %w[year month day].each do |date_type|
      if @config['generation']["#{date_type}_archives"]

        date_key = post.date.strftime(@config['date_formats'][date_type])
        (@archives[date_type.to_sym][date_key] ||= []) << post
      end
    end

    @posts << post
  end

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

  @latest_post = @posts.first
end

#scan_templates ⇒ Object



72
73
74
75
76
77
# 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)
    @templates[template.slug] = template
  end
end