git.iko.soy/typeable/pandoc
on master help sign in
Simplify plumbing for document transformation.
e15a4baJohn MacFarlane
src/Text/Pandoc/App.hs+21−25
@@ -68,10 +68,10 @@ import System.IO (stdout)
68import System.IO.Error (isDoesNotExistError)
68import System.IO.Error (isDoesNotExistError)
69import Text.Pandoc
69import Text.Pandoc
70import Text.Pandoc.Builder (setMeta)
70import Text.Pandoc.Builder (setMeta)
71−import Text.Pandoc.Class (PandocIO, getLog, withMediaBag)
71+import Text.Pandoc.Class (PandocIO, getLog, withMediaBag, getMediaBag)
72import Text.Pandoc.Highlighting (highlightingStyles)
72import Text.Pandoc.Highlighting (highlightingStyles)
73import Text.Pandoc.Lua ( runLuaFilter )
73import Text.Pandoc.Lua ( runLuaFilter )
74−import Text.Pandoc.MediaBag (MediaBag, extractMediaBag, mediaDirectory)
74+import Text.Pandoc.MediaBag (extractMediaBag, mediaDirectory)
75import Text.Pandoc.PDF (makePDF)
75import Text.Pandoc.PDF (makePDF)
76import Text.Pandoc.Process (pipeProcess)
76import Text.Pandoc.Process (pipeProcess)
77import Text.Pandoc.SelfContained (makeSelfContained, makeDataURI)
77import Text.Pandoc.SelfContained (makeSelfContained, makeDataURI)
@@ -391,20 +391,16 @@ convertWithOpts opts = do
391 E.throwIO PandocFailOnWarningError
391 E.throwIO PandocFailOnWarningError
392 return res
392 return res
393
393
394− let sourceToDoc :: [FilePath] -> PandocIO (Pandoc, MediaBag)
394+ let sourceToDoc :: [FilePath] -> PandocIO Pandoc
395 sourceToDoc sources' =
395 sourceToDoc sources' =
396 case reader of
396 case reader of
397 StringReader r
397 StringReader r
398− | optFileScope opts || readerName == "json" -> do
398+ | optFileScope opts || readerName == "json" ->
399− pairs <- mapM
400− (readSource >=> withMediaBag . r readerOpts) sources
399+ mconcat <$> mapM (readSource >=> r readerOpts) sources
401− return (mconcat (map fst pairs), mconcat (map snd pairs))
402 | otherwise ->
400 | otherwise ->
403− readSources sources' >>= withMediaBag . r readerOpts
401+ readSources sources' >>= r readerOpts
404− ByteStringReader r -> do
402+ ByteStringReader r ->
405− pairs <- mapM (readFile' >=>
406− withMediaBag . r readerOpts) sources
403+ mconcat <$> mapM (readFile' >=> r readerOpts) sources
407− return (mconcat (map fst pairs), mconcat (map snd pairs))
408
404
409 metadata <- if format == "jats" &&
405 metadata <- if format == "jats" &&
410 lookup "csl" (optMetadata opts) == Nothing &&
406 lookup "csl" (optMetadata opts) == Nothing &&
@@ -416,16 +412,15 @@ convertWithOpts opts = do
416 else return $ optMetadata opts
412 else return $ optMetadata opts
417
413
418 runIO' $ do
414 runIO' $ do
419− (doc, media) <- sourceToDoc sources
415+ (doc, media) <- withMediaBag $ sourceToDoc sources >>=
420− doc' <- (maybe return (extractMedia media) (optExtractMedia opts) >=>
416+ (maybe return extractMedia (optExtractMedia opts)
421− return . flip (foldr addMetadata) metadata >=>
417+ >=> return . flip (foldr addMetadata) metadata
422− applyTransforms transforms >=>
418+ >=> applyTransforms transforms
423− applyLuaFilters datadir (optLuaFilters opts) [format] >=>
419+ >=> applyLuaFilters datadir (optLuaFilters opts) [format]
424− applyFilters datadir filters' [format]) doc
420+ >=> applyFilters datadir filters' [format])
425
421
426 case writer of
422 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
429 StringWriter f
424 StringWriter f
430 | pdfOutput -> do
425 | pdfOutput -> do
431 -- make sure writer is latex, beamer, context, html5 or ms
426 -- make sure writer is latex, beamer, context, html5 or ms
@@ -445,7 +440,7 @@ convertWithOpts opts = do
445 when (isNothing mbPdfProg) $ liftIO $ E.throwIO $
440 when (isNothing mbPdfProg) $ liftIO $ E.throwIO $
446 PandocPDFProgramNotFoundError pdfprog
441 PandocPDFProgramNotFoundError pdfprog
447
442
448− res <- makePDF pdfprog f writerOptions verbosity media doc'
443+ res <- makePDF pdfprog f writerOptions verbosity media doc
449 case res of
444 case res of
450 Right pdf -> writeFnBinary outputFile pdf
445 Right pdf -> writeFnBinary outputFile pdf
451 Left err' -> liftIO $
446 Left err' -> liftIO $
@@ -462,7 +457,7 @@ convertWithOpts opts = do
462 format == "docbook") && optAscii opts
457 format == "docbook") && optAscii opts
463 then toEntities
458 then toEntities
464 else id
459 else id
465− output <- f writerOptions doc'
460+ output <- f writerOptions doc
466 selfcontain (output ++ ['\n' | not standalone]) >>=
461 selfcontain (output ++ ['\n' | not standalone]) >>=
467 writerFn outputFile . handleEntities
462 writerFn outputFile . handleEntities
468
463
@@ -728,12 +723,13 @@ defaultWriterName x =
728
723
729-- Transformations of a Pandoc document post-parsing:
724-- Transformations of a Pandoc document post-parsing:
730
725
731−extractMedia :: MonadIO m => MediaBag -> FilePath -> Pandoc -> m Pandoc
726+extractMedia :: FilePath -> Pandoc -> PandocIO Pandoc
732−extractMedia media dir d =
727+extractMedia dir d = do
728+ media <- getMediaBag
733 case [fp | (fp, _, _) <- mediaDirectory media] of
729 case [fp | (fp, _, _) <- mediaDirectory media] of
734 [] -> return d
730 [] -> return d
735 fps -> do
731 fps -> do
736− extractMediaBag True dir media
732+ liftIO $ extractMediaBag True dir media
737 return $ walk (adjustImagePath dir fps) d
733 return $ walk (adjustImagePath dir fps) d
738
734
739adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
735adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline