Add lua filter for 'tikz' diagrams
parent
4fbdacb859
commit
b2234aa463
|
@ -0,0 +1,63 @@
|
|||
local system = require 'pandoc.system'
|
||||
|
||||
local tikz_doc_template = [[
|
||||
\documentclass{standalone}[border=0.2cm]
|
||||
\usepackage{tikz}
|
||||
\usetikzlibrary{automata, positioning, arrows.meta}
|
||||
\begin{document}
|
||||
%s
|
||||
\end{document}
|
||||
]]
|
||||
|
||||
local function tikz2image(src, filetype, outfile)
|
||||
system.with_temporary_directory('tikz2image', function (tmpdir)
|
||||
system.with_working_directory(tmpdir, function()
|
||||
local f = io.open('tikz.tex', 'w')
|
||||
f:write(tikz_doc_template:format(src))
|
||||
f:close()
|
||||
os.execute('pdflatex tikz.tex')
|
||||
if filetype == 'pdf' then
|
||||
os.rename('tikz.pdf', outfile)
|
||||
else
|
||||
os.execute('pdf2svg tikz.pdf ' .. outfile)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
extension_for = {
|
||||
html = 'svg',
|
||||
html4 = 'svg',
|
||||
html5 = 'svg',
|
||||
latex = 'pdf',
|
||||
beamer = 'pdf' }
|
||||
|
||||
local function file_exists(name)
|
||||
local f = io.open(name, 'r')
|
||||
if f ~= nil then
|
||||
io.close(f)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
local function starts_with(start, str)
|
||||
return str:sub(1, #start) == start
|
||||
end
|
||||
|
||||
function RawBlock(el)
|
||||
if starts_with('\\begin{tikzpicture}', el.text) then
|
||||
local filetype = extension_for[FORMAT] or 'svg'
|
||||
local fbasename = pandoc.sha1(el.text) .. '.' .. filetype
|
||||
-- Save to './img' if it does not exist yet
|
||||
local fname = system.get_working_directory() .. '/img/' .. fbasename
|
||||
if not file_exists(fname) then
|
||||
tikz2image(el.text, filetype, fname)
|
||||
end
|
||||
-- Return `<img src="img/<file-name>" />` wrapped in a paragraph
|
||||
return pandoc.Para({pandoc.Image({}, 'img/' .. fbasename)})
|
||||
else
|
||||
return el
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue