Class: Dimples::Site
- Inherits:
-
Object
- Object
- Dimples::Site
- Defined in:
- lib/dimples/site.rb
Instance Attribute Summary collapse
-
#archives ⇒ Object
Returns the value of attribute archives.
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#config ⇒ Object
Returns the value of attribute config.
-
#latest_post ⇒ Object
Returns the value of attribute latest_post.
-
#output_paths ⇒ Object
Returns the value of attribute output_paths.
-
#page_class ⇒ Object
Returns the value of attribute page_class.
-
#pages ⇒ Object
Returns the value of attribute pages.
-
#post_class ⇒ Object
Returns the value of attribute post_class.
-
#posts ⇒ Object
Returns the value of attribute posts.
-
#source_paths ⇒ Object
Returns the value of attribute source_paths.
-
#templates ⇒ Object
Returns the value of attribute templates.
Instance Method Summary collapse
- #copy_assets ⇒ Object
- #generate ⇒ Object
- #generate_archives ⇒ Object
- #generate_categories ⇒ Object
- #generate_category_feeds ⇒ Object
- #generate_feed(path, options) ⇒ Object
- #generate_pages ⇒ Object
- #generate_posts ⇒ Object
- #generate_posts_feed ⇒ Object
-
#initialize(config = {}) ⇒ Site
constructor
A new instance of Site.
- #paginate(posts:, title: nil, paths:, layout: false) ⇒ Object
- #pagination_url(page, paths) ⇒ Object
- #prepare_site ⇒ Object
- #scan_files ⇒ Object
- #scan_pages ⇒ Object
- #scan_posts ⇒ Object
- #scan_templates ⇒ Object
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.(@config['source_path']) @output_paths[:site] = File.(@config['destination_path']) %w{pages posts templates public}.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
184 185 186 187 188 189 190 |
# File 'lib/dimples/site.rb', line 184 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 55 |
# File 'lib/dimples/site.rb', line 42 def generate prepare_site scan_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'] copy_assets end |
#generate_archives ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/dimples/site.rb', line 142 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
136 137 138 139 140 |
# File 'lib/dimples/site.rb', line 136 def generate_categories @categories.each_pair do |slug, posts| paginate(posts: posts, title: slug.capitalize, paths: [@output_paths[:posts], slug], layout: @config['layouts']['category']) end end |
#generate_category_feeds ⇒ Object
178 179 180 181 182 |
# File 'lib/dimples/site.rb', line 178 def generate_category_feeds @categories.each_pair do |slug, posts| generate_feed(File.join(@output_paths[:posts], slug), {posts: posts[0..@config['pagination']['per_page'] - 1], category: slug}) end end |
#generate_feed(path, options) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/dimples/site.rb', line 160 def generate_feed(path, ) feed = @page_class.new(self) feed.filename = 'feed' feed.extension = 'atom' feed.layout = 'feed' begin feed.write(path, ) rescue => e raise "Failed to generate feed #{path} (#{e})" end end |
#generate_pages ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/dimples/site.rb', line 126 def generate_pages @pages.each do |page| begin page.write(@output_paths[:site]) rescue => e raise "Failed to render page #{page.path.gsub(@source_paths[:root], '')} (#{e})" end end end |
#generate_posts ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dimples/site.rb', line 112 def generate_posts @posts.each do |post| begin post.write(@output_paths[:posts]) rescue => e raise "Failed to render post #{post.path} (#{e})" end end if @config['generation']['paginated_posts'] paginate(posts: @posts, paths: [@output_paths[:posts]], layout: @config['layouts']['posts']) end end |
#generate_posts_feed ⇒ Object
174 175 176 |
# File 'lib/dimples/site.rb', line 174 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
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 234 235 236 237 |
# File 'lib/dimples/site.rb', line 201 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 range = posts.slice((index - 1) * per_page, per_page) page = @page_class.new(self) page.layout = layout page.title = title || @templates[layout].title page_paths = paths.clone pagination = { page: index, pages: page_count, post_count: posts.length, path: pagination_url(index, page_paths) } page_paths << "page#{index}" if index != 1 pagination[:previous_page] = pagination[:page] - 1 if (pagination[:page] - 1) > 0 pagination[:next_page] = pagination[:page] + 1 if (pagination[:page] + 1) <= pagination[:pages] path = File.join(page_paths) begin page.write(path, {posts: range, pagination: pagination}) rescue => e raise "Failed to generate paginated page #{path} (#{e})" end end end |
#pagination_url(page, paths) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/dimples/site.rb', line 192 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
57 58 59 60 61 62 63 64 |
# File 'lib/dimples/site.rb', line 57 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_files ⇒ Object
66 67 68 69 70 |
# File 'lib/dimples/site.rb', line 66 def scan_files scan_templates scan_pages scan_posts end |
#scan_pages ⇒ Object
79 80 81 82 83 |
# File 'lib/dimples/site.rb', line 79 def scan_pages Dir.glob(File.join(@source_paths[:pages], '**', '*.*')).each do |path| @pages << @page_class.new(self, path) end end |
#scan_posts ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dimples/site.rb', line 85 def scan_posts Dir.glob(File.join(@source_paths[:posts], '*.*')).reverse.each do |path| post = @post_class.new(self, path) post.categories.each do |category| (@categories[category] ||= []) << 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 |