git.iko.soy/typeable/pandoc
on master help sign in
pandoc.hs: Rewrote some of the logic for clarity.
commit0565a81676e1f5b033253ddcf9da4f691d09b7bb authorJohn MacFarlane <jgm@berkeley.edu> dateThu 31 Jul 2014, 12:51 (12 years ago) parent6dd2418
1 file changed+31−25
pandoc.hs+31−25
pandoc.hs+31−25
@@ -1,4 +1,4 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE CPP, TupleSections #-}
22{-
33Copyright (C) 2006-2014 John MacFarlane <jgm@berkeley.edu>
44
@@ -57,7 +57,7 @@ import System.IO.Error ( isDoesNotExistError )
5757import qualified Control.Exception as E
5858import Control.Exception.Extensible ( throwIO )
5959import qualified Text.Pandoc.UTF8 as UTF8
60−import Control.Monad (when, unless, liftM)
60+import Control.Monad (when, unless, liftM, (>=>))
6161import Data.Foldable (foldrM)
6262import Network.URI (parseURI, isURI, URI(..))
6363import qualified Data.ByteString.Lazy as B
@@ -1214,28 +1214,34 @@ main = do
12141214
12151215 (doc, media) <-
12161216 case reader of
1217− StringReader r-> do
1218− inp <- readSources sources >>=
1219− handleIncludes' . convertTabs . intercalate "\n"
1220− d <- r readerOpts inp
1221− return (d, mempty)
1222− ByteStringReader r -> do
1223− (d, media) <- readFiles sources >>= r readerOpts
1224− d' <- case mbExtractMedia of
1225− Just dir -> do
1226− case [fp | (fp, _, _) <- mediaDirectory media] of
1227− [] -> return d
1228− fps -> do
1229− extractMediaBag True dir media
1230− return $ walk (adjustImagePath dir fps) d
1231− _ -> return d
1232− return (d', media)
1217+ StringReader r-> (, mempty) <$>
1218+ ( readSources >=>
1219+ handleIncludes' . convertTabs . intercalate "\n" >=>
1220+ r readerOpts ) sources
1221+ ByteStringReader r -> readFiles sources >>= r readerOpts
12331222
12341223 let writerOptions' = writerOptions{ writerMediaBag = media }
12351224
1236− let doc0 = M.foldWithKey setMeta doc metadata
1237− let doc1 = foldr ($) doc0 transforms
1238− doc2 <- foldrM ($) doc1 $ map ($ [writerName']) plugins
1225+ let extractMedia d = do
1226+ case mbExtractMedia of
1227+ Just dir -> do
1228+ case [fp | (fp, _, _) <- mediaDirectory media] of
1229+ [] -> return d
1230+ fps -> do
1231+ extractMediaBag True dir media
1232+ return $ walk (adjustImagePath dir fps) d
1233+ _ -> return d
1234+
1235+ let adjustMetadata d = return $ M.foldWithKey setMeta d metadata
1236+
1237+ let applyTransforms d = return $ foldr ($) d transforms
1238+
1239+ let applyPlugins d = foldrM ($) d $ map ($ [writerName']) plugins
1240+
1241+ doc' <- (extractMedia >=>
1242+ adjustMetadata >=>
1243+ applyTransforms >=>
1244+ applyPlugins) doc
12391245
12401246 let writeBinary :: B.ByteString -> IO ()
12411247 writeBinary = B.writeFile (UTF8.encodePath outputFile)
@@ -1245,8 +1251,8 @@ main = do
12451251 writerFn f = UTF8.writeFile f
12461252
12471253 case writer of
1248− IOStringWriter f -> f writerOptions' doc2 >>= writerFn outputFile
1249− IOByteStringWriter f -> f writerOptions' doc2 >>= writeBinary
1254+ IOStringWriter f -> f writerOptions' doc' >>= writerFn outputFile
1255+ IOByteStringWriter f -> f writerOptions' doc' >>= writeBinary
12501256 PureStringWriter f
12511257 | pdfOutput -> do
12521258 -- make sure writer is latex or beamer
@@ -1260,14 +1266,14 @@ main = do
12601266 err 41 $ latexEngine ++ " not found. " ++
12611267 latexEngine ++ " is needed for pdf output."
12621268
1263− res <- makePDF latexEngine f writerOptions' doc2
1269+ res <- makePDF latexEngine f writerOptions' doc'
12641270 case res of
12651271 Right pdf -> writeBinary pdf
12661272 Left err' -> do
12671273 B.hPutStr stderr $ err'
12681274 B.hPut stderr $ B.pack [10]
12691275 err 43 "Error producing PDF from TeX source"
1270− | otherwise -> selfcontain (f writerOptions' doc2 ++
1276+ | otherwise -> selfcontain (f writerOptions' doc' ++
12711277 ['\n' | not standalone'])
12721278 >>= writerFn outputFile . handleEntities
12731279 where htmlFormat = writerName' `elem`