Muse reader: add support for images with specified width

This commit is contained in:
Alexander Krotov 2018-05-31 00:57:58 +03:00
parent f4b71d94f7
commit 1f78efff3b
2 changed files with 15 additions and 4 deletions

View file

@ -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

View file

@ -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"))