{-# 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 :: ReaderOptions -> a -> m Pandoc
readNative ReaderOptions
_ a
s =
let t :: Text
t = Sources -> Text
sourcesToText (Sources -> Text) -> (a -> Sources) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Sources
forall a. ToSources a => a -> Sources
toSources (a -> Text) -> a -> Text
forall a b. (a -> b) -> a -> b
$ a
s
in case Either PandocError Pandoc
-> (Pandoc -> Either PandocError Pandoc)
-> Maybe Pandoc
-> Either PandocError Pandoc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta ([Block] -> Pandoc)
-> Either PandocError [Block] -> Either PandocError Pandoc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError [Block]
readBlocks Text
t) Pandoc -> Either PandocError Pandoc
forall a b. b -> Either a b
Right (Text -> Maybe Pandoc
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
t) of
Right Pandoc
doc -> Pandoc -> m Pandoc
forall (m :: * -> *) a. Monad m => a -> m a
return Pandoc
doc
Left PandocError
_ -> 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 Text
"couldn't read native"
readBlocks :: Text -> Either PandocError [Block]
readBlocks :: Text -> Either PandocError [Block]
readBlocks Text
s = Either PandocError [Block]
-> ([Block] -> Either PandocError [Block])
-> Maybe [Block]
-> Either PandocError [Block]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
:[]) (Block -> [Block])
-> Either PandocError Block -> Either PandocError [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError Block
readBlock Text
s) [Block] -> Either PandocError [Block]
forall a b. b -> Either a b
Right (Text -> Maybe [Block]
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 = Either PandocError Block
-> (Block -> Either PandocError Block)
-> Maybe Block
-> Either PandocError Block
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Inline] -> Block
Plain ([Inline] -> Block)
-> Either PandocError [Inline] -> Either PandocError Block
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError [Inline]
readInlines Text
s) Block -> Either PandocError Block
forall a b. b -> Either a b
Right (Text -> Maybe Block
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 = Either PandocError [Inline]
-> ([Inline] -> Either PandocError [Inline])
-> Maybe [Inline]
-> Either PandocError [Inline]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[]) (Inline -> [Inline])
-> Either PandocError Inline -> Either PandocError [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either PandocError Inline
readInline Text
s) [Inline] -> Either PandocError [Inline]
forall a b. b -> Either a b
Right (Text -> Maybe [Inline]
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 = Either PandocError Inline
-> (Inline -> Either PandocError Inline)
-> Maybe Inline
-> Either PandocError Inline
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (PandocError -> Either PandocError Inline
forall a b. a -> Either a b
Left (PandocError -> Either PandocError Inline)
-> (Text -> PandocError) -> Text -> Either PandocError Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PandocError
PandocParseError (Text -> Either PandocError Inline)
-> Text -> Either PandocError Inline
forall a b. (a -> b) -> a -> b
$ Text
"Could not read: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
s) Inline -> Either PandocError Inline
forall a b. b -> Either a b
Right (Text -> Maybe Inline
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
s)