{-# LANGUAGE CPP                 #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections       #-}
{- |
   Module      : Text.Pandoc.App
   Copyright   : Copyright (C) 2006-2022 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley@edu>
   Stability   : alpha
   Portability : portable

Does a pandoc conversion based on command-line options.
-}
module Text.Pandoc.App.OutputSettings
  ( OutputSettings (..)
  , optToOutputSettings
  ) where
import qualified Data.Map as M
import qualified Data.Text as T
import Text.DocTemplates (toVal, Context(..), Val(..))
import qualified Control.Exception as E
import Control.Monad
import Control.Monad.Except (throwError)
import Control.Monad.Trans
import Data.Char (toLower)
import Data.List (find)
import Data.Maybe (fromMaybe)
import Skylighting (defaultSyntaxMap)
import Skylighting.Parser (addSyntaxDefinition, parseSyntaxDefinition)
import System.Directory (getCurrentDirectory)
import System.Exit (exitSuccess)
import System.FilePath
import System.IO (stdout)
import Text.Pandoc
import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths)
import Text.Pandoc.App.Opt (Opt (..))
import Text.Pandoc.App.CommandLineOptions (engines, setVariable)
import Text.Pandoc.Highlighting (lookupHighlightingStyle)
import Text.Pandoc.Writers.Custom (writeCustom)
import qualified Text.Pandoc.UTF8 as UTF8

readUtf8File :: PandocMonad m => FilePath -> m T.Text
readUtf8File :: forall (m :: * -> *). PandocMonad m => FilePath -> m Text
readUtf8File = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
UTF8.toText forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readFileStrict

-- | Settings specifying how document output should be produced.
data OutputSettings m = OutputSettings
  { forall (m :: * -> *). OutputSettings m -> Text
outputFormat :: T.Text
  , forall (m :: * -> *). OutputSettings m -> Writer m
outputWriter :: Writer m
  , forall (m :: * -> *). OutputSettings m -> Text
outputWriterName :: T.Text
  , forall (m :: * -> *). OutputSettings m -> WriterOptions
outputWriterOptions :: WriterOptions
  , forall (m :: * -> *). OutputSettings m -> Maybe FilePath
outputPdfProgram :: Maybe String
  }

-- | Get output settings from command line options.
optToOutputSettings :: (PandocMonad m, MonadIO m) => Opt -> m (OutputSettings m)
optToOutputSettings :: forall (m :: * -> *).
(PandocMonad m, MonadIO m) =>
Opt -> m (OutputSettings m)
optToOutputSettings Opt
opts = do
  let outputFile :: FilePath
outputFile = forall a. a -> Maybe a -> a
fromMaybe FilePath
"-" (Opt -> Maybe FilePath
optOutputFile Opt
opts)

  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Opt -> Bool
optDumpArgs Opt
opts) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
    Handle -> Text -> IO ()
UTF8.hPutStrLn Handle
stdout (FilePath -> Text
T.pack FilePath
outputFile)
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Handle -> Text -> IO ()
UTF8.hPutStrLn Handle
stdout forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Text
T.pack) (forall a. a -> Maybe a -> a
fromMaybe [] forall a b. (a -> b) -> a -> b
$ Opt -> Maybe [FilePath]
optInputFiles Opt
opts)
    forall a. IO a
exitSuccess

  Maybe Text
epubMetadata <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall (m :: * -> *). PandocMonad m => FilePath -> m Text
readUtf8File forall a b. (a -> b) -> a -> b
$ Opt -> Maybe FilePath
optEpubMetadata Opt
opts

  let pdfOutput :: Bool
pdfOutput = forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (FilePath -> FilePath
takeExtension FilePath
outputFile) forall a. Eq a => a -> a -> Bool
== FilePath
".pdf" Bool -> Bool -> Bool
||
                  Opt -> Maybe Text
optTo Opt
opts forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just Text
"pdf"
  (Text
writerName, Maybe FilePath
maybePdfProg) <-
    if Bool
pdfOutput
       then forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Maybe Text -> Maybe FilePath -> IO (Text, Maybe FilePath)
pdfWriterAndProg
               (case Opt -> Maybe Text
optTo Opt
opts of
                  Just Text
"pdf" -> forall a. Maybe a
Nothing
                  Maybe Text
x          -> Maybe Text
x)
               (Opt -> Maybe FilePath
optPdfEngine Opt
opts)
       else case Opt -> Maybe Text
optTo Opt
opts of
              Just Text
f -> forall (m :: * -> *) a. Monad m => a -> m a
return (Text
f, forall a. Maybe a
Nothing)
              Maybe Text
Nothing
               | FilePath
outputFile forall a. Eq a => a -> a -> Bool
== FilePath
"-" -> forall (m :: * -> *) a. Monad m => a -> m a
return (Text
"html", forall a. Maybe a
Nothing)
               | Bool
otherwise ->
                     case [FilePath] -> Maybe Text
formatFromFilePaths [FilePath
outputFile] of
                           Maybe Text
Nothing -> do
                             forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report forall a b. (a -> b) -> a -> b
$ [Text] -> Text -> LogMessage
CouldNotDeduceFormat
                                [FilePath -> Text
T.pack forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
takeExtension FilePath
outputFile] Text
"html"
                             forall (m :: * -> *) a. Monad m => a -> m a
return (Text
"html", forall a. Maybe a
Nothing)
                           Just Text
f  -> forall (m :: * -> *) a. Monad m => a -> m a
return (Text
f, forall a. Maybe a
Nothing)

  let format :: Text
format = if Text
".lua" Text -> Text -> Bool
`T.isSuffixOf` Text
writerName
                  then Text
writerName
                  else Text -> Text
T.toLower forall a b. (a -> b) -> a -> b
$ Text -> Text
baseWriterName Text
writerName

  let makeSandboxed :: Writer PandocPure -> Writer m
makeSandboxed Writer PandocPure
pureWriter =
          let files :: [FilePath]
files = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (:) (Opt -> Maybe FilePath
optReferenceDoc Opt
opts) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (:) (Opt -> Maybe FilePath
optEpubMetadata Opt
opts) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (:) (Opt -> Maybe FilePath
optEpubCoverImage Opt
opts) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (:) (Opt -> Maybe FilePath
optCSL Opt
opts) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (:) (Opt -> Maybe FilePath
optCitationAbbreviations Opt
opts) forall a b. (a -> b) -> a -> b
$
                      Opt -> [FilePath]
optEpubFonts Opt
opts forall a. [a] -> [a] -> [a]
++
                      Opt -> [FilePath]
optBibliography Opt
opts
           in  case Writer PandocPure
pureWriter of
                 TextWriter WriterOptions -> Pandoc -> PandocPure Text
w -> forall (m :: * -> *).
(WriterOptions -> Pandoc -> m Text) -> Writer m
TextWriter forall a b. (a -> b) -> a -> b
$ \WriterOptions
o Pandoc
d -> forall (m :: * -> *) a.
(PandocMonad m, MonadIO m) =>
[FilePath] -> PandocPure a -> m a
sandbox [FilePath]
files (WriterOptions -> Pandoc -> PandocPure Text
w WriterOptions
o Pandoc
d)
                 ByteStringWriter WriterOptions -> Pandoc -> PandocPure ByteString
w
                            -> forall (m :: * -> *).
(WriterOptions -> Pandoc -> m ByteString) -> Writer m
ByteStringWriter forall a b. (a -> b) -> a -> b
$ \WriterOptions
o Pandoc
d -> forall (m :: * -> *) a.
(PandocMonad m, MonadIO m) =>
[FilePath] -> PandocPure a -> m a
sandbox [FilePath]
files (WriterOptions -> Pandoc -> PandocPure ByteString
w WriterOptions
o Pandoc
d)


  (Writer m
writer, Extensions
writerExts) <-
            if Text
