{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Readers.Native
   Copyright   : Copyright (C) 2011-2024 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Conversion of a string representation of a pandoc type (@Pandoc@,
@[Block]@, @Block@, @[Inline]@, or @Inline@) to a @Pandoc@ document.
-}
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)

-- | Read native formatted text and return a Pandoc document.
-- The input may be a full pandoc document, a block list, a block,
-- an inline list, or an inline.  Thus, for example,
--
-- > Str "hi"
--
-- will be treated as if it were
--
-- > Pandoc nullMeta [Plain [Str "hi"]]
--
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 (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 a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Pandoc
doc
        Left PandocError
_    -> PandocError -> m Pandoc
forall a. PandocError -> m a
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)