public
Last active

Jekyll ArchivePage class

  • Download Gist
archivepage.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
module Jekyll
class ArchivePage
include Convertible
 
attr_accessor :site, :pager, :name, :ext, :basename, :dir, :data, :content, :output
 
# Initialize new ArchivePage
# +site+ is the Site
# +month+ is the month
# +posts+ is the list of posts for the month
#
# Returns <ArchivePage>
def initialize(site, month, posts)
@site = site
@month = month
self.ext = '.html'
self.basename = 'index'
self.content = <<-EOS
<ul>
{% for post in page.posts %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
EOS
self.data = {
'layout' => 'default',
'type' => 'archive',
'title' => "Archive for #{month}",
'posts' => posts
}
end
 
# Add any necessary layouts
# +layouts+ is a Hash of {"name" => "layout"}
# +site_payload+ is the site payload hash
#
# Returns nothing
def render(layouts, site_payload)
payload = {
"page" => self.to_liquid,
"paginator" => pager.to_liquid
}.deep_merge(site_payload)
do_layout(payload, layouts)
end
 
def url
File.join("/", @month, "index.html")
end
def to_liquid
self.data.deep_merge({
"url" => self.url,
"content" => self.content
})
end
 
# Write the generated page file to the destination directory.
# +dest_prefix+ is the String path to the destination dir
# +dest_suffix+ is a suffix path to the destination dir
#
# Returns nothing
def write(dest_prefix, dest_suffix = nil)
dest = dest_prefix
dest = File.join(dest, dest_suffix) if dest_suffix
FileUtils.mkdir_p(dest)
# The url needs to be unescaped in order to preserve the
# correct filename
path = File.join(dest, CGI.unescape(self.url))
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |f|
f.write(self.output)
end
end
 
def html?
true
end
end
end

Having ArchivePage inherit Page seems to fix errors in the latest version of Jekyll. See my fork: https://gist.github.com/nlindley/6409459

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.