".lua" Text -> Text -> Bool
`T.isSuffixOf` Text
format
               then forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *).
(WriterOptions -> Pandoc -> m Text) -> Writer m
TextWriter
                       (\WriterOptions
o Pandoc
d -> forall (m :: * -> *).
(PandocMonad m, MonadIO m) =>
FilePath -> WriterOptions -> Pandoc -> m Text
writeCustom (Text -> FilePath
T.unpack Text
writerName) WriterOptions
o Pandoc
d), forall a. Monoid a => a
mempty)
               else if Opt -> Bool
optSandbox Opt
opts
                       then
                         case forall a. PandocPure a -> Either PandocError a
runPure (forall (m :: * -> *).
PandocMonad m =>
Text -> m (Writer m, Extensions)
getWriter Text
writerName) of
                           Left PandocError
e -> forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError PandocError
e
                           Right (Writer PandocPure
w, Extensions
wexts) ->
                                  forall (m :: * -> *) a. Monad m => a -> m a
return (forall {m :: * -> *}.
(PandocMonad m, MonadIO m) =>
Writer PandocPure -> Writer m
makeSandboxed Writer PandocPure
w, Extensions
wexts)
                       else forall (m :: * -> *).
PandocMonad m =>
Text -> m (Writer m, Extensions)
getWriter (Text -> Text
T.toLower Text
writerName)

  let standalone :: Bool
standalone = Opt -> Bool
optStandalone Opt
opts Bool -> Bool -> Bool
|| Bool -> Bool
not (Text -> Bool
isTextFormat Text
format) Bool -> Bool -> Bool
|| Bool
pdfOutput

  let addSyntaxMap :: SyntaxMap -> FilePath -> m SyntaxMap
addSyntaxMap SyntaxMap
existingmap FilePath
f = do
        Either FilePath Syntax
res <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IO (Either FilePath Syntax)
parseSyntaxDefinition FilePath
f)
        case Either FilePath Syntax
res of
              Left FilePath
errstr -> forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocSyntaxMapError forall a b. (a -> b) -> a -> b
$ FilePath -> Text
T.pack FilePath
errstr
              Right Syntax
syn   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Syntax -> SyntaxMap -> SyntaxMap
addSyntaxDefinition Syntax
syn SyntaxMap
existingmap

  SyntaxMap
syntaxMap <- forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM forall {m :: * -> *}.
(MonadIO m, MonadError PandocError m) =>
SyntaxMap -> FilePath -> m SyntaxMap
addSyntaxMap SyntaxMap
defaultSyntaxMap
                     (Opt -> [FilePath]
optSyntaxDefinitions Opt
opts)

  Maybe Style
hlStyle <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (forall (m :: * -> *). PandocMonad m => FilePath -> m Style
lookupHighlightingStyle forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> FilePath
T.unpack) forall a b. (a -> b) -> a -> b
$
               Opt -> Maybe Text
optHighlightStyle Opt
opts

  let setVariableM :: Text -> Text -> Context Text -> m (Context Text)
setVariableM Text
k Text
v = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Context Text -> Context Text
setVariable Text
k Text
v

  let setListVariableM :: Text -> [a] -> Context a -> m (Context a)
setListVariableM Text
_ [] Context a
ctx = forall (m :: * -> *) a. Monad m => a -> m a
return Context a
ctx
      setListVariableM Text
k [a]
vs Context a
ctx = do
        let ctxMap :: Map Text (Val a)
ctxMap = forall a. Context a -> Map Text (Val a)
unContext Context a
ctx
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Map Text (Val a) -> Context a
Context forall a b. (a -> b) -> a -> b
$
          case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
