Class: Texrack::LatexToPdf

Inherits:
Object
  • Object
show all
Defined in:
lib/texrack/latex_to_pdf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(latex, logger = Logger.new(STDOUT)) ⇒ LatexToPdf

Returns a new instance of LatexToPdf.



4
5
6
7
# File 'lib/texrack/latex_to_pdf.rb', line 4

def initialize(latex, logger=Logger.new(STDOUT))
  @latex  = latex
  @logger = logger
end

Instance Attribute Details

#latexObject (readonly)

Returns the value of attribute latex.



2
3
4
# File 'lib/texrack/latex_to_pdf.rb', line 2

def latex
  @latex
end

#loggerObject (readonly)

Returns the value of attribute logger.



2
3
4
# File 'lib/texrack/latex_to_pdf.rb', line 2

def logger
  @logger
end

Instance Method Details

#commandObject



49
50
51
# File 'lib/texrack/latex_to_pdf.rb', line 49

def command
  Texrack.config[:pdflatex] || "pdflatex"
end

#generate_pdfObject

Converts a string of LaTeX code into a binary string of PDF.

pdflatex is used to convert the file.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/texrack/latex_to_pdf.rb', line 12

def generate_pdf
  raise Texrack::LatexNotFoundError unless has_pdflatex?

  logger.debug "Generating PDF from #{latex}"
  input = Tempfile.new(["texrack-input", ".tex"])
  input.binmode
  input.write(latex)
  input.flush

  Process.waitpid(
    fork do
      begin
        Dir.chdir File.dirname(input)
        args = %w[-shell-escape -interaction batchmode -halt-on-error] + [input.path]

        logger.debug "Texrack executing: #{command} #{args}"
        exec command, *args
      rescue
        logger.error "#{$!.message}:\n#{$!.backtrace.join("\n")}\n"
      ensure
        Process.exit! 1
      end
    end)

  output  = input.path.sub(/\.tex$/, '.pdf')
  logfile = input.path.sub(/\.tex$/, '.log')
  if !File.exist?(output)
    loglines = File.read(logfile)
    logger.error "Unable to find file #{output}"
    logger.error "LaTeX output: #{loglines}"
    raise Texrack::LatexFailedError
  end

  input.close
  output
end