diff options
| author | John MacFarlane <[email protected]> | 2023-07-12 08:43:26 -0700 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2023-07-12 09:13:32 -0700 |
| commit | 29c5afb305c32129db4e81cd489cd3bd2a14c2e9 (patch) | |
| tree | f374ef2ee808802dee471d0a218fdfbe91e6fded | |
| parent | 21b51d214d10062fd3f128814832beb12d11e804 (diff) | |
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.
| -rw-r--r-- | src/Text/Pandoc/Writers/RST.hs | 33 | ||||
| -rw-r--r-- | test/command/4420.md | 6 | ||||
| -rw-r--r-- | test/command/6948.md | 6 | ||||
| -rw-r--r-- | test/command/figures-rst.md | 3 | ||||
| -rw-r--r-- | test/writer.rst | 2 |
5 files changed, 23 insertions, 27 deletions
diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index a9ee4694d..8d6c47fc3 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -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 diff --git a/test/command/4420.md b/test/command/4420.md index 9d809a106..11eaabd81 100644 --- a/test/command/4420.md +++ b/test/command/4420.md @@ -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 ``` diff --git a/test/command/6948.md b/test/command/6948.md index 8803aebe9..844ef7f96 100644 --- a/test/command/6948.md +++ b/test/command/6948.md @@ -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: diff --git a/test/command/figures-rst.md b/test/command/figures-rst.md index fac1145ad..a5f22daa1 100644 --- a/test/command/figures-rst.md +++ b/test/command/figures-rst.md @@ -6,5 +6,8 @@ Figure float with caption at the figure level. ^D .. figure:: foo.png + name: fig-id :alt: fig: + + Caption ``` diff --git a/test/writer.rst b/test/writer.rst index d4a0f7335..17fa7f419 100644 --- a/test/writer.rst +++ b/test/writer.rst @@ -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 |
