Class: JekyllPandocMultipleFormats::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-pandoc-multiple-formats/printer.rb

Direct Known Subclasses

Binder, Imposition, Unite

Constant Summary collapse

TEMPLATE =
<<-EOT.gsub(/^\s+/, '')
\\documentclass[@@sheetsize@@,10pt]{article}

\\usepackage{pgfpages}
\\usepackage{pdfpages}

\\pgfpagesuselayout{@@nup@@ on 1}[@@sheetsize@@,@@extra_options@@]

\\begin{document}
  \\includepdf[pages={@@pages@@}]{@@document@@}
\\end{document}
EOT
SHEET_SIZES =

TODO allow custom sheet sizes

{
  a7paper:   2,
  a6paper:   4,
  a5paper:   8,
  a4paper:  16,
  a3paper:  32,
  a2paper:  64,
  a1paper: 128,
  a0paper: 256
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, papersize = nil, sheetsize = nil, extra_options = nil) ⇒ Printer

Returns a new instance of Printer.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 57

def initialize(file, papersize = nil, sheetsize = nil, extra_options = nil)
  return unless /\.pdf\Z/ =~ file
  return unless pdf = PDF::Info.new(file)

  @original_file = File.realpath(file)
  @papersize     = papersize || 'a5paper'
  @sheetsize     = sheetsize || 'a4paper'
  @pages         = pdf.[:page_count]
  @nup           = SHEET_SIZES[@sheetsize.to_sym] / SHEET_SIZES[@papersize.to_sym]
  @extra_options = extra_options || ''

  # These layouts require a landscape page
  @extra_options << 'landscape' if is_landscape?

  self
end

Instance Attribute Details

#extra_optionsObject

Returns the value of attribute extra_options.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def extra_options
  @extra_options
end

#nupObject

Returns the value of attribute nup.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def nup
  @nup
end

#original_fileObject

Returns the value of attribute original_file.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def original_file
  @original_file
end

#output_fileObject

Returns the value of attribute output_file.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def output_file
  @output_file
end

#pagesObject

Returns the value of attribute pages.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def pages
  @pages
end

#papersizeObject

Returns the value of attribute papersize.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def papersize
  @papersize
end

#relative_path(from) ⇒ Object

Returns the value of attribute relative_path.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def relative_path
  @relative_path
end

#sheetsizeObject

Returns the value of attribute sheetsize.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def sheetsize
  @sheetsize
end

#templateObject

Returns the value of attribute template.



54
55
56
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 54

def template
  @template
end

Instance Method Details

#is_landscape?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 88

def is_landscape?
  [2,8,32,128].include? @nup
end

#render_templateObject



96
97
98
99
100
101
102
103
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 96

def render_template
  @template = TEMPLATE
    .gsub('@@nup@@',           @nup.to_s)
    .gsub('@@sheetsize@@',     @sheetsize)
    .gsub('@@extra_options@@', @extra_options)
    .gsub('@@document@@',      @original_file)
    .gsub('@@pages@@',         to_nup * ',')
end

#round_to_nearest(int, near) ⇒ Object



92
93
94
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 92

def round_to_nearest(int, near)
  (int + (near - 1)) / near * near
end

#writeObject



78
79
80
81
82
83
84
85
86
# File 'lib/jekyll-pandoc-multiple-formats/printer.rb', line 78

def write
  # Create the imposed file
  pdflatex = RTeX::Document.new(template)
  pdflatex.to_pdf do |pdf_file|
    FileUtils.cp pdf_file, @output_file
  end

  File.exists? @output_file
end