git.iko.soy/typeable/pandoc
on master help sign in
PDF, Docx, EPUB, and ODT writers now automatically use MediaBag.
234652aJohn MacFarlane
pandoc.hs+10−8
@@ -1222,21 +1222,23 @@ main = do
1222 | src `elem` paths = Image lab (dir ++ "/" ++ src, tit)
1222 | src `elem` paths = Image lab (dir ++ "/" ++ src, tit)
1223 adjustImagePath _ _ x = x
1223 adjustImagePath _ _ x = x
1224
1224
1225− (doc, writerOptions') <-
1225+ (doc, media) <-
1226 case reader of
1226 case reader of
1227 StringReader r-> do
1227 StringReader r-> do
1228 inp <- readSources sources >>=
1228 inp <- readSources sources >>=
1229 handleIncludes' . convertTabs . intercalate "\n"
1229 handleIncludes' . convertTabs . intercalate "\n"
1230 d <- r readerOpts inp
1230 d <- r readerOpts inp
1231− return (d, writerOptions)
1231+ return (d, M.empty)
1232 ByteStringReader r -> do
1232 ByteStringReader r -> do
1233 (d, media) <- readFiles sources >>= r readerOpts
1233 (d, media) <- readFiles sources >>= r readerOpts
1234− case mbExtractMedia of
1234+ d' <- case mbExtractMedia of
1235− Just dir | not (M.null media) -> do
1235+ Just dir | not (M.null media) -> do
1236− mapM_ (writeMedia dir) $ M.toList media
1236+ mapM_ (writeMedia dir) $ M.toList media
1237− let d' = walk (adjustImagePath dir (M.keys media)) d
1237+ return $ walk (adjustImagePath dir (M.keys media)) d
1238+ _ -> return d
1239+ return (d', media)
1240+
1238− return (d', writerOptions{ writerMediaBag = media })
1241+ let writerOptions' = writerOptions{ writerMediaBag = media }
1239− _ -> return (d, writerOptions)
1240
1242
1241 let doc0 = M.foldWithKey setMeta doc metadata
1243 let doc0 = M.foldWithKey setMeta doc metadata
1242 let doc1 = foldr ($) doc0 transforms
1244 let doc1 = foldr ($) doc0 transforms
src/Text/Pandoc/PDF.hs+7−7
@@ -45,7 +45,7 @@ import Data.Maybe (fromMaybe)
45import qualified Text.Pandoc.UTF8 as UTF8
45import qualified Text.Pandoc.UTF8 as UTF8
46import Text.Pandoc.Definition
46import Text.Pandoc.Definition
47import Text.Pandoc.Walk (walkM)
47import Text.Pandoc.Walk (walkM)
48−import Text.Pandoc.Shared (fetchItem, warn, withTempDir)
48+import Text.Pandoc.Shared (fetchItem', warn, withTempDir)
49import Text.Pandoc.Options (WriterOptions(..))
49import Text.Pandoc.Options (WriterOptions(..))
50import Text.Pandoc.MIME (extensionFromMimeType)
50import Text.Pandoc.MIME (extensionFromMimeType)
51import Text.Pandoc.Process (pipeProcess)
51import Text.Pandoc.Process (pipeProcess)
@@ -65,26 +65,26 @@ makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex)
65 -> Pandoc -- ^ document
65 -> Pandoc -- ^ document
66 -> IO (Either ByteString ByteString)
66 -> IO (Either ByteString ByteString)
67makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
67makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
68− doc' <- handleImages (writerSourceURL opts) tmpdir doc
68+ doc' <- handleImages opts tmpdir doc
69 let source = writer opts doc'
69 let source = writer opts doc'
70 tex2pdf' tmpdir program source
70 tex2pdf' tmpdir program source
71
71
72−handleImages :: Maybe String -- ^ source base URL
72+handleImages :: WriterOptions
73 -> FilePath -- ^ temp dir to store images
73 -> FilePath -- ^ temp dir to store images
74 -> Pandoc -- ^ document
74 -> Pandoc -- ^ document
75 -> IO Pandoc
75 -> IO Pandoc
76−handleImages baseURL tmpdir = walkM (handleImage' baseURL tmpdir)
76+handleImages opts tmpdir = walkM (handleImage' opts tmpdir)
77
77
78−handleImage' :: Maybe String
78+handleImage' :: WriterOptions
79 -> FilePath
79 -> FilePath
80 -> Inline
80 -> Inline
81 -> IO Inline
81 -> IO Inline
82−handleImage' baseURL tmpdir (Image ils (src,tit)) = do
82+handleImage' opts tmpdir (Image ils (src,tit)) = do
83 exists <- doesFileExist src
83 exists <- doesFileExist src
84 if exists
84 if exists
85 then return $ Image ils (src,tit)
85 then return $ Image ils (src,tit)
86 else do
86 else do
87− res <- fetchItem baseURL src
87+ res <- fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
88 case res of
88 case res of
89 Right (contents, Just mime) -> do
89 Right (contents, Just mime) -> do
90 let ext = fromMaybe (takeExtension src) $
90 let ext = fromMaybe (takeExtension src) $
src/Text/Pandoc/Shared.hs+2−3
@@ -793,10 +793,9 @@ fetchItem sourceURL s
793 return (cont, mime)
793 return (cont, mime)
794
794
795-- | Like 'fetchItem', but also looks for items in a 'MediaBag'.
795-- | Like 'fetchItem', but also looks for items in a 'MediaBag'.
796−fetchItem' :: Maybe MediaBag -> Maybe String -> String
796+fetchItem' :: MediaBag -> Maybe String -> String
797 -> IO (Either E.SomeException (BS.ByteString, Maybe String))
797 -> IO (Either E.SomeException (BS.ByteString, Maybe String))
798−fetchItem' Nothing sourceURL s = fetchItem sourceURL s
799−fetchItem' (Just media) sourceURL s = do
798+fetchItem' media sourceURL s = do
800 case M.lookup s media of
799 case M.lookup s media of
801 Nothing -> fetchItem sourceURL s
800 Nothing -> fetchItem sourceURL s
802 Just bs -> do
801 Just bs -> do
src/Text/Pandoc/Writers/Docx.hs+2−1
@@ -830,7 +830,8 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
830 case M.lookup src imgs of
830 case M.lookup src imgs of
831 Just (_,_,_,elt,_) -> return [elt]
831 Just (_,_,_,elt,_) -> return [elt]
832 Nothing -> do
832 Nothing -> do
833+ res <- liftIO $
833− res <- liftIO $ fetchItem (writerSourceURL opts) src
834+ fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
834 case res of
835 case res of
835 Left (_ :: E.SomeException) -> do
836 Left (_ :: E.SomeException) -> do
836 liftIO $ warn $ "Could not find image `" ++ src ++ "', skipping..."
837 liftIO $ warn $ "Could not find image `" ++ src ++ "', skipping..."
src/Text/Pandoc/Writers/EPUB.hs+2−1
@@ -360,7 +360,8 @@ writeEPUB opts doc@(Pandoc meta _) = do
360 walkM (transformBlock opts' mediaRef)
360 walkM (transformBlock opts' mediaRef)
361 pics <- readIORef mediaRef
361 pics <- readIORef mediaRef
362 let readPicEntry entries (oldsrc, newsrc) = do
362 let readPicEntry entries (oldsrc, newsrc) = do
363+ res <- fetchItem' (writerMediaBag opts')
363− res <- fetchItem (writerSourceURL opts') oldsrc
364+ (writerSourceURL opts') oldsrc
364 case res of
365 case res of
365 Left _ -> do
366 Left _ -> do
366 warn $ "Could not find media `" ++ oldsrc ++ "', skipping..."
367 warn $ "Could not find media `" ++ oldsrc ++ "', skipping..."
src/Text/Pandoc/Writers/ODT.hs+2−2
@@ -38,7 +38,7 @@ import qualified Data.ByteString.Lazy as B
38import Text.Pandoc.UTF8 ( fromStringLazy )
38import Text.Pandoc.UTF8 ( fromStringLazy )
39import Codec.Archive.Zip
39import Codec.Archive.Zip
40import Text.Pandoc.Options ( WriterOptions(..) )
40import Text.Pandoc.Options ( WriterOptions(..) )
41−import Text.Pandoc.Shared ( stringify, readDataFile, fetchItem, warn )
41+import Text.Pandoc.Shared ( stringify, readDataFile, fetchItem', warn )
42import Text.Pandoc.ImageSize ( imageSize, sizeInPoints )
42import Text.Pandoc.ImageSize ( imageSize, sizeInPoints )
43import Text.Pandoc.MIME ( getMimeType )
43import Text.Pandoc.MIME ( getMimeType )
44import Text.Pandoc.Definition
44import Text.Pandoc.Definition
@@ -131,7 +131,7 @@ writeODT opts doc@(Pandoc meta _) = do
131
131
132transformPicMath :: WriterOptions -> IORef [Entry] -> Inline -> IO Inline
132transformPicMath :: WriterOptions -> IORef [Entry] -> Inline -> IO Inline
133transformPicMath opts entriesRef (Image lab (src,_)) = do
133transformPicMath opts entriesRef (Image lab (src,_)) = do
134− res <- fetchItem (writerSourceURL opts) src
134+ res <- fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
135 case res of
135 case res of
136 Left (_ :: E.SomeException) -> do
136 Left (_ :: E.SomeException) -> do
137 warn $ "Could not find image `" ++ src ++ "', skipping..."
137 warn $ "Could not find image `" ++ src ++ "', skipping..."