git.iko.soy/typeable/pandoc
on master help sign in
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.

commit0a4ba91994c875b0eaf22b76a6153c0d1de0f018 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 9 Feb 2017, 21:26 (10 years ago) parent6949d74
1 file changed+6−51
src/Text/Pandoc/Class.hs+6−51
src/Text/Pandoc/Class.hs+6−51
@@ -49,7 +49,6 @@ module Text.Pandoc.Class ( PandocMonad(..)
4949 , getMediaBag
5050 , setMediaBag
5151 , insertMedia
52− , insertDeferredMedia
5352 , fetchItem
5453 , getInputFiles
5554 , getOutputFile
@@ -106,8 +105,6 @@ import System.IO.Error
106105import System.IO (stderr)
107106import qualified Data.Map as M
108107import Text.Pandoc.Error
109−import Data.Monoid
110−import Data.Maybe (catMaybes)
111108import Text.Printf (printf)
112109
113110class (Functor m, Applicative m, Monad m, MonadError PandocError m)
@@ -163,34 +160,16 @@ report level msg = do
163160 modifyCommonState $ \st -> st{ stLog = (level, msg) : stLog st }
164161
165162setMediaBag :: PandocMonad m => MediaBag -> m ()
166−setMediaBag mb = modifyCommonState $
167− \st -> st{stDeferredMediaBag = DeferredMediaBag mb mempty}
163+setMediaBag mb = modifyCommonState $ \st -> st{stMediaBag = mb}
168164
169165getMediaBag :: PandocMonad m => m MediaBag
170−getMediaBag = do
171− fetchDeferredMedia
172− DeferredMediaBag mb' _ <- getsCommonState stDeferredMediaBag
173− return mb'
174−
175−fetchDeferredMedia :: PandocMonad m => m ()
176−fetchDeferredMedia = do
177− (DeferredMediaBag mb defMedia) <- getsCommonState stDeferredMediaBag
178− fetchedMedia <- catMaybes <$> mapM fetchMediaItem defMedia
179− setMediaBag $ foldr
180− (\(fp, bs, mbMime) mb' -> MB.insertMedia fp mbMime (BL.fromStrict bs) mb')
181− mb fetchedMedia
166+getMediaBag = getsCommonState stMediaBag
182167
183168insertMedia :: PandocMonad m => FilePath -> Maybe MimeType -> BL.ByteString -> m ()
184169insertMedia fp mime bs = do
185− (DeferredMediaBag mb dm) <- getsCommonState stDeferredMediaBag
170+ mb <- getsCommonState stMediaBag
186171 let mb' = MB.insertMedia fp mime bs mb
187− modifyCommonState $ \st -> st{stDeferredMediaBag =DeferredMediaBag mb' dm }
188−
189−insertDeferredMedia :: PandocMonad m => FilePath -> m ()
190−insertDeferredMedia fp = do
191− (DeferredMediaBag mb dm) <- getsCommonState stDeferredMediaBag
192− modifyCommonState $
193− \st -> st{stDeferredMediaBag = DeferredMediaBag mb ((DeferredMediaPath fp) : dm)}
172+ modifyCommonState $ \st -> st{stMediaBag = mb' }
194173
195174getInputFiles :: PandocMonad m => m (Maybe [FilePath])
196175getInputFiles = getsCommonState stInputFiles
@@ -218,32 +197,8 @@ readFileFromDirs (d:ds) f = catchError
218197
219198--
220199
221−newtype DeferredMediaPath = DeferredMediaPath {unDefer :: String}
222− deriving (Show, Eq)
223−
224−data DeferredMediaBag = DeferredMediaBag MediaBag [DeferredMediaPath]
225− deriving (Show)
226−
227−instance Monoid DeferredMediaBag where
228− mempty = DeferredMediaBag mempty mempty
229− mappend (DeferredMediaBag mb lst) (DeferredMediaBag mb' lst') =
230− DeferredMediaBag (mb <> mb') (lst <> lst')
231−
232−-- the internal function for downloading individual items. We want to
233−-- catch errors and return a Nothing with a warning, so we can
234−-- continue without erroring out.
235−fetchMediaItem :: PandocMonad m
236− => DeferredMediaPath
237− -> m (Maybe (FilePath, B.ByteString, Maybe MimeType))
238−fetchMediaItem dfp =
239− (do (bs, mbmime) <- downloadOrRead Nothing (unDefer dfp)
240− return $ Just $ (unDefer dfp, bs, mbmime))
241− `catchError`
242− (const $ do warning ("Couldn't access media at " ++ unDefer dfp)
243− return Nothing)
244−
245200data CommonState = CommonState { stLog :: [(Verbosity, String)]
246− , stDeferredMediaBag :: DeferredMediaBag
201+ , stMediaBag :: MediaBag
247202 , stInputFiles :: Maybe [FilePath]
248203 , stOutputFile :: Maybe FilePath
249204 , stVerbosity :: Verbosity
@@ -251,7 +206,7 @@ data CommonState = CommonState { stLog :: [(Verbosity, String)]
251206
252207instance Default CommonState where
253208 def = CommonState { stLog = []
254− , stDeferredMediaBag = mempty
209+ , stMediaBag = mempty
255210 , stInputFiles = Nothing
256211 , stOutputFile = Nothing
257212 , stVerbosity = WARNING