{-# LANGUAGE FlexibleInstances, FlexibleContexts, MultiParamTypeClasses #-}
module Text.Pandoc.JSON ( module Text.Pandoc.Definition
, ToJSONFilter(..)
)
where
import Text.Pandoc.Definition
import Text.Pandoc.Walk
import Control.Monad.IO.Class(MonadIO(liftIO))
import Data.Maybe (listToMaybe)
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import Data.Aeson
import System.Environment (getArgs)
class ToJSONFilter m a where
toJSONFilter :: a -> m ()
instance (Walkable a Pandoc) => ToJSONFilter IO (a -> a) where
toJSONFilter :: (a -> a) -> IO ()
toJSONFilter a -> a
f = IO ByteString
BL.getContents forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
ByteString -> IO ()
BL.putStr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> ByteString
encode forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. Walkable a b => (a -> a) -> b -> b
walk a -> a
f :: Pandoc -> Pandoc) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. HasCallStack => [Char] -> a
error forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode'
instance (Walkable a Pandoc, MonadIO m) => ToJSONFilter m (a -> m a) where
toJSONFilter :: (a -> m a) -> m ()
toJSONFilter a -> m a
f = do
ByteString
c <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ByteString
BL.getContents
Pandoc
r <- forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM a -> m a
f (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. HasCallStack => [Char] -> a
error forall a. a -> a
id (forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode' ByteString
c) :: Pandoc)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ByteString -> IO ()
BL.putStr (forall a. ToJSON a => a -> ByteString
encode (Pandoc
r :: Pandoc)))
instance (Walkable [a] Pandoc, MonadIO m) => ToJSONFilter m (a -> m [a]) where
toJSONFilter :: (a -> m [a]) -> m ()
toJSONFilter a -> m [a]
f = do
ByteString
c <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ByteString
BL.getContents
Pandoc
r <- (forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM a -> m [a]
f)) (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. HasCallStack => [Char] -> a
error forall a. a -> a
id (forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode' ByteString
c) :: Pandoc)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ByteString -> IO ()
BL.putStr (forall a. ToJSON a => a -> ByteString
encode (Pandoc
r :: Pandoc)))
instance (ToJSONFilter m a, MonadIO m) => ToJSONFilter m ([String] -> a) where
toJSONFilter :: ([[Char]] -> a) -> m ()
toJSONFilter [[Char]] -> a
f = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO [[Char]]
getArgs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. ToJSONFilter m a => a -> m ()
toJSONFilter forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> a
f
instance (ToJSONFilter m a, MonadIO m) => ToJSONFilter m (Maybe Format -> a) where
toJSONFilter :: (Maybe Format -> a) -> m ()
toJSONFilter Maybe Format -> a
f = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO [[Char]]
getArgs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. ToJSONFilter m a => a -> m ()
toJSONFilter forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Format -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Format
Format forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe a
listToMaybe