{-# LANGUAGE RankNTypes #-}
module Text.Pandoc.CrossRef (
crossRefBlocks
, crossRefMeta
, defaultCrossRefAction
, runCrossRef
, runCrossRefIO
, module SG
, defaultMeta
, CrossRefM
, CrossRefEnv(..)
) where
import qualified Control.Monad.Reader as R
import Control.Monad.State
import Text.Pandoc
import Text.Pandoc.CrossRef.References
import Text.Pandoc.CrossRef.References.Monad
import Text.Pandoc.CrossRef.Util.CodeBlockCaptions
import Text.Pandoc.CrossRef.Util.ModifyMeta
import Text.Pandoc.CrossRef.Util.Options as O
import Text.Pandoc.CrossRef.Util.Settings
import Text.Pandoc.CrossRef.Util.Settings.Gen as SG
data CrossRefEnv = CrossRefEnv {
CrossRefEnv -> Meta
creSettings :: Meta
, CrossRefEnv -> Options
creOptions :: Options
}
type CrossRefM a = R.Reader CrossRefEnv a
crossRefBlocks :: [Block] -> CrossRefM [Block]
crossRefBlocks :: [Block] -> CrossRefM [Block]
crossRefBlocks [Block]
blocks = do
Options
opts <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.asks CrossRefEnv -> Options
creOptions
let
doWalk :: WS [Block]
doWalk =
forall (m :: * -> *) a b.
(Monad m, Data a, Data b) =>
(a -> m a) -> b -> m b
bottomUpM [Block] -> WS [Block]
mkCodeBlockCaptions [Block]
blocks
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Data a => a -> WS a
replaceAll
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a b.
(Monad m, Data a, Data b) =>
(a -> m a) -> b -> m b
bottomUpM [Inline] -> WS [Inline]
replaceRefs
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a b.
(Monad m, Data a, Data b) =>
(a -> m a) -> b -> m b
bottomUpM [Block] -> WS [Block]
listOf
([Block]
result, References
st) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall s a. State s a -> s -> (a, s)
runState forall a. Default a => a
def forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
R.runReaderT Options
opts forall a b. (a -> b) -> a -> b
$ forall a. WS a -> ReaderT Options (State References) a
runWS WS [Block]
doWalk
References
st seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return [Block]
result
crossRefMeta :: CrossRefM Meta
crossRefMeta :: CrossRefM Meta
crossRefMeta = do
Options
opts <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.asks CrossRefEnv -> Options
creOptions
Meta
dtv <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
R.asks CrossRefEnv -> Meta
creSettings
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Options -> Meta -> Meta
modifyMeta Options
opts Meta
dtv
defaultCrossRefAction :: Pandoc -> CrossRefM Pandoc
defaultCrossRefAction :: Pandoc -> CrossRefM Pandoc
defaultCrossRefAction (Pandoc Meta
_ [Block]
bs) = do
Meta
meta' <- CrossRefM Meta
crossRefMeta
[Block]
bs' <- [Block] -> CrossRefM [Block]
crossRefBlocks [Block]
bs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Meta -> [Block] -> Pandoc
Pandoc Meta
meta' [Block]
bs'
runCrossRef :: forall a b. Meta -> Maybe Format -> (a -> CrossRefM b) -> a -> b
runCrossRef :: forall a b. Meta -> Maybe Format -> (a -> CrossRefM b) -> a -> b
runCrossRef Meta
meta Maybe Format
fmt a -> CrossRefM b
action a
arg = forall r a. Reader r a -> r -> a
R.runReader (a -> CrossRefM b
action a
arg) CrossRefEnv
env
where
settings :: Meta
settings = Meta
defaultMeta forall a. Semigroup a => a -> a -> a
<> Meta
meta
env :: CrossRefEnv
env = CrossRefEnv {
creSettings :: Meta
creSettings = Meta
settings
, creOptions :: Options
creOptions = Meta -> Maybe Format -> Options
getOptions Meta
settings Maybe Format
fmt
}
runCrossRefIO :: forall a b. Meta -> Maybe Format -> (a -> CrossRefM b) -> a -> IO b
runCrossRefIO :: forall a b. Meta -> Maybe Format -> (a -> CrossRefM b) -> a -> IO b
runCrossRefIO Meta
meta Maybe Format
fmt a -> CrossRefM b
action a
arg = do
Meta
settings <- Maybe Format -> Meta -> IO Meta
getSettings Maybe Format
fmt Meta
meta
let
env :: CrossRefEnv
env = CrossRefEnv {
creSettings :: Meta
creSettings = Meta
settings
, creOptions :: Options
creOptions = Meta -> Maybe Format -> Options
getOptions Meta
settings Maybe Format
fmt
}
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall r a. Reader r a -> r -> a
R.runReader (a -> CrossRefM b
action a
arg) CrossRefEnv
env