@@ -47,7 +47,7 @@ import Data.Maybe (fromMaybe)
4747import qualified Text.Pandoc.UTF8 as UTF8
4848import Text.Pandoc.Definition
4949import Text.Pandoc.Walk (walkM)
50−import Text.Pandoc.Shared (fetchItem', warn, withTempDir)
50+import Text.Pandoc.Shared (fetchItem', warn, withTempDir, inDirectory)
5151import Text.Pandoc.Options (WriterOptions(..))
5252import Text.Pandoc.MIME (extensionFromMimeType, getMimeType)
5353import Text.Pandoc.Process (pipeProcess)
@@ -71,7 +71,9 @@ makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
7171 doc' <- handleImages opts tmpdir doc
7272 let source = writer opts doc'
7373 args = writerLaTeXArgs opts
74− tex2pdf' (writerVerbose opts) args tmpdir program source
74+ case program of
75+ "context" -> context2pdf (writerVerbose opts) tmpdir source
76+ _ -> tex2pdf' (writerVerbose opts) args tmpdir program source
7577
7678handleImages :: WriterOptions
7779 -> FilePath -- ^ temp dir to store images
@@ -166,6 +168,14 @@ extractMsg log' = do
166168 then log'
167169 else BC.unlines (msg'' ++ lineno)
168170
171+extractConTeXtMsg :: ByteString -> ByteString
172+extractConTeXtMsg log' = do
173+ let msg' = take 1 $
174+ dropWhile (not . ("tex error" `BC.isPrefixOf`)) $ BC.lines log'
175+ if null msg'
176+ then log'
177+ else BC.unlines msg'
178+
169179-- running tex programs
170180
171181-- Run a TeX program on an input bytestring and return (exit code,
@@ -224,3 +234,55 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do
224234 else return Nothing
225235 return (exit, out <> err, pdf)
226236
237+context2pdf :: Bool -- ^ Verbose output
238+ -> FilePath -- ^ temp directory for output
239+ -> String -- ^ ConTeXt source
240+ -> IO (Either ByteString ByteString)
241+context2pdf verbose tmpDir source = inDirectory tmpDir $ do
242+ let file = "input.tex"
243+ UTF8.writeFile file source
244+#ifdef _WINDOWS
245+ -- note: we want / even on Windows, for TexLive
246+ let tmpDir' = changePathSeparators tmpDir
247+#else
248+ let tmpDir' = tmpDir
249+#endif
250+ let programArgs = ["--batchmode"] ++ [file]
251+ env' <- getEnvironment
252+ let sep = searchPathSeparator:[]
253+ let texinputs = maybe (".." ++ sep) ((".." ++ sep) ++)
254+ $ lookup "TEXINPUTS" env'
255+ let env'' = ("TEXINPUTS", texinputs) :
256+ [(k,v) | (k,v) <- env', k /= "TEXINPUTS"]
257+ when verbose $ do
258+ putStrLn $ "[makePDF] temp dir:"
259+ putStrLn tmpDir'
260+ putStrLn $ "[makePDF] Command line:"
261+ putStrLn $ "context" ++ " " ++ unwords (map show programArgs)
262+ putStr "\n"
263+ putStrLn $ "[makePDF] Environment:"
264+ mapM_ print env''
265+ putStr "\n"
266+ putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
267+ B.readFile file >>= B.putStr
268+ putStr "\n"
269+ (exit, out, err) <- pipeProcess (Just env'') "context" programArgs BL.empty
270+ when verbose $ do
271+ B.hPutStr stdout out
272+ B.hPutStr stderr err
273+ putStr "\n"
274+ let pdfFile = replaceExtension file ".pdf"
275+ pdfExists <- doesFileExist pdfFile
276+ mbPdf <- if pdfExists
277+ -- We read PDF as a strict bytestring to make sure that the
278+ -- temp directory is removed on Windows.
279+ -- See https://github.com/jgm/pandoc/issues/1192.
280+ then (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
281+ else return Nothing
282+ let log' = out <> err
283+ case (exit, mbPdf) of
284+ (ExitFailure _, _) -> do
285+ let logmsg = extractConTeXtMsg log'
286+ return $ Left logmsg
287+ (ExitSuccess, Nothing) -> return $ Left ""
288+ (ExitSuccess, Just pdf) -> return $ Right pdf