Skip to content

Commit 44bfd2d

Browse files
committed
diagram-generator: shorten and clarify CodeBlock handler
1 parent 963a197 commit 44bfd2d

File tree

1 file changed

+46
-61
lines changed

1 file changed

+46
-61
lines changed

diagram-generator/diagram-generator.lua

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ local function convert_with_inkscape(filetype)
162162
pdf_file,
163163
outfile
164164
)
165-
io.stderr:write(inkscape_command .. '\n')
166165
local command_output = io.popen(inkscape_command)
167166
-- TODO: print output when debugging.
168167
command_output:close()
@@ -300,9 +299,6 @@ end
300299

301300
-- Executes each document's code block to find matching code blocks:
302301
function CodeBlock(block)
303-
-- Predefine a potential image:
304-
local fname = nil
305-
306302
-- Using a table with all known generators i.e. converters:
307303
local converters = {
308304
plantuml = plantuml,
@@ -323,67 +319,56 @@ function CodeBlock(block)
323319
local success, img = pcall(img_converter, block.text,
324320
filetype, block.attributes["additionalPackages"] or nil)
325321

326-
-- Was ok?
327-
if success and img then
328-
-- Hash the figure name and content:
329-
fname = pandoc.sha1(img) .. "." .. filetype
330-
331-
-- Store the data in the media bag:
332-
pandoc.mediabag.insert(fname, mimetype, img)
333-
334-
else
335-
336-
-- an error occured; img contains the error message
337-
io.stderr:write(tostring(img))
322+
-- Bail if an error occured; img contains the error message when that
323+
-- happens.
324+
if not (success and img) then
325+
io.stderr:write(tostring(img or "no image data has been returned."))
338326
io.stderr:write('\n')
339327
error 'Image conversion failed. Aborting.'
340-
341328
end
342329

343-
-- Case: This code block was an image e.g. PlantUML or dot/Graphviz, etc.:
344-
if fname then
345-
346-
-- Define the default caption:
347-
local caption = {}
348-
local enableCaption = nil
349-
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
353-
354-
-- This is pandoc's current hack to enforce a caption:
355-
enableCaption = "fig:"
356-
end
357-
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"]
369-
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
330+
-- If we got here, then the transformation went ok and `img` contains
331+
-- the image data.
332+
333+
-- Create figure name by hashing the image content
334+
local fname = pandoc.sha1(img) .. "." .. filetype
335+
336+
-- Store the data in the media bag:
337+
pandoc.mediabag.insert(fname, mimetype, img)
338+
339+
local enable_caption = nil
340+
341+
-- If the user defines a caption, read it as Markdown.
342+
local caption = block.attributes.caption
343+
and pandoc.read(block.attributes.caption).blocks[1].content
344+
or {}
345+
346+
-- A non-empty caption means that this image is a figure. We have to
347+
-- set the image title to "fig:" for pandoc to treat it as such.
348+
local title = #caption > 0 and "fig:" or ""
349+
350+
-- Transfer identifier and other relevant attributes from the code
351+
-- block to the image. Currently, only `name` is kept as an attribute.
352+
-- This allows a figure block starting with:
353+
--
354+
-- ```{#fig:example .plantuml caption="Image created by **PlantUML**."}
355+
--
356+
-- to be referenced as @fig:example outside of the figure when used
357+
-- with `pandoc-crossref`.
358+
local img_attr = pandoc.Attr {
359+
id = block.identifier,
360+
name = block.attributes.name
361+
}
362+
363+
-- Create a new image for the document's structure. Attach the user's
364+
-- caption. Also use a hack (fig:) to enforce pandoc to create a
365+
-- figure i.e. attach a caption to the image.
366+
local img_obj = pandoc.Image(caption, fname, title, img_attr)
367+
368+
-- Finally, put the image inside an empty paragraph. By returning the
369+
-- resulting paragraph object, the source code block gets replaced by
370+
-- the image:
371+
return pandoc.Para{ img_obj }
387372
end
388373

389374
-- Normally, pandoc will run the function in the built-in order Inlines ->

0 commit comments

Comments
 (0)