Muse reader: add support for floating images

This commit is contained in:
Alexander Krotov 2018-05-31 23:31:27 +03:00
parent 3181023ca1
commit 5fbc981fc2
2 changed files with 24 additions and 6 deletions

View file

@ -35,7 +35,6 @@ TODO:
- Page breaks (five "*")
- Org tables
- table.el tables
- Floating images
- <cite> tag
-}
module Text.Pandoc.Readers.Muse (readMuse) where
@ -965,18 +964,31 @@ explicitLink = try $ do
image :: PandocMonad m => MuseParser m (F Inlines)
image = try $ do
string "[["
(url, (ext, width)) <- manyUntil (noneOf "]") $ (imageExtensionAndOptions <* char ']')
(url, (ext, width, align)) <- manyUntil (noneOf "]") $ (imageExtensionAndOptions <* char ']')
content <- optionMaybe linkContent
char ']'
let widthAttr = maybeToList (("width",) . (++ "%") <$> width)
return $ B.imageWith ("", [], widthAttr) (url ++ ext) mempty <$> fromMaybe (return mempty) content
let widthAttr = case align of
Just 'f' -> [("width", (fromMaybe "100" width) ++ "%"), ("height", "75%")]
_ -> maybeToList (("width",) . (++ "%") <$> width)
let alignClass = case align of
Just 'r' -> ["align-right"]
Just 'l' -> ["align-left"]
Just 'f' -> []
Nothing -> []
return $ B.imageWith ("", alignClass, 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)
(width, align) <- option (Nothing, Nothing) imageAttrs
return (ext, width, align)
imageAttrs = do
many1 spaceChar
width <- optionMaybe (many1 digit)
many spaceChar
align <- optionMaybe (oneOf "rlf")
return (width, align)
link :: PandocMonad m => MuseParser m (F Inlines)
link = try $ do

View file

@ -209,6 +209,12 @@ tests =
, "At least one space is required between image filename and width" =:
"[[image.jpg60]]" =?>
para (link "image.jpg60" mempty (str "image.jpg60"))
, "Left-aligned image with width" =:
"[[image.png 60 l][Image]]" =?>
para (imageWith ("", ["align-left"], [("width", "60%")]) "image.png" "" (str "Image"))
, "Right-aligned image with width" =:
"[[image.png 60 r][Image]]" =?>
para (imageWith ("", ["align-right"], [("width", "60%")]) "image.png" "" (str "Image"))
, "Image link" =:
"[[URL:image.jpg]]" =?>
para (link "image.jpg" "" (str "image.jpg"))