{-# LANGUAGE NoImplicitPrelude #-}
module Text.Pandoc.Readers.Native ( readNative ) where
import Prelude
import Text.Pandoc.Definition
import Text.Pandoc.Options (ReaderOptions)
import Text.Pandoc.Shared (safeRead)
import Control.Monad.Except (throwError)
import Data.Text (Text, unpack)
import Text.Pandoc.Class
import Text.Pandoc.Error
readNative :: PandocMonad m
=> ReaderOptions
-> Text
-> m Pandoc
readNative _ s =
case maybe (Pandoc nullMeta <$> readBlocks s) Right (safeRead (unpack s)) of
Right doc -> return doc
Left _ -> throwError $ PandocParseError "couldn't read native"
readBlocks :: Text -> Either PandocError [Block]
readBlocks s = maybe ((:[]) <$> readBlock s) Right (safeRead (unpack s))
readBlock :: Text -> Either PandocError Block
readBlock s = maybe (Plain <$> readInlines s) Right (safeRead (unpack s))
readInlines :: Text -> Either PandocError [Inline]
readInlines s = maybe ((:[]) <$> readInline s) Right (safeRead (unpack s))
readInline :: Text -> Either PandocError Inline
readInline s = maybe (Left . PandocParseError $ "Could not read: " ++ unpack s) Right (safeRead (unpack s))