{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Readers.Native ( readNative ) where
import Text.Pandoc.Definition
import Text.Pandoc.Options (ReaderOptions)
import Text.Pandoc.Shared (safeRead)
import Control.Monad.Except (throwError)
import Data.Text (Text)
import Text.Pandoc.Class.PandocMonad (PandocMonad)
import Text.Pandoc.Error
import Text.Pandoc.Sources (ToSources(..), sourcesToText)
readNative :: (PandocMonad m, ToSources a)
=> ReaderOptions
-> a
-> m Pandoc
readNative :: forall (m :: * -> *) a.
(PandocMonad m, ToSources a) =>
ReaderOptions -> a -> m Pandoc
readNative ReaderOptions
_ a
s =
let t :: Text
t = Sources -> Text
sourcesToText forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToSources a => a -> Sources
toSources forall a b. (a -> b) -> a -> b
$ a
s
in case forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError [Block]
readBlocks Text
t) forall a b. b -> Either a b
Right (forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
t) of
Right Pandoc
doc -> forall (m :: * -> *) a. Monad m => a -> m a
return Pandoc
doc
Left PandocError
_ -> forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocParseError Text
"couldn't read native"
readBlocks :: Text -> Either PandocError [Block]
readBlocks :: Text -> Either PandocError [Block]
readBlocks Text
s = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((forall a. a -> [a] -> [a]
:[]) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError Block
readBlock Text
s) forall a b. b -> Either a b
Right (forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
s)
readBlock :: Text -> Either PandocError Block
readBlock :: Text -> Either PandocError Block
readBlock Text
s = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Inline] -> Block
Plain forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError [Inline]
readInlines Text
s) forall a b. b -> Either a b
Right (forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
s)
readInlines :: Text -> Either PandocError [Inline]
readInlines :: Text -> Either PandocError [Inline]
readInlines Text
s = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((forall a. a -> [a] -> [a]
:[]) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError Inline
readInline Text
s) forall a b. b -> Either a b
Right (forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
s)
readInline :: Text -> Either PandocError Inline
readInline :: Text -> Either PandocError Inline
readInline Text
s = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PandocError
PandocParseError forall a b. (a -> b) -> a -> b
$ Text
"Could not read: " forall a. Semigroup a => a -> a -> a
<> Text
s) forall a b. b -> Either a b
Right (forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
s)