RST reader: parse :alt: on figure.

Also give a better default if `alt` is not specified, using
the stringified caption rather than the filename.

Partially addresses #11140.

Add failing test case for Markdown writer issue in #11140.
This commit is contained in:
John MacFarlane
2025-09-15 10:50:04 +02:00
parent e2f088f4e5
commit 9c36d884d5
5 changed files with 85 additions and 6 deletions
+2 -1
View File
@@ -855,8 +855,9 @@ directive' = do
let figcls = concatMap (T.words . snd) figclasskv
let figattr = ("", figcls ++ aligncls, [])
let capt = B.caption Nothing (B.plain caption <> legend)
let alt = maybe caption (B.text . trim) (lookup "alt" fields)
return $ B.figureWith figattr capt $
B.plain (B.imageWith (imgident, imgcls, imgkvs) src "" (B.text src))
B.plain (B.imageWith (imgident, imgcls, imgkvs) src "" alt)
"image" -> do
let src = escapeURI $ trim top
let alt = B.str $ maybe "image" trim $ lookup "alt" fields
+52
View File
@@ -0,0 +1,52 @@
```
% pandoc -f rst -t native
.. figure:: image.png
:alt: my alt
Caption
^D
[ Figure
( "" , [] , [] )
(Caption Nothing [ Plain [ Str "Caption" ] ])
[ Plain
[ Image
( "" , [] , [] )
[ Str "my" , Space , Str "alt" ]
( "image.png" , "" )
]
]
]
```
```
% pandoc -f rst -t native
.. figure:: image.png
Caption
^D
[ Figure
( "" , [] , [] )
(Caption Nothing [ Plain [ Str "Caption" ] ])
[ Plain
[ Image
( "" , [] , [] ) [ Str "Caption" ] ( "image.png" , "" )
]
]
]
```
```
% pandoc -f native -t markdown
[ Figure
( "" , [] , [] )
(Caption
Nothing [ Plain [ Str "Caption" ] ])
[ Plain
[ Image ( "" , [] , [] ) [ Str "my", Space, Str "alt" ] ( "image.png" , "" )
]
]
]
^D
![Caption](image.png){alt="my alt"}
```
+22 -1
View File
@@ -37,7 +37,28 @@
[ Plain
[ Image
( "test" , [] , [ ( "width" , "1in" ) ] )
[ Str "img1.jpg" ]
[ Str "The"
, Space
, Str "caption."
, Space
, Str "Here's"
, Space
, Str "what"
, Space
, Str "piggybacking"
, Space
, Str "on"
, Space
, Str "caption"
, Space
, Str "would"
, Space
, Str "look"
, Space
, Str "like"
, Space
, Str "{#fig:1}"
]
( "img1.jpg" , "" )
]
]
+8 -1
View File
@@ -24,7 +24,14 @@
[ Plain
[ Image
( "" , [] , [ ( "width" , "1in" ) ] )
[ Str "example.png" ]
[ Str "This"
, Space
, Str "is"
, Space
, Str "a"
, Space
, Str "caption."
]
( "example.png" , "" )
]
]
+1 -3
View File
@@ -11,9 +11,7 @@
(Caption Nothing [ Plain [ Str "capt" ] ])
[ Plain
[ Image
( "" , [] , [] )
[ Str "img/dummy.png" ]
( "img/dummy.png" , "" )
( "" , [] , [] ) [ Str "capt" ] ( "img/dummy.png" , "" )
]
]
]