aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2020-12-13 10:45:03 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2020-12-13 15:25:46 -0800
commitc43e2dc0f4ce639c8d0bd156afea2fd07ffc91ff (patch)
tree8310833f7c0ed4676ace291528b5ecaf97539235
parent32902d0fad22af823fa765603d22437580b4b5e2 (diff)
downloadpandoc-c43e2dc0f4ce639c8d0bd156afea2fd07ffc91ff.tar.gz
RST writer: better image handling.
- An image alone in its paragraph (but not a figure) is now rendered as an independent image, with an `alt` attribute if a description is supplied. - An inline image that is not alone in its paragraph will be rendered, as before, using a substitution. Such an image cannot have a "center", "left", or "right" alignment, so the classes `align-center`, `align-left`, or `align-right` are ignored. However, `align-top`, `align-middle`, `align-bottom` will generate a corresponding `align` attribute. Closes #6948.
-rw-r--r--src/Text/Pandoc/Writers/RST.hs30
-rw-r--r--test/command/4420.md11
-rw-r--r--test/command/6948.md31
3 files changed, 52 insertions, 20 deletions
diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs
index 43bf382b7..8beeef46a 100644
--- a/src/Text/Pandoc/Writers/RST.hs
+++ b/src/Text/Pandoc/Writers/RST.hs
@@ -143,9 +143,12 @@ pictToRST (label, (attr, src, _, mbtarget)) = do
let (_, cls, _) = attr
classes = case cls of
[] -> empty
- ["align-right"] -> ":align: right"
- ["align-left"] -> ":align: left"
- ["align-center"] -> ":align: center"
+ ["align-top"] -> ":align: top"
+ ["align-middle"] -> ":align: middle"
+ ["align-bottom"] -> ":align: bottom"
+ ["align-center"] -> empty
+ ["align-right"] -> empty
+ ["align-left"] -> empty
_ -> ":class: " <> literal (T.unwords cls)
return $ nowrap
$ ".. |" <> label' <> "| image:: " <> literal src $$ hang 3 empty (classes $$ dims)
@@ -215,19 +218,28 @@ blockToRST (Div (ident,classes,_kvs) bs) = do
nest 3 contents $$
blankline
blockToRST (Plain inlines) = inlineListToRST inlines
--- title beginning with fig: indicates that the image is a figure
-blockToRST (Para [Image attr txt (src,T.stripPrefix "fig:" -> Just tit)]) = do
- capt <- inlineListToRST txt
+blockToRST (Para [Image attr txt (src, rawtit)]) = do
+ description <- inlineListToRST txt
dims <- imageDimsToRST attr
- let fig = "figure:: " <> literal src
- alt = ":alt: " <> if T.null tit then capt else literal tit
+ -- title beginning with fig: indicates that the image is a figure
+ let (isfig, tit) = case T.stripPrefix "fig:" rawtit of
+ Nothing -> (False, rawtit)
+ Just tit' -> (True, tit')
+ let fig | isfig = "figure:: " <> literal src
+ | otherwise = "image:: " <> literal src
+ alt | isfig = ":alt: " <> if T.null tit then description else literal tit
+ | null txt = empty
+ | otherwise = ":alt: " <> description
+ capt | isfig = description
+ | otherwise = empty
(_,cls,_) = attr
classes = case cls of
[] -> empty
["align-right"] -> ":align: right"
["align-left"] -> ":align: left"
["align-center"] -> ":align: center"
- _ -> ":figclass: " <> literal (T.unwords cls)
+ _ | isfig -> ":figclass: " <> literal (T.unwords cls)
+ | otherwise -> ":class: " <> literal (T.unwords cls)
return $ hang 3 ".. " (fig $$ alt $$ classes $$ dims $+$ capt) $$ blankline
blockToRST (Para inlines)
| LineBreak `elem` inlines =
diff --git a/test/command/4420.md b/test/command/4420.md
index 3e7008935..36d697234 100644
--- a/test/command/4420.md
+++ b/test/command/4420.md
@@ -1,16 +1,5 @@
```
% pandoc -f native -t rst
-[Image ("",["align-right"],[("width","100px")]) [Str "image"] ("foo.png","")]
-^D
-|image|
-
-.. |image| image:: foo.png
- :align: right
- :width: 100px
-```
-
-```
-% pandoc -f native -t rst
[Para [Image ("",["align-right"],[("width","100px")]) [Str "image"] ("foo.png","fig:test")]]
^D
.. figure:: foo.png
diff --git a/test/command/6948.md b/test/command/6948.md
new file mode 100644
index 000000000..8803aebe9
--- /dev/null
+++ b/test/command/6948.md
@@ -0,0 +1,31 @@
+Treat an image alone in its paragraph (but not a figure)
+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
+```
+
+Here we just omit the center attribute as it's not valid:
+```
+% pandoc -f native -t rst
+[Para [Str "hi",Space,Image ("",["align-center"],[]) [Str "https://pandoc.org/diagram.jpg"] ("https://pandoc.org/diagram.jpg","")]]
+^D
+hi |https://pandoc.org/diagram.jpg|
+
+.. |https://pandoc.org/diagram.jpg| image:: https://pandoc.org/diagram.jpg
+```
+
+But we can use top, middle, or bottom alignment:
+```
+% pandoc -f native -t rst
+[Para [Str "hi",Space,Image ("",["align-top"],[]) [Str "https://pandoc.org/diagram.jpg"] ("https://pandoc.org/diagram.jpg","")]]
+^D
+hi |https://pandoc.org/diagram.jpg|
+
+.. |https://pandoc.org/diagram.jpg| image:: https://pandoc.org/diagram.jpg
+ :align: top
+```