@@ -28,12 +28,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2828
2929Conversion of LaTeX documents to PDF.
3030-}
31−module Text.Pandoc.PDF ( tex2pdf ) where
31+module Text.Pandoc.PDF ( makePDF ) where
3232
3333import System.IO.Temp
3434import Data.ByteString.Lazy (ByteString)
3535import qualified Data.ByteString.Lazy as B
3636import qualified Data.ByteString.Lazy.Char8 as BC
37+import qualified Data.ByteString as BS
3738import System.Exit (ExitCode (..))
3839import System.FilePath
3940import System.Directory
@@ -42,9 +43,15 @@ import System.Environment
4243import Control.Exception (evaluate)
4344import System.IO (hClose)
4445import Control.Concurrent (putMVar, takeMVar, newEmptyMVar, forkIO)
45−import Text.Pandoc.UTF8 as UTF8
4646import Control.Monad (unless)
4747import Data.List (isInfixOf)
48+import qualified Data.ByteString.Base64 as B64
49+import qualified Text.Pandoc.UTF8 as UTF8
50+import Text.Pandoc.Definition
51+import Text.Pandoc.Generic (bottomUpM)
52+import Text.Pandoc.Shared (fetchItem, warn)
53+import Text.Pandoc.Options (WriterOptions(..))
54+import Text.Pandoc.MIME (extensionFromMimeType)
4855
4956withTempDir :: String -> (FilePath -> IO a) -> IO a
5057withTempDir =
@@ -54,12 +61,45 @@ withTempDir =
5461 withSystemTempDirectory
5562#endif
5663
57−tex2pdf :: String -- ^ tex program (pdflatex, lualatex, xelatex)
58− -> String -- ^ latex source
64+makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex)
65+ -> (WriterOptions -> Pandoc -> String) -- ^ writer
66+ -> WriterOptions -- ^ options
67+ -> Pandoc -- ^ document
5968 -> IO (Either ByteString ByteString)
60−tex2pdf program source = withTempDir "tex2pdf." $ \tmpdir ->
69+makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
70+ doc' <- handleImages (writerSourceDirectory opts) tmpdir doc
71+ let source = writer opts doc'
6172 tex2pdf' tmpdir program source
6273
74+handleImages :: String -- ^ source directory/base URL
75+ -> FilePath -- ^ temp dir to store images
76+ -> Pandoc -- ^ document
77+ -> IO Pandoc
78+handleImages baseURL tmpdir = bottomUpM (handleImage' baseURL tmpdir)
79+
80+handleImage' :: String
81+ -> FilePath
82+ -> Inline
83+ -> IO Inline
84+handleImage' baseURL tmpdir (Image ils (src,tit)) = do
85+ exists <- doesFileExist src
86+ if exists
87+ then return $ Image ils (src,tit)
88+ else do
89+ res <- fetchItem baseURL src
90+ case res of
91+ Right (contents, Just mime) -> do
92+ let ext = maybe (takeExtension src) id $
93+ extensionFromMimeType mime
94+ let basename = UTF8.toString $ B64.encode $ UTF8.fromString src
95+ let fname = tmpdir </> basename <.> ext
96+ BS.writeFile fname contents
97+ return $ Image ils (fname,tit)
98+ _ -> do
99+ warn $ "Could not find image `" ++ src ++ "', skipping..."
100+ return $ Image ils (src,tit)
101+handleImage' _ _ x = return x
102+
63103tex2pdf' :: FilePath -- ^ temp directory for output
64104 -> String -- ^ tex program
65105 -> String -- ^ tex source