2014-08-30 18:45:58 -07:00
|
|
|
{-# LANGUAGE OverloadedStrings, CPP, ScopedTypeVariables #-}
|
2012-01-20 18:57:16 -08:00
|
|
|
{-
|
2015-04-26 10:18:29 -07:00
|
|
|
Copyright (C) 2012-2015 John MacFarlane <jgm@berkeley.edu>
|
2012-01-20 18:57:16 -08:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
-}
|
|
|
|
|
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc.PDF
|
2015-04-26 10:18:29 -07:00
|
|
|
Copyright : Copyright (C) 2012-2015 John MacFarlane
|
2012-01-20 18:57:16 -08:00
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
|
|
Stability : alpha
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Conversion of LaTeX documents to PDF.
|
|
|
|
-}
|
2013-07-20 12:14:43 -07:00
|
|
|
module Text.Pandoc.PDF ( makePDF ) where
|
2012-01-20 18:57:16 -08:00
|
|
|
|
|
|
|
import Data.ByteString.Lazy (ByteString)
|
|
|
|
import qualified Data.ByteString.Lazy as B
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BC
|
2013-07-20 12:14:43 -07:00
|
|
|
import qualified Data.ByteString as BS
|
2015-11-09 11:15:11 -08:00
|
|
|
import Text.Pandoc.Compat.Monoid ((<>))
|
2012-01-20 19:04:08 -08:00
|
|
|
import System.Exit (ExitCode (..))
|
2012-01-20 18:57:16 -08:00
|
|
|
import System.FilePath
|
2014-12-26 11:19:55 -07:00
|
|
|
import System.IO (stderr, stdout)
|
2012-01-20 18:57:16 -08:00
|
|
|
import System.Directory
|
2014-05-28 10:40:50 -07:00
|
|
|
import Data.Digest.Pure.SHA (showDigest, sha1)
|
2013-07-18 21:51:23 -07:00
|
|
|
import System.Environment
|
2014-12-26 11:19:55 -07:00
|
|
|
import Control.Monad (unless, when, (<=<))
|
2014-08-30 18:45:58 -07:00
|
|
|
import qualified Control.Exception as E
|
2012-02-07 22:00:22 -08:00
|
|
|
import Data.List (isInfixOf)
|
2013-12-19 21:07:09 -05:00
|
|
|
import Data.Maybe (fromMaybe)
|
2013-07-20 12:14:43 -07:00
|
|
|
import qualified Text.Pandoc.UTF8 as UTF8
|
|
|
|
import Text.Pandoc.Definition
|
2013-08-10 18:45:00 -07:00
|
|
|
import Text.Pandoc.Walk (walkM)
|
2015-10-20 08:15:12 -07:00
|
|
|
import Text.Pandoc.Shared (fetchItem', warn, withTempDir, inDirectory)
|
2013-07-20 12:14:43 -07:00
|
|
|
import Text.Pandoc.Options (WriterOptions(..))
|
2014-08-12 15:09:43 +01:00
|
|
|
import Text.Pandoc.MIME (extensionFromMimeType, getMimeType)
|
2013-08-08 15:15:20 -07:00
|
|
|
import Text.Pandoc.Process (pipeProcess)
|
|
|
|
import qualified Data.ByteString.Lazy as BL
|
2014-08-12 15:09:43 +01:00
|
|
|
import qualified Codec.Picture as JP
|
2014-03-10 11:16:09 -07:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
import Data.List (intercalate)
|
|
|
|
#endif
|
2012-01-20 18:57:16 -08:00
|
|
|
|
2014-03-10 11:16:09 -07:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
changePathSeparators :: FilePath -> FilePath
|
|
|
|
changePathSeparators = intercalate "/" . splitDirectories
|
2013-04-26 20:33:15 -07:00
|
|
|
#endif
|
|
|
|
|
2013-07-20 12:14:43 -07:00
|
|
|
makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex)
|
|
|
|
-> (WriterOptions -> Pandoc -> String) -- ^ writer
|
|
|
|
-> WriterOptions -- ^ options
|
|
|
|
-> Pandoc -- ^ document
|
2012-01-20 19:11:35 -08:00
|
|
|
-> IO (Either ByteString ByteString)
|
2013-07-20 12:14:43 -07:00
|
|
|
makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
|
2014-07-30 14:07:31 -07:00
|
|
|
doc' <- handleImages opts tmpdir doc
|
2013-07-20 12:14:43 -07:00
|
|
|
let source = writer opts doc'
|
2015-03-04 15:25:56 +05:30
|
|
|
args = writerLaTeXArgs opts
|
2015-10-20 08:15:12 -07:00
|
|
|
case program of
|
|
|
|
"context" -> context2pdf (writerVerbose opts) tmpdir source
|
|
|
|
_ -> tex2pdf' (writerVerbose opts) args tmpdir program source
|
2012-01-20 18:57:16 -08:00
|
|
|
|
2014-07-30 14:07:31 -07:00
|
|
|
handleImages :: WriterOptions
|
2013-07-20 12:14:43 -07:00
|
|
|
-> FilePath -- ^ temp dir to store images
|
|
|
|
-> Pandoc -- ^ document
|
|
|
|
-> IO Pandoc
|
2014-08-12 15:09:43 +01:00
|
|
|
handleImages opts tmpdir = walkM (convertImages tmpdir) <=< walkM (handleImage' opts tmpdir)
|
2013-07-20 12:14:43 -07:00
|
|
|
|
2014-07-30 14:07:31 -07:00
|
|
|
handleImage' :: WriterOptions
|
2013-07-20 12:14:43 -07:00
|
|
|
-> FilePath
|
|
|
|
-> Inline
|
|
|
|
-> IO Inline
|
2015-04-02 21:04:43 -07:00
|
|
|
handleImage' opts tmpdir (Image attr ils (src,tit)) = do
|
2013-07-20 12:14:43 -07:00
|
|
|
exists <- doesFileExist src
|
|
|
|
if exists
|
2015-04-02 21:04:43 -07:00
|
|
|
then return $ Image attr ils (src,tit)
|
2013-07-20 12:14:43 -07:00
|
|
|
else do
|
2014-07-30 14:07:31 -07:00
|
|
|
res <- fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
|
2013-07-20 12:14:43 -07:00
|
|
|
case res of
|
|
|
|
Right (contents, Just mime) -> do
|
2013-12-19 21:07:09 -05:00
|
|
|
let ext = fromMaybe (takeExtension src) $
|
2013-07-20 12:14:43 -07:00
|
|
|
extensionFromMimeType mime
|
2014-05-28 10:40:50 -07:00
|
|
|
let basename = showDigest $ sha1 $ BL.fromChunks [contents]
|
2014-03-10 16:23:57 -07:00
|
|
|
let fname = tmpdir </> basename <.> ext
|
2013-07-20 12:14:43 -07:00
|
|
|
BS.writeFile fname contents
|
2015-04-02 21:04:43 -07:00
|
|
|
return $ Image attr ils (fname,tit)
|
2013-07-20 12:14:43 -07:00
|
|
|
_ -> do
|
|
|
|
warn $ "Could not find image `" ++ src ++ "', skipping..."
|
2015-04-02 21:04:43 -07:00
|
|
|
return $ Image attr ils (src,tit)
|
2013-07-20 12:14:43 -07:00
|
|
|
handleImage' _ _ x = return x
|
|
|
|
|
2014-08-12 15:09:43 +01:00
|
|
|
convertImages :: FilePath -> Inline -> IO Inline
|
2015-04-02 21:04:43 -07:00
|
|
|
convertImages tmpdir (Image attr ils (src, tit)) = do
|
2014-08-12 15:09:43 +01:00
|
|
|
img <- convertImage tmpdir src
|
|
|
|
newPath <-
|
|
|
|
case img of
|
2015-04-12 21:18:21 -07:00
|
|
|
Left e -> src <$ warn e
|
2014-08-30 18:45:58 -07:00
|
|
|
Right fp -> return fp
|
2015-04-02 21:04:43 -07:00
|
|
|
return (Image attr ils (newPath, tit))
|
2014-08-12 15:09:43 +01:00
|
|
|
convertImages _ x = return x
|
|
|
|
|
|
|
|
-- Convert formats which do not work well in pdf to png
|
2014-08-30 18:45:58 -07:00
|
|
|
convertImage :: FilePath -> FilePath -> IO (Either String FilePath)
|
2014-08-12 15:09:43 +01:00
|
|
|
convertImage tmpdir fname =
|
|
|
|
case mime of
|
|
|
|
Just "image/png" -> doNothing
|
|
|
|
Just "image/jpeg" -> doNothing
|
|
|
|
Just "application/pdf" -> doNothing
|
2014-08-30 18:45:58 -07:00
|
|
|
_ -> JP.readImage fname >>= \res ->
|
|
|
|
case res of
|
2015-04-12 21:59:06 -07:00
|
|
|
Left _ -> return $ Left $ "Unable to convert `" ++
|
2015-04-12 21:18:21 -07:00
|
|
|
fname ++ "' for use with pdflatex."
|
2014-08-30 18:45:58 -07:00
|
|
|
Right img ->
|
|
|
|
E.catch (Right fileOut <$ JP.savePngImage fileOut img) $
|
|
|
|
\(e :: E.SomeException) -> return (Left (show e))
|
2014-08-12 15:09:43 +01:00
|
|
|
where
|
|
|
|
fileOut = replaceDirectory (replaceExtension fname (".png")) tmpdir
|
|
|
|
mime = getMimeType fname
|
2014-08-30 18:45:58 -07:00
|
|
|
doNothing = return (Right fname)
|
2014-08-12 15:09:43 +01:00
|
|
|
|
2014-12-26 11:19:55 -07:00
|
|
|
tex2pdf' :: Bool -- ^ Verbose output
|
2015-03-04 15:25:56 +05:30
|
|
|
-> [String] -- ^ Arguments to the latex-engine
|
2014-12-26 11:19:55 -07:00
|
|
|
-> FilePath -- ^ temp directory for output
|
2012-01-20 21:36:04 -08:00
|
|
|
-> String -- ^ tex program
|
2012-01-20 18:57:16 -08:00
|
|
|
-> String -- ^ tex source
|
2012-01-20 19:11:35 -08:00
|
|
|
-> IO (Either ByteString ByteString)
|
2015-03-04 15:25:56 +05:30
|
|
|
tex2pdf' verbose args tmpDir program source = do
|
2012-02-07 22:00:22 -08:00
|
|
|
let numruns = if "\\tableofcontents" `isInfixOf` source
|
2012-03-19 09:25:33 -07:00
|
|
|
then 3 -- to get page numbers
|
|
|
|
else 2 -- 1 run won't give you PDF bookmarks
|
2015-03-04 15:25:56 +05:30
|
|
|
(exit, log', mbPdf) <- runTeXProgram verbose program args 1 numruns tmpDir source
|
2012-01-20 18:57:16 -08:00
|
|
|
case (exit, mbPdf) of
|
2013-08-11 16:16:24 -07:00
|
|
|
(ExitFailure _, _) -> do
|
|
|
|
let logmsg = extractMsg log'
|
|
|
|
let extramsg =
|
|
|
|
case logmsg of
|
2015-01-11 15:18:40 +01:00
|
|
|
x | ("! Package inputenc Error" `BC.isPrefixOf` x
|
|
|
|
&& program /= "xelatex")
|
|
|
|
-> "\nTry running pandoc with --latex-engine=xelatex."
|
2013-08-11 16:16:24 -07:00
|
|
|
_ -> ""
|
2014-03-19 11:09:36 -07:00
|
|
|
return $ Left $ logmsg <> extramsg
|
|
|
|
(ExitSuccess, Nothing) -> return $ Left ""
|
2012-01-20 18:57:16 -08:00
|
|
|
(ExitSuccess, Just pdf) -> return $ Right pdf
|
|
|
|
|
|
|
|
-- parsing output
|
|
|
|
|
|
|
|
extractMsg :: ByteString -> ByteString
|
2012-01-20 19:04:08 -08:00
|
|
|
extractMsg log' = do
|
|
|
|
let msg' = dropWhile (not . ("!" `BC.isPrefixOf`)) $ BC.lines log'
|
2012-01-20 18:57:16 -08:00
|
|
|
let (msg'',rest) = break ("l." `BC.isPrefixOf`) msg'
|
|
|
|
let lineno = take 1 rest
|
2012-01-21 14:18:36 -08:00
|
|
|
if null msg'
|
|
|
|
then log'
|
|
|
|
else BC.unlines (msg'' ++ lineno)
|
2012-01-20 18:57:16 -08:00
|
|
|
|
2015-10-20 08:15:12 -07:00
|
|
|
extractConTeXtMsg :: ByteString -> ByteString
|
|
|
|
extractConTeXtMsg log' = do
|
|
|
|
let msg' = take 1 $
|
|
|
|
dropWhile (not . ("tex error" `BC.isPrefixOf`)) $ BC.lines log'
|
|
|
|
if null msg'
|
|
|
|
then log'
|
|
|
|
else BC.unlines msg'
|
|
|
|
|
2012-01-20 18:57:16 -08:00
|
|
|
-- running tex programs
|
|
|
|
|
|
|
|
-- Run a TeX program on an input bytestring and return (exit code,
|
|
|
|
-- contents of stdout, contents of produced PDF if any). Rerun
|
2012-02-06 12:12:23 -08:00
|
|
|
-- a fixed number of times to resolve references.
|
2015-03-04 15:25:56 +05:30
|
|
|
runTeXProgram :: Bool -> String -> [String] -> Int -> Int -> FilePath -> String
|
2012-01-20 18:57:16 -08:00
|
|
|
-> IO (ExitCode, ByteString, Maybe ByteString)
|
2015-03-04 15:25:56 +05:30
|
|
|
runTeXProgram verbose program args runNumber numRuns tmpDir source = do
|
2014-03-10 16:23:57 -07:00
|
|
|
let file = tmpDir </> "input.tex"
|
2012-02-06 12:12:23 -08:00
|
|
|
exists <- doesFileExist file
|
|
|
|
unless exists $ UTF8.writeFile file source
|
2014-02-23 20:35:07 -08:00
|
|
|
#ifdef _WINDOWS
|
2014-03-10 16:23:57 -07:00
|
|
|
-- note: we want / even on Windows, for TexLive
|
|
|
|
let tmpDir' = changePathSeparators tmpDir
|
|
|
|
let file' = changePathSeparators file
|
2014-02-23 20:35:07 -08:00
|
|
|
#else
|
2014-03-10 16:23:57 -07:00
|
|
|
let tmpDir' = tmpDir
|
|
|
|
let file' = file
|
2014-02-23 20:35:07 -08:00
|
|
|
#endif
|
2014-03-10 16:23:57 -07:00
|
|
|
let programArgs = ["-halt-on-error", "-interaction", "nonstopmode",
|
2015-07-08 17:37:54 -07:00
|
|
|
"-output-directory", tmpDir'] ++ args ++ [file']
|
2014-03-10 16:23:57 -07:00
|
|
|
env' <- getEnvironment
|
|
|
|
let sep = searchPathSeparator:[]
|
|
|
|
let texinputs = maybe (tmpDir' ++ sep) ((tmpDir' ++ sep) ++)
|
2013-07-18 21:51:23 -07:00
|
|
|
$ lookup "TEXINPUTS" env'
|
|
|
|
let env'' = ("TEXINPUTS", texinputs) :
|
|
|
|
[(k,v) | (k,v) <- env', k /= "TEXINPUTS"]
|
2014-12-26 11:19:55 -07:00
|
|
|
when (verbose && runNumber == 1) $ do
|
2015-05-20 15:43:42 -07:00
|
|
|
putStrLn $ "[makePDF] temp dir:"
|
|
|
|
putStrLn tmpDir'
|
2014-12-26 11:19:55 -07:00
|
|
|
putStrLn $ "[makePDF] Command line:"
|
|
|
|
putStrLn $ program ++ " " ++ unwords (map show programArgs)
|
|
|
|
putStr "\n"
|
|
|
|
putStrLn $ "[makePDF] Environment:"
|
|
|
|
mapM_ print env''
|
|
|
|
putStr "\n"
|
|
|
|
putStrLn $ "[makePDF] Contents of " ++ file' ++ ":"
|
|
|
|
B.readFile file' >>= B.putStr
|
|
|
|
putStr "\n"
|
2013-08-08 15:15:20 -07:00
|
|
|
(exit, out, err) <- pipeProcess (Just env'') program programArgs BL.empty
|
2014-12-26 11:19:55 -07:00
|
|
|
when verbose $ do
|
|
|
|
putStrLn $ "[makePDF] Run #" ++ show runNumber
|
|
|
|
B.hPutStr stdout out
|
|
|
|
B.hPutStr stderr err
|
|
|
|
putStr "\n"
|
|
|
|
if runNumber <= numRuns
|
2015-03-04 15:25:56 +05:30
|
|
|
then runTeXProgram verbose program args (runNumber + 1) numRuns tmpDir source
|
2012-02-06 12:12:23 -08:00
|
|
|
else do
|
|
|
|
let pdfFile = replaceDirectory (replaceExtension file ".pdf") tmpDir
|
|
|
|
pdfExists <- doesFileExist pdfFile
|
|
|
|
pdf <- if pdfExists
|
2014-04-05 19:57:42 -07:00
|
|
|
-- We read PDF as a strict bytestring to make sure that the
|
|
|
|
-- temp directory is removed on Windows.
|
|
|
|
-- See https://github.com/jgm/pandoc/issues/1192.
|
|
|
|
then (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
|
2012-02-06 12:12:23 -08:00
|
|
|
else return Nothing
|
|
|
|
return (exit, out <> err, pdf)
|
2012-01-20 18:57:16 -08:00
|
|
|
|
2015-10-20 08:15:12 -07:00
|
|
|
context2pdf :: Bool -- ^ Verbose output
|
|
|
|
-> FilePath -- ^ temp directory for output
|
|
|
|
-> String -- ^ ConTeXt source
|
|
|
|
-> IO (Either ByteString ByteString)
|
|
|
|
context2pdf verbose tmpDir source = inDirectory tmpDir $ do
|
|
|
|
let file = "input.tex"
|
|
|
|
UTF8.writeFile file source
|
|
|
|
#ifdef _WINDOWS
|
|
|
|
-- note: we want / even on Windows, for TexLive
|
|
|
|
let tmpDir' = changePathSeparators tmpDir
|
|
|
|
#else
|
|
|
|
let tmpDir' = tmpDir
|
|
|
|
#endif
|
|
|
|
let programArgs = ["--batchmode"] ++ [file]
|
|
|
|
env' <- getEnvironment
|
|
|
|
let sep = searchPathSeparator:[]
|
|
|
|
let texinputs = maybe (".." ++ sep) ((".." ++ sep) ++)
|
|
|
|
$ lookup "TEXINPUTS" env'
|
|
|
|
let env'' = ("TEXINPUTS", texinputs) :
|
|
|
|
[(k,v) | (k,v) <- env', k /= "TEXINPUTS"]
|
|
|
|
when verbose $ do
|
|
|
|
putStrLn $ "[makePDF] temp dir:"
|
|
|
|
putStrLn tmpDir'
|
|
|
|
putStrLn $ "[makePDF] Command line:"
|
|
|
|
putStrLn $ "context" ++ " " ++ unwords (map show programArgs)
|
|
|
|
putStr "\n"
|
|
|
|
putStrLn $ "[makePDF] Environment:"
|
|
|
|
mapM_ print env''
|
|
|
|
putStr "\n"
|
|
|
|
putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
|
|
|
|
B.readFile file >>= B.putStr
|
|
|
|
putStr "\n"
|
|
|
|
(exit, out, err) <- pipeProcess (Just env'') "context" programArgs BL.empty
|
|
|
|
when verbose $ do
|
|
|
|
B.hPutStr stdout out
|
|
|
|
B.hPutStr stderr err
|
|
|
|
putStr "\n"
|
|
|
|
let pdfFile = replaceExtension file ".pdf"
|
|
|
|
pdfExists <- doesFileExist pdfFile
|
|
|
|
mbPdf <- if pdfExists
|
|
|
|
-- We read PDF as a strict bytestring to make sure that the
|
|
|
|
-- temp directory is removed on Windows.
|
|
|
|
-- See https://github.com/jgm/pandoc/issues/1192.
|
|
|
|
then (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
|
|
|
|
else return Nothing
|
|
|
|
let log' = out <> err
|
|
|
|
case (exit, mbPdf) of
|
|
|
|
(ExitFailure _, _) -> do
|
|
|
|
let logmsg = extractConTeXtMsg log'
|
|
|
|
return $ Left logmsg
|
|
|
|
(ExitSuccess, Nothing) -> return $ Left ""
|
|
|
|
(ExitSuccess, Just pdf) -> return $ Right pdf
|