diff --git a/lib/jekyll-archives.rb b/lib/jekyll-archives.rb index 94e59a9..0176594 100644 --- a/lib/jekyll-archives.rb +++ b/lib/jekyll-archives.rb @@ -6,14 +6,6 @@ module Archives autoload :Archive, 'jekyll-archives/archive' autoload :VERSION, 'jekyll-archives/version' - if (Jekyll.const_defined? :Hooks) - Jekyll::Hooks.register :site, :after_reset do |site| - # We need to disable incremental regen for Archives to generate with the - # correct content - site.regenerator.instance_variable_set(:@disabled, true) - end - end - class Archives < Jekyll::Generator safe true @@ -92,6 +84,22 @@ def enabled?(archive) end end + # Renders the archives into the layouts + def render + payload = @site.site_payload + @archives.each do |archive| + archive.render(@site.layouts, payload) + end + end + + # Write archives to their destination + def write + @archives.each do |archive| + archive.write(@site.dest) if archive.regenerate? + archive.add_dependencies + end + end + def tags @site.post_attr_hash('tags') end diff --git a/lib/jekyll-archives/archive.rb b/lib/jekyll-archives/archive.rb index c87ea89..74769e1 100644 --- a/lib/jekyll-archives/archive.rb +++ b/lib/jekyll-archives/archive.rb @@ -13,6 +13,7 @@ class Archive < Jekyll::Page name path url + permalink ).freeze # Initialize a new Archive page @@ -30,8 +31,8 @@ def initialize(site, title, type, posts) @config = site.config['jekyll-archives'] # Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb) - if title.to_s.length - @slug = Utils.slugify(title.to_s) + if title.is_a? String + @slug = Utils.slugify(title) end # Use ".html" for file extension and url for path @@ -86,6 +87,46 @@ def url raise ArgumentError.new "Template \"#{template}\" provided is invalid." end + def permalink + data && data.is_a?(Hash) && data['permalink'] + end + + # Add any necessary layouts to this post + # + # layouts - The Hash of {"name" => "layout"}. + # site_payload - The site payload Hash. + # + # Returns nothing. + def render(layouts, site_payload) + payload = Utils.deep_merge_hashes({ + "page" => to_liquid + }, site_payload) + + do_layout(payload, layouts) + end + + # Add dependencies for incremental mode + def add_dependencies + if defined? site.regenerator + archive_path = site.in_dest_dir(relative_path) + site.regenerator.add(archive_path) + @posts.each do |post| + site.regenerator.add_dependency(archive_path, post.path) + end + end + end + + # Convert this Convertible's data to a Hash suitable for use by Liquid. + # + # Returns the Hash representation of this Convertible. + def to_liquid(attrs = nil) + further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute| + [attribute, send(attribute)] + }] + + Utils.deep_merge_hashes(data, further_data) + end + # Produce a title object suitable for Liquid based on type of archive. # # Returns a String (for tag and category archives) and nil for @@ -115,6 +156,14 @@ def relative_path path end + def regenerate? + if defined? site.regenerator + site.regenerator.regenerate?(self) + else + true + end + end + # Returns the object as a debug String. def inspect "#"