RST writer: fix figure handling.

This fixes a number of regressions from pandoc 2.x.

Properly handle caption, alt attribute in figures.

No longer treat a paragraph with a single image in it as a figure
(we have a dedicated Figure element now).

Closes #8930, closes #8871.
This commit is contained in:
John MacFarlane
2023-07-12 08:43:26 -07:00
parent 21b51d214d
commit 29c5afb305
5 changed files with 23 additions and 27 deletions
+13 -20
View File
@@ -262,21 +262,6 @@ blockToRST (Div (ident,classes,_kvs) bs) = do
nest 3 contents $$
blankline
blockToRST (Plain inlines) = inlineListToRST inlines
blockToRST (Para [Image attr txt (src, _)]) = do
description <- inlineListToRST txt
dims <- imageDimsToRST attr
let fig = "image:: " <> literal src
alt | null txt = empty
| otherwise = ":alt: " <> description
capt = empty
(_,cls,_) = attr
classes = case cls of
[] -> empty
["align-right"] -> ":align: right"
["align-left"] -> ":align: left"
["align-center"] -> ":align: center"
_ -> ":class: " <> literal (T.unwords cls)
return $ hang 3 ".. " (fig $$ alt $$ classes $$ dims $+$ capt) $$ blankline
blockToRST (Para inlines)
| LineBreak `elem` inlines =
linesToLineBlock $ splitBy (==LineBreak) inlines
@@ -394,13 +379,21 @@ blockToRST (DefinitionList items) = do
-- ensure that sublists have preceding blank line
return $ blankline $$ vcat contents $$ blankline
blockToRST (Figure (ident, classes, _) _ body) = do
blockToRST (Figure (ident, classes, _kvs)
(Caption _ longCapt) body) = do
let figure attr txt (src, tit) = do
description <- inlineListToRST txt
capt <- blockListToRST longCapt
dims <- imageDimsToRST attr
let fig = "figure:: " <> literal src
alt = ":alt: " <> if T.null tit then description else literal tit
capt = description
let fig = "figure::" <+> literal src
alt = if null txt
then if T.null tit
then empty
else ":alt:" <+> literal tit
else ":alt:" <+> description
name = if T.null ident
then empty
else "name:" <+> literal ident
(_,cls,_) = attr
align = case cls of
[] -> empty
@@ -408,7 +401,7 @@ blockToRST (Figure (ident, classes, _) _ body) = do
["align-left"] -> ":align: left"
["align-center"] -> ":align: center"
_ -> ":figclass: " <> literal (T.unwords cls)
return $ hang 3 ".. " (fig $$ alt $$ align $$ dims $+$ capt)
return $ hang 3 ".. " (fig $$ name $$ alt $$ align $$ dims $+$ capt)
$$ blankline
case body of
[Para [Image attr txt tgt]] -> figure attr txt tgt
+3 -3
View File
@@ -2,8 +2,8 @@
% pandoc -f native -t rst
[Para [Image ("",["align-right"],[("width","100px")]) [Str "image"] ("foo.png","fig:test")]]
^D
.. image:: foo.png
:alt: image
:align: right
|image|
.. |image| image:: foo.png
:width: 100px
```
+3 -3
View File
@@ -4,9 +4,9 @@ as an independent image:
% pandoc -f native -t rst
[Para [Image ("",["align-center"],[]) [Str "https://pandoc.org/diagram.jpg"] ("https://pandoc.org/diagram.jpg","")]]
^D
.. image:: https://pandoc.org/diagram.jpg
:alt: https://pandoc.org/diagram.jpg
:align: center
|https://pandoc.org/diagram.jpg|
.. |https://pandoc.org/diagram.jpg| image:: https://pandoc.org/diagram.jpg
```
Here we just omit the center attribute as it's not valid:
+3
View File
@@ -6,5 +6,8 @@ Figure float with caption at the figure level.
^D
.. figure:: foo.png
name: fig-id
:alt: fig:
Caption
```
+1 -1
View File
@@ -784,7 +784,7 @@ Images
From “Voyage dans la Lune” by Georges Melies (1902):
.. figure:: lalune.jpg
:alt: Voyage dans la Lune
:alt: lalune
lalune