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) ⇒ Object
- #pagination_url(page, paths) ⇒ Object
- #prepare_categories ⇒ 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 41 42 |
# 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[: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
189 190 191 192 193 194 195 |
# File 'lib/dimples/site.rb', line 189 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dimples/site.rb', line 44 def generate( = {}) .merge!() prepare_site prepare_categories 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/dimples/site.rb', line 151 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
145 146 147 148 149 |
# File 'lib/dimples/site.rb', line 145 def generate_categories @categories.each_value do |category| paginate(posts: category.posts, title: category.name, paths: [@output_paths[:categories], category.slug], layout: @config['layouts']['category']) end end |
#generate_category_feeds ⇒ Object
183 184 185 186 187 |
# File 'lib/dimples/site.rb', line 183 def generate_category_feeds @categories.each_value do |category| generate_feed(File.join(@output_paths[:categories], category.slug), {posts: category.posts[0..@config['pagination']['per_page'] - 1], category: category.slug}) end end |
#generate_feed(path, options) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/dimples/site.rb', line 169 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
119 120 121 122 123 124 125 126 127 |
# File 'lib/dimples/site.rb', line 119 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
139 140 141 142 143 |
# File 'lib/dimples/site.rb', line 139 def generate_pages @pages.each do |page| page.write(@output_paths[:site]) end end |
#generate_posts ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/dimples/site.rb', line 129 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
179 180 181 |
# File 'lib/dimples/site.rb', line 179 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
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 238 239 |
# File 'lib/dimples/site.rb', line 206 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
197 198 199 200 201 202 203 204 |
# File 'lib/dimples/site.rb', line 197 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_categories ⇒ Object
70 71 72 73 74 |
# File 'lib/dimples/site.rb', line 70 def prepare_categories @config["categories"].each do |category| @categories[category["slug"]] = Dimples::Category.new(category["slug"], category["name"]) end 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 |
#scan_files ⇒ Object
76 77 78 79 80 |
# File 'lib/dimples/site.rb', line 76 def scan_files scan_templates scan_pages scan_posts end |
#scan_pages ⇒ Object
89 90 91 92 93 |
# File 'lib/dimples/site.rb', line 89 def scan_pages Dir.glob(File.join(@source_paths[:pages], '**', '*.*')).each do |path| @pages << @page_class.new(self, path) end end |
#scan_posts ⇒ Object
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 95 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 %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
82 83 84 85 86 87 |
# File 'lib/dimples/site.rb', line 82 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 |