class Jekyll::PandocGenerator

Attributes

config[RW]
site[RW]

Public Instance Methods

generate(site) click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/generator.rb, line 31
def generate(site)
  @site     ||= site
  @config   ||= JekyllPandocMultipleFormats::Config.new(@site.config['pandoc'])

  return if @config.skip?

  # we create a single array of files
  @pandoc_files = []

  @config.outputs.each_pair do |output, extra_flags|
    @site.posts.docs.each do |post|

      pandoc_file = PandocFile.new(@site, output, post)
      next unless pandoc_file.write

      @site.keep_files << pandoc_file.relative_path
      @pandoc_files << pandoc_file
    end

    @site.post_attr_hash('categories').each_pair do |title, posts|
      posts.sort!
      pandoc_file = PandocFile.new(@site, output, posts, title)

      if @site.keep_files.include? pandoc_file.relative_path
        puts "#{pandoc_file.relative_path} is a category file AND a post file"
        puts 'change the category name to fix this'
        next
      end

      next unless pandoc_file.write

      @site.keep_files << pandoc_file.relative_path
      @pandoc_files << pandoc_file
    end
  end

  @pandoc_files.each do |pandoc_file|
    # If output is PDF, we also create the imposed PDF
    if pandoc_file.pdf?

      if @config.imposition?

        imposed_file = JekyllPandocMultipleFormats::Imposition
          .new(pandoc_file.path, pandoc_file.papersize,
          pandoc_file.sheetsize, pandoc_file.signature)

        imposed_file.write
        @site.keep_files << imposed_file.relative_path(@site.dest)
      end

      # If output is PDF, we also create the imposed PDF
      if @config.binder?

        binder_file = JekyllPandocMultipleFormats::Binder
          .new(pandoc_file.path, pandoc_file.papersize,
          pandoc_file.sheetsize)

        binder_file.write
        @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
    end
  end
end