Skip to content

Commit

Permalink
Markdown reader: use title of implicit figure as short caption.
Browse files Browse the repository at this point in the history
The title of an implicit figure, if set, is used as the short caption of
a figure. The short caption of a figure replaces the full caption in the
list of figures.

Closes: jgm#7915
  • Loading branch information
tarleb committed Feb 13, 2023
1 parent 351a8e6 commit debbae1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,9 @@ implicitFigure (ident, classes, attribs) capt url title =
_ -> capt
attribs' = filter ((/= "alt") . fst) attribs
figattr = (ident, mempty, mempty)
caption = B.simpleCaption $ B.plain capt
caption = if T.null title
then B.simpleCaption $ B.plain capt
else B.caption (Just . B.toList $ B.text title) $ B.plain capt
figbody = B.plain $ B.imageWith ("", classes, attribs') url title alt
in B.figureWith figattr caption figbody

Expand Down
11 changes: 6 additions & 5 deletions src/Text/Pandoc/Writers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -672,21 +672,22 @@ blockToMarkdown' opts (Figure figattr capt body) = do
let combinedAttr imgattr = case imgattr of
("", cls, kv) | (figid, [], []) <- figattr -> Just (figid, cls, kv)
_ -> Nothing
let combinedAlt alt = case capt of
let combinedAlt alt title = case capt of
Caption Nothing [] -> if null alt
then Just [Str "image"]
else Just alt
Caption Nothing [Plain captInlines]
| captInlines == alt -> Just captInlines
Caption short [Plain captInlines]
| captInlines == alt
, title == maybe "" stringify short -> Just captInlines
_ -> Nothing
case body of
[Plain [Image imgAttr alt (src, ttl)]]
| isEnabled Ext_implicit_figures opts
, Just descr <- combinedAlt alt
, Just descr <- combinedAlt alt ttl
, Just imgAttr' <- combinedAttr imgAttr
, isEnabled Ext_link_attributes opts || imgAttr' == nullAttr
-> do
-- use implicit figures if possible
-- Lossless representation as implicit figure is possible
let tgt' = (src, fromMaybe ttl $ T.stripPrefix "fig:" ttl)
contents <- inlineListToMarkdown opts [Image imgAttr' descr tgt']
return $ contents <> blankline
Expand Down
12 changes: 11 additions & 1 deletion test/testsuite.native
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,17 @@ Pandoc
]
, Figure
( "" , [] , [] )
(Caption Nothing [ Plain [ Str "lalune" ] ])
(Caption
(Just
[ Str "Voyage"
, Space
, Str "dans"
, Space
, Str "la"
, Space
, Str "Lune"
])
[ Plain [ Str "lalune" ] ])
[ Plain
[ Image
( "" , [] , [] )
Expand Down
2 changes: 1 addition & 1 deletion test/writer.context
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ or here: <http://example.com/>

From \quotation{Voyage dans la Lune} by Georges Melies (1902):

\startplacefigure[title={lalune}]
\startplacefigure[title={lalune},list={Voyage dans la Lune}]
{\externalfigure[lalune.jpg]}
\stopplacefigure

Expand Down
2 changes: 1 addition & 1 deletion test/writer.latex
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ From ``Voyage dans la Lune'' by Georges Melies (1902):
\begin{figure}
\centering
\includegraphics{lalune.jpg}
\caption{lalune}
\caption[Voyage dans la Lune]{lalune}
\end{figure}

Here is a movie \includegraphics{movie.jpg} icon.
Expand Down
12 changes: 11 additions & 1 deletion test/writer.native
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,17 @@ Pandoc
]
, Figure
( "" , [] , [] )
(Caption Nothing [ Plain [ Str "lalune" ] ])
(Caption
(Just
[ Str "Voyage"
, Space
, Str "dans"
, Space
, Str "la"
, Space
, Str "Lune"
])
[ Plain [ Str "lalune" ] ])
[ Plain
[ Image
( "" , [] , [] )
Expand Down

0 comments on commit debbae1

Please sign in to comment.