k Map Text (Val a)
ctxMap of
              Just (ListVal [Val a]
xs) -> forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
k
                                  (forall a. [Val a] -> Val a
ListVal forall a b. (a -> b) -> a -> b
$ [Val a]
xs forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map forall a b. ToContext a b => b -> Val a
toVal [a]
vs) Map Text (Val a)
ctxMap
              Just Val a
v -> forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
k
                         (forall a. [Val a] -> Val a
ListVal forall a b. (a -> b) -> a -> b
$ Val a
v forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map forall a b. ToContext a b => b -> Val a
toVal [a]
vs) Map Text (Val a)
ctxMap
              Maybe (Val a)
Nothing -> forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
k (forall a b. ToContext a b => b -> Val a
toVal [a]
vs) Map Text (Val a)
ctxMap

  let getTextContents :: FilePath -> f Text
getTextContents FilePath
fp = ByteString -> Text
UTF8.toText forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *).
PandocMonad m =>
Text -> m (ByteString, Maybe Text)
fetchItem (FilePath -> Text
T.pack FilePath
fp)

  let setFilesVariableM :: Text -> [FilePath] -> Context a -> m (Context a)
setFilesVariableM Text
k [FilePath]
fps Context a
ctx = do
        [Text]
xs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall (m :: * -> *). PandocMonad m => FilePath -> m Text
getTextContents [FilePath]
fps
        forall {m :: * -> *} {a} {a}.
(Monad m, ToContext a a, ToContext a [a]) =>
Text -> [a] -> Context a -> m (Context a)
setListVariableM Text
k [Text]
xs Context a
ctx

  FilePath
curdir <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO FilePath
getCurrentDirectory

  Context Text
variables <-
    forall (m :: * -> *) a. Monad m => a -> m a
return (Opt -> Context Text
optVariables Opt
opts)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall {m :: * -> *} {a} {a}.
(Monad m, ToContext a a, ToContext a [a]) =>
Text -> [a] -> Context a -> m (Context a)
setListVariableM Text
"sourcefile"
      (forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Text
"-"] (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FilePath -> Text
T.pack) (Opt -> Maybe [FilePath]
optInputFiles Opt
opts))
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall {m :: * -> *}.
Monad m =>
Text -> Text -> Context Text -> m (Context Text)
setVariableM Text
"outputfile" (FilePath -> Text
T.pack FilePath
outputFile)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall {m :: * -> *} {a}.
(PandocMonad m, ToContext a [Text], ToContext a Text) =>
Text -> [FilePath] -> Context a -> m (Context a)
setFilesVariableM Text
"include-before" (Opt -> [FilePath]
optIncludeBeforeBody Opt
opts)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall {m :: * -> *} {a}.
(PandocMonad m, ToContext a [Text], ToContext a Text) =>
Text -> [FilePath] -> Context a -> m (Context a)
setFilesVariableM Text
"include-after" (Opt -> [FilePath]
optIncludeAfterBody Opt
opts)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall {m :: * -> *} {a}.
(PandocMonad m, ToContext a [Text], ToContext a Text) =>
Text -> [FilePath] -> Context a -> m (Context a)
setFilesVariableM Text
"header-includes" (Opt -> [FilePath]
optIncludeInHeader Opt
opts)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall {m :: * -> *} {a} {a}.
(Monad m, ToContext a a, ToContext a [a]) =>
Text -> [a] -> Context a -> m (Context a)
setListVariableM Text
"css" (forall a b. (a -> b) -> [a] -> [b]
map FilePath -> Text
T.pack forall a b. (a -> b) -> a -> b
$ Opt -> [FilePath]
optCss Opt
opts)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *) a. Monad m => a -> m a
return (forall {m :: * -> *}.
Monad m =>
Text -> Text -> Context Text -> m (Context Text)
setVariableM Text
"title-prefix") (Opt -> Maybe Text
optTitlePrefix Opt
opts)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *) a. Monad m => a -> m a
return (forall {m :: * -> *}.
Monad m =>
Text -> Text -> Context Text -> m (Context Text)
setVariableM Text
"epub-cover-image")
                 (FilePath -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Opt -> Maybe FilePath
optEpubCoverImage Opt
opts)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall {m :: * -> *}.
Monad m =>
Text -> Text -> Context Text -> m (Context Text)
setVariableM Text
"curdir" (FilePath -> Text
T.pack FilePath
curdir)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    (\Context Text
vars ->  if Text
format forall a. Eq a => a -> a -> Bool
== Text
"dzslides"
                  then do
                      Text
dztempl <- ByteString -> Text
UTF8.toText forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile
                                   (FilePath
"dzslides" FilePath -> FilePath -> FilePath
</> FilePath
"template.html")
                      let dzline :: Text
dzline = Text
"<!-- {{{{ dzslides core"
                      let dzcore :: Text
dzcore = [Text] -> Text
T.unlines
                                 forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
dzline Text -> Text -> Bool
`T.isPrefixOf`))
                                 forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
