git.iko.soy/typeable/pandoc
on master help sign in
Muse reader: add support for floating images
commit5fbc981fc24d24652de3061353f09d1912928fba authorAlexander Krotov <ilabdsf@gmail.com> dateThu 31 May 2018, 23:31 (8 years ago) parent3181023
2 files changed+24−6
src/Text/Pandoc/Readers/Muse.hs+18−6 test/Tests/Readers/Muse.hs+6
src/Text/Pandoc/Readers/Muse.hs+18−6
@@ -35,7 +35,6 @@ TODO:
3535- Page breaks (five "*")
3636- Org tables
3737- table.el tables
38−- Floating images
3938- <cite> tag
4039-}
4140module Text.Pandoc.Readers.Muse (readMuse) where
@@ -965,18 +964,31 @@ explicitLink = try $ do
965964image :: PandocMonad m => MuseParser m (F Inlines)
966965image = try $ do
967966 string "[["
968− (url, (ext, width)) <- manyUntil (noneOf "]") $ (imageExtensionAndOptions <* char ']')
967+ (url, (ext, width, align)) <- manyUntil (noneOf "]") $ (imageExtensionAndOptions <* char ']')
969968 content <- optionMaybe linkContent
970969 char ']'
971− let widthAttr = maybeToList (("width",) . (++ "%") <$> width)
972− return $ B.imageWith ("", [], widthAttr) (url ++ ext) mempty <$> fromMaybe (return mempty) content
970+ let widthAttr = case align of
971+ Just 'f' -> [("width", (fromMaybe "100" width) ++ "%"), ("height", "75%")]
972+ _ -> maybeToList (("width",) . (++ "%") <$> width)
973+ let alignClass = case align of
974+ Just 'r' -> ["align-right"]
975+ Just 'l' -> ["align-left"]
976+ Just 'f' -> []
977+ Nothing -> []
978+ return $ B.imageWith ("", alignClass, widthAttr) (url ++ ext) mempty <$> fromMaybe (return mempty) content
973979 where -- Taken from muse-image-regexp defined in Emacs Muse file lisp/muse-regexps.el
974980 imageExtensions = [".eps", ".gif", ".jpg", ".jpeg", ".pbm", ".png", ".tiff", ".xbm", ".xpm"]
975981 imageExtension = choice (try . string <$> imageExtensions)
976982 imageExtensionAndOptions = do
977983 ext <- imageExtension
978− width <- optionMaybe (many1 spaceChar *> many1 digit)
979− return (ext, width)
984+ (width, align) <- option (Nothing, Nothing) imageAttrs
985+ return (ext, width, align)
986+ imageAttrs = do
987+ many1 spaceChar
988+ width <- optionMaybe (many1 digit)
989+ many spaceChar
990+ align <- optionMaybe (oneOf "rlf")
991+ return (width, align)
980992
981993link :: PandocMonad m => MuseParser m (F Inlines)
982994link = try $ do
test/Tests/Readers/Muse.hs+6
@@ -209,6 +209,12 @@ tests =
209209 , "At least one space is required between image filename and width" =:
210210 "[[image.jpg60]]" =?>
211211 para (link "image.jpg60" mempty (str "image.jpg60"))
212+ , "Left-aligned image with width" =:
213+ "[[image.png 60 l][Image]]" =?>
214+ para (imageWith ("", ["align-left"], [("width", "60%")]) "image.png" "" (str "Image"))
215+ , "Right-aligned image with width" =:
216+ "[[image.png 60 r][Image]]" =?>
217+ para (imageWith ("", ["align-right"], [("width", "60%")]) "image.png" "" (str "Image"))
212218 , "Image link" =:
213219 "[[URL:image.jpg]]" =?>
214220 para (link "image.jpg" "" (str "image.jpg"))