From 1f78efff3b903158da557dbe83c2350954d40a2e Mon Sep 17 00:00:00 2001
From: Alexander Krotov <ilabdsf@gmail.com>
Date: Thu, 31 May 2018 00:57:58 +0300
Subject: [PATCH] Muse reader: add support for images with specified width

---
 src/Text/Pandoc/Readers/Muse.hs | 13 +++++++++----
 test/Tests/Readers/Muse.hs      |  6 ++++++
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/Text/Pandoc/Readers/Muse.hs b/src/Text/Pandoc/Readers/Muse.hs
index 9eda0d4f3..72909dc4d 100644
--- a/src/Text/Pandoc/Readers/Muse.hs
+++ b/src/Text/Pandoc/Readers/Muse.hs
@@ -35,7 +35,7 @@ TODO:
 - Page breaks (five "*")
 - Org tables
 - table.el tables
-- Images with attributes (floating and width)
+- Floating images
 - <cite> tag
 -}
 module Text.Pandoc.Readers.Muse (readMuse) where
@@ -50,7 +50,7 @@ import Data.List (intercalate)
 import Data.List.Split (splitOn)
 import qualified Data.Map as M
 import qualified Data.Set as Set
-import Data.Maybe (fromMaybe, isNothing)
+import Data.Maybe (fromMaybe, isNothing, maybeToList)
 import Data.Text (Text, unpack)
 import Text.HTML.TagSoup
 import Text.Pandoc.Builder (Blocks, Inlines)
@@ -965,13 +965,18 @@ explicitLink = try $ do
 image :: PandocMonad m => MuseParser m (F Inlines)
 image = try $ do
   string "[["
-  (url, ext) <- manyUntil (noneOf "]") $ (imageExtension <* char ']')
+  (url, (ext, width)) <- manyUntil (noneOf "]") $ (imageExtensionAndOptions <* char ']')
   content <- optionMaybe linkContent
   char ']'
-  return $ B.image (url ++ ext) "" <$> fromMaybe (return mempty) content
+  let widthAttr = maybeToList (("width",) . (++ "%") <$> width)
+  return $ B.imageWith ("", [], widthAttr) (url ++ ext) mempty <$> fromMaybe (return mempty) content
   where -- Taken from muse-image-regexp defined in Emacs Muse file lisp/muse-regexps.el
         imageExtensions = [".eps", ".gif", ".jpg", ".jpeg", ".pbm", ".png", ".tiff", ".xbm", ".xpm"]
         imageExtension = choice (try . string <$> imageExtensions)
+        imageExtensionAndOptions = do
+          ext <- imageExtension
+          width <- optionMaybe (many1 spaceChar *> many1 digit)
+          return (ext, width)
 
 link :: PandocMonad m => MuseParser m (F Inlines)
 link = try $ do
diff --git a/test/Tests/Readers/Muse.hs b/test/Tests/Readers/Muse.hs
index 6b4e0fdbd..1146aa6d2 100644
--- a/test/Tests/Readers/Muse.hs
+++ b/test/Tests/Readers/Muse.hs
@@ -203,6 +203,12 @@ tests =
         , "Image with space in filename" =:
           "[[image name.jpg]]" =?>
           para (image "image name.jpg" "" mempty)
+        , "Image with width" =:
+          "[[image.jpg 60]]" =?>
+          para (imageWith ("", [], [("width", "60%")]) "image.jpg" mempty mempty)
+        , "At least one space is required between image filename and width" =:
+          "[[image.jpg60]]" =?>
+          para (link "image.jpg60" mempty (str "image.jpg60"))
         , "Image link" =:
           "[[URL:image.jpg]]" =?>
           para (link "image.jpg" "" (str "image.jpg"))