dztempl
                      forall {m :: * -> *}.
Monad m =>
Text -> Text -> Context Text -> m (Context Text)
setVariableM Text
"dzslides-core" Text
dzcore Context Text
vars
                  else forall (m :: * -> *) a. Monad m => a -> m a
return Context Text
vars)

  Maybe (Template Text)
templ <- case Opt -> Maybe FilePath
optTemplate Opt
opts of
                  Maybe FilePath
_ | Bool -> Bool
not Bool
standalone -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
                  Maybe FilePath
Nothing -> forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => Text -> m (Template Text)
compileDefaultTemplate Text
format
                  Just FilePath
tp -> do
                    -- strip off extensions
                    let tp' :: FilePath
tp' = case FilePath -> FilePath
takeExtension FilePath
tp of
                                   FilePath
"" -> FilePath
tp FilePath -> FilePath -> FilePath
<.> Text -> FilePath
T.unpack Text
format
                                   FilePath
_  -> FilePath
tp
                    Either FilePath (Template Text)
res <- forall (m :: * -> *). PandocMonad m => FilePath -> m Text
getTemplate FilePath
tp' forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. WithPartials m a -> m a
runWithPartials forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(TemplateMonad m, TemplateTarget a) =>
FilePath -> Text -> m (Either FilePath (Template a))
compileTemplate FilePath
tp'
                    case Either FilePath (Template Text)
res of
                      Left  FilePath
e -> forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocTemplateError forall a b. (a -> b) -> a -> b
$ FilePath -> Text
T.pack FilePath
e
                      Right Template Text
t -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Template Text
t

  let writerOpts :: WriterOptions
