Skip to content

Commit 14651cc

Browse files
smancilltarleb
authored andcommitted
table-short-captions: fix support for Pandod 2.10+
Commit 6e1b2cc (table-short-captions: add support for pandoc 2.10, 2020-07-15) doesn't work because it treats the new `Table.caption.long` object as the pre 2.10 `Table.caption` object, but they are completely different lists. `Table.caption.long` is a list of `Blocks`, where each block has a `content` key which is a `List` of inlines (or it should be, a test would be needed for more complex captions?). The pre 2.10 `Table.caption` is a `List` of inlines, so they cannot share the same code to be manipulated. Fix by just splitting into two different functions. They do follow the same structure, but it is better to keep them separated, otherwise it would require too many ifs, or code that is not clear. For pre 2.10 `Table.caption` just keep the code as it is. For the new `Table.caption.long`, ensure that the outer list of `Blocks` is taken into account when searching for the inline `Span` with the short caption. Now all tests pass again.
1 parent fa30121 commit 14651cc

File tree

1 file changed

+72
-19
lines changed

1 file changed

+72
-19
lines changed

table-short-captions/table-short-captions.lua

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,24 @@ local function parse_table_attrs(attr)
9292
return label, short_caption, unlisted
9393
end
9494

95-
--- Wraps a table with shortcaption code
95+
function is_properties_span(inl)
96+
return (inl.t) and (inl.t == "Span") -- is span
97+
and (inl.content) and (#inl.content == 0) -- is empty span
98+
end
99+
100+
--- Parse the caption for Pandoc < 2.10
96101
-- @tparam Table tbl : The table with {}-wrapped properties in the caption
97-
-- @treturn List[Blocks] : The table with {label} in the caption,
98-
-- optionally wrapped in shortcaption code
99-
function rewrite_longtable_caption(tbl)
100-
local caption
101-
if PANDOC_VERSION >= {2,10} then
102-
caption = pandoc.List(tbl.caption.long)
103-
else
104-
caption = tbl.caption
105-
end
102+
-- @treturn ?string : The "short-caption" property, if present.
103+
-- @treturn bool : Whether ".unlisted" appeared in the classes
104+
function parse_short_caption_legacy(tbl)
105+
local caption = tbl.caption
106+
106107
-- Escape if there is no caption present.
107108
if not caption or #caption == 0 then
108109
return nil
109110
end
110111

111112
-- Try find the properties block
112-
local is_properties_span = function (inl)
113-
return (inl.t) and (inl.t == "Span") -- is span
114-
and (inl.content) and (#inl.content == 0) -- is empty span
115-
end
116113
local propspan, idx = caption:find_if(is_properties_span)
117114

118115
-- If we couldn't find properties, escape.
@@ -132,13 +129,69 @@ function rewrite_longtable_caption(tbl)
132129
end
133130

134131
-- set new caption
132+
tbl.caption = caption
133+
134+
return short_caption, unlisted
135+
end
136+
137+
--- Parse the caption for Pandoc >= 2.10
138+
-- @tparam Table tbl : The table with {}-wrapped properties in the caption
139+
-- @treturn ?string : The "short-caption" property, if present.
140+
-- @treturn bool : Whether ".unlisted" appeared in the classes
141+
function parse_short_caption(tbl)
142+
local caption = pandoc.List(tbl.caption.long)
143+
144+
-- Escape if there is no caption present.
145+
if #caption == 0 then
146+
return nil
147+
end
148+
149+
-- Try find the properties block
150+
local find_properties = function (list)
151+
for bidx, block in ipairs(list) do
152+
local propspan, idx = block.content:find_if(is_properties_span)
153+
if propspan then
154+
return propspan, bidx, idx
155+
end
156+
end
157+
end
158+
local propspan, bidx, idx = find_properties(caption)
159+
160+
-- If we couldn't find properties, escape.
161+
if not propspan then
162+
return nil
163+
end
164+
165+
-- Otherwise, parse it all
166+
local label, short_caption, unlisted = parse_table_attrs(propspan.attr)
167+
168+
-- Excise the span from the caption
169+
caption[bidx].content[idx] = nil
170+
171+
-- Put label back into caption for pandoc-crossref
172+
if label then
173+
caption[bidx].content:extend {pandoc.Str("{#"..label.."}")}
174+
end
175+
176+
-- set new caption
177+
tbl.caption.long = caption
178+
tbl.caption.short = short_caption
179+
and pandoc.read(short_caption, FORMAT).blocks[1].content
180+
or nil
181+
182+
return short_caption, unlisted
183+
end
184+
185+
--- Wraps a table with shortcaption code
186+
-- @tparam Table tbl : The table with {}-wrapped properties in the caption
187+
-- @treturn List[Blocks] : The table with {label} in the caption,
188+
-- optionally wrapped in shortcaption code
189+
function rewrite_longtable_caption(tbl)
190+
local short_caption, unlisted
135191
if PANDOC_VERSION >= {2,10} then
136-
tbl.caption.long = caption
137-
tbl.caption.short = short_caption
138-
and pandoc.read(short_caption, FORMAT).blocks[1].content
139-
or nil
192+
short_caption, unlisted = parse_short_caption(tbl)
140193
else
141-
tbl.caption = caption
194+
short_caption, unlisted = parse_short_caption_legacy(tbl)
142195
end
143196

144197
-- Place new table

0 commit comments

Comments
 (0)