{-# 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 Data.Text (Text)
import Text.Pandoc (Extension (..), Pandoc, ReaderOptions(..), WrapOption(..),
WriterOptions(..), def, 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 :: Text -> Pandoc
readHtml :: Text -> 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)
-> (Text -> Either PandocError Pandoc) -> Text -> 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)
-> (Text -> PandocPure Pandoc) -> Text -> 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] }
readMarkdown :: Text -> Pandoc
readMarkdown :: Text -> 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)
-> (Text -> Either PandocError Pandoc) -> Text -> 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)
-> (Text -> PandocPure Pandoc) -> Text -> 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 }
readLaTeX :: Text -> Pandoc
readLaTeX :: Text -> 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)
-> (Text -> Either PandocError Pandoc) -> Text -> 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)
-> (Text -> PandocPure Pandoc) -> Text -> 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] }
readNative :: Text -> Pandoc
readNative :: Text -> 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)
-> (Text -> Either PandocError Pandoc) -> Text -> 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)
-> (Text -> PandocPure Pandoc) -> Text -> 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
writeMarkdown, writePlain, writeNative, writeHtmlString :: Pandoc -> Text
writeMarkdown :: Pandoc -> Text
writeMarkdown = (PandocError -> Text)
-> (Text -> Text) -> Either PandocError Text -> Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Text
forall a. Monoid a => a
mempty Text -> Text
forall a. a -> a
id (Either PandocError Text -> Text)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> Text
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 -> Text
writePlain = (PandocError -> Text)
-> (Text -> Text) -> Either PandocError Text -> Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Text
forall a. Monoid a => a
mempty Text -> Text
forall a. a -> a
id (Either PandocError Text -> Text)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> Text
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 -> Text
writeNative = (PandocError -> Text)
-> (Text -> Text) -> Either PandocError Text -> Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Text
forall a. Monoid a => a
mempty Text -> Text
forall a. a -> a
id (Either PandocError Text -> Text)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> Text
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 -> Text
writeHtmlString = (PandocError -> Text)
-> (Text -> Text) -> Either PandocError Text -> Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Text
forall a. Monoid a => a
mempty Text -> Text
forall a. a -> a
id (Either PandocError Text -> Text)
-> (Pandoc -> Either PandocError Text) -> Pandoc -> Text
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 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 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 (ByteString, Maybe Text)
r) -> (ByteString, Maybe Text)
-> Either SomeException (ByteString, Maybe Text)
forall a b. b -> Either a b
Right (ByteString, Maybe Text)
r