33 lines
1.3 KiB
Lua
33 lines
1.3 KiB
Lua
-- Copyright (c) 2026 Marc Wäckerlin. SPDX-License-Identifier: MIT
|
|||
|
|
-- Every drawing a document shows, converted while pandoc reads the document.
|
||
|
|
--
|
||
|
|
-- graphicx cannot read an SVG, and pandoc's own answer to one is the svg
|
||
|
|
-- package, which calls inkscape from inside the LaTeX run and therefore needs
|
||
|
|
-- shell escape — a document could then run any command it likes. So the drawing
|
||
|
|
-- is converted here, before a single line of LaTeX exists, and the picture in
|
||
|
|
-- the document points at the PDF that comes out.
|
||
|
|
--
|
||
|
|
-- The two directories arrive in the environment, from the converter: where the
|
||
|
|
-- document lies, and where the build may write.
|
||
|
|
|
||
|
|
local build = os.getenv('MD2PDF_BUILD')
|
||
|
|
local source = os.getenv('MD2PDF_SOURCE_DIR')
|
||
|
|
|
||
|
|
local function absolute(file)
|
||
|
|
return file:sub(1, 1) == '/' and file or source .. '/' .. file
|
||
|
|
end
|
||
|
|
|
||
|
|
function Image(image)
|
||
|
|
if not image.src:lower():match('%.svg$') then return nil end
|
||
|
|
local name = image.src:gsub('.*/', ''):gsub('%.[Ss][Vv][Gg]$', '') .. '.pdf'
|
||
|
|
local target = build .. '/' .. name
|
||
|
|
local converted, message = pcall(pandoc.pipe, 'inkscape',
|
||
|
|
{'--export-type=pdf', '--export-filename=' .. target,
|
||
|
|
absolute(image.src)}, '')
|
||
|
|
if not converted then
|
||
|
|
error('the drawing ' .. image.src .. ' could not be converted: '
|
||
|
|
.. tostring(message))
|
||
|
|
end
|
||
|
|
image.src = target
|
||
|
|
return image
|
||
|
|
end
|