class Jekyll::PandocFile

Attributes

config[R]
flags[R]
format[R]
papersize[R]
posts[R]
sheetsize[R]
signature[R]
site[R]
slug[R]
title[R]
url[R]

Public Class Methods

new(site, format, posts, title = nil) click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 32
def initialize(site, format, posts, title = nil)
  @site   = site
  @config = JekyllPandocMultipleFormats::Config.new(@site.config['pandoc']).config
  @format = format
  @flags  = []

  if posts.is_a? Array
    @posts = posts

    raise ArgumentError.new "'title' argument is required for multipost file" unless title

    @title = title
  else
    @posts = [posts]
    @title = posts.data['title'] unless title
  end

  @slug  = Utils.slugify(title)
end

Public Instance Methods

binary?() click to toggle source

These formats are binary files and must use the -o flag

# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 234
def binary?
  %w[pdf epub epub3 odt docx].include? @format
end
command() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 221
def command
  'pandoc ' << flags
end
content() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 132
def content
  if single_post?
    single_post.content
  else
    header_re = /^(#+.*\n*|.*\n[=-]+\n*)\Z/
    bib_title = ""
    @posts.map do |post|
      bib_title = post.content.match(header_re).to_s if bib_title.empty?
      # remove bibliography titles
      # since pandoc does it's own bibliography output, it recommends
      # leaving an empty chapter title to mark it as such
      post.content.gsub(header_re, '')
    # we add the first bibliography title we can find in the end
    end.join("\n\n\n") << bib_title
  end
end
cover() click to toggle source

Returns a cover, without checking if it exists

It assumes covers are in PNG format

# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 173
def cover
  if single_post? && single_post.data['cover']
    File.join(@site.config['source'], single_post.data['cover'])
  else
    File.join(@site.config['source'], @config['covers_dir'], "#{@slug}.png")
  end
end
epub?() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 229
def epub?
  %w[epub epub3].include? @format
end
has_cover?() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 242
def has_cover?
  File.exists? cover
end
path() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 52
def path
  # path is full destination path with all elements of permalink
  path = @site.in_dest_dir(relative_path)
end
pdf?() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 225
def pdf?
  @format == 'pdf'
end
pdf_cover() click to toggle source

Returns a PDF cover

# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 182
def pdf_cover
  cover.gsub(/\.[^\.]+\Z/, '.pdf')
end
pdf_cover!() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 186
def pdf_cover!
  if has_cover? && !File.exists?(pdf_cover)
    Open3::popen3("convert \"#{cover}\" \"#{pdf_cover}\"") do |stdin, stdout, stderr, thread|
      STDERR.print stderr.read

      # Wait for the process to finish
      thread.value
    end
  end

  File.exists?(pdf_cover)
end
relative_path() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 57
def relative_path
  path = URL.unescape_path(url)
  path.gsub! /^\//, ''

  # but if the post is going to be index.html, use slug + format
  # (ie /year/month/slug/slug.pdf)
  if url.end_with? '/'
    path = File.join(path, @slug)
    path << '.'
    path << @format
  end

  path
end
single_post?() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 238
def single_post?
  @posts.count == 1
end
url_placeholders() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 85
def url_placeholders
  { output_ext: @format,
    slug: @slug,
    title: @title }
end
write() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 149
def write
  FileUtils.mkdir_p(File.dirname(path))
  # Remove the file before creating it
  FileUtils.rm_f(path)
  # Move to the source dir since everything will be relative to that
  Dir::chdir(@site.config['source']) do
    # Do the stuff
    Open3::popen3(command) do |stdin, stdout, stderr, thread|
      stdin.puts yaml_metadata
      stdin.puts content
      stdin.close
      STDERR.print stderr.read

      # Wait for the process to finish
      thread.value
    end
  end

  File.exists?(path)
end
yaml_metadata() click to toggle source

adds post metadata as yaml metadata

# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 92
def yaml_metadata
  if single_post?

    # if we were to merge config to data, the default options would
    # take precedence
    data = Jekyll::Utils.deep_merge_hashes @config, single_post.data
    single_post.merge_data! data


    # we extract the excerpt because it serializes as an object and
    # breaks pandoc
    metadata = single_post.data.reject{ |k| k == 'excerpt' }

    if @config['date_format']
      metadata['date'] = metadata['date'].strftime(@config['date_format'])
    else
      metadata.delete('date')
    end
  else
    # we have to use this fugly syntax because jekyll doesn't do
    # symbols
    metadata = {
      'date'      => @config['date_format'] ? Date.today.strftime(@config['date_format']) : nil,
      'title'     => @title,
      'author'    => nil,
      'papersize' => papersize,
      'sheetsize' => sheetsize,
      'signature' => signature
    }
  end

  # fix page sizes, pandoc uses 'A4' while printer.rb uses
  # 'a4paper'
  %w[papersize sheetsize].each do |size|
    metadata[size] = fix_size metadata[size]
  end

  metadata.to_yaml << "\n---\n"
end

Private Instance Methods

find_option(name) click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 264
def find_option(name)
  if @posts.any? { |p| p.data.key? name }
    @posts.select { |p| p.data.key? name }.first.data[name]
  else
    @config[name]
  end
end
fix_size(size) click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 272
def fix_size(size)
  size.gsub /paper$/, ''
end
single_post() click to toggle source
# File lib/jekyll-pandoc-multiple-formats-jekyll34/pandoc_file.rb, line 260
def single_post
  @posts.first
end