@@ -39,14 +39,13 @@ module Text.Pandoc.App (
3939 ) where
4040import Control.Applicative ((<|>))
4141import qualified Control.Exception as E
42−import Control.Monad.Except (catchError, throwError)
42+import Control.Monad.Except (throwError)
4343import Control.Monad
4444import Control.Monad.Trans
4545import Data.Aeson (eitherDecode', encode)
4646import qualified Data.ByteString as BS
4747import qualified Data.ByteString.Lazy as B
4848import Data.Char (toLower, toUpper)
49−import Data.Digest.Pure.SHA (sha1, showDigest)
5049import qualified Data.Set as Set
5150import Data.Foldable (foldrM)
5251import Data.List (intercalate, isPrefixOf, isSuffixOf, sort)
@@ -70,19 +69,16 @@ import System.IO (stdout)
7069import System.IO.Error (isDoesNotExistError)
7170import Text.Pandoc
7271import Text.Pandoc.Builder (setMeta)
73−import Text.Pandoc.Class (PandocIO, getLog, withMediaBag, getMediaBag,
74− fetchItem, insertMedia, report)
72+import Text.Pandoc.Class (PandocIO, getLog, withMediaBag,
73+ extractMedia, fillMedia)
7574import Text.Pandoc.Highlighting (highlightingStyles)
7675import Text.Pandoc.Lua ( runLuaFilter )
77−import Text.Pandoc.MediaBag (extractMediaBag, mediaDirectory)
78−import Text.Pandoc.MIME (extensionFromMimeType)
7976import Text.Pandoc.PDF (makePDF)
8077import Text.Pandoc.Process (pipeProcess)
8178import Text.Pandoc.SelfContained (makeSelfContained, makeDataURI)
8279import Text.Pandoc.Shared (headerShift, openURL, readDataFile,
8380 readDataFileUTF8, safeRead, tabFilter)
8481import qualified Text.Pandoc.UTF8 as UTF8
85−import Text.Pandoc.Walk (walkM, walk)
8682import Text.Pandoc.XML (toEntities)
8783import Text.Printf
8884#ifndef _WINDOWS
@@ -731,48 +727,6 @@ defaultWriterName x =
731727
732728-- Transformations of a Pandoc document post-parsing:
733729
734−-- | Traverse tree, filling media bag.
735−fillMedia :: Maybe String -> Pandoc -> PandocIO Pandoc
736−fillMedia sourceURL d = walkM handleImage d
737− where handleImage :: Inline -> PandocIO Inline
738− handleImage (Image attr lab (src, tit)) = catchError
739− (do (bs, mt) <- fetchItem sourceURL src
740− let ext = fromMaybe (takeExtension src)
741− (mt >>= extensionFromMimeType)
742− let bs' = B.fromChunks [bs]
743− let basename = showDigest $ sha1 bs'
744− let fname = basename <.> ext
745− insertMedia fname mt bs'
746− return $ Image attr lab (fname, tit))
747− (\e -> do
748− case e of
749− PandocResourceNotFound _ -> do
750− report $ CouldNotFetchResource src
751− "replacing image with description"
752− -- emit alt text
753− return $ Span ("",["image"],[]) lab
754− PandocHttpError u er -> do
755− report $ CouldNotFetchResource u
756− (show er ++ "\rReplacing image with description.")
757− -- emit alt text
758− return $ Span ("",["image"],[]) lab
759− _ -> throwError e)
760− handleImage x = return x
761−
762−extractMedia :: FilePath -> Pandoc -> PandocIO Pandoc
763−extractMedia dir d = do
764− media <- getMediaBag
765− case [fp | (fp, _, _) <- mediaDirectory media] of
766− [] -> return d
767− fps -> do
768− liftIO $ extractMediaBag True dir media
769− return $ walk (adjustImagePath dir fps) d
770−
771−adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
772−adjustImagePath dir paths (Image attr lab (src, tit))
773− | src `elem` paths = Image attr lab (dir ++ "/" ++ src, tit)
774−adjustImagePath _ _ x = x
775−
776730applyTransforms :: Monad m => [Transform] -> Pandoc -> m Pandoc
777731applyTransforms transforms d = return $ foldr ($) d transforms
778732