local system = require 'pandoc.system' local tikz_doc_template = [[ \documentclass{standalone} \usepackage{xcolor} \usepackage{tikz} %s \begin{document} \nopagecolor %s \end{document} ]] local function tikz2image(src, outfile, before) system.with_temporary_directory('tikz2image', function (tmpdir) system.with_working_directory(tmpdir, function() local f = io.open('tikz.tex', 'w') local tex = tikz_doc_template:format(before, src) if not f then return end f:write(tex) f:close() os.execute('pdflatex tikz.tex') os.execute('pdf2svg tikz.pdf ' .. outfile) end) end) end function CodeBlock(el) if not el.classes:includes('tikz') then return el end local fbasename = pandoc.sha1(el.text) .. '.svg' local fname = system.get_working_directory() .. '/.build/' .. fbasename tikz2image(el.text, fname, el.attributes.before) el.attributes.before = nil local img = pandoc.Image({}, fname) img.attributes = el.attributes return img end