writerOpts = forall a. Default a => a
def {
          writerTemplate :: Maybe (Template Text)
writerTemplate         = Maybe (Template Text)
templ
        , writerVariables :: Context Text
writerVariables        = Context Text
variables
        , writerTabStop :: Int
writerTabStop          = Opt -> Int
optTabStop Opt
opts
        , writerTableOfContents :: Bool
writerTableOfContents  = Opt -> Bool
optTableOfContents Opt
opts
        , writerHTMLMathMethod :: HTMLMathMethod
writerHTMLMathMethod   = Opt -> HTMLMathMethod
optHTMLMathMethod Opt
opts
        , writerIncremental :: Bool
writerIncremental      = Opt -> Bool
optIncremental Opt
opts
        , writerCiteMethod :: CiteMethod
writerCiteMethod       = Opt -> CiteMethod
optCiteMethod Opt
opts
        , writerNumberSections :: Bool
writerNumberSections   = Opt -> Bool
optNumberSections Opt
opts
        , writerNumberOffset :: [Int]
writerNumberOffset     = Opt -> [Int]
optNumberOffset Opt
opts
        , writerSectionDivs :: Bool
writerSectionDivs      = Opt -> Bool
optSectionDivs Opt
opts
        , writerExtensions :: Extensions
writerExtensions       = Extensions
writerExts
        , writerReferenceLinks :: Bool
writerReferenceLinks   = Opt -> Bool
optReferenceLinks Opt
opts
        , writerReferenceLocation :: ReferenceLocation
writerReferenceLocation = Opt -> ReferenceLocation
optReferenceLocation Opt
opts
        , writerDpi :: Int
writerDpi              = Opt -> Int
optDpi Opt
opts
        , writerWrapText :: WrapOption
writerWrapText         = Opt -> WrapOption
optWrap Opt
opts
        , writerColumns :: Int
writerColumns          = Opt -> Int
optColumns Opt
opts
        , writerEmailObfuscation :: ObfuscationMethod
writerEmailObfuscation = Opt -> ObfuscationMethod
optEmailObfuscation Opt
opts
        , writerIdentifierPrefix :: Text
writerIdentifierPrefix = Opt -> Text
optIdentifierPrefix Opt
opts
        , writerHtmlQTags :: Bool
writerHtmlQTags        = Opt -> Bool
optHtmlQTags Opt
opts
        , writerTopLevelDivision :: TopLevelDivision
writerTopLevelDivision = Opt -> TopLevelDivision
optTopLevelDivision Opt
opts
        , writerListings :: Bool
writerListings         = Opt -> Bool
optListings Opt
opts
        , writerSlideLevel :: Maybe Int
writerSlideLevel       = Opt -> Maybe Int
optSlideLevel Opt
opts
        , writerHighlightStyle :: Maybe Style
writerHighlightStyle   = Maybe Style
hlStyle
        , writerSetextHeaders :: Bool
writerSetextHeaders    = Opt -> Bool
optSetextHeaders Opt
opts
        , writerEpubSubdirectory :: Text
writerEpubSubdirectory = FilePath -> Text
T.pack forall a b. (a -> b) -> a -> b
$ Opt -> FilePath
optEpubSubdirectory Opt
opts
        , writerEpubMetadata :: Maybe Text
writerEpubMetadata     = Maybe Text
epubMetadata
        , writerEpubFonts :: [FilePath]
writerEpubFonts        = Opt -> [FilePath]
optEpubFonts Opt
opts
        , writerEpubChapterLevel :: Int
writerEpubChapterLevel = Opt -> Int
optEpubChapterLevel Opt
opts
        , writerTOCDepth :: Int
writerTOCDepth         = Opt -> Int
optTOCDepth Opt
opts
        , writerReferenceDoc :: Maybe FilePath
writerReferenceDoc     = Opt -> Maybe FilePath
optReferenceDoc Opt
opts
        , writerSyntaxMap :: SyntaxMap
writerSyntaxMap        = SyntaxMap
syntaxMap
        , writerPreferAscii :: Bool
writerPreferAscii      = Opt -> Bool
optAscii Opt
opts
        }
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ OutputSettings
    { outputFormat :: Text
outputFormat = Text
format
    , outputWriter :: Writer m
outputWriter = Writer m
writer
    , outputWriterName :: Text
outputWriterName = Text
writerName
    , outputWriterOptions :: WriterOptions
outputWriterOptions = WriterOptions
writerOpts
    , outputPdfProgram :: Maybe FilePath
outputPdfProgram = Maybe FilePath
maybePdfProg
    }

baseWriterName :: T.Text -> T.Text
baseWriterName :: Text -> Text
baseWriterName = (Char -> Bool) -> Text -> Text
T.takeWhile (\Char
c -> Char
c forall a. Eq a => a -> a -> Bool
/= Char
'+' Bool -> Bool -> Bool
&& Char
c forall a. Eq a => a -> a -> Bool
/= Char
'-')

pdfWriterAndProg :: Maybe T.Text              -- ^ user-specified writer name
                 -> Maybe String              -- ^ user-specified pdf-engine
                 -> IO (T.Text, Maybe String) -- ^ IO (writerName, maybePdfEngineProg)
pdfWriterAndProg :: Maybe Text -> Maybe FilePath -> IO (Text, Maybe FilePath)
pdfWriterAndProg Maybe Text
mWriter Maybe FilePath
mEngine =
  case Maybe Text -> Maybe FilePath -> Either Text (Text, FilePath)
go Maybe Text
mWriter Maybe FilePath
mEngine of
      Right (Text
writ, FilePath
prog) -> forall (m :: * -> *) a. Monad m => a -> m a
return (Text
writ, forall a. a -> Maybe a
Just FilePath
prog)
      Left Text
err           -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => e -> IO a
E.throwIO forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocAppError Text
err
    where
      go :: Maybe Text -> Maybe FilePath -> Either Text (Text, FilePath)
go Maybe Text
Nothing Maybe FilePath
Nothing       = forall a b. b -> Either a b
Right (Text
"latex", FilePath
"pdflatex")
      go (Just Text
writer) Maybe FilePath
Nothing = (Text
writer,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either Text FilePath
engineForWriter Text
writer
      go Maybe Text
Nothing (Just FilePath
engine) = (,FilePath
engine) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> Either Text Text
writerForEngine (FilePath -> FilePath
takeBaseName FilePath
engine)
      go (Just Text
writer) (Just FilePath
engine) | Text -> Bool
isCustomWriter Text
writer =
           -- custom writers can produce any format, so assume the user knows
           -- what they are doing.
           forall a b. b -> Either a b
Right (Text
writer, FilePath
engine)
      go (Just Text
writer) (Just FilePath
engine) =
           case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (forall a. Eq a => a -> a -> Bool
== (Text -> Text
baseWriterName Text
writer, FilePath -> FilePath
takeBaseName FilePath
engine)) [(Text, FilePath)]
engines of
                Just (Text, FilePath)
_  -> forall a b. b -> Either a b
Right (Text
writer, FilePath
engine)
                Maybe (Text, FilePath)
Nothing -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"pdf-engine " forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
T.pack FilePath
engine forall a. Semigroup a => a -> a -> a
<>
                           Text
" is not compatible with output format " forall a. Semigroup a => a -> a -> a
<> Text
writer

      writerForEngine :: FilePath -> Either Text Text
writerForEngine FilePath
eng = case [Text
f | (Text
f,FilePath
e) <- [(Text, FilePath)]
engines, FilePath
e forall a. Eq a => a -> a -> Bool
== FilePath
eng] of
                                 Text
fmt : [Text]
_ -> forall a b. b -> Either a b
Right Text
fmt
                                 []      -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                                   Text
"pdf-engine " forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
T.pack FilePath
eng forall a. Semigroup a => a -> a -> a
<> Text
" not known"

      engineForWriter :: Text -> Either Text FilePath
engineForWriter Text
"pdf" = forall a b. a -> Either a b
Left Text
"pdf writer"
      engineForWriter Text
w = case [FilePath
e | (Text
f,FilePath
e) <- [(Text, FilePath)]
engines, Text
f forall a. Eq a => a -> a -> Bool
== Text -> Text
baseWriterName Text
w] of
                                FilePath
eng : [FilePath]
_ -> forall a b. b -> Either a b
Right FilePath
eng
                                []      -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$
                                   Text
"cannot produce pdf output from " forall a. Semigroup a => a -> a -> a
<> Text
w

      isCustomWriter :: Text -> Bool
isCustomWriter Text
w = Text
".lua" Text -> Text -> Bool
`T.isSuffixOf` Text
w

isTextFormat :: T.Text -> Bool
isTextFormat :: Text -> Bool
isTextFormat Text
s =
  Text
s forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text
"odt",Text
"docx",Text
"epub2",Text
"epub3",Text
"epub",Text
"pptx",Text
"pdf"]