@@ -1,5 +1,6 @@
11{-# LANGUAGE RelaxedPolyRec #-} -- needed for inlinesBetween on GHC < 7
2−{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances,
3+ GeneralizedNewtypeDeriving #-}
34{-
45Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
56
@@ -49,9 +50,9 @@ import Text.Pandoc.XML ( fromEntities )
4950import Data.Monoid
5051import qualified Data.Sequence as Seq -- TODO leaky abstraction, need better isNull in Builder
5152import Control.Applicative ((<$>), (<*), (*>), (<$))
53+import Control.Monad
5254import Text.HTML.TagSoup
5355import Text.HTML.TagSoup.Match (tagOpen)
54−import Control.Monad.Reader
5556
5657-- | Read markdown from an input string and return a Pandoc document.
5758readMarkdown :: ReaderOptions -- ^ Reader options
@@ -60,13 +61,6 @@ readMarkdown :: ReaderOptions -- ^ Reader options
6061readMarkdown opts s =
6162 (readWith parseMarkdown) def{ stateOptions = opts } (s ++ "\n\n")
6263
63−type F a = Reader ParserState a
64−
65−instance Monoid a => Monoid (Reader ParserState a) where
66− mempty = return mempty
67− mappend = liftM2 mappend
68− mconcat = liftM mconcat . sequence
69−
7064trimInlinesF :: F Inlines -> F Inlines
7165trimInlinesF = liftM trimInlines
7266
@@ -100,7 +94,7 @@ isBlank _ = False
10094--
10195
10296isNull :: F Inlines -> Bool
103−isNull ils = Seq.null $ unInlines (runReader ils def)
97+isNull ils = Seq.null $ unInlines (runF ils def)
10498
10599spnl :: Parser [Char] st ()
106100spnl = try $ do
@@ -143,7 +137,7 @@ inlinesInBalancedBrackets :: Parser [Char] ParserState (F Inlines)
143137inlinesInBalancedBrackets = try $ do
144138 char '['
145139 result <- manyTill ( (do lookAhead $ try $ do x <- inline
146− guard (runReader x def == B.str "[")
140+ guard (runF x def == B.str "[")
147141 bal <- inlinesInBalancedBrackets
148142 return $ (\x -> B.str "[" <> x <> B.str "]") <$> bal)
149143 <|> inline)
@@ -198,10 +192,10 @@ parseMarkdown = do
198192 (title, authors, date) <- option (mempty,return [],mempty) titleBlock
199193 blocks <- parseBlocks
200194 st <- getState
201− return $ B.setTitle (runReader title st)
202− $ B.setAuthors (runReader authors st)
203− $ B.setDate (runReader date st)
204− $ B.doc $ runReader blocks st
195+ return $ B.setTitle (runF title st)
196+ $ B.setAuthors (runF authors st)
197+ $ B.setDate (runF date st)
198+ $ B.doc $ runF blocks st
205199
206200--
207201-- initial pass for references and notes
@@ -1147,7 +1141,7 @@ exampleRef = try $ do
11471141 char '@'
11481142 lab <- many1 (alphaNum <|> oneOf "-_")
11491143 return $ do
1150− st <- ask
1144+ st <- askF
11511145 return $ case M.lookup lab (stateExamples st) of
11521146 Just n -> B.str (show n)
11531147 Nothing -> B.str ('@':lab)
@@ -1402,7 +1396,7 @@ referenceLink constructor (lab, raw) = do
14021396 let dropBrackets = reverse . dropRB . reverse . dropLB
14031397 fallback <- parseFromString (mconcat <$> many inline) $ dropBrackets raw
14041398 return $ do
1405− keys <- asks stateKeys
1399+ keys <- asksF stateKeys
14061400 case M.lookup key keys of
14071401 Nothing -> (\x -> B.str "[" <> x <> B.str "]" <> B.str raw') <$> fallback
14081402 Just (src,tit) -> constructor src tit <$> lab
@@ -1427,15 +1421,15 @@ note = try $ do
14271421 guardEnabled Ext_footnotes
14281422 ref <- noteMarker
14291423 return $ do
1430− notes <- asks stateNotes'
1424+ notes <- asksF stateNotes'
14311425 case lookup ref notes of
14321426 Nothing -> return $ B.str $ "[^" ++ ref ++ "]"
14331427 Just contents -> do
1434− st <- ask
1428+ st <- askF
14351429 -- process the note in a context that doesn't resolve
14361430 -- notes, to avoid infinite looping with notes inside
14371431 -- notes:
1438− let contents' = runReader contents st{ stateNotes' = [] }
1432+ let contents' = runF contents st{ stateNotes' = [] }
14391433 return $ B.note contents'
14401434
14411435inlineNote :: Parser [Char] ParserState (F Inlines)