Skip to content

Commit edb7ec3

Browse files
committed
diagram-generator: use Figure constructor in pandoc 3 and later
Fixes: #261
1 parent a22c866 commit edb7ec3

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

diagram-generator/diagram-generator.lua

+47-31
Original file line numberDiff line numberDiff line change
@@ -353,37 +353,53 @@ function CodeBlock(block)
353353

354354
-- If the user defines a caption, read it as Markdown.
355355
local caption = block.attributes.caption
356-
and pandoc.read(block.attributes.caption).blocks[1].content
357-
or {}
358-
359-
-- A non-empty caption means that this image is a figure. We have to
360-
-- set the image title to "fig:" for pandoc to treat it as such.
361-
local title = #caption > 0 and "fig:" or ""
362-
363-
-- Transfer identifier and other relevant attributes from the code
364-
-- block to the image. The `name` is kept as an attribute.
365-
-- This allows a figure block starting with:
366-
--
367-
-- ```{#fig:example .plantuml caption="Image created by **PlantUML**."}
368-
--
369-
-- to be referenced as @fig:example outside of the figure when used
370-
-- with `pandoc-crossref`.
371-
local img_attr = {
372-
id = block.identifier,
373-
name = block.attributes.name,
374-
width = block.attributes.width,
375-
height = block.attributes.height
376-
}
377-
378-
-- Create a new image for the document's structure. Attach the user's
379-
-- caption. Also use a hack (fig:) to enforce pandoc to create a
380-
-- figure i.e. attach a caption to the image.
381-
local img_obj = pandoc.Image(caption, fname, title, img_attr)
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{ img_obj }
356+
and pandoc.read(block.attributes.caption).blocks
357+
or pandoc.Blocks{}
358+
local alt = pandoc.utils.blocks_to_inlines(caption)
359+
360+
if PANDOC_VERSION < 3 then
361+
-- A non-empty caption means that this image is a figure. We have to
362+
-- set the image title to "fig:" for pandoc to treat it as such.
363+
local title = #caption > 0 and "fig:" or ""
364+
365+
-- Transfer identifier and other relevant attributes from the code
366+
-- block to the image. The `name` is kept as an attribute.
367+
-- This allows a figure block starting with:
368+
--
369+
-- ```{#fig:example .plantuml caption="Image created by **PlantUML**."}
370+
--
371+
-- to be referenced as @fig:example outside of the figure when used
372+
-- with `pandoc-crossref`.
373+
local img_attr = {
374+
id = block.identifier,
375+
name = block.attributes.name,
376+
width = block.attributes.width,
377+
height = block.attributes.height
378+
}
379+
380+
-- Create a new image for the document's structure. Attach the user's
381+
-- caption. Also use a hack (fig:) to enforce pandoc to create a
382+
-- figure i.e. attach a caption to the image.
383+
local img_obj = pandoc.Image(alt, fname, title, img_attr)
384+
385+
-- Finally, put the image inside an empty paragraph. By returning the
386+
-- resulting paragraph object, the source code block gets replaced by
387+
-- the image:
388+
return pandoc.Para{ img_obj }
389+
else
390+
local fig_attr = {
391+
id = block.identifier,
392+
name = block.attributes.name,
393+
}
394+
local img_attr = {
395+
width = block.attributes.width,
396+
height = block.attributes.height,
397+
}
398+
local img_obj = pandoc.Image(alt, fname, "", img_attr)
399+
400+
-- Create a figure that contains just this image.
401+
return pandoc.Figure(pandoc.Plain{img_obj}, caption, fig_attr)
402+
end
387403
end
388404

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

0 commit comments

Comments
 (0)