@@ -41,6 +41,7 @@ module Text.Pandoc.Readers.Muse (readMuse) where
4141
4242import Prelude
4343import Control.Monad
44+import Control.Monad.Reader
4445import Control.Monad.Except (throwError)
4546import Data.Bifunctor
4647import Data.Char (isAlphaNum)
@@ -68,7 +69,7 @@ readMuse :: PandocMonad m
6869 -> m Pandoc
6970readMuse opts s = do
7071 let input = crFilter s
71− res <- mapLeft (PandocParsecError $ unpack input) `liftM` runParserT parseMuse def{ museOptions = opts } "source" input
72+ res <- mapLeft (PandocParsecError $ unpack input) `liftM` (runReaderT (runParserT parseMuse def{ museOptions = opts } "source" input) def)
7273 case res of
7374 Left e -> throwError e
7475 Right d -> return d
@@ -82,7 +83,6 @@ data MuseState = MuseState { museMeta :: F Meta -- ^ Document metadata
8283 , museLastStrPos :: Maybe SourcePos -- ^ Position after last str parsed
8384 , museLogMessages :: [LogMessage]
8485 , museNotes :: M.Map String (SourcePos, F Blocks)
85− , museInLink :: Bool -- ^ True when parsing a link description to avoid nested links
8686 , museInPara :: Bool -- ^ True when looking for a paragraph terminator
8787 }
8888
@@ -94,11 +94,17 @@ instance Default MuseState where
9494 , museLastStrPos = Nothing
9595 , museLogMessages = []
9696 , museNotes = M.empty
97− , museInLink = False
9897 , museInPara = False
9998 }
10099
101−type MuseParser = ParserT Text MuseState
100+data MuseEnv =
101+ MuseEnv { museInLink :: Bool }
102+
103+instance Default MuseEnv where
104+ def = MuseEnv { museInLink = False -- ^ True when parsing a link description to avoid nested links
105+ }
106+
107+type MuseParser m = ParserT Text MuseState (ReaderT MuseEnv m)
102108
103109instance HasReaderOptions MuseState where
104110 extractReaderOptions = museOptions
@@ -778,7 +784,7 @@ anchor = try $ do
778784-- | Parse a footnote reference.
779785footnote :: PandocMonad m => MuseParser m (F Inlines)
780786footnote = try $ do
781− inLink <- museInLink <$> getState
787+ inLink <- asks museInLink
782788 guard $ not inLink
783789 ref <- noteMarker
784790 return $ do
@@ -915,12 +921,9 @@ symbol = return . B.str <$> count 1 nonspaceChar
915921-- | Parse a link or image.
916922linkOrImage :: PandocMonad m => MuseParser m (F Inlines)
917923linkOrImage = try $ do
918− st <- getState
919− guard $ not $ museInLink st
920− setState $ st{ museInLink = True }
921− res <- explicitLink <|> image <|> link
922− updateState (\state -> state { museInLink = False })
923− return res
924+ inLink <- asks museInLink
925+ guard $ not inLink
926+ local (\s -> s { museInLink = True }) (explicitLink <|> image <|> link)
924927
925928linkContent :: PandocMonad m => MuseParser m (F Inlines)
926929linkContent = trimInlinesF . mconcat