From 30644b291b7a23c4e41b69611a8ee5c8d386c3c3 Mon Sep 17 00:00:00 2001
From: mb21 <mb21@users.noreply.github.com>
Date: Thu, 3 Dec 2015 11:50:54 +0100
Subject: [PATCH] RST reader: image attributes

---
 src/Text/Pandoc/Readers/RST.hs | 21 ++++++++++++++-------
 tests/rst-reader.native        |  2 +-
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs
index 85f34d9d8..7be0cd392 100644
--- a/src/Text/Pandoc/Readers/RST.hs
+++ b/src/Text/Pandoc/Readers/RST.hs
@@ -541,6 +541,12 @@ directive' = do
   body <- option "" $ try $ blanklines >> indentedBlock
   optional blanklines
   let body' = body ++ "\n\n"
+      imgAttr cl = ("", classes, getAtt "width" ++ getAtt "height")
+        where
+          classes = words $ maybe "" trim $ lookup cl fields
+          getAtt k = case lookup k fields of
+                       Just v  -> [(k, filter (not . isSpace) v)]
+                       Nothing -> []
   case label of
         "raw" -> return $ B.rawBlock (trim top) (stripTrailingNewlines body)
         "role" -> addNewRole top $ map (\(k,v) -> (k, trim v)) fields
@@ -590,15 +596,16 @@ directive' = do
         "figure" -> do
            (caption, legend) <- parseFromString extractCaption body'
            let src = escapeURI $ trim top
-           return $ B.para (B.image src "fig:" caption) <> legend
+           return $ B.para (B.imageWith (imgAttr "figclass") src "fig:" caption) <> legend
         "image" -> do
            let src = escapeURI $ trim top
            let alt = B.str $ maybe "image" trim $ lookup "alt" fields
+           let attr = imgAttr "class"
            return $ B.para
                   $ case lookup "target" fields of
                           Just t  -> B.link (escapeURI $ trim t) ""
-                                     $ B.image src "" alt
-                          Nothing -> B.image src "" alt
+                                     $ B.imageWith attr src "" alt
+                          Nothing -> B.imageWith attr src "" alt
         "class" -> do
             let attrs = ("", (splitBy isSpace $ trim top), map (\(k,v) -> (k, trimr v)) fields)
             --  directive content or the first immediately following element
@@ -812,10 +819,10 @@ substKey = try $ do
   res <- B.toList <$> directive'
   il <- case res of
              -- use alt unless :alt: attribute on image:
-             [Para [Image _ [Str "image"] (src,tit)]] ->
-                return $ B.image src tit alt
-             [Para [Link _ [Image _ [Str "image"] (src,tit)] (src',tit')]] ->
-                return $ B.link src' tit' (B.image src tit alt)
+             [Para [Image attr [Str "image"] (src,tit)]] ->
+                return $ B.imageWith attr src tit alt
+             [Para [Link _ [Image attr [Str "image"] (src,tit)] (src',tit')]] ->
+                return $ B.link src' tit' (B.imageWith attr src tit alt)
              [Para ils] -> return $ B.fromList ils
              _          -> mzero
   let key = toKey $ stripFirstAndLast ref
diff --git a/tests/rst-reader.native b/tests/rst-reader.native
index 0178f28a8..4752d76ff 100644
--- a/tests/rst-reader.native
+++ b/tests/rst-reader.native
@@ -221,7 +221,7 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
 ,Header 1 ("images",[],[]) [Str "Images"]
 ,Para [Str "From",Space,Quoted DoubleQuote [Str "Voyage",Space,Str "dans",Space,Str "la",Space,Str "Lune"],Space,Str "by",Space,Str "Georges",Space,Str "Melies",Space,Str "(1902):"]
 ,Para [Image ("",[],[]) [Str "image"] ("lalune.jpg","")]
-,Para [Image ("",[],[]) [Str "Voyage dans la Lune"] ("lalune.jpg","")]
+,Para [Image ("",[],[("height","2343")]) [Str "Voyage dans la Lune"] ("lalune.jpg","")]
 ,Para [Str "Here",Space,Str "is",Space,Str "a",Space,Str "movie",Space,Image ("",[],[]) [Str "movie"] ("movie.jpg",""),Space,Str "icon."]
 ,Para [Str "And",Space,Str "an",Space,Link ("",[],[]) [Image ("",[],[]) [Str "A movie"] ("movie.jpg","")] ("/url",""),Str "."]
 ,Header 1 ("comments",[],[]) [Str "Comments"]