8
8
for the code to generate a GraphViz image.
9
9
]]
10
10
11
- -- The PlantUML path. If set, uses the environment variable PLANTUML or the value "plantuml.jar" (local PlantUML version).
12
- -- In order to define a PlantUML version per pandoc document, use the meta data to define the key "plantumlPath".
11
+ -- The PlantUML path. If set, uses the environment variable PLANTUML or the
12
+ -- value "plantuml.jar" (local PlantUML version). In order to define a
13
+ -- PlantUML version per pandoc document, use the meta data to define the key
14
+ -- "plantumlPath".
13
15
local plantumlPath = os.getenv (" PLANTUML" ) or " plantuml.jar"
14
16
15
- -- The Inkscape path. In order to define an Inkscape version
16
- -- per pandoc document, use the meta data to define the key "inkscapePath".
17
+ -- The Inkscape path. In order to define an Inkscape version per pandoc
18
+ -- document, use the meta data to define the key "inkscapePath".
17
19
local inkscapePath = os.getenv (" INKSCAPE" ) or " inkscape"
18
20
19
21
-- The Python path. In order to define a Python version per pandoc document,
20
22
-- use the meta data to define the key "pythonPath".
21
23
local pythonPath = os.getenv (" PYTHON" )
22
24
23
- -- The Python environment's activate script. Can be set on a per document basis
24
- -- by using the meta data key "activatePythonPath".
25
+ -- The Python environment's activate script. Can be set on a per document
26
+ -- basis by using the meta data key "activatePythonPath".
25
27
local pythonActivatePath = os.getenv (" PYTHON_ACTIVATE" )
26
28
27
29
-- The Java path. In order to define a Java version per pandoc document,
28
30
-- use the meta data to define the key "javaPath".
29
31
local javaPath = os.getenv (" JAVA_HOME" )
30
32
if javaPath then
31
- javaPath = javaPath .. package.config :sub (1 ,1 ) .. " bin" .. package.config :sub (1 ,1 ) .. " java"
33
+ javaPath = javaPath .. package.config :sub (1 ,1 ) .. " bin"
34
+ .. package.config :sub (1 ,1 ) .. " java"
32
35
else
33
36
javaPath = " java"
34
37
end
35
38
36
- -- The dot (Graphviz) path. In order to define a dot version per pandoc document,
37
- -- use the meta data to define the key "dotPath".
39
+ -- The dot (Graphviz) path. In order to define a dot version per pandoc
40
+ -- document, use the meta data to define the key "dotPath".
38
41
local dotPath = os.getenv (" DOT" ) or " dot"
39
42
40
- -- The pdflatex path. In order to define a pdflatex version per pandoc document,
41
- -- use the meta data to define the key "pdflatexPath".
43
+ -- The pdflatex path. In order to define a pdflatex version per pandoc
44
+ -- document, use the meta data to define the key "pdflatexPath".
42
45
local pdflatexPath = os.getenv (" PDFLATEX" ) or " pdflatex"
43
46
44
47
-- The default format is SVG i.e. vector graphics:
@@ -100,7 +103,7 @@ local function tikz2image(src, filetype, additionalPackages)
100
103
-- Build and write the LaTeX document:
101
104
local f = io.open (tmp .. " .tex" , ' w' )
102
105
f :write (" \\ documentclass{standalone}\n\\ usepackage{tikz}\n " )
103
-
106
+
104
107
-- Any additional package(s) are desired?
105
108
if additionalPackages then
106
109
f :write (additionalPackages )
@@ -119,35 +122,37 @@ local function tikz2image(src, filetype, additionalPackages)
119
122
local knownFormat = false
120
123
121
124
if filetype == " png" then
122
-
125
+
123
126
-- Append the subcommands to convert into a PNG file:
124
- baseCommand = baseCommand .. " --export-png=" .. tmp .. " .png --export-dpi=300"
127
+ baseCommand = baseCommand .. " --export-png="
128
+ .. tmp .. " .png --export-dpi=300"
125
129
knownFormat = true
126
130
127
131
elseif filetype == " svg" then
128
-
132
+
129
133
-- Append the subcommands to convert into a SVG file:
130
134
baseCommand = baseCommand .. " --export-plain-svg=" .. tmp .. " .svg"
131
135
knownFormat = true
132
136
133
137
end
134
138
135
- -- Unfortunately, continuation is only possible, if we know the actual format:
139
+ -- Unfortunately, continuation is only possible, if we know the actual
140
+ -- format:
136
141
local imgData = nil
137
142
if knownFormat then
138
143
139
144
-- We know the desired format. Thus, execute Inkscape:
140
145
os.execute (" \" " .. inkscapePath .. " \" " .. baseCommand )
141
-
146
+
142
147
-- Try to open the image:
143
148
local r = io.open (tmp .. " ." .. filetype , ' rb' )
144
-
149
+
145
150
-- Read the image, if available:
146
151
if r then
147
152
imgData = r :read (" *all" )
148
153
r :close ()
149
154
end
150
-
155
+
151
156
-- Delete the image tmp file:
152
157
os.remove (outfile )
153
158
end
163
168
164
169
-- Run Python to generate an image:
165
170
local function py2image (code , filetype )
166
-
171
+
167
172
-- Define the temp files:
168
173
local outfile = string.format (" ./tmp-python/file.%s" , filetype )
169
174
local tmp = " ./tmp-python/file"
@@ -184,7 +189,9 @@ local function py2image(code, filetype)
184
189
f :close ()
185
190
186
191
-- Execute Python in the desired environment:
187
- os.execute (pythonActivatePath .. " && " .. pythonPath .. " " .. tmp .. " .py" )
192
+ os.execute (
193
+ pythonActivatePath .. " && " .. pythonPath .. " " .. tmp .. " .py"
194
+ )
188
195
189
196
-- Try to open the written image:
190
197
local r = io.open (outfile , ' rb' )
@@ -220,13 +227,13 @@ function CodeBlock(block)
220
227
-- Call the correct converter which belongs to the used class:
221
228
local success , img = pcall (converters [block .classes [1 ]], block .text ,
222
229
filetype , block .attributes [" additionalPackages" ] or nil )
223
-
230
+
224
231
-- Was ok?
225
232
if success and img then
226
-
233
+
227
234
-- Hash the figure name and content:
228
235
fname = pandoc .sha1 (img ) .. " ." .. filetype
229
-
236
+
230
237
-- Store the data in the media bag:
231
238
pandoc .mediabag .insert (fname , mimetype , img )
232
239
end
@@ -246,27 +253,30 @@ function CodeBlock(block)
246
253
enableCaption = " fig:"
247
254
end
248
255
249
- -- Create a new image for the document's structure. Attach the user's caption.
250
- -- Also use a hack (fig:) to enforce pandoc to create a figure i.e. attach
251
- -- a caption to the image.
256
+ -- Create a new image for the document's structure. Attach the user's
257
+ -- caption. Also use a hack (fig:) to enforce pandoc to create a
258
+ -- figure i.e. attach a caption to the image.
252
259
local imgObj = pandoc .Image (caption , fname , enableCaption )
253
260
254
- -- Now, transfer the attribute "name" from the code block to the new image block.
255
- -- It might gets used by the figure numbering lua filter. If the figure numbering
256
- -- gets not used, this additional attribute gets ignored as well.
261
+ -- Now, transfer the attribute "name" from the code block to the new
262
+ -- image block. It might gets used by the figure numbering lua filter.
263
+ -- If the figure numbering gets not used, this additional attribute
264
+ -- gets ignored as well.
257
265
if block .attributes [" name" ] then
258
266
imgObj .attributes [" name" ] = block .attributes [" name" ]
259
267
end
260
268
261
- -- Finally, put the image inside an empty paragraph. By returning the resulting
262
- -- paragraph object, the source code block gets replaced by the image:
269
+ -- Finally, put the image inside an empty paragraph. By returning the
270
+ -- resulting paragraph object, the source code block gets replaced by
271
+ -- the image:
263
272
return pandoc .Para { imgObj }
264
273
end
265
274
end
266
275
267
- -- Normally, pandoc will run the function in the built-in order Inlines -> Blocks -> Meta -> Pandoc.
268
- -- We instead want Meta -> Blocks. Thus, we must define our custom order:
276
+ -- Normally, pandoc will run the function in the built-in order Inlines ->
277
+ -- Blocks -> Meta -> Pandoc. We instead want Meta -> Blocks. Thus, we must
278
+ -- define our custom order:
269
279
return {
270
280
{Meta = Meta },
271
281
{CodeBlock = CodeBlock },
272
- }
282
+ }
0 commit comments