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(options = {}) ⇒ Object
- #generate_archives ⇒ Object
- #generate_categories ⇒ Object
- #generate_category_feeds ⇒ Object
- #generate_feed(path, options) ⇒ Object
- #generate_files ⇒ 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, context: {}) ⇒ Object
- #prepare_page(page) ⇒ Object
- #prepare_post(post) ⇒ Object
- #prepare_site ⇒ Object
- #prepare_template(template) ⇒ 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 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) = @source_paths[:root] = File.(@config['source_path']) @output_paths[:site] = File.(@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
#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
202 203 204 205 206 207 208 |
# File 'lib/dimples/site.rb', line 202 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( = {}) .merge!() 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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dimples/site.rb', line 164 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_categories ⇒ Object
157 158 159 160 161 162 |
# File 'lib/dimples/site.rb', line 157 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_feeds ⇒ Object
196 197 198 199 200 |
# File 'lib/dimples/site.rb', line 196 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
182 183 184 185 186 187 188 189 190 |
# File 'lib/dimples/site.rb', line 182 def generate_feed(path, ) feed = @page_class.new(self) feed.filename = 'feed' feed.extension = 'atom' feed.layout = 'feed' feed.write(path, ) end |
#generate_files ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/dimples/site.rb', line 131 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
151 152 153 154 155 |
# File 'lib/dimples/site.rb', line 151 def generate_pages @pages.each do |page| page.write(@output_paths[:site]) end end |
#generate_posts ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/dimples/site.rb', line 141 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_feed ⇒ Object
192 193 194 |
# File 'lib/dimples/site.rb', line 192 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
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 243 244 245 246 247 |
# File 'lib/dimples/site.rb', line 210 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_page(page) ⇒ Object
125 126 |
# File 'lib/dimples/site.rb', line 125 def prepare_page(page) end |
#prepare_post(post) ⇒ Object
128 129 |
# File 'lib/dimples/site.rb', line 128 def prepare_post(post) end |
#prepare_site ⇒ Object
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 |
#prepare_template(template) ⇒ Object
122 123 |
# File 'lib/dimples/site.rb', line 122 def prepare_template(template) end |
#scan_files ⇒ Object
70 71 72 73 74 |
# File 'lib/dimples/site.rb', line 70 def scan_files scan_templates scan_pages scan_posts end |
#scan_pages ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/dimples/site.rb', line 84 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_posts ⇒ Object
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 118 119 120 |
# File 'lib/dimples/site.rb', line 92 def scan_posts Dir.glob(File.join(@source_paths[:posts], '*.*')).reverse.each do |path| post = @post_class.new(self, path) next if ![:include_drafts] && post.draft prepare_post(post) 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_templates ⇒ Object
76 77 78 79 80 81 82 |
# 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) prepare_template(template) @templates[template.slug] = template end end |