aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2020-05-25 09:10:26 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2020-05-25 09:10:26 +0200
commit37ef57be5154e316b758332428c18ecc22240617 (patch)
treeb9e81024c5e54eb542d9b8fe99be35e0bc6a2365
parent2aa0832320ff1cc7f6268e59380830784de083c3 (diff)
downloadpandoc-37ef57be5154e316b758332428c18ecc22240617.tar.gz
lua-filters.md: use pandoc.system module in TikZ example
Showcase temporary directory handling with `with_temporary_directory` and `with_working_directory`.
-rw-r--r--doc/lua-filters.md87
1 files changed, 48 insertions, 39 deletions
diff --git a/doc/lua-filters.md b/doc/lua-filters.md
index 7cae8c186..c5f7d2713 100644
--- a/doc/lua-filters.md
+++ b/doc/lua-filters.md
@@ -653,59 +653,68 @@ document is built. (A more sophisticated version of this might
put these in a special cache directory.)
``` lua
+local system = require 'pandoc.system'
+
+local tikz_doc_template = [[
+\documentclass{standalone}
+\usepackage{xcolor}
+\usepackage{tikz}
+\begin{document}
+\nopagecolor
+%s
+\end{document}
+]]
+
local function tikz2image(src, filetype, outfile)
- local tmp = os.tmpname()
- local tmpdir = string.match(tmp, "^(.*[\\/])") or "."
- local f = io.open(tmp .. ".tex", 'w')
- f:write("\\documentclass{standalone}\n\\usepackage{xcolor}\n\\usepackage{tikz}\n\\begin{document}\n\\nopagecolor\n")
- f:write(src)
- f:write("\n\\end{document}\n")
- f:close()
- os.execute("pdflatex -output-directory " .. tmpdir .. " " .. tmp)
- if filetype == 'pdf' then
- os.rename(tmp .. ".pdf", outfile)
- else
- os.execute("pdf2svg " .. tmp .. ".pdf " .. outfile)
- end
- os.remove(tmp .. ".tex")
- os.remove(tmp .. ".pdf")
- os.remove(tmp .. ".log")
- os.remove(tmp .. ".aux")
+ 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' }
+ 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
+ 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
+ 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 fname = pandoc.sha1(el.text) .. "." .. filetype
- if not file_exists(fname) then
- tikz2image(el.text, filetype, fname)
- end
- return pandoc.Para({pandoc.Image({}, fname)})
- else
- return el
+ if starts_with('\\begin{tikzpicture}', el.text) then
+ local filetype = extension_for[FORMAT] or 'svg'
+ local fname = system.get_working_directory() .. '/' ..
+ pandoc.sha1(el.text) .. '.' .. filetype
+ if not file_exists(fname) then
+ tikz2image(el.text, filetype, fname)
end
+ return pandoc.Para({pandoc.Image({}, fname)})
+ else
+ return el
+ end
end
```