@@ -68,14 +68,14 @@ local mimetype = "image/svg+xml"
6868-- vector graphics. In these cases, we use a different format
6969-- such as PNG:
7070if FORMAT == " docx" then
71- filetype = " png"
72- mimetype = " image/png"
71+ filetype = " png"
72+ mimetype = " image/png"
7373elseif FORMAT == " pptx" then
74- filetype = " png"
75- mimetype = " image/png"
74+ filetype = " png"
75+ mimetype = " image/png"
7676elseif FORMAT == " rtf" then
77- filetype = " png"
78- mimetype = " image/png"
77+ filetype = " png"
78+ mimetype = " image/png"
7979end
8080
8181-- Execute the meta data table to determine the paths. This function
@@ -212,46 +212,46 @@ end
212212-- Run Python to generate an image:
213213local function py2image (code , filetype )
214214
215- -- Define the temp files:
216- local outfile = string.format (' %s.%s' , os.tmpname (), filetype )
217- local pyfile = os.tmpname ()
215+ -- Define the temp files:
216+ local outfile = string.format (' %s.%s' , os.tmpname (), filetype )
217+ local pyfile = os.tmpname ()
218218
219- -- Replace the desired destination's file type in the Python code:
220- local extendedCode = string.gsub (code , " %$FORMAT%$" , filetype )
219+ -- Replace the desired destination's file type in the Python code:
220+ local extendedCode = string.gsub (code , " %$FORMAT%$" , filetype )
221221
222- -- Replace the desired destination's path in the Python code:
223- extendedCode = string.gsub (extendedCode , " %$DESTINATION%$" , outfile )
222+ -- Replace the desired destination's path in the Python code:
223+ extendedCode = string.gsub (extendedCode , " %$DESTINATION%$" , outfile )
224224
225- -- Write the Python code:
226- local f = io.open (pyfile , ' w' )
227- f :write (extendedCode )
228- f :close ()
225+ -- Write the Python code:
226+ local f = io.open (pyfile , ' w' )
227+ f :write (extendedCode )
228+ f :close ()
229229
230- -- Execute Python in the desired environment:
231- local pycmd = python_path .. ' ' .. pyfile
232- local command = python_activate_path
233- and python_activate_path .. ' && ' .. pycmd
234- or pycmd
235- os.execute (command )
230+ -- Execute Python in the desired environment:
231+ local pycmd = python_path .. ' ' .. pyfile
232+ local command = python_activate_path
233+ and python_activate_path .. ' && ' .. pycmd
234+ or pycmd
235+ os.execute (command )
236236
237- -- Try to open the written image:
238- local r = io.open (outfile , ' rb' )
239- local imgData = nil
237+ -- Try to open the written image:
238+ local r = io.open (outfile , ' rb' )
239+ local imgData = nil
240240
241- -- When the image exist, read it:
242- if r then
243- imgData = r :read (" *all" )
244- r :close ()
245- else
246- io.stderr :write (string.format (" File '%s' could not be opened" , outfile ))
247- error ' Could not create image from python code.'
248- end
241+ -- When the image exist, read it:
242+ if r then
243+ imgData = r :read (" *all" )
244+ r :close ()
245+ else
246+ io.stderr :write (string.format (" File '%s' could not be opened" , outfile ))
247+ error ' Could not create image from python code.'
248+ end
249249
250- -- Delete the tmp files:
251- os.remove (pyfile )
252- os.remove (outfile )
250+ -- Delete the tmp files:
251+ os.remove (pyfile )
252+ os.remove (outfile )
253253
254- return imgData
254+ return imgData
255255end
256256
257257--
@@ -300,97 +300,96 @@ end
300300
301301-- Executes each document's code block to find matching code blocks:
302302function CodeBlock (block )
303+ -- Predefine a potential image:
304+ local fname = nil
305+
306+ -- Using a table with all known generators i.e. converters:
307+ local converters = {
308+ plantuml = plantuml ,
309+ graphviz = graphviz ,
310+ tikz = tikz2image ,
311+ py2image = py2image ,
312+ asymptote = asymptote ,
313+ }
314+
315+ -- Check if a converter exists for this block. If not, return the block
316+ -- unchanged.
317+ local img_converter = converters [block .classes [1 ]]
318+ if not img_converter then
319+ return nil
320+ end
303321
304- -- Predefine a potential image:
305- local fname = nil
306-
307- -- Using a table with all known generators i.e. converters:
308- local converters = {
309- plantuml = plantuml ,
310- graphviz = graphviz ,
311- tikz = tikz2image ,
312- py2image = py2image ,
313- asymptote = asymptote ,
314- }
315-
316- -- Check if a converter exists for this block. If not, return the block
317- -- unchanged.
318- local img_converter = converters [block .classes [1 ]]
319- if not img_converter then
320- return nil
321- end
322+ -- Call the correct converter which belongs to the used class:
323+ local success , img = pcall (img_converter , block .text ,
324+ filetype , block .attributes [" additionalPackages" ] or nil )
325+
326+ -- Was ok?
327+ if success and img then
328+ -- Hash the figure name and content:
329+ fname = pandoc .sha1 (img ) .. " ." .. filetype
322330
323- -- Call the correct converter which belongs to the used class:
324- local success , img = pcall (img_converter , block .text ,
325- filetype , block .attributes [" additionalPackages" ] or nil )
331+ -- Store the data in the media bag:
332+ pandoc .mediabag .insert (fname , mimetype , img )
326333
327- -- Was ok?
328- if success and img then
329- -- Hash the figure name and content:
330- fname = pandoc .sha1 (img ) .. " ." .. filetype
334+ else
335+
336+ -- an error occured; img contains the error message
337+ io.stderr :write (tostring (img ))
338+ io.stderr :write (' \n ' )
339+ error ' Image conversion failed. Aborting.'
340+
341+ end
331342
332- -- Store the data in the media bag :
333- pandoc . mediabag . insert ( fname , mimetype , img )
343+ -- Case: This code block was an image e.g. PlantUML or dot/Graphviz, etc. :
344+ if fname then
334345
335- else
346+ -- Define the default caption:
347+ local caption = {}
348+ local enableCaption = nil
336349
337- -- an error occured; img contains the error message
338- io.stderr :write (tostring (img ))
339- io.stderr :write (' \n ' )
340- error ' Image conversion failed. Aborting.'
350+ -- If the user defines a caption, use it:
351+ if block .attributes [" caption" ] then
352+ caption = pandoc .read (block .attributes .caption ).blocks [1 ].content
341353
354+ -- This is pandoc's current hack to enforce a caption:
355+ enableCaption = " fig:"
342356 end
343357
344- -- Case: This code block was an image e.g. PlantUML or dot/Graphviz, etc.:
345- if fname then
346-
347- -- Define the default caption:
348- local caption = {}
349- local enableCaption = nil
350-
351- -- If the user defines a caption, use it:
352- if block .attributes [" caption" ] then
353- caption = pandoc .read (block .attributes .caption ).blocks [1 ].content
354-
355- -- This is pandoc's current hack to enforce a caption:
356- enableCaption = " fig:"
357- end
358-
359- -- Create a new image for the document's structure. Attach the user's
360- -- caption. Also use a hack (fig:) to enforce pandoc to create a
361- -- figure i.e. attach a caption to the image.
362- local imgObj = pandoc .Image (caption , fname , enableCaption )
363-
364- -- Now, transfer the attribute "name" from the code block to the new
365- -- image block. It might gets used by the figure numbering lua filter.
366- -- If the figure numbering gets not used, this additional attribute
367- -- gets ignored as well.
368- if block .attributes [" name" ] then
369- imgObj .attributes [" name" ] = block .attributes [" name" ]
370- end
371-
372- -- Transfer the identifier from the code block to the new image block
373- -- to enable downstream filters like pandoc-crossref. This allows a figure
374- -- block starting with:
375- --
376- -- ```{#fig:pumlExample .plantuml caption="This is an image, created by **PlantUML**."}
377- --
378- -- to be referenced as @fig:pumlExample outside of the figure.
379- if block .identifier then
380- imgObj .identifier = block .identifier
381- end
382-
383- -- Finally, put the image inside an empty paragraph. By returning the
384- -- resulting paragraph object, the source code block gets replaced by
385- -- the image:
386- return pandoc .Para { imgObj }
358+ -- Create a new image for the document's structure. Attach the user's
359+ -- caption. Also use a hack (fig:) to enforce pandoc to create a
360+ -- figure i.e. attach a caption to the image.
361+ local imgObj = pandoc .Image (caption , fname , enableCaption )
362+
363+ -- Now, transfer the attribute "name" from the code block to the new
364+ -- image block. It might gets used by the figure numbering lua filter.
365+ -- If the figure numbering gets not used, this additional attribute
366+ -- gets ignored as well.
367+ if block .attributes [" name" ] then
368+ imgObj .attributes [" name" ] = block .attributes [" name" ]
387369 end
370+
371+ -- Transfer the identifier from the code block to the new image block
372+ -- to enable downstream filters like pandoc-crossref. This allows a figure
373+ -- block starting with:
374+ --
375+ -- ```{#fig:pumlExample .plantuml caption="This is an image, created by **PlantUML**."}
376+ --
377+ -- to be referenced as @fig:pumlExample outside of the figure.
378+ if block .identifier then
379+ imgObj .identifier = block .identifier
380+ end
381+
382+ -- Finally, put the image inside an empty paragraph. By returning the
383+ -- resulting paragraph object, the source code block gets replaced by
384+ -- the image:
385+ return pandoc .Para { imgObj }
386+ end
388387end
389388
390389-- Normally, pandoc will run the function in the built-in order Inlines ->
391390-- Blocks -> Meta -> Pandoc. We instead want Meta -> Blocks. Thus, we must
392391-- define our custom order:
393392return {
394- {Meta = Meta },
395- {CodeBlock = CodeBlock },
393+ {Meta = Meta },
394+ {CodeBlock = CodeBlock },
396395}
0 commit comments