Skip to content

Commit

Permalink
feat(pandocast): Support Pandoc 3.1+ implicit figures (Pandoc types 1…
Browse files Browse the repository at this point in the history
….23+)
  • Loading branch information
Omikhleia authored and Didier Willis committed Sep 28, 2024
1 parent ebe6796 commit 98b6ef4
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions inputters/pandocast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ local createCommand, createStructuredCommand
= SU.ast.createCommand, SU.ast.createStructuredCommand

local Pandoc = {
API_VERSION = { 1, 22, 0 } -- Supported API version (semver)
MIN_API_VERSION = { 1, 22, 0 }, -- Minimal supported API version (semver)
CUR_API_VERSION = { 1, 23, 0 } -- Current supported API version (semver)
}

local function checkAstSemver(version)
-- We shouldn't care the patch level.
-- The Pandoc AST may change upon "minor" updates, though.
local major, minor = table.unpack(version)
local expected_major, expected_minor = table.unpack(Pandoc.API_VERSION)
if not major or major ~= expected_major then
local min_major, min_minor = table.unpack(Pandoc.MIN_API_VERSION)
local cur_major, cur_minor = table.unpack(Pandoc.CUR_API_VERSION)
if not major or major ~= min_major then
SU.error("Unsupported Pandoc AST major version " .. major
.. ", only version " .. expected_major.. " is supported")
.. ", only version " .. min_major.. " is supported")
end
if not minor or minor < expected_minor then
if not minor or minor < min_minor then
SU.error("Unsupported Pandoc AST version " .. table.concat(version, ".")
.. ", needing at least " .. table.concat(Pandoc.API_VERSION, "."))
.. ", needing at least " .. table.concat(Pandoc.MIN_API_VERSION, "."))
end
if minor ~= expected_minor then
if minor > cur_minor then
-- Warn and pray.
-- IMPLEMENTATON NOTE: Kept simple for now.
-- When this occurs, we may check to properly handle version updates
SU.warn("Pandoc AST version " .. table.concat(version, ".")
.. ", is more recent than supported " .. table.concat(Pandoc.API_VERSION, ".")
.. ", is more recent than supported " .. table.concat(Pandoc.CUR_API_VERSION, ".")
.. ", there could be issues.")
end
end
Expand Down Expand Up @@ -81,6 +83,7 @@ local HasMultipleArgs = {
RawInline = true,
RawBlock = true,
Table = true,
Figure = true,
}

-- Parser AST-walking logic.
Expand Down Expand Up @@ -415,6 +418,40 @@ function Renderer:Table (_, caption, colspecs, thead, tbodies, tfoot)
return createStructuredCommand("markdown:internal:captioned-table", {}, captioned)
end

-- Figure Attr Caption [Block]
-- Added in Pandoc types 1.23, and now used when "implicit_figures" is enabled.
-- (This is the case by default in Pandoc >= 3.1)
function Renderer:Figure (attributes, caption, blocks)
local options = pandocAttributes(attributes)
-- Caption (Maybe ShortCaption) [Block]
-- ShortCaption is either null or [Inline]
local captionContent = self:render(caption[#caption])
local content = self:render(blocks)

-- More weirdness that expected? Can't say, but it's surely an "ad-hoc" choice:
-- Considering ![caption](image){#id .class key=value}
-- - The id makes is promulgated to the Figure
-- - While other classes and key-values are left on the Image.
-- (Checked with Pandoc 3.4 on ![caption](image){#fig .doh width="1cm"})
if options.id then
-- In our case, we move the id to the caption, which may be be the numbered
-- element for use in cross-references...
local id = options.id
options.id = nil
return createStructuredCommand("markdown:internal:captioned-figure", options, {
content,
createCommand("caption", {}, {
createCommand("label", { marker = id }),
captionContent
})
})
end
return createStructuredCommand("markdown:internal:captioned-figure", options, {
content,
createCommand("caption", {}, captionContent)
})
end

-- PANDOC AST INLINES

-- Str Text
Expand Down

0 comments on commit 98b6ef4

Please sign in to comment.