diff options
author | John MacFarlane <jgm@berkeley.edu> | 2017-02-09 21:26:24 +0100 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-02-09 21:26:24 +0100 |
commit | 0a4ba91994c875b0eaf22b76a6153c0d1de0f018 (patch) | |
tree | 2bbef149ce288843eec9e6b7d928025f1047e7d9 /src | |
parent | 6949d74e01cceab360db4dd8f4b1c0550b246908 (diff) | |
download | pandoc-0a4ba91994c875b0eaf22b76a6153c0d1de0f018.tar.gz |
Reverted deferred media bag code.
This was not actually being used. Since it adds considerable
complexity, it's best not to include it unless we are
actually going to use it.
The original thought was that we could do all loading in the
readers, always deferred and thus costless. This was supposed
to eliminate the need to traverse trees loading resources in
the docx, epub, odt writers and in PDF and SelfContained.
(It would also have the side effect that --extract-media could
be used with all input formats. This wasn't an intended side
effect, and it could be debated whether it's desirable, since
--extract-media was originally designed to extract the media
contained in a docx or odt or epub container.)
However, we never actually took the step of moving all of this
work to the readers, for a couple of reasons. The main reason
is that we'd still need to fetch resources in the docx,
epub, odt, pdf and self-contained writers, since the Pandoc AST might
have been built programatically and hence not generated by a reader.
So it's not clear that doing lazy loading in the readers would have
any real advantage.
I'm still not completely sure about this --- if we change our
minds it would be easy to undo this commit.
@jkr comments welcome.
Diffstat (limited to 'src')
-rw-r--r-- | src/Text/Pandoc/Class.hs | 57 |
1 files changed, 6 insertions, 51 deletions
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs index e6913566f..86158d632 100644 --- a/src/Text/Pandoc/Class.hs +++ b/src/Text/Pandoc/Class.hs @@ -49,7 +49,6 @@ module Text.Pandoc.Class ( PandocMonad(..) , getMediaBag , setMediaBag , insertMedia - , insertDeferredMedia , fetchItem , getInputFiles , getOutputFile @@ -106,8 +105,6 @@ import System.IO.Error import System.IO (stderr) import qualified Data.Map as M import Text.Pandoc.Error -import Data.Monoid -import Data.Maybe (catMaybes) import Text.Printf (printf) class (Functor m, Applicative m, Monad m, MonadError PandocError m) @@ -163,34 +160,16 @@ report level msg = do modifyCommonState $ \st -> st{ stLog = (level, msg) : stLog st } setMediaBag :: PandocMonad m => MediaBag -> m () -setMediaBag mb = modifyCommonState $ - \st -> st{stDeferredMediaBag = DeferredMediaBag mb mempty} +setMediaBag mb = modifyCommonState $ \st -> st{stMediaBag = mb} getMediaBag :: PandocMonad m => m MediaBag -getMediaBag = do - fetchDeferredMedia - DeferredMediaBag mb' _ <- getsCommonState stDeferredMediaBag - return mb' - -fetchDeferredMedia :: PandocMonad m => m () -fetchDeferredMedia = do - (DeferredMediaBag mb defMedia) <- getsCommonState stDeferredMediaBag - fetchedMedia <- catMaybes <$> mapM fetchMediaItem defMedia - setMediaBag $ foldr - (\(fp, bs, mbMime) mb' -> MB.insertMedia fp mbMime (BL.fromStrict bs) mb') - mb fetchedMedia +getMediaBag = getsCommonState stMediaBag insertMedia :: PandocMonad m => FilePath -> Maybe MimeType -> BL.ByteString -> m () insertMedia fp mime bs = do - (DeferredMediaBag mb dm) <- getsCommonState stDeferredMediaBag + mb <- getsCommonState stMediaBag let mb' = MB.insertMedia fp mime bs mb - modifyCommonState $ \st -> st{stDeferredMediaBag =DeferredMediaBag mb' dm } - -insertDeferredMedia :: PandocMonad m => FilePath -> m () -insertDeferredMedia fp = do - (DeferredMediaBag mb dm) <- getsCommonState stDeferredMediaBag - modifyCommonState $ - \st -> st{stDeferredMediaBag = DeferredMediaBag mb ((DeferredMediaPath fp) : dm)} + modifyCommonState $ \st -> st{stMediaBag = mb' } getInputFiles :: PandocMonad m => m (Maybe [FilePath]) getInputFiles = getsCommonState stInputFiles @@ -218,32 +197,8 @@ readFileFromDirs (d:ds) f = catchError -- -newtype DeferredMediaPath = DeferredMediaPath {unDefer :: String} - deriving (Show, Eq) - -data DeferredMediaBag = DeferredMediaBag MediaBag [DeferredMediaPath] - deriving (Show) - -instance Monoid DeferredMediaBag where - mempty = DeferredMediaBag mempty mempty - mappend (DeferredMediaBag mb lst) (DeferredMediaBag mb' lst') = - DeferredMediaBag (mb <> mb') (lst <> lst') - --- the internal function for downloading individual items. We want to --- catch errors and return a Nothing with a warning, so we can --- continue without erroring out. -fetchMediaItem :: PandocMonad m - => DeferredMediaPath - -> m (Maybe (FilePath, B.ByteString, Maybe MimeType)) -fetchMediaItem dfp = - (do (bs, mbmime) <- downloadOrRead Nothing (unDefer dfp) - return $ Just $ (unDefer dfp, bs, mbmime)) - `catchError` - (const $ do warning ("Couldn't access media at " ++ unDefer dfp) - return Nothing) - data CommonState = CommonState { stLog :: [(Verbosity, String)] - , stDeferredMediaBag :: DeferredMediaBag + , stMediaBag :: MediaBag , stInputFiles :: Maybe [FilePath] , stOutputFile :: Maybe FilePath , stVerbosity :: Verbosity @@ -251,7 +206,7 @@ data CommonState = CommonState { stLog :: [(Verbosity, String)] instance Default CommonState where def = CommonState { stLog = [] - , stDeferredMediaBag = mempty + , stMediaBag = mempty , stInputFiles = Nothing , stOutputFile = Nothing , stVerbosity = WARNING |