{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
module Text.Pandoc.JSON ( module Text.Pandoc.Definition
, ToJSONFilter(..)
)
where
import Text.Pandoc.Definition
import Text.Pandoc.Walk
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 a where
toJSONFilter :: a -> IO ()
instance (Walkable a Pandoc) => ToJSONFilter (a -> a) where
toJSONFilter :: (a -> a) -> IO ()
toJSONFilter a -> a
f = IO ByteString
BL.getContents IO ByteString -> (ByteString -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
ByteString -> IO ()
BL.putStr (ByteString -> IO ())
-> (ByteString -> ByteString) -> ByteString -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pandoc -> ByteString
forall a. ToJSON a => a -> ByteString
encode (Pandoc -> ByteString)
-> (ByteString -> Pandoc) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a -> a) -> Pandoc -> Pandoc
forall a b. Walkable a b => (a -> a) -> b -> b
walk a -> a
f :: Pandoc -> Pandoc) (Pandoc -> Pandoc)
-> (ByteString -> Pandoc) -> ByteString -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Pandoc)
-> (Pandoc -> Pandoc) -> Either [Char] Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either [Char] -> Pandoc
forall a. HasCallStack => [Char] -> a
error Pandoc -> Pandoc
forall a. a -> a
id (Either [Char] Pandoc -> Pandoc)
-> (ByteString -> Either [Char] Pandoc) -> ByteString -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
ByteString -> Either [Char] Pandoc
forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode'
instance (Walkable a Pandoc) => ToJSONFilter (a -> IO a) where
toJSONFilter :: (a -> IO a) -> IO ()
toJSONFilter a -> IO a
f = IO ByteString
BL.getContents IO ByteString -> (ByteString -> IO Pandoc) -> IO Pandoc
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
((a -> IO a) -> Pandoc -> IO Pandoc
forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM a -> IO a
f :: Pandoc -> IO Pandoc) (Pandoc -> IO Pandoc)
-> (ByteString -> Pandoc) -> ByteString -> IO Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Pandoc)
-> (Pandoc -> Pandoc) -> Either [Char] Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either [Char] -> Pandoc
forall a. HasCallStack => [Char] -> a
error Pandoc -> Pandoc
forall a. a -> a
id (Either [Char] Pandoc -> Pandoc)
-> (ByteString -> Either [Char] Pandoc) -> ByteString -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either [Char] Pandoc
forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode' IO Pandoc -> (Pandoc -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
ByteString -> IO ()
BL.putStr (ByteString -> IO ()) -> (Pandoc -> ByteString) -> Pandoc -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pandoc -> ByteString
forall a. ToJSON a => a -> ByteString
encode
instance (Walkable [a] Pandoc) => ToJSONFilter (a -> [a]) where
toJSONFilter :: (a -> [a]) -> IO ()
toJSONFilter a -> [a]
f = IO ByteString
BL.getContents IO ByteString -> (ByteString -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
ByteString -> IO ()
BL.putStr (ByteString -> IO ())
-> (ByteString -> ByteString) -> ByteString -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pandoc -> ByteString
forall a. ToJSON a => a -> ByteString
encode (Pandoc -> ByteString)
-> (ByteString -> Pandoc) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([a] -> [a]) -> Pandoc -> Pandoc
forall a b. Walkable a b => (a -> a) -> b -> b
walk ((a -> [a]) -> [a] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap a -> [a]
f) :: Pandoc -> Pandoc) (Pandoc -> Pandoc)
-> (ByteString -> Pandoc) -> ByteString -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
([Char] -> Pandoc)
-> (Pandoc -> Pandoc) -> Either [Char] Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either [Char] -> Pandoc
forall a. HasCallStack => [Char] -> a
error Pandoc -> Pandoc
forall a. a -> a
id (Either [Char] Pandoc -> Pandoc)
-> (ByteString -> Either [Char] Pandoc) -> ByteString -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either [Char] Pandoc
forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode'
instance (Walkable [a] Pandoc) => ToJSONFilter (a -> IO [a]) where
toJSONFilter :: (a -> IO [a]) -> IO ()
toJSONFilter a -> IO [a]
f = IO ByteString
BL.getContents IO ByteString -> (ByteString -> IO Pandoc) -> IO Pandoc
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
(([a] -> IO [a]) -> Pandoc -> IO Pandoc
forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM (([[a]] -> [a]) -> IO [[a]] -> IO [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (IO [[a]] -> IO [a]) -> ([a] -> IO [[a]]) -> [a] -> IO [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> IO [a]) -> [a] -> IO [[a]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM a -> IO [a]
f) :: Pandoc -> IO Pandoc) (Pandoc -> IO Pandoc)
-> (ByteString -> Pandoc) -> ByteString -> IO Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
([Char] -> Pandoc)
-> (Pandoc -> Pandoc) -> Either [Char] Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either [Char] -> Pandoc
forall a. HasCallStack => [Char] -> a
error Pandoc -> Pandoc
forall a. a -> a
id (Either [Char] Pandoc -> Pandoc)
-> (ByteString -> Either [Char] Pandoc) -> ByteString -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either [Char] Pandoc
forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode' IO Pandoc -> (Pandoc -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
ByteString -> IO ()
BL.putStr (ByteString -> IO ()) -> (Pandoc -> ByteString) -> Pandoc -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pandoc -> ByteString
forall a. ToJSON a => a -> ByteString
encode
instance (ToJSONFilter a) => ToJSONFilter ([String] -> a) where
toJSONFilter :: ([[Char]] -> a) -> IO ()
toJSONFilter [[Char]] -> a
f = IO [[Char]]
getArgs IO [[Char]] -> ([[Char]] -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> IO ()
forall a. ToJSONFilter a => a -> IO ()
toJSONFilter (a -> IO ()) -> ([[Char]] -> a) -> [[Char]] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> a
f
instance (ToJSONFilter a) => ToJSONFilter (Maybe Format -> a) where
toJSONFilter :: (Maybe Format -> a) -> IO ()
toJSONFilter Maybe Format -> a
f = IO [[Char]]
getArgs IO [[Char]] -> ([[Char]] -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> IO ()
forall a. ToJSONFilter a => a -> IO ()
toJSONFilter (a -> IO ()) -> ([[Char]] -> a) -> [[Char]] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Format -> a
f (Maybe Format -> a) -> ([[Char]] -> Maybe Format) -> [[Char]] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Format) -> Maybe [Char] -> Maybe Format
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Format
Format (Text -> Format) -> ([Char] -> Text) -> [Char] -> Format
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack) (Maybe [Char] -> Maybe Format)
-> ([[Char]] -> Maybe [Char]) -> [[Char]] -> Maybe Format
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> Maybe [Char]
forall a. [a] -> Maybe a
listToMaybe