{-# LANGUAGE CPP, ScopedTypeVariables, NoImplicitPrelude #-}
module Text.CSL.Compat.Pandoc (
writeMarkdown,
writePlain,
writeNative,
writeHtmlString,
readNative,
readHtml,
readMarkdown,
readLaTeX,
fetchItem,
pipeProcess ) where
import Prelude
import qualified Control.Exception as E
import System.Exit (ExitCode)
import Data.ByteString.Lazy as BL
import Data.ByteString as B
import Text.Pandoc (Pandoc, ReaderOptions(..), def, WrapOption(..),
WriterOptions(..))
import Text.Pandoc (Extension(..), pandocExtensions)
import qualified Text.Pandoc as Pandoc
import qualified Text.Pandoc.Process
import qualified Data.Text as T
import Text.Pandoc.MIME (MimeType)
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Class (runPure, runIO)
import qualified Text.Pandoc.Class (fetchItem)
import Control.Monad.Except (runExceptT, lift)
import Text.Pandoc.Extensions (extensionsFromList, disableExtension)
readHtml, readLaTeX, readMarkdown, readNative :: String -> Pandoc
writeMarkdown, writePlain, writeNative, writeHtmlString :: Pandoc -> String
readHtml :: String -> Pandoc
readHtml = (PandocError -> Pandoc)
-> (Pandoc -> Pandoc) -> Either PandocError Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Pandoc
forall a. Monoid a => a
mempty Pandoc -> Pandoc
forall a. a -> a
id (Either PandocError Pandoc -> Pandoc)
-> (String -> Either PandocError Pandoc) -> String -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Pandoc -> Either PandocError Pandoc
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Pandoc -> Either PandocError Pandoc)
-> (String -> PandocPure Pandoc)
-> String
-> Either PandocError Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReaderOptions -> Text -> PandocPure Pandoc
forall (m :: * -> *).
PandocMonad m =>
ReaderOptions -> Text -> m Pandoc
Pandoc.readHtml
ReaderOptions
forall a. Default a => a
def{ readerExtensions :: Extensions
readerExtensions = [Extension] -> Extensions
extensionsFromList [Extension
Ext_native_divs,
Extension
Ext_native_spans, Extension
Ext_raw_html, Extension
Ext_smart] } (Text -> PandocPure Pandoc)
-> (String -> Text) -> String -> PandocPure Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> Text
T.pack
readMarkdown :: String -> Pandoc
readMarkdown = (PandocError -> Pandoc)
-> (Pandoc -> Pandoc) -> Either PandocError Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Pandoc
forall a. Monoid a => a
mempty Pandoc -> Pandoc
forall a. a -> a
id (Either PandocError Pandoc -> Pandoc)
-> (String -> Either PandocError Pandoc) -> String -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Pandoc -> Either PandocError Pandoc
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Pandoc -> Either PandocError Pandoc)
-> (String -> PandocPure Pandoc)
-> String
-> Either PandocError Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReaderOptions -> Text -> PandocPure Pandoc
forall (m :: * -> *).
PandocMonad m =>
ReaderOptions -> Text -> m Pandoc
Pandoc.readMarkdown
ReaderOptions
forall a. Default a => a
def{ readerExtensions :: Extensions
readerExtensions = Extensions
pandocExtensions, readerStandalone :: Bool
readerStandalone = Bool
True } (Text -> PandocPure Pandoc)
-> (String -> Text) -> String -> PandocPure Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> Text
T.pack
readLaTeX :: String -> Pandoc
readLaTeX = (PandocError -> Pandoc)
-> (Pandoc -> Pandoc) -> Either PandocError Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Pandoc
forall a. Monoid a => a
mempty Pandoc -> Pandoc
forall a. a -> a
id (Either PandocError Pandoc -> Pandoc)
-> (String -> Either PandocError Pandoc) -> String -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Pandoc -> Either PandocError Pandoc
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Pandoc -> Either PandocError Pandoc)
-> (String -> PandocPure Pandoc)
-> String
-> Either PandocError Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReaderOptions -> Text -> PandocPure Pandoc
forall (m :: * -> *).
PandocMonad m =>
ReaderOptions -> Text -> m Pandoc
Pandoc.readLaTeX
ReaderOptions
forall a. Default a => a
def{ readerExtensions :: Extensions
readerExtensions = [Extension] -> Extensions
extensionsFromList [Extension
Ext_raw_tex, Extension
Ext_smart] } (Text -> PandocPure Pandoc)
-> (String -> Text) -> String -> PandocPure Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> Text
T.pack
readNative :: String -> Pandoc
readNative = (PandocError -> Pandoc)
-> (Pandoc -> Pandoc) -> Either PandocError Pandoc -> Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Pandoc
forall a. Monoid a => a
mempty Pandoc -> Pandoc
forall a. a -> a
id (Either PandocError Pandoc -> Pandoc)
-> (String -> Either PandocError Pandoc) -> String -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Pandoc -> Either PandocError Pandoc
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Pandoc -> Either PandocError Pandoc)
-> (String -> PandocPure Pandoc)
-> String
-> Either PandocError Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReaderOptions -> Text -> PandocPure Pandoc
forall (m :: * -> *).
PandocMonad m =>
ReaderOptions -> Text -> m Pandoc
Pandoc.readNative ReaderOptions
forall a. Default a => a
def (Text -> PandocPure Pandoc)
-> (String -> Text) -> String -> PandocPure Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
writeMarkdown :: Pandoc -> String
writeMarkdown = (PandocError -> String)
-> (Text -> String) -> Either PandocError Text -> String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> String
forall a. Monoid a => a
mempty Text -> String
T.unpack (Either PandocError Text -> String)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Text -> Either PandocError Text
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Text -> Either PandocError Text)
-> (Pandoc -> PandocPure Text) -> Pandoc -> Either PandocError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterOptions -> Pandoc -> PandocPure Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
Pandoc.writeMarkdown
WriterOptions
forall a. Default a => a
def{ writerExtensions :: Extensions
writerExtensions = Extension -> Extensions -> Extensions
disableExtension Extension
Ext_smart (Extensions -> Extensions) -> Extensions -> Extensions
forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
disableExtension Extension
Ext_bracketed_spans (Extensions -> Extensions) -> Extensions -> Extensions
forall a b. (a -> b) -> a -> b
$
Extension -> Extensions -> Extensions
disableExtension Extension
Ext_raw_attribute (Extensions -> Extensions) -> Extensions -> Extensions
forall a b. (a -> b) -> a -> b
$
Extensions
pandocExtensions,
writerWrapText :: WrapOption
writerWrapText = WrapOption
WrapNone }
writePlain :: Pandoc -> String
writePlain = (PandocError -> String)
-> (Text -> String) -> Either PandocError Text -> String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> String
forall a. Monoid a => a
mempty Text -> String
T.unpack (Either PandocError Text -> String)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Text -> Either PandocError Text
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Text -> Either PandocError Text)
-> (Pandoc -> PandocPure Text) -> Pandoc -> Either PandocError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterOptions -> Pandoc -> PandocPure Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
Pandoc.writePlain WriterOptions
forall a. Default a => a
def
writeNative :: Pandoc -> String
writeNative = (PandocError -> String)
-> (Text -> String) -> Either PandocError Text -> String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> String
forall a. Monoid a => a
mempty Text -> String
T.unpack (Either PandocError Text -> String)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Text -> Either PandocError Text
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Text -> Either PandocError Text)
-> (Pandoc -> PandocPure Text) -> Pandoc -> Either PandocError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterOptions -> Pandoc -> PandocPure Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
Pandoc.writeNative WriterOptions
forall a. Default a => a
def{ writerTemplate :: Maybe (Template Text)
writerTemplate = Template Text -> Maybe (Template Text)
forall a. a -> Maybe a
Just Template Text
forall a. Monoid a => a
mempty }
writeHtmlString :: Pandoc -> String
writeHtmlString = (PandocError -> String)
-> (Text -> String) -> Either PandocError Text -> String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> String
forall a. Monoid a => a
mempty Text -> String
T.unpack (Either PandocError Text -> String)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PandocPure Text -> Either PandocError Text
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure Text -> Either PandocError Text)
-> (Pandoc -> PandocPure Text) -> Pandoc -> Either PandocError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterOptions -> Pandoc -> PandocPure Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
Pandoc.writeHtml4String
WriterOptions
forall a. Default a => a
def{ writerExtensions :: Extensions
writerExtensions = [Extension] -> Extensions
extensionsFromList
[Extension
Ext_native_divs, Extension
Ext_native_spans, Extension
Ext_raw_html],
writerWrapText :: WrapOption
writerWrapText = WrapOption
WrapPreserve }
pipeProcess :: Maybe [(String, String)] -> FilePath -> [String]
-> BL.ByteString -> IO (ExitCode,BL.ByteString)
pipeProcess :: Maybe [(String, String)]
-> String -> [String] -> ByteString -> IO (ExitCode, ByteString)
pipeProcess = Maybe [(String, String)]
-> String -> [String] -> ByteString -> IO (ExitCode, ByteString)
Text.Pandoc.Process.pipeProcess
fetchItem :: String
-> IO (Either E.SomeException (B.ByteString, Maybe MimeType))
fetchItem :: String -> IO (Either SomeException (ByteString, Maybe Text))
fetchItem s :: String
s = do
Either PandocError (Either PandocError (ByteString, Maybe Text))
res <- PandocIO (Either PandocError (ByteString, Maybe Text))
-> IO
(Either PandocError (Either PandocError (ByteString, Maybe Text)))
forall a. PandocIO a -> IO (Either PandocError a)
runIO (PandocIO (Either PandocError (ByteString, Maybe Text))
-> IO
(Either PandocError (Either PandocError (ByteString, Maybe Text))))
-> PandocIO (Either PandocError (ByteString, Maybe Text))
-> IO
(Either PandocError (Either PandocError (ByteString, Maybe Text)))
forall a b. (a -> b) -> a -> b
$ ExceptT PandocError PandocIO (ByteString, Maybe Text)
-> PandocIO (Either PandocError (ByteString, Maybe Text))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT PandocError PandocIO (ByteString, Maybe Text)
-> PandocIO (Either PandocError (ByteString, Maybe Text)))
-> ExceptT PandocError PandocIO (ByteString, Maybe Text)
-> PandocIO (Either PandocError (ByteString, Maybe Text))
forall a b. (a -> b) -> a -> b
$ PandocIO (ByteString, Maybe Text)
-> ExceptT PandocError PandocIO (ByteString, Maybe Text)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (PandocIO (ByteString, Maybe Text)
-> ExceptT PandocError PandocIO (ByteString, Maybe Text))
-> PandocIO (ByteString, Maybe Text)
-> ExceptT PandocError PandocIO (ByteString, Maybe Text)
forall a b. (a -> b) -> a -> b
$ Text -> PandocIO (ByteString, Maybe Text)
forall (m :: * -> *).
PandocMonad m =>
Text -> m (ByteString, Maybe Text)
Text.Pandoc.Class.fetchItem (Text -> PandocIO (ByteString, Maybe Text))
-> Text -> PandocIO (ByteString, Maybe Text)
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
s
Either SomeException (ByteString, Maybe Text)
-> IO (Either SomeException (ByteString, Maybe Text))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either SomeException (ByteString, Maybe Text)
-> IO (Either SomeException (ByteString, Maybe Text)))
-> Either SomeException (ByteString, Maybe Text)
-> IO (Either SomeException (ByteString, Maybe Text))
forall a b. (a -> b) -> a -> b
$ case Either PandocError (Either PandocError (ByteString, Maybe Text))
res of
Left e :: PandocError
e -> SomeException -> Either SomeException (ByteString, Maybe Text)
forall a b. a -> Either a b
Left (PandocError -> SomeException
forall e. Exception e => e -> SomeException
E.toException PandocError
e)
Right (Left (PandocError
e :: PandocError)) -> SomeException -> Either SomeException (ByteString, Maybe Text)
forall a b. a -> Either a b
Left (PandocError -> SomeException
forall e. Exception e => e -> SomeException
E.toException PandocError
e)
Right (Right r :: (ByteString, Maybe Text)
r) -> (ByteString, Maybe Text)
-> Either SomeException (ByteString, Maybe Text)
forall a b. b -> Either a b
Right (ByteString, Maybe Text)
r