git.iko.soy/typeable/pandoc
on master help sign in
Improved sample lua tikz filter in lua-filters docs (#5445)
32e358bMatthew Doty
doc/lua-filters.md+21−12
@@ -550,8 +550,8 @@ end
550
550
551This filter converts raw LaTeX tikz environments into images. It
551This filter converts raw LaTeX tikz environments into images. It
552works with both PDF and HTML output. The tikz code is compiled
552works with both PDF and HTML output. The tikz code is compiled
553−to an image using `pdflatex`, and the image is converted (if
553+to an image using `pdflatex`, and the image is converted from pdf
554−necessary) from pdf to png format using ImageMagick's `convert`,
554+to svg format using [`pdf2svg`](https://github.com/dawbarton/pdf2svg),
555so both of these must be in the system path. Converted images
555so both of these must be in the system path. Converted images
556are cached in the working directory and given filenames based on
556are cached in the working directory and given filenames based on
557a hash of the source, so that they need not be regenerated each
557a hash of the source, so that they need not be regenerated each
@@ -563,7 +563,7 @@ local function tikz2image(src, filetype, outfile)
563 local tmp = os.tmpname()
563 local tmp = os.tmpname()
564 local tmpdir = string.match(tmp, "^(.*[\\/])") or "."
564 local tmpdir = string.match(tmp, "^(.*[\\/])") or "."
565 local f = io.open(tmp .. ".tex", 'w')
565 local f = io.open(tmp .. ".tex", 'w')
566− f:write("\\documentclass{standalone}\n\\usepackage{tikz}\n\\begin{document}\n")
566+ f:write("\\documentclass{standalone}\n\\usepackage{xcolor}\n\\usepackage{tikz}\n\\begin{document}\n\\nopagecolor\n")
567 f:write(src)
567 f:write(src)
568 f:write("\n\\end{document}\n")
568 f:write("\n\\end{document}\n")
569 f:close()
569 f:close()
@@ -571,7 +571,7 @@ local function tikz2image(src, filetype, outfile)
571 if filetype == 'pdf' then
571 if filetype == 'pdf' then
572 os.rename(tmp .. ".pdf", outfile)
572 os.rename(tmp .. ".pdf", outfile)
573 else
573 else
574− os.execute("convert " .. tmp .. ".pdf " .. outfile)
574+ os.execute("pdf2svg " .. tmp .. ".pdf " .. outfile)
575 end
575 end
576 os.remove(tmp .. ".tex")
576 os.remove(tmp .. ".tex")
577 os.remove(tmp .. ".pdf")
577 os.remove(tmp .. ".pdf")
@@ -580,9 +580,9 @@ local function tikz2image(src, filetype, outfile)
580end
580end
581
581
582extension_for = {
582extension_for = {
583− html = 'png',
583+ html = 'svg',
584− html4 = 'png',
584+ html4 = 'svg',
585− html5 = 'png',
585+ html5 = 'svg',
586 latex = 'pdf',
586 latex = 'pdf',
587 beamer = 'pdf' }
587 beamer = 'pdf' }
588
588
@@ -596,13 +596,22 @@ local function file_exists(name)
596 end
596 end
597end
597end
598
598
599+local function starts_with(start, str)
600+ return str:sub(1, #start) == start
601+end
602+
603+
599function RawBlock(el)
604function RawBlock(el)
605+ if starts_with("\\begin{tikzpicture}", el.text) then
600− local filetype = extension_for[FORMAT] or "png"
606+ local filetype = extension_for[FORMAT] or "svg"
601− local fname = pandoc.sha1(el.text) .. "." .. filetype
607+ local fname = pandoc.sha1(el.text) .. "." .. filetype
602− if not file_exists(fname) then
608+ if not file_exists(fname) then
603− tikz2image(el.text, filetype, fname)
609+ tikz2image(el.text, filetype, fname)
610+ end
611+ return pandoc.Para({pandoc.Image({}, fname)})
612+ else
613+ return el
604 end
614 end
605− return pandoc.Para({pandoc.Image({}, fname)})
606end
615end
607```
616```
608
617