module Clckwrks.Markup.Pandoc where
import Clckwrks.Types (Trust(..))
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar (newEmptyMVar, readMVar, putMVar)
import Control.Monad.Trans (MonadIO(liftIO))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Text.HTML.SanitizeXSS (sanitizeBalance)
import System.Exit (ExitCode(ExitFailure, ExitSuccess))
import System.IO (hClose)
import System.Process (waitForProcess, runInteractiveProcess)
pandoc :: (MonadIO m) =>
Maybe [String]
-> Trust
-> Text
-> m (Either Text Text)
pandoc mArgs trust txt = liftIO $
do let args = case mArgs of
Nothing -> []
(Just a) -> a
(inh, outh, errh, ph) <- runInteractiveProcess "pandoc" args Nothing Nothing
_ <- forkIO $ do T.hPutStr inh txt
hClose inh
mvOut <- newEmptyMVar
_ <- forkIO $ do c <- T.hGetContents outh
putMVar mvOut c
mvErr <- newEmptyMVar
_ <- forkIO $ do c <- T.hGetContents errh
putMVar mvErr c
ec <- waitForProcess ph
case ec of
(ExitFailure _) ->
do e <- readMVar mvErr
return (Left e)
ExitSuccess ->
do m <- readMVar mvOut
return (Right ((if (trust == Untrusted) then sanitizeBalance else id) m))