git.iko.soy/typeable/pandoc
on master help sign in
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.

commitc43e2dc0f4ce639c8d0bd156afea2fd07ffc91ff authorJohn MacFarlane <jgm@berkeley.edu> dateSun 13 Dec 2020, 10:45 (6 years ago) parent32902d0
3 files changed+52−20
src/Text/Pandoc/Writers/RST.hs+21−9 test/command/4420.md−11 test/command/6948.md+31
src/Text/Pandoc/Writers/RST.hs+21−9
@@ -143,9 +143,12 @@ pictToRST (label, (attr, src, _, mbtarget)) = do
143143 let (_, cls, _) = attr
144144 classes = case cls of
145145 [] -> empty
146− ["align-right"] -> ":align: right"
147− ["align-left"] -> ":align: left"
148− ["align-center"] -> ":align: center"
146+ ["align-top"] -> ":align: top"
147+ ["align-middle"] -> ":align: middle"
148+ ["align-bottom"] -> ":align: bottom"
149+ ["align-center"] -> empty
150+ ["align-right"] -> empty
151+ ["align-left"] -> empty
149152 _ -> ":class: " <> literal (T.unwords cls)
150153 return $ nowrap
151154 $ ".. |" <> label' <> "| image:: " <> literal src $$ hang 3 empty (classes $$ dims)
@@ -215,19 +218,28 @@ blockToRST (Div (ident,classes,_kvs) bs) = do
215218 nest 3 contents $$
216219 blankline
217220blockToRST (Plain inlines) = inlineListToRST inlines
218−-- title beginning with fig: indicates that the image is a figure
219−blockToRST (Para [Image attr txt (src,T.stripPrefix "fig:" -> Just tit)]) = do
220− capt <- inlineListToRST txt
221+blockToRST (Para [Image attr txt (src, rawtit)]) = do
222+ description <- inlineListToRST txt
221223 dims <- imageDimsToRST attr
222− let fig = "figure:: " <> literal src
223− alt = ":alt: " <> if T.null tit then capt else literal tit
224+ -- title beginning with fig: indicates that the image is a figure
225+ let (isfig, tit) = case T.stripPrefix "fig:" rawtit of
226+ Nothing -> (False, rawtit)
227+ Just tit' -> (True, tit')
228+ let fig | isfig = "figure:: " <> literal src
229+ | otherwise = "image:: " <> literal src
230+ alt | isfig = ":alt: " <> if T.null tit then description else literal tit
231+ | null txt = empty
232+ | otherwise = ":alt: " <> description
233+ capt | isfig = description
234+ | otherwise = empty
224235 (_,cls,_) = attr
225236 classes = case cls of
226237 [] -> empty
227238 ["align-right"] -> ":align: right"
228239 ["align-left"] -> ":align: left"
229240 ["align-center"] -> ":align: center"
230− _ -> ":figclass: " <> literal (T.unwords cls)
241+ _ | isfig -> ":figclass: " <> literal (T.unwords cls)
242+ | otherwise -> ":class: " <> literal (T.unwords cls)
231243 return $ hang 3 ".. " (fig $$ alt $$ classes $$ dims $+$ capt) $$ blankline
232244blockToRST (Para inlines)
233245 | LineBreak `elem` inlines =
test/command/4420.md−11
@@ -1,14 +1,3 @@
1−```
2−% pandoc -f native -t rst
3−[Image ("",["align-right"],[("width","100px")]) [Str "image"] ("foo.png","")]
4−^D
5−|image|
6−
7−.. |image| image:: foo.png
8− :align: right
9− :width: 100px
10−```
11−
121```
132% pandoc -f native -t rst
143[Para [Image ("",["align-right"],[("width","100px")]) [Str "image"] ("foo.png","fig:test")]]
test/command/6948.mdnew file+31
@@ -0,0 +1,31 @@
1+Treat an image alone in its paragraph (but not a figure)
2+as an independent image:
3+```
4+% pandoc -f native -t rst
5+[Para [Image ("",["align-center"],[]) [Str "https://pandoc.org/diagram.jpg"] ("https://pandoc.org/diagram.jpg","")]]
6+^D
7+.. image:: https://pandoc.org/diagram.jpg
8+ :alt: https://pandoc.org/diagram.jpg
9+ :align: center
10+```
11+
12+Here we just omit the center attribute as it's not valid:
13+```
14+% pandoc -f native -t rst
15+[Para [Str "hi",Space,Image ("",["align-center"],[]) [Str "https://pandoc.org/diagram.jpg"] ("https://pandoc.org/diagram.jpg","")]]
16+^D
17+hi |https://pandoc.org/diagram.jpg|
18+
19+.. |https://pandoc.org/diagram.jpg| image:: https://pandoc.org/diagram.jpg
20+```
21+
22+But we can use top, middle, or bottom alignment:
23+```
24+% pandoc -f native -t rst
25+[Para [Str "hi",Space,Image ("",["align-top"],[]) [Str "https://pandoc.org/diagram.jpg"] ("https://pandoc.org/diagram.jpg","")]]
26+^D
27+hi |https://pandoc.org/diagram.jpg|
28+
29+.. |https://pandoc.org/diagram.jpg| image:: https://pandoc.org/diagram.jpg
30+ :align: top
31+```