diff --git a/lib/jekyll-pandoc-multiple-formats/generator.rb b/lib/jekyll-pandoc-multiple-formats/generator.rb index 86a44c6e79fc5659c78b3ddef44210af51cff70d..33b70536181e37673e2eb75ce9bfc1e52fdc9e02 100644 --- a/lib/jekyll-pandoc-multiple-formats/generator.rb +++ b/lib/jekyll-pandoc-multiple-formats/generator.rb @@ -55,6 +55,27 @@ class PandocGenerator < Generator @pandoc_files << pandoc_file end + # Add covers to PDFs after building ready for print files + def add_cover(pandoc_file) + return unless pandoc_file.has_cover? + # Generate the cover + return unless pandoc_file.pdf_cover! + + united_output = pandoc_file.path.gsub(/\.pdf\Z/, '-cover.pdf') + united_file = JekyllPandocMultipleFormats::Unite + .new(united_output, + [pandoc_file.pdf_cover,pandoc_file.path,pandoc_file.pdf_contra].compact, + ['-', pandoc_file.posts.first.data['pages'] || '2-', '-']) + + Jekyll.logger.info pandoc_file.pdf_contra + + return unless united_file.write + + # Replace the original file with the one with cover + FileUtils.rm_f(pandoc_file.path) + FileUtils.mv(united_output, pandoc_file.path) + end + def general_full_for_output(output) title = @site.config.dig('title') Jekyll.logger.info 'Pandoc:', "Generating full file #{title}" @@ -85,10 +106,18 @@ class PandocGenerator < Generator @config.outputs.each_pair do |output, _| Jekyll.logger.info 'Pandoc:', "Generating #{output}" - @site.posts.docs.each do |post| - Jekyll::Hooks.trigger :posts, :pre_render, post, { format: output } - generate_post_for_output(post, output) if @config.generate_posts? - Jekyll::Hooks.trigger :posts, :post_render, post, { format: output } + + # We only want the collections that are rendered, including posts + collections = @site.config['collections'].select do |c,v| + v['output'] == true + end.keys + + collections.each do |collection| + @site.collections[collection].docs.each do |post| + Jekyll::Hooks.trigger :posts, :pre_render, post, { format: output } + generate_post_for_output(post, output) if @config.generate_posts? + Jekyll::Hooks.trigger :posts, :post_render, post, { format: output } + end end if @config.generate_categories? @@ -104,6 +133,11 @@ class PandocGenerator < Generator # If output is PDF, we also create the imposed PDF next unless pandoc_file.pdf? + cover = @site.config['collections'][pandoc_file.collection] + .fetch('cover', 'after') + + add_cover(pandoc_file) if cover == 'before' + if @config.imposition? imposed_file = JekyllPandocMultipleFormats::Imposition @@ -125,20 +159,7 @@ class PandocGenerator < Generator @site.keep_files << binder_file.relative_path(@site.dest) end - # Add covers to PDFs after building ready for print files - if pandoc_file.has_cover? - # Generate the cover - next unless pandoc_file.pdf_cover! - united_output = pandoc_file.path.gsub(/\.pdf\Z/, '-cover.pdf') - united_file = JekyllPandocMultipleFormats::Unite - .new(united_output, [pandoc_file.pdf_cover,pandoc_file.path]) - - if united_file.write - # Replace the original file with the one with cover - FileUtils.rm_f(pandoc_file.path) - FileUtils.mv(united_output, pandoc_file.path) - end - end + add_cover(pandoc_file) if cover == 'after' end end end diff --git a/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb b/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb index 956a649d07eae8e67d4b1a6663982c5ae42b8ede..bc1f8d274365e3b76ed8a5997f78aab099d4ea55 100644 --- a/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb +++ b/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb @@ -100,7 +100,12 @@ module Jekyll def url_placeholders { output_ext: @format, slug: @slug, - title: @title } + title: @title, + collection: collection } + end + + def collection + posts.first.collection.label end # adds post metadata as yaml metadata @@ -247,6 +252,12 @@ module Jekyll cover.gsub(/\.[^\.]+\Z/, '.pdf') end + def pdf_contra + if single_post? && single_post.data['contra'] + single_post.data['contra'].gsub(/\.[^\.]+\Z/, '.pdf') + end + end + def pdf_cover! if has_cover? && !File.exists?(pdf_cover) Open3::popen3("convert \"#{cover}\" \"#{pdf_cover}\"") do |stdin, stdout, stderr, thread| diff --git a/lib/jekyll-pandoc-multiple-formats/unite.rb b/lib/jekyll-pandoc-multiple-formats/unite.rb index 04b08dd039bb5fbaa00796d7121efb6a9bc12ba6..557c8e385902e554fd367bd2809f668010bc7f3c 100644 --- a/lib/jekyll-pandoc-multiple-formats/unite.rb +++ b/lib/jekyll-pandoc-multiple-formats/unite.rb @@ -32,14 +32,15 @@ module JekyllPandocMultipleFormats \\end{document} EOT - INCLUDE_TEMPLATE = '\\includepdf[fitpaper=true,pages=-]{@@document@@}' + INCLUDE_TEMPLATE = '\\includepdf[fitpaper=true,pages=@@pages@@]{@@document@@}' attr_accessor :files, :template - def initialize(output_file, files) + def initialize(output_file, files, pages) raise ArgumentError.new 'An array of filenames is required' unless files.is_a? Array @output_file = output_file + @pages = pages self.files = files render_template @@ -60,8 +61,8 @@ module JekyllPandocMultipleFormats end def render_template - includes = @files.map do |f| - INCLUDE_TEMPLATE.gsub(/@@document@@/, f) + includes = @files.map.with_index do |f, i| + INCLUDE_TEMPLATE.gsub(/@@document@@/, f).gsub(/@@pages@@/, @pages[i] || '-') end @template = TEMPLATE.gsub('@@include@@', includes.join("\n"))