git.iko.soy/typeable/pandoc
on master help sign in
Provide shared F monad functions for Markdown and Org readers

The `F` monads used for delayed evaluation of certain values in the Markdown and Org readers are based on a shared data type capturing the common pattern of both `F` types.

commit31caa616a9353e073eb86be7889b7087e14a48ac authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 27 Apr 2017, 21:48 (9 years ago) parent97addc2
4 files changed+36−52
src/Text/Pandoc/Parsing.hs+25−10 src/Text/Pandoc/Readers/Markdown.hs+1−4 src/Text/Pandoc/Readers/Org/ParserState.hs+9−37 src/Text/Pandoc/Readers/Org/Parsing.hs+1−1
src/Text/Pandoc/Parsing.hs+25−10
@@ -108,10 +108,13 @@ module Text.Pandoc.Parsing ( anyLine,
108108 applyMacros',
109109 Parser,
110110 ParserT,
111− F(..),
111+ F,
112+ Future(..),
112113 runF,
113114 askF,
114115 asksF,
116+ returnF,
117+ trimInlinesF,
115118 token,
116119 (<+?>),
117120 extractIdClass,
@@ -175,7 +178,7 @@ where
175178
176179import Text.Pandoc.Definition
177180import Text.Pandoc.Options
178−import Text.Pandoc.Builder (Blocks, Inlines, rawBlock, HasMeta(..))
181+import Text.Pandoc.Builder (Blocks, Inlines, rawBlock, HasMeta(..), trimInlines)
179182import qualified Text.Pandoc.Builder as B
180183import Text.Pandoc.XML (fromEntities)
181184import qualified Text.Pandoc.UTF8 as UTF8 (putStrLn)
@@ -205,18 +208,30 @@ type Parser t s = Parsec t s
205208
206209type ParserT = ParsecT
207210
208−newtype F a = F { unF :: Reader ParserState a } deriving (Monad, Applicative, Functor)
211+-- | Reader monad wrapping the parser state. This is used to possibly delay
212+-- evaluation until all relevant information has been parsed and made available
213+-- in the parser state.
214+newtype Future s a = Future { runDelayed :: Reader s a }
215+ deriving (Monad, Applicative, Functor)
209216
210−runF :: F a -> ParserState -> a
211−runF = runReader . unF
217+type F = Future ParserState
212218
213−askF :: F ParserState
214−askF = F ask
219+runF :: Future s a -> s -> a
220+runF = runReader . runDelayed
215221
216−asksF :: (ParserState -> a) -> F a
217−asksF f = F $ asks f
222+askF :: Future s s
223+askF = Future ask
218224
219−instance Monoid a => Monoid (F a) where
225+asksF :: (s -> a) -> Future s a
226+asksF f = Future $ asks f
227+
228+returnF :: Monad m => a -> m (Future s a)
229+returnF = return . return
230+
231+trimInlinesF :: Future s Inlines -> Future s Inlines
232+trimInlinesF = liftM trimInlines
233+
234+instance Monoid a => Monoid (Future s a) where
220235 mempty = return mempty
221236 mappend = liftM2 mappend
222237 mconcat = liftM mconcat . sequence
src/Text/Pandoc/Readers/Markdown.hs+1−4
@@ -50,7 +50,7 @@ import Data.Yaml (ParseException (..), YamlException (..), YamlMark (..))
5050import qualified Data.Yaml as Yaml
5151import System.FilePath (addExtension, takeExtension)
5252import Text.HTML.TagSoup
53−import Text.Pandoc.Builder (Blocks, Inlines, trimInlines)
53+import Text.Pandoc.Builder (Blocks, Inlines)
5454import qualified Text.Pandoc.Builder as B
5555import Text.Pandoc.Class (PandocMonad, report)
5656import Text.Pandoc.Definition
@@ -80,9 +80,6 @@ readMarkdown opts s = do
8080 Right result -> return result
8181 Left e -> throwError e
8282
83−trimInlinesF :: F Inlines -> F Inlines
84−trimInlinesF = liftM trimInlines
85−
8683--
8784-- Constants and data structure definitions
8885--
src/Text/Pandoc/Readers/Org/ParserState.hs+9−37
@@ -39,7 +39,7 @@ module Text.Pandoc.Readers.Org.ParserState
3939 , TodoState (..)
4040 , activeTodoMarkers
4141 , registerTodoSequence
42− , F(..)
42+ , F
4343 , askF
4444 , asksF
4545 , trimInlinesF
@@ -50,14 +50,13 @@ module Text.Pandoc.Readers.Org.ParserState
5050 , optionsToParserState
5151 ) where
5252
53−import Control.Monad (liftM, liftM2)
54−import Control.Monad.Reader (Reader, ReaderT, ask, asks, local, runReader)
53+import Control.Monad.Reader (ReaderT, asks, local)
5554
5655import Data.Default (Default (..))
5756import qualified Data.Map as M
5857import qualified Data.Set as Set
5958
60−import Text.Pandoc.Builder (Blocks, Inlines, trimInlines)
59+import Text.Pandoc.Builder (Blocks, Inlines)
6160import Text.Pandoc.Definition (Meta (..), nullMeta)
6261import Text.Pandoc.Options (ReaderOptions (..))
6362import Text.Pandoc.Logging
@@ -65,7 +64,12 @@ import Text.Pandoc.Parsing (HasHeaderMap (..), HasIdentifierList (..),
6564 HasLogMessages (..),
6665 HasLastStrPosition (..), HasQuoteContext (..),
6766 HasReaderOptions (..), ParserContext (..),
68− QuoteContext (..), SourcePos)
67+ QuoteContext (..), SourcePos, Future,
68+ askF, asksF, returnF, runF, trimInlinesF)
69+
70+-- | This is used to delay evaluation until all relevant information has been
71+-- parsed and made available in the parser state.
72+type F = Future OrgParserState
6973
7074-- | An inline note / footnote containing the note key and its (inline) value.
7175type OrgNoteRecord = (String, F Blocks)
@@ -229,35 +233,3 @@ defaultExportSettings = ExportSettings
229233 , exportWithEmail = True
230234 , exportWithTodoKeywords = True
231235 }
232−
233−
234−--
235−-- Parser state reader
236−--
237−
238−-- | Reader monad wrapping the parser state. This is used to delay evaluation
239−-- until all relevant information has been parsed and made available in the
240−-- parser state. See also the newtype of the same name in
241−-- Text.Pandoc.Parsing.
242−newtype F a = F { unF :: Reader OrgParserState a
243− } deriving (Functor, Applicative, Monad)
244−
245−instance Monoid a => Monoid (F a) where
246− mempty = return mempty
247− mappend = liftM2 mappend
248− mconcat = fmap mconcat . sequence
249−
250−runF :: F a -> OrgParserState -> a
251−runF = runReader . unF
252−
253−askF :: F OrgParserState
254−askF = F ask
255−
256−asksF :: (OrgParserState -> a) -> F a
257−asksF f = F $ asks f
258−
259−trimInlinesF :: F Inlines -> F Inlines
260−trimInlinesF = liftM trimInlines
261−
262−returnF :: Monad m => a -> m (F a)
263−returnF = return . return
src/Text/Pandoc/Readers/Org/Parsing.hs+1−1
@@ -110,7 +110,7 @@ module Text.Pandoc.Readers.Org.Parsing
110110
111111import Text.Pandoc.Readers.Org.ParserState
112112
113−import Text.Pandoc.Parsing hiding (anyLine, blanklines, newline,
113+import Text.Pandoc.Parsing hiding (F, anyLine, blanklines, newline,
114114 parseFromString)
115115import qualified Text.Pandoc.Parsing as P
116116