git.iko.soy/typeable/pandoc
on master help sign in
PDF, Docx, EPUB, and ODT writers now automatically use MediaBag.

The MediaBag is thread through from the reader, with no need to extract to files.

commit234652a4b87a22cd937bcccb8a42f6ea1552a0f7 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 30 Jul 2014, 14:07 (12 years ago) parent28321a1
6 files changed+25−22
pandoc.hs+10−8 src/Text/Pandoc/PDF.hs+7−7 src/Text/Pandoc/Shared.hs+2−3 src/Text/Pandoc/Writers/Docx.hs+2−1 src/Text/Pandoc/Writers/EPUB.hs+2−1 src/Text/Pandoc/Writers/ODT.hs+2−2
pandoc.hs+10−8
@@ -1222,21 +1222,23 @@ main = do
12221222 | src `elem` paths = Image lab (dir ++ "/" ++ src, tit)
12231223 adjustImagePath _ _ x = x
12241224
1225− (doc, writerOptions') <-
1225+ (doc, media) <-
12261226 case reader of
12271227 StringReader r-> do
12281228 inp <- readSources sources >>=
12291229 handleIncludes' . convertTabs . intercalate "\n"
12301230 d <- r readerOpts inp
1231− return (d, writerOptions)
1231+ return (d, M.empty)
12321232 ByteStringReader r -> do
12331233 (d, media) <- readFiles sources >>= r readerOpts
1234− case mbExtractMedia of
1235− Just dir | not (M.null media) -> do
1236− mapM_ (writeMedia dir) $ M.toList media
1237− let d' = walk (adjustImagePath dir (M.keys media)) d
1238− return (d', writerOptions{ writerMediaBag = media })
1239− _ -> return (d, writerOptions)
1234+ d' <- case mbExtractMedia of
1235+ Just dir | not (M.null media) -> do
1236+ mapM_ (writeMedia dir) $ M.toList media
1237+ return $ walk (adjustImagePath dir (M.keys media)) d
1238+ _ -> return d
1239+ return (d', media)
1240+
1241+ let writerOptions' = writerOptions{ writerMediaBag = media }
12401242
12411243 let doc0 = M.foldWithKey setMeta doc metadata
12421244 let doc1 = foldr ($) doc0 transforms
src/Text/Pandoc/PDF.hs+7−7
@@ -45,7 +45,7 @@ import Data.Maybe (fromMaybe)
4545import qualified Text.Pandoc.UTF8 as UTF8
4646import Text.Pandoc.Definition
4747import Text.Pandoc.Walk (walkM)
48−import Text.Pandoc.Shared (fetchItem, warn, withTempDir)
48+import Text.Pandoc.Shared (fetchItem', warn, withTempDir)
4949import Text.Pandoc.Options (WriterOptions(..))
5050import Text.Pandoc.MIME (extensionFromMimeType)
5151import Text.Pandoc.Process (pipeProcess)
@@ -65,26 +65,26 @@ makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex)
6565 -> Pandoc -- ^ document
6666 -> IO (Either ByteString ByteString)
6767makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
68− doc' <- handleImages (writerSourceURL opts) tmpdir doc
68+ doc' <- handleImages opts tmpdir doc
6969 let source = writer opts doc'
7070 tex2pdf' tmpdir program source
7171
72−handleImages :: Maybe String -- ^ source base URL
72+handleImages :: WriterOptions
7373 -> FilePath -- ^ temp dir to store images
7474 -> Pandoc -- ^ document
7575 -> IO Pandoc
76−handleImages baseURL tmpdir = walkM (handleImage' baseURL tmpdir)
76+handleImages opts tmpdir = walkM (handleImage' opts tmpdir)
7777
78−handleImage' :: Maybe String
78+handleImage' :: WriterOptions
7979 -> FilePath
8080 -> Inline
8181 -> IO Inline
82−handleImage' baseURL tmpdir (Image ils (src,tit)) = do
82+handleImage' opts tmpdir (Image ils (src,tit)) = do
8383 exists <- doesFileExist src
8484 if exists
8585 then return $ Image ils (src,tit)
8686 else do
87− res <- fetchItem baseURL src
87+ res <- fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
8888 case res of
8989 Right (contents, Just mime) -> do
9090 let ext = fromMaybe (takeExtension src) $
src/Text/Pandoc/Shared.hs+2−3
@@ -793,10 +793,9 @@ fetchItem sourceURL s
793793 return (cont, mime)
794794
795795-- | Like 'fetchItem', but also looks for items in a 'MediaBag'.
796−fetchItem' :: Maybe MediaBag -> Maybe String -> String
796+fetchItem' :: MediaBag -> Maybe String -> String
797797 -> 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
800799 case M.lookup s media of
801800 Nothing -> fetchItem sourceURL s
802801 Just bs -> do
src/Text/Pandoc/Writers/Docx.hs+2−1
@@ -830,7 +830,8 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
830830 case M.lookup src imgs of
831831 Just (_,_,_,elt,_) -> return [elt]
832832 Nothing -> do
833− res <- liftIO $ fetchItem (writerSourceURL opts) src
833+ res <- liftIO $
834+ fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
834835 case res of
835836 Left (_ :: E.SomeException) -> do
836837 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
360360 walkM (transformBlock opts' mediaRef)
361361 pics <- readIORef mediaRef
362362 let readPicEntry entries (oldsrc, newsrc) = do
363− res <- fetchItem (writerSourceURL opts') oldsrc
363+ res <- fetchItem' (writerMediaBag opts')
364+ (writerSourceURL opts') oldsrc
364365 case res of
365366 Left _ -> do
366367 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
3838import Text.Pandoc.UTF8 ( fromStringLazy )
3939import Codec.Archive.Zip
4040import Text.Pandoc.Options ( WriterOptions(..) )
41−import Text.Pandoc.Shared ( stringify, readDataFile, fetchItem, warn )
41+import Text.Pandoc.Shared ( stringify, readDataFile, fetchItem', warn )
4242import Text.Pandoc.ImageSize ( imageSize, sizeInPoints )
4343import Text.Pandoc.MIME ( getMimeType )
4444import Text.Pandoc.Definition
@@ -131,7 +131,7 @@ writeODT opts doc@(Pandoc meta _) = do
131131
132132transformPicMath :: WriterOptions -> IORef [Entry] -> Inline -> IO Inline
133133transformPicMath opts entriesRef (Image lab (src,_)) = do
134− res <- fetchItem (writerSourceURL opts) src
134+ res <- fetchItem' (writerMediaBag opts) (writerSourceURL opts) src
135135 case res of
136136 Left (_ :: E.SomeException) -> do
137137 warn $ "Could not find image `" ++ src ++ "', skipping..."