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

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

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

  @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

  @output_paths[:archives] = File.join(@output_paths[:site], @config['paths']['archives'])
  @output_paths[:posts] = File.join(@output_paths[:site], @config['paths']['posts'])
  @output_paths[:categories] = File.join(@output_paths[:site], @config['paths']['categories'])
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

#copy_assetsObject



188
189
190
191
192
193
194
# File 'lib/dimples/site.rb', line 188

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

#generate(options = {}) ⇒ Object



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

def generate(options = {})
  @generation_options.merge!(options)

  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_archivesObject



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

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[:archives], 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_categoriesObject



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

def generate_categories
  @categories.each do |slug, posts|
    category_name = @config['category_names'][slug] || slug.capitalize
    paginate(posts: posts, title: category_name, paths: [@output_paths[:categories], slug], layout: @config['layouts']['category'], context: {category: slug})
  end
end

#generate_category_feedsObject



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

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

#generate_feed(path, options) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/dimples/site.rb', line 168

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_filesObject



117
118
119
120
121
122
123
124
125
# File 'lib/dimples/site.rb', line 117

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_pagesObject



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

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

#generate_postsObject



127
128
129
130
131
132
133
134
135
# File 'lib/dimples/site.rb', line 127

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

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

#generate_posts_feedObject



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

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, context: {}) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
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
# File 'lib/dimples/site.rb', line 196

def paginate(posts:, title: nil, paths:, layout: false, context: {})
  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

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

  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_path
    }

    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}" : '')
    context.merge!({posts: posts.slice((index - 1) * per_page, per_page), pagination: pagination})

    page.write(output_path, context)
  end
end

#prepare_siteObject



61
62
63
64
65
66
67
68
# File 'lib/dimples/site.rb', line 61

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_filesObject



70
71
72
73
74
# File 'lib/dimples/site.rb', line 70

def scan_files
  scan_templates
  scan_pages
  scan_posts
end

#scan_pagesObject



83
84
85
86
87
# File 'lib/dimples/site.rb', line 83

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

#scan_postsObject



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
# File 'lib/dimples/site.rb', line 89

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

    post.categories.each do |slug|
      (@categories[slug] ||= []) << 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_templatesObject



76
77
78
79
80
81
# File 'lib/dimples/site.rb', line 76

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