@@ -68,10 +68,10 @@ import System.IO (stdout)
6868import System.IO.Error (isDoesNotExistError)
6969import Text.Pandoc
7070import Text.Pandoc.Builder (setMeta)
71−import Text.Pandoc.Class (PandocIO, getLog, withMediaBag)
71+import Text.Pandoc.Class (PandocIO, getLog, withMediaBag, getMediaBag)
7272import Text.Pandoc.Highlighting (highlightingStyles)
7373import Text.Pandoc.Lua ( runLuaFilter )
74−import Text.Pandoc.MediaBag (MediaBag, extractMediaBag, mediaDirectory)
74+import Text.Pandoc.MediaBag (extractMediaBag, mediaDirectory)
7575import Text.Pandoc.PDF (makePDF)
7676import Text.Pandoc.Process (pipeProcess)
7777import Text.Pandoc.SelfContained (makeSelfContained, makeDataURI)
@@ -391,20 +391,16 @@ convertWithOpts opts = do
391391 E.throwIO PandocFailOnWarningError
392392 return res
393393
394− let sourceToDoc :: [FilePath] -> PandocIO (Pandoc, MediaBag)
394+ let sourceToDoc :: [FilePath] -> PandocIO Pandoc
395395 sourceToDoc sources' =
396396 case reader of
397397 StringReader r
398− | optFileScope opts || readerName == "json" -> do
399− pairs <- mapM
400− (readSource >=> withMediaBag . r readerOpts) sources
401− return (mconcat (map fst pairs), mconcat (map snd pairs))
398+ | optFileScope opts || readerName == "json" ->
399+ mconcat <$> mapM (readSource >=> r readerOpts) sources
402400 | otherwise ->
403− readSources sources' >>= withMediaBag . r readerOpts
404− ByteStringReader r -> do
405− pairs <- mapM (readFile' >=>
406− withMediaBag . r readerOpts) sources
407− return (mconcat (map fst pairs), mconcat (map snd pairs))
401+ readSources sources' >>= r readerOpts
402+ ByteStringReader r ->
403+ mconcat <$> mapM (readFile' >=> r readerOpts) sources
408404
409405 metadata <- if format == "jats" &&
410406 lookup "csl" (optMetadata opts) == Nothing &&
@@ -416,16 +412,15 @@ convertWithOpts opts = do
416412 else return $ optMetadata opts
417413
418414 runIO' $ do
419− (doc, media) <- sourceToDoc sources
420− doc' <- (maybe return (extractMedia media) (optExtractMedia opts) >=>
421− return . flip (foldr addMetadata) metadata >=>
422− applyTransforms transforms >=>
423− applyLuaFilters datadir (optLuaFilters opts) [format] >=>
424− applyFilters datadir filters' [format]) doc
415+ (doc, media) <- withMediaBag $ sourceToDoc sources >>=
416+ (maybe return extractMedia (optExtractMedia opts)
417+ >=> return . flip (foldr addMetadata) metadata
418+ >=> applyTransforms transforms
419+ >=> applyLuaFilters datadir (optLuaFilters opts) [format]
420+ >=> applyFilters datadir filters' [format])
425421
426422 case writer of
427− -- StringWriter f -> f writerOptions doc' >>= writerFn outputFile
428− ByteStringWriter f -> f writerOptions doc' >>= writeFnBinary outputFile
423+ ByteStringWriter f -> f writerOptions doc >>= writeFnBinary outputFile
429424 StringWriter f
430425 | pdfOutput -> do
431426 -- make sure writer is latex, beamer, context, html5 or ms
@@ -445,7 +440,7 @@ convertWithOpts opts = do
445440 when (isNothing mbPdfProg) $ liftIO $ E.throwIO $
446441 PandocPDFProgramNotFoundError pdfprog
447442
448− res <- makePDF pdfprog f writerOptions verbosity media doc'
443+ res <- makePDF pdfprog f writerOptions verbosity media doc
449444 case res of
450445 Right pdf -> writeFnBinary outputFile pdf
451446 Left err' -> liftIO $
@@ -462,7 +457,7 @@ convertWithOpts opts = do
462457 format == "docbook") && optAscii opts
463458 then toEntities
464459 else id
465− output <- f writerOptions doc'
460+ output <- f writerOptions doc
466461 selfcontain (output ++ ['\n' | not standalone]) >>=
467462 writerFn outputFile . handleEntities
468463
@@ -728,12 +723,13 @@ defaultWriterName x =
728723
729724-- Transformations of a Pandoc document post-parsing:
730725
731−extractMedia :: MonadIO m => MediaBag -> FilePath -> Pandoc -> m Pandoc
732−extractMedia media dir d =
726+extractMedia :: FilePath -> Pandoc -> PandocIO Pandoc
727+extractMedia dir d = do
728+ media <- getMediaBag
733729 case [fp | (fp, _, _) <- mediaDirectory media] of
734730 [] -> return d
735731 fps -> do
736− extractMediaBag True dir media
732+ liftIO $ extractMediaBag True dir media
737733 return $ walk (adjustImagePath dir fps) d
738734
739735adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline