Created
April 21, 2012 18:06
-
-
Save zmanji/2438873 to your computer and use it in GitHub Desktop.
LaTeX filter for Nanoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a LaTeX filter for nanoc | |
require 'open3' | |
require 'fileutils' | |
class LatexFilter < Nanoc::Filter | |
identifier :latex | |
type :text => :binary | |
# Idea, use `latexmk` to do all of the hard work assuming a full | |
# MacTeX/TeXLive install. | |
def run(content, params={}) | |
rc = <<-eos | |
$latex = 'latex -interaction=nonstopmode'; | |
$pdflatex = 'pdflatex -interaction=nonstopmode'; | |
eos | |
input = "" | |
output = "" | |
Dir.mktmpdir{|dir| | |
input = "#{dir}/doc.tex" | |
output = "#{dir}/doc.pdf" | |
open(input, "w") {|io| | |
io.write(content) | |
} | |
rcfile = "#{dir}/latexmkrc" | |
open(rcfile, "w") {|io| | |
io.write(rc) | |
} | |
command = "latexmk -recorder -pdf #{input} -aux-directory=#{dir} -output-directory=#{dir}" | |
latexmk_output = '' | |
latexmk_err = '' | |
status = 0 | |
options = {:chdir => dir} | |
Open3::popen3(command, options) do |stdin, stdout, stderr, wait_thr| | |
latexmk_output = stdout.read | |
latexmk_err = stderr.read | |
status = wait_thr.value | |
end | |
if status.exitstatus != 0 | |
puts latexmk_output | |
puts latexmk_err | |
raise "LATEXMK DID NOT EXIT SUCCESSFULLY" | |
end | |
FileUtils.cp output, output_filename | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment