Skip to content

Instantly share code, notes, and snippets.

@NV
Created September 26, 2012 14:19
Show Gist options
  • Save NV/3788329 to your computer and use it in GitHub Desktop.
Save NV/3788329 to your computer and use it in GitHub Desktop.
Jekyll plugin for creating an alternative view (e.g. print view) for a post
require 'delegate'
module Jekyll
class PrintablePage < DelegateClass(Page)
def initialize(page)
@real_page = page
@dir = page.dir
#@base = page.base
# page.base is undefined, but it doesn’t seem to be used anyway
@name = page.name
@site = page.site
super(@real_page)
self.data = @real_page.data.clone
self.data['layout'] = @site.config['printable_layout'] || 'printable'
@printable_html = @site.config['printable_html'] || 'printable.html'
end
def destination(page)
page_path = super(page)
page_path.sub('index.html', @printable_html)
end
def write(dest)
# This is a copy/paste of the original Jekyll’s method.
# I don’t know how to avoid it, I’m just a Ruby newbie.
path = destination(dest)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |f|
f.write(self.output)
end
end
end
class Site
alias orig_write write
def write
orig_write
self.posts.each do |page|
printable_page = PrintablePage.new(page)
printable_page.render(self.layouts, site_payload)
printable_page.write(self.dest)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment