@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE OverloadedStrings #-}
23{-# LANGUAGE DeriveDataTypeable #-}
34{-# LANGUAGE GeneralizedNewtypeDeriving #-}
45{- |
@@ -27,26 +28,31 @@ import qualified Data.Map as M
2728import Data.Maybe (fromMaybe)
2829import Data.Typeable (Typeable)
2930import System.FilePath
30−import qualified System.FilePath.Posix as Posix
3131import Text.Pandoc.MIME (MimeType, getMimeTypeDef)
32+import Data.Text (Text)
33+import qualified Data.Text as T
3234
3335-- | A container for a collection of binary resources, with names and
3436-- mime types. Note that a 'MediaBag' is a Monoid, so 'mempty'
3537-- can be used for an empty 'MediaBag', and '<>' can be used to append
3638-- two 'MediaBag's.
37−newtype MediaBag = MediaBag (M.Map [FilePath] (MimeType, BL.ByteString))
39+newtype MediaBag = MediaBag (M.Map Text (MimeType, BL.ByteString))
3840 deriving (Semigroup, Monoid, Data, Typeable)
3941
4042instance Show MediaBag where
4143 show bag = "MediaBag " ++ show (mediaDirectory bag)
4244
45+-- | We represent paths with /, in normalized form.
46+canonicalize :: FilePath -> Text
47+canonicalize = T.replace "\\" "/" . T.pack . normalise
48+
4349-- | Delete a media item from a 'MediaBag', or do nothing if no item corresponds
4450-- to the given path.
4551deleteMedia :: FilePath -- ^ relative path and canonical name of resource
4652 -> MediaBag
4753 -> MediaBag
4854deleteMedia fp (MediaBag mediamap) =
49− MediaBag $ M.delete (splitDirectories fp) mediamap
55+ MediaBag $ M.delete (canonicalize fp) mediamap
5056
5157-- | Insert a media item into a 'MediaBag', replacing any existing
5258-- value with the same name.
@@ -56,7 +62,7 @@ insertMedia :: FilePath -- ^ relative path and canonical name of resource
5662 -> MediaBag
5763 -> MediaBag
5864insertMedia fp mbMime contents (MediaBag mediamap) =
59− MediaBag (M.insert (splitDirectories fp) (mime, contents) mediamap)
65+ MediaBag (M.insert (canonicalize fp) (mime, contents) mediamap)
6066 where mime = fromMaybe fallback mbMime
6167 fallback = case takeExtension fp of
6268 ".gz" -> getMimeTypeDef $ dropExtension fp
@@ -66,16 +72,16 @@ insertMedia fp mbMime contents (MediaBag mediamap) =
6672lookupMedia :: FilePath
6773 -> MediaBag
6874 -> Maybe (MimeType, BL.ByteString)
69−lookupMedia fp (MediaBag mediamap) = M.lookup (splitDirectories fp) mediamap
75+lookupMedia fp (MediaBag mediamap) = M.lookup (canonicalize fp) mediamap
7076
7177-- | Get a list of the file paths stored in a 'MediaBag', with
7278-- their corresponding mime types and the lengths in bytes of the contents.
7379mediaDirectory :: MediaBag -> [(FilePath, MimeType, Int)]
7480mediaDirectory (MediaBag mediamap) =
7581 M.foldrWithKey (\fp (mime,contents) ->
76− ((Posix.joinPath fp, mime, fromIntegral $ BL.length contents):)) [] mediamap
82+ ((T.unpack fp, mime, fromIntegral (BL.length contents)):)) [] mediamap
7783
7884mediaItems :: MediaBag -> [(FilePath, MimeType, BL.ByteString)]
7985mediaItems (MediaBag mediamap) =
8086 M.foldrWithKey (\fp (mime,contents) ->
81− ((Posix.joinPath fp, mime, contents):)) [] mediamap
87+ ((T.unpack fp, mime, contents):)) [] mediamap