@@ -48,6 +48,7 @@ import Data.List (isInfixOf)
48import Data.Maybe (fromMaybe)
48import Data.Maybe (fromMaybe)
49import qualified Text.Pandoc.UTF8 as UTF8
49import qualified Text.Pandoc.UTF8 as UTF8
50import Text.Pandoc.Definition
50import Text.Pandoc.Definition
51+import Text.Pandoc.MediaBag
51import Text.Pandoc.Walk (walkM)
52import Text.Pandoc.Walk (walkM)
52import Text.Pandoc.Shared (warn, withTempDir, inDirectory, stringify)
53import Text.Pandoc.Shared (warn, withTempDir, inDirectory, stringify)
53import Text.Pandoc.Writers.Shared (getField, metaToJSON)
54import Text.Pandoc.Writers.Shared (getField, metaToJSON)
@@ -72,9 +73,10 @@ makePDF :: MonadIO m
72 -- xelatex, context, wkhtmltopdf)
73 -- xelatex, context, wkhtmltopdf)
73 -> (WriterOptions -> Pandoc -> PandocIO String) -- ^ writer
74 -> (WriterOptions -> Pandoc -> PandocIO String) -- ^ writer
74 -> WriterOptions -- ^ options
75 -> WriterOptions -- ^ options
76+ -> MediaBag -- ^ media
75 -> Pandoc -- ^ document
77 -> Pandoc -- ^ document
76 -> m (Either ByteString ByteString)
78 -> m (Either ByteString ByteString)
77−makePDF "wkhtmltopdf" writer opts doc@(Pandoc meta _) = liftIO $ do
79+makePDF "wkhtmltopdf" writer opts _mediabag doc@(Pandoc meta _) = liftIO $ do
78 let mathArgs = case writerHTMLMathMethod opts of
80 let mathArgs = case writerHTMLMathMethod opts of
79 -- with MathJax, wait til all math is rendered:
81 -- with MathJax, wait til all math is rendered:
80 MathJax _ -> ["--run-script", "MathJax.Hub.Register.StartupHook('End Typeset', function() { window.status = 'mathjax_loaded' });",
82 MathJax _ -> ["--run-script", "MathJax.Hub.Register.StartupHook('End Typeset', function() { window.status = 'mathjax_loaded' });",
@@ -97,33 +99,37 @@ makePDF "wkhtmltopdf" writer opts doc@(Pandoc meta _) = liftIO $ do
98 source <- runIOorExplode $ writer opts doc
100 source <- runIOorExplode $ writer opts doc
99 html2pdf (writerVerbose opts) args source
101 html2pdf (writerVerbose opts) args source
102+makePDF program writer opts mediabag doc =
100−makePDF program writer opts doc = liftIO $ withTempDir "tex2pdf." $ \tmpdir -> do
103+ liftIO $ withTempDir "tex2pdf." $ \tmpdir -> do
101− doc' <- handleImages opts tmpdir doc
104+ doc' <- handleImages opts mediabag tmpdir doc
102− source <- runIOorExplode $ writer opts doc'
105+ source <- runIOorExplode $ writer opts doc'
103− let args = writerLaTeXArgs opts
106+ let args = writerLaTeXArgs opts
104− case takeBaseName program of
107+ case takeBaseName program of
105− "context" -> context2pdf (writerVerbose opts) tmpdir source
108+ "context" -> context2pdf (writerVerbose opts) tmpdir source
106− prog | prog `elem` ["pdflatex", "lualatex", "xelatex"]
109+ prog | prog `elem` ["pdflatex", "lualatex", "xelatex"]
107− -> tex2pdf' (writerVerbose opts) args tmpdir program source
110+ -> tex2pdf' (writerVerbose opts) args tmpdir program source
108− _ -> return $ Left $ UTF8.fromStringLazy $ "Unknown program " ++ program
111+ _ -> return $ Left $ UTF8.fromStringLazy $ "Unknown program " ++ program
110handleImages :: WriterOptions
113handleImages :: WriterOptions
111 -> FilePath -- ^ temp dir to store images
115 -> FilePath -- ^ temp dir to store images
112 -> Pandoc -- ^ document
116 -> Pandoc -- ^ document
113 -> IO Pandoc
117 -> IO Pandoc
118+handleImages opts mediabag tmpdir =
114−handleImages opts tmpdir = walkM (convertImages tmpdir) <=< walkM (handleImage' opts tmpdir)
119+ walkM (convertImages tmpdir) <=< walkM (handleImage' opts mediabag tmpdir)
116handleImage' :: WriterOptions
121handleImage' :: WriterOptions
117 -> FilePath
123 -> FilePath
118 -> Inline
124 -> Inline
119 -> IO Inline
125 -> IO Inline
120−handleImage' opts tmpdir (Image attr ils (src,tit)) = do
126+handleImage' opts mediabag tmpdir (Image attr ils (src,tit)) = do
121 exists <- doesFileExist src
127 exists <- doesFileExist src
122 if exists
128 if exists
123 then return $ Image attr ils (src,tit)
129 then return $ Image attr ils (src,tit)
125 res <- runIO $ do
131 res <- runIO $ do
126− setMediaBag $ writerMediaBag opts
132+ setMediaBag mediabag
127 fetchItem (writerSourceURL opts) src
133 fetchItem (writerSourceURL opts) src
128 case res of
134 case res of
129 Right (contents, Just mime) -> do
135 Right (contents, Just mime) -> do
@@ -137,7 +143,7 @@ handleImage' opts tmpdir (Image attr ils (src,tit)) = do
137 warn $ "Could not find image `" ++ src ++ "', skipping..."
143 warn $ "Could not find image `" ++ src ++ "', skipping..."
138 -- return alt text
144 -- return alt text
139 return $ Emph ils
145 return $ Emph ils
140−handleImage' _ _ x = return x
146+handleImage' _ _ _ x = return x
142convertImages :: FilePath -> Inline -> IO Inline
148convertImages :: FilePath -> Inline -> IO Inline
143convertImages tmpdir (Image attr ils (src, tit)) = do
149convertImages tmpdir (Image attr ils (src, tit)) = do