-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagram-generator.lua
396 lines (338 loc) · 12.5 KB
/
diagram-generator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
--[[
diagram-generator – create images and figures from code blocks.
This Lua filter is used to create images with or without captions
from code blocks. Currently PlantUML, GraphViz, Tikz, and Python
can be processed. For further details, see README.md.
Copyright: © 2018-2021 John MacFarlane <[email protected]>,
2018 Florian Schätzig <[email protected]>,
2019 Thorsten Sommer <[email protected]>,
2019-2021 Albert Krewinkel <[email protected]>
License: MIT – see LICENSE file for details
]]
-- Module pandoc.system is required and was added in version 2.7.3
PANDOC_VERSION:must_be_at_least '2.7.3'
local system = require 'pandoc.system'
local utils = require 'pandoc.utils'
local stringify = function (s)
return type(s) == 'string' and s or utils.stringify(s)
end
local with_temporary_directory = system.with_temporary_directory
local with_working_directory = system.with_working_directory
-- The PlantUML path. If set, uses the environment variable PLANTUML or the
-- value "plantuml.jar" (local PlantUML version). In order to define a
-- PlantUML version per pandoc document, use the meta data to define the key
-- "plantuml_path".
local plantuml_path = os.getenv("PLANTUML") or "plantuml.jar"
-- The Inkscape path. In order to define an Inkscape version per pandoc
-- document, use the meta data to define the key "inkscape_path".
local inkscape_path = os.getenv("INKSCAPE") or "inkscape"
-- The Python path. In order to define a Python version per pandoc document,
-- use the meta data to define the key "python_path".
local python_path = os.getenv("PYTHON") or "python"
-- The Python environment's activate script. Can be set on a per document
-- basis by using the meta data key "activatePythonPath".
local python_activate_path = os.getenv("PYTHON_ACTIVATE")
-- The Java path. In order to define a Java version per pandoc document,
-- use the meta data to define the key "java_path".
local java_path = os.getenv("JAVA_HOME")
if java_path then
java_path = java_path .. package.config:sub(1,1) .. "bin"
.. package.config:sub(1,1) .. "java"
else
java_path = "java"
end
-- The dot (Graphviz) path. In order to define a dot version per pandoc
-- document, use the meta data to define the key "dot_path".
local dot_path = os.getenv("DOT") or "dot"
-- The pdflatex path. In order to define a pdflatex version per pandoc
-- document, use the meta data to define the key "pdflatex_path".
local pdflatex_path = os.getenv("PDFLATEX") or "pdflatex"
-- The asymptote path. There is also the metadata variable
-- "asymptote_path".
local asymptote_path = os.getenv ("ASYMPTOTE") or "asy"
-- The default format is SVG i.e. vector graphics:
local filetype = "svg"
local mimetype = "image/svg+xml"
-- Check for output formats that potentially cannot use SVG
-- vector graphics. In these cases, we use a different format
-- such as PNG:
if FORMAT == "docx" then
filetype = "png"
mimetype = "image/png"
elseif FORMAT == "pptx" then
filetype = "png"
mimetype = "image/png"
elseif FORMAT == "rtf" then
filetype = "png"
mimetype = "image/png"
end
-- Execute the meta data table to determine the paths. This function
-- must be called first to get the desired path. If one of these
-- meta options was set, it gets used instead of the corresponding
-- environment variable:
function Meta(meta)
plantuml_path = stringify(
meta.plantuml_path or meta.plantumlPath or plantuml_path
)
inkscape_path = stringify(
meta.inkscape_path or meta.inkscapePath or inkscape_path
)
python_path = stringify(
meta.python_path or meta.pythonPath or python_path
)
python_activate_path =
meta.activate_python_path or meta.activatePythonPath or python_activate_path
python_activate_path = python_activate_path and stringify(python_activate_path)
java_path = stringify(
meta.java_path or meta.javaPath or java_path
)
dot_path = stringify(
meta.path_dot or meta.dotPath or dot_path
)
pdflatex_path = stringify(
meta.pdflatex_path or meta.pdflatexPath or pdflatex_path
)
asymptote_path = stringify(
meta.asymptote_path or meta.asymptotePath or asymptote_path
)
end
-- Call plantuml.jar with some parameters (cf. PlantUML help):
local function plantuml(puml, filetype)
return pandoc.pipe(
java_path,
{"-jar", plantuml_path, "-t" .. filetype, "-pipe", "-charset", "UTF8"},
puml
)
end
-- Call dot (GraphViz) in order to generate the image
-- (thanks @muxueqz for this code):
local function graphviz(code, filetype)
return pandoc.pipe(dot_path, {"-T" .. filetype}, code)
end
--
-- TikZ
--
--- LaTeX template used to compile TikZ images. Takes additional
--- packages as the first, and the actual TikZ code as the second
--- argument.
local tikz_template = [[
\documentclass{standalone}
\usepackage{tikz}
%% begin: additional packages
%s
%% end: additional packages
\begin{document}
%s
\end{document}
]]
-- Returns a function which takes the filename of a PDF or SVG file
-- and a target filename, and writes the input as the given format.
-- Returns `nil` if conversion into the target format is not possible.
local function convert_with_inkscape(filetype)
-- Build the basic Inkscape command for the conversion
local inkscape_output_args
-- Check inksape version.
-- TODO: this can be removed if supporting older Inkscape is not important.
local inkscape_v_string = io.popen(inkscape_path .. " --version"):read()
local inkscape_v_major = inkscape_v_string:gmatch("([0-9]*)%.")()
local isv1 = tonumber(inkscape_v_major) >= 1
local cmd_arg = isv1 and '"%s" "%s" -o "%s" ' or '"%s" --without-gui --file="%s" '
if filetype == 'png' then
local png_arg = isv1 and '--export-type=png' or '--export-png="%s"'
output_args = png_arg .. '--export-dpi=300'
elseif filetype == 'svg' then
output_args = isv1 and '--export-type=svg --export-plain-svg' or '--export-plain-svg="%s"'
else
return nil
end
return function (pdf_file, outfile)
local inkscape_command = string.format(
cmd_arg .. output_args,
inkscape_path,
pdf_file,
outfile
)
local command_output = io.popen(inkscape_command)
-- TODO: print output when debugging.
command_output:close()
end
end
--- Compile LaTeX with Tikz code to an image
local function tikz2image(src, filetype, additional_packages)
local convert = convert_with_inkscape(filetype)
-- Bail if there is now known way from PDF to the target format.
if not convert then
error(string.format("Don't know how to convert pdf to %s.", filetype))
end
return with_temporary_directory("tikz2image", function (tmpdir)
return with_working_directory(tmpdir, function ()
-- Define file names:
local file_template = "%s/tikz-image.%s"
local tikz_file = file_template:format(tmpdir, "tex")
local pdf_file = file_template:format(tmpdir, "pdf")
local outfile = file_template:format(tmpdir, filetype)
-- Build and write the LaTeX document:
local f = io.open(tikz_file, 'w')
f:write(tikz_template:format(additional_packages or '', src))
f:close()
-- Execute the LaTeX compiler:
pandoc.pipe(pdflatex_path, {'-output-directory', tmpdir, tikz_file}, '')
convert(pdf_file, outfile)
-- Try to open and read the image:
local img_data
local r = io.open(outfile, 'rb')
if r then
img_data = r:read("*all")
r:close()
else
-- TODO: print warning
end
return img_data
end)
end)
end
-- Run Python to generate an image:
local function py2image(code, filetype)
-- Define the temp files:
local outfile = string.format('%s.%s', os.tmpname(), filetype)
local pyfile = os.tmpname()
-- Replace the desired destination's file type in the Python code:
local extendedCode = string.gsub(code, "%$FORMAT%$", filetype)
-- Replace the desired destination's path in the Python code:
extendedCode = string.gsub(extendedCode, "%$DESTINATION%$", outfile)
-- Write the Python code:
local f = io.open(pyfile, 'w')
f:write(extendedCode)
f:close()
-- Execute Python in the desired environment:
local pycmd = python_path .. ' ' .. pyfile
local command = python_activate_path
and python_activate_path .. ' && ' .. pycmd
or pycmd
os.execute(command)
-- Try to open the written image:
local r = io.open(outfile, 'rb')
local imgData = nil
-- When the image exist, read it:
if r then
imgData = r:read("*all")
r:close()
else
io.stderr:write(string.format("File '%s' could not be opened", outfile))
error 'Could not create image from python code.'
end
-- Delete the tmp files:
os.remove(pyfile)
os.remove(outfile)
return imgData
end
--
-- Asymptote
--
local function asymptote(code, filetype)
local convert
if filetype ~= 'svg' and filetype ~= 'png' then
error(string.format("Conversion to %s not implemented", filetype))
end
return with_temporary_directory(
"asymptote",
function(tmpdir)
return with_working_directory(
tmpdir,
function ()
local asy_file = "pandoc_diagram.asy"
local svg_file = "pandoc_diagram.svg"
local f = io.open(asy_file, 'w')
f:write(code)
f:close()
pandoc.pipe(asymptote_path, {"-f", "svg", "-o", "pandoc_diagram", asy_file}, "")
local r
if filetype == 'svg' then
r = io.open(svg_file, 'rb')
else
local png_file = "pandoc_diagram.png"
convert_with_inkscape("png")(svg_file, png_file)
r = io.open(png_file, 'rb')
end
local img_data
if r then
img_data = r:read("*all")
r:close()
else
error("could not read asymptote result file")
end
return img_data
end)
end)
end
-- Executes each document's code block to find matching code blocks:
function CodeBlock(block)
-- Using a table with all known generators i.e. converters:
local converters = {
plantuml = plantuml,
graphviz = graphviz,
tikz = tikz2image,
py2image = py2image,
asymptote = asymptote,
}
-- Check if a converter exists for this block. If not, return the block
-- unchanged.
local img_converter = converters[block.classes[1]]
if not img_converter then
return nil
end
-- Call the correct converter which belongs to the used class:
local success, img = pcall(img_converter, block.text,
filetype, block.attributes["additionalPackages"] or nil)
-- Bail if an error occured; img contains the error message when that
-- happens.
if not (success and img) then
io.stderr:write(tostring(img or "no image data has been returned."))
io.stderr:write('\n')
error 'Image conversion failed. Aborting.'
end
-- If we got here, then the transformation went ok and `img` contains
-- the image data.
-- Create figure name by hashing the image content
local filename = block.attributes.filename or pandoc.sha1(img)
local fname = filename .. "." .. filetype
-- Store the data in the media bag:
pandoc.mediabag.insert(fname, mimetype, img)
local enable_caption = nil
-- If the user defines a caption, read it as Markdown.
local caption = block.attributes.caption
and pandoc.read(block.attributes.caption).blocks[1].content
or {}
-- A non-empty caption means that this image is a figure. We have to
-- set the image title to "fig:" for pandoc to treat it as such.
local title = #caption > 0 and "fig:" or ""
-- Transfer identifier and other relevant attributes from the code
-- block to the image. Currently, only `name` is kept as an attribute.
-- This allows a figure block starting with:
--
-- ```{#fig:example .plantuml caption="Image created by **PlantUML**."}
--
-- to be referenced as @fig:example outside of the figure when used
-- with `pandoc-crossref`.
local img_attr = {
id = block.identifier,
name = block.attributes.name,
width = block.attributes.width,
height = block.attributes.height,
}
-- Create a new image for the document's structure. Attach the user's
-- caption. Also use a hack (fig:) to enforce pandoc to create a
-- figure i.e. attach a caption to the image.
local img_obj = pandoc.Image(caption, fname, title, img_attr)
-- Finally, put the image inside an empty paragraph. By returning the
-- resulting paragraph object, the source code block gets replaced by
-- the image:
return pandoc.Para{ img_obj }
end
-- Normally, pandoc will run the function in the built-in order Inlines ->
-- Blocks -> Meta -> Pandoc. We instead want Meta -> Blocks. Thus, we must
-- define our custom order:
return {
{Meta = Meta},
{CodeBlock = CodeBlock},
}