{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Readers.Org ( readOrg ) where
import Prelude
import Text.Pandoc.Readers.Org.Blocks (blockList, meta)
import Text.Pandoc.Readers.Org.ParserState (optionsToParserState)
import Text.Pandoc.Readers.Org.Parsing (OrgParser, readWithM)
import Text.Pandoc.Class (PandocMonad)
import Text.Pandoc.Definition
import Text.Pandoc.Error
import Text.Pandoc.Options
import Text.Pandoc.Parsing (reportLogMessages)
import Text.Pandoc.Shared (crFilter)
import Control.Monad.Except (throwError)
import Control.Monad.Reader (runReaderT)
import Data.Text (Text)
readOrg :: PandocMonad m
=> ReaderOptions
-> Text
-> m Pandoc
readOrg :: ReaderOptions -> Text -> m Pandoc
readOrg opts :: ReaderOptions
opts s :: Text
s = do
Either PandocError Pandoc
parsed <- (ReaderT OrgParserLocal m (Either PandocError Pandoc)
-> OrgParserLocal -> m (Either PandocError Pandoc))
-> OrgParserLocal
-> ReaderT OrgParserLocal m (Either PandocError Pandoc)
-> m (Either PandocError Pandoc)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT OrgParserLocal m (Either PandocError Pandoc)
-> OrgParserLocal -> m (Either PandocError Pandoc)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT OrgParserLocal
forall a. Default a => a
def (ReaderT OrgParserLocal m (Either PandocError Pandoc)
-> m (Either PandocError Pandoc))
-> ReaderT OrgParserLocal m (Either PandocError Pandoc)
-> m (Either PandocError Pandoc)
forall a b. (a -> b) -> a -> b
$
ParserT Text OrgParserState (ReaderT OrgParserLocal m) Pandoc
-> OrgParserState
-> Text
-> ReaderT OrgParserLocal m (Either PandocError Pandoc)
forall s (m :: * -> *) st a.
(Stream s m Char, ToText s) =>
ParserT s st m a -> st -> s -> m (Either PandocError a)
readWithM ParserT Text OrgParserState (ReaderT OrgParserLocal m) Pandoc
forall (m :: * -> *). PandocMonad m => OrgParser m Pandoc
parseOrg (ReaderOptions -> OrgParserState
optionsToParserState ReaderOptions
opts)
(Text -> Text
crFilter Text
s Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\n\n")
case Either PandocError Pandoc
parsed of
Right result :: Pandoc
result -> Pandoc -> m Pandoc
forall (m :: * -> *) a. Monad m => a -> m a
return Pandoc
result
Left _ -> PandocError -> m Pandoc
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocParseError "problem parsing org"
parseOrg :: PandocMonad m => OrgParser m Pandoc
parseOrg :: OrgParser m Pandoc
parseOrg = do
[Block]
blocks' <- OrgParser m [Block]
forall (m :: * -> *). PandocMonad m => OrgParser m [Block]
blockList
Meta
meta' <- OrgParser m Meta
forall (m :: * -> *). Monad m => OrgParser m Meta
meta
ParserT Text OrgParserState (ReaderT OrgParserLocal m) ()
forall (m :: * -> *) st s.
(PandocMonad m, HasLogMessages st) =>
ParserT s st m ()
reportLogMessages
Pandoc -> OrgParser m Pandoc
forall (m :: * -> *) a. Monad m => a -> m a
return (Pandoc -> OrgParser m Pandoc) -> Pandoc -> OrgParser m Pandoc
forall a b. (a -> b) -> a -> b
$ Meta -> [Block] -> Pandoc
Pandoc Meta
meta' [Block]
blocks'