Class: Nanoc::Latexmk::Filters::LatexmkFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/nanoc/latexmk/filters/latexmk.rb

Constant Summary collapse

TMPFILE_NAME =
'nanoc-latexmk.tex'
ENGINES =
{
  pdflatex: '-pdf',
  xelatex:  '-xelatex'
}.freeze
LATEX_PARAMS =
%w[-interaction=nonstopmode -halt-on-error -file-line-error].freeze
DEFAULT_PARAMS =
{
  engine: :pdflatex,
  command_params: LATEX_PARAMS,
  shell_escape: false
}.freeze

Instance Method Summary collapse

Instance Method Details

#run(content, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nanoc/latexmk/filters/latexmk.rb', line 27

def run(content, params = {})
  params = DEFAULT_PARAMS.merge(params)

  raise 'Unknown Engine' unless ENGINES.key? params[:engine].to_sym

  latex_params = []
  latex_params += params[:command_params]

  latex_params << if params[:shell_escape]
                     '-shell-escape'
                   else
                     '-no-shell-escape'
                   end

  Dir.mktmpdir do |dir|
    File.open(File.join(dir, TMPFILE_NAME), 'w') do |f|
      f.write(content)
      f.flush

      latexmk_command = ['latexmk', ENGINES[params[:engine].to_sym]] \
                        + latex_params.map { |p| '-latexoption=' + p } \
                        + ["-output-directory=#{dir}", f.path ]

      puts "Running latexmk command: #{latexmk_command}"

      raise 'Build Error' unless system(*latexmk_command)
      system('mv', f.path.sub('.tex', '.pdf'), output_filename)
    end
  end
end