class Pdflatex::Converter

Public Class Methods

convert(latex_file, output_path) click to toggle source
# File lib/pdflatex.rb, line 7
    def self.convert(latex_file, output_path)
      path_to_latex_file = File.expand_path(latex_file)
      expanded_output_path = File.expand_path(output_path)

      latex_file_name = File.basename(latex_file)

      # Do not use docker image if ENV key is set
      dont_use_docker = ENV.has_key?('DONT_USE_DOCKER')

      cmd = if dont_use_docker
              <<-CMD.gsub(/\s+/, ' ')
                latexmk -outdir=#{expanded_output_path} -pdf #{path_to_latex_file}
              CMD
            else
              <<-CMD.gsub(/\s+/, ' ')
                docker run
                --rm
                --user $(id -u)
                -v #{path_to_latex_file}:/pdflatex/#{latex_file_name}
                -v #{expanded_output_path}:/pdflatex/out
                swissdrg/pdflatex:ride
                latexmk -outdir=out -pdf #{latex_file_name}
              CMD
            end

      output = `#{cmd}`

      if $?.success?
        file_name = File.basename(latex_file_name, '.tex')
        # remove auxilary files
        %w(aux fdb_latexmk fls log).each do |ext|
          file_path = File.join(expanded_output_path, "#{file_name}.#{ext}")
          File.delete(file_path) if File.exist?(file_path)
        end
      end

      output
    end