-
-
Save brianjking/2377c9f87bcf65072364cbc034eb397c to your computer and use it in GitHub Desktop.
PDF Output for Middleman
This file contains hidden or 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
# Only the relevant parts of config.rb are included here | |
require "extensions/pdf" | |
# ... | |
configure :build do | |
activate :pdf do |pdf| | |
pdf.print_template = "/catalogue/print-template.html" | |
end | |
end |
This file contains hidden or 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
class PDF < Middleman::Extension | |
option :print_template | |
def initialize(app, options_hash={}, &block) | |
super | |
app.after_build do |builder| | |
input_path = "extensions/filelist.txt" | |
output_path = "pdf/terracottas.pdf" | |
puts `prince --input-list=#{input_path} -o #{output_path}` | |
end | |
end | |
def after_configuration | |
pdf_proxy(app.data.catalogue) | |
end | |
def manipulate_resource_list(resources) | |
pagelist = generate_pagelist | |
baseurl = "build/" | |
frontmatter, entries, backmatter = sort_contents(resources) | |
frontmatter.each { |p| pagelist.puts baseurl + p.destination_path } | |
entries.each { |p| pagelist.puts baseurl + p.destination_path } | |
backmatter.each { |p| pagelist.puts baseurl + p.destination_path } | |
pagelist.close | |
# return value of this method becomes the new sitemap | |
resources | |
end | |
private | |
def generate_pagelist | |
f = File.new("./extensions/filelist.txt", "w") | |
end | |
def pdf_proxy(collection) | |
collection.each do |cat, entry| | |
app.proxy "/print-catalogue/#{cat}.html", options.print_template, | |
:locals => { :entry => entry }, :ignore => true | |
end | |
end | |
def sort_contents(resources) | |
pages = resources.find_all { |p| p.data.sort_order } | |
frontmatter = pages.find_all { |p| p.data.sort_order < 100 } | |
backmatter = pages.find_all { |p| p.data.sort_order >= 100 } | |
entries = resources.find_all { |p| p.data.catalogue == true } | |
entries.select! { |p| p.destination_path.include? "print-catalogue"} | |
frontmatter.sort_by! { |p| p.data.sort_order } | |
backmatter.sort_by! { |p| p.data.sort_order } | |
entries.sort_by! { |p| p.metadata[:locals][:entry][:info][:cat] } | |
return frontmatter, entries, backmatter | |
end | |
end | |
::Middleman::Extensions.register(:pdf, PDF) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment