git.iko.soy/typeable/pandoc
on master help sign in
RST writer: better image handling.
c43e2dcJohn MacFarlane
src/Text/Pandoc/Writers/RST.hs+21−9
@@ -143,9 +143,12 @@ pictToRST (label, (attr, src, _, mbtarget)) = do
143 let (_, cls, _) = attr
143 let (_, cls, _) = attr
144 classes = case cls of
144 classes = case cls of
145 [] -> empty
145 [] -> empty
146− ["align-right"] -> ":align: right"
146+ ["align-top"] -> ":align: top"
147+ ["align-middle"] -> ":align: middle"
148+ ["align-bottom"] -> ":align: bottom"
149+ ["align-center"] -> empty
150+ ["align-right"] -> empty
147− ["align-left"] -> ":align: left"
151+ ["align-left"] -> empty
148− ["align-center"] -> ":align: center"
149 _ -> ":class: " <> literal (T.unwords cls)
152 _ -> ":class: " <> literal (T.unwords cls)
150 return $ nowrap
153 return $ nowrap
151 $ ".. |" <> label' <> "| image:: " <> literal src $$ hang 3 empty (classes $$ dims)
154 $ ".. |" <> label' <> "| image:: " <> literal src $$ hang 3 empty (classes $$ dims)
@@ -215,19 +218,28 @@ blockToRST (Div (ident,classes,_kvs) bs) = do
215 nest 3 contents $$
218 nest 3 contents $$
216 blankline
219 blankline
217blockToRST (Plain inlines) = inlineListToRST inlines
220blockToRST (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
221+blockToRST (Para [Image attr txt (src, rawtit)]) = do
220− capt <- inlineListToRST txt
222+ description <- inlineListToRST txt
221 dims <- imageDimsToRST attr
223 dims <- imageDimsToRST attr
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')
222− let fig = "figure:: " <> literal src
228+ let fig | isfig = "figure:: " <> literal src
229+ | otherwise = "image:: " <> literal src
223− alt = ":alt: " <> if T.null tit then capt else literal tit
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
224 (_,cls,_) = attr
235 (_,cls,_) = attr
225 classes = case cls of
236 classes = case cls of
226 [] -> empty
237 [] -> empty
227 ["align-right"] -> ":align: right"
238 ["align-right"] -> ":align: right"
228 ["align-left"] -> ":align: left"
239 ["align-left"] -> ":align: left"
229 ["align-center"] -> ":align: center"
240 ["align-center"] -> ":align: center"
230− _ -> ":figclass: " <> literal (T.unwords cls)
241+ _ | isfig -> ":figclass: " <> literal (T.unwords cls)
242+ | otherwise -> ":class: " <> literal (T.unwords cls)
231 return $ hang 3 ".. " (fig $$ alt $$ classes $$ dims $+$ capt) $$ blankline
243 return $ hang 3 ".. " (fig $$ alt $$ classes $$ dims $+$ capt) $$ blankline
232blockToRST (Para inlines)
244blockToRST (Para inlines)
233 | LineBreak `elem` inlines =
245 | 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−
12```
1```
13% pandoc -f native -t rst
2% pandoc -f native -t rst
14[Para [Image ("",["align-right"],[("width","100px")]) [Str "image"] ("foo.png","fig:test")]]
3[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+```