Why I made this: imathis/octopress#772
Created
September 26, 2012 14:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
I confirm this code still works with Jekyll 4.3.x, with some adaptations.
My current code that also uses a separate directory so that it can be easily excluded from Jekyll Watcher: