{-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TupleSections #-} {- | Module : Text.Pandoc.App Copyright : Copyright (C) 2006-2019 John MacFarlane License : GNU GPL, version 2 or above Maintainer : John MacFarlane Stability : alpha Portability : portable Does a pandoc conversion based on command-line options. -} module Text.Pandoc.App.OutputSettings ( OutputSettings (..) , optToOutputSettings ) where import Prelude import qualified Control.Exception as E import Control.Monad import Control.Monad.Except (catchError, throwError) import Control.Monad.Trans import Data.Char (toLower) import Data.List (find, isPrefixOf, isSuffixOf) 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, lookupHighlightStyle, setVariable) import qualified Text.Pandoc.UTF8 as UTF8 -- | Settings specifying how document output should be produced. data OutputSettings = OutputSettings { outputFormat :: String , outputWriter :: Writer PandocIO , outputWriterName :: String , outputWriterOptions :: WriterOptions , outputPdfProgram :: Maybe String } readUtf8File :: PandocMonad m => FilePath -> m String readUtf8File = fmap UTF8.toString . readFileStrict -- | Get output settings from command line options. optToOutputSettings :: Opt -> PandocIO OutputSettings optToOutputSettings opts = do let outputFile = fromMaybe "-" (optOutputFile opts) when (optDumpArgs opts) . liftIO $ do UTF8.hPutStrLn stdout outputFile mapM_ (UTF8.hPutStrLn stdout) (optInputFiles opts) exitSuccess epubMetadata <- case optEpubMetadata opts of Nothing -> return Nothing Just fp -> Just <$> readUtf8File fp let pdfOutput = map toLower (takeExtension outputFile) == ".pdf" (writerName, maybePdfProg) <- if pdfOutput then liftIO $ pdfWriterAndProg (optTo opts) (optPdfEngine opts) else case optTo opts of Nothing | outputFile == "-" -> return ("html", Nothing) | otherwise -> case formatFromFilePaths [outputFile] of Nothing -> do report $ CouldNotDeduceFormat [takeExtension outputFile] "html" return ("html", Nothing) Just f -> return (f, Nothing) Just f -> return (f, Nothing) let format = if ".lua" `isSuffixOf` writerName then writerName else map toLower $ baseWriterName writerName (writer :: Writer PandocIO, writerExts) <- if ".lua" `isSuffixOf` format then return (TextWriter (\o d -> writeCustom writerName o d) :: Writer PandocIO, mempty) else getWriter (map toLower writerName) let standalone = optStandalone opts || not (isTextFormat format) || pdfOutput let addSyntaxMap existingmap f = do res <- liftIO (parseSyntaxDefinition f) case res of Left errstr -> throwError $ PandocSyntaxMapError errstr Right syn -> return $ addSyntaxDefinition syn existingmap syntaxMap <- foldM addSyntaxMap defaultSyntaxMap (optSyntaxDefinitions opts) hlStyle <- maybe (return Nothing) (fmap Just . lookupHighlightStyle) (optHighlightStyle opts) -- note: this reverses the list constructed in option parsing, -- which in turn was reversed from the command-line order, -- so we end up with the correct order in the variable list: let withList _ [] vars = return vars withList f (x:xs) vars = f x vars >>= withList f xs let setVariableM k v = return . setVariable k v let addContentsAsVariable varname fp vars = do s <- UTF8.toString . fst <$> fetchItem fp setVariableM varname s vars curdir <- liftIO getCurrentDirectory variables <- withList (setVariableM "sourcefile") -- we reverse this list because, unlike -- the other option lists here, it is -- not reversed when parsed from CLI arguments. -- See withList, above. (reverse $ optInputFiles opts) (optVariables opts) >>= setVariableM "outputfile" (fromMaybe "-" (optOutputFile opts)) >>= withList (addContentsAsVariable "include-before") (optIncludeBeforeBody opts) >>= withList (addContentsAsVariable "include-after") (optIncludeAfterBody opts) >>= withList (addContentsAsVariable "header-includes") (optIncludeInHeader opts) >>= withList (setVariableM "css") (optCss opts) >>= maybe return (setVariableM "title-prefix") (optTitlePrefix opts) >>= maybe return (setVariableM "epub-cover-image") (optEpubCoverImage opts) >>= setVariableM "curdir" curdir >>= (\vars -> if format == "dzslides" then do dztempl <- UTF8.toString <$> readDataFile ("dzslides" "template.html") let dzline = "