2017-03-04 13:03:41 +01:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
2014-07-31 12:00:21 -07:00
|
|
|
{-
|
2017-05-13 23:30:13 +02:00
|
|
|
Copyright (C) 2014-2015, 2017 John MacFarlane <jgm@berkeley.edu>
|
2014-07-31 12:00:21 -07:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
-}
|
|
|
|
|
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc.MediaBag
|
2017-05-13 23:30:13 +02:00
|
|
|
Copyright : Copyright (C) 2014-2015, 2017 John MacFarlane
|
2014-07-31 12:00:21 -07:00
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
|
|
Stability : alpha
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Definition of a MediaBag object to hold binary resources, and an
|
|
|
|
interface for interacting with it.
|
|
|
|
-}
|
|
|
|
module Text.Pandoc.MediaBag (
|
|
|
|
MediaBag,
|
|
|
|
lookupMedia,
|
|
|
|
insertMedia,
|
|
|
|
mediaDirectory,
|
|
|
|
) where
|
2017-03-04 13:03:41 +01:00
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import Data.Data (Data)
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Data.Typeable (Typeable)
|
2014-07-31 12:00:21 -07:00
|
|
|
import System.FilePath
|
2015-09-26 22:40:58 -07:00
|
|
|
import qualified System.FilePath.Posix as Posix
|
2014-08-17 20:42:30 +04:00
|
|
|
import Text.Pandoc.MIME (MimeType, getMimeTypeDef)
|
2014-07-31 12:00:21 -07:00
|
|
|
|
|
|
|
-- | A container for a collection of binary resources, with names and
|
|
|
|
-- mime types. Note that a 'MediaBag' is a Monoid, so 'mempty'
|
|
|
|
-- can be used for an empty 'MediaBag', and '<>' can be used to append
|
|
|
|
-- two 'MediaBag's.
|
2014-09-25 12:19:52 +01:00
|
|
|
newtype MediaBag = MediaBag (M.Map [String] (MimeType, BL.ByteString))
|
2015-03-19 17:07:18 +01:00
|
|
|
deriving (Monoid, Data, Typeable)
|
2014-07-31 12:00:21 -07:00
|
|
|
|
|
|
|
instance Show MediaBag where
|
|
|
|
show bag = "MediaBag " ++ show (mediaDirectory bag)
|
|
|
|
|
|
|
|
-- | Insert a media item into a 'MediaBag', replacing any existing
|
|
|
|
-- value with the same name.
|
2014-08-17 20:42:30 +04:00
|
|
|
insertMedia :: FilePath -- ^ relative path and canonical name of resource
|
|
|
|
-> Maybe MimeType -- ^ mime type (Nothing = determine from extension)
|
|
|
|
-> BL.ByteString -- ^ contents of resource
|
2014-07-31 12:00:21 -07:00
|
|
|
-> MediaBag
|
|
|
|
-> MediaBag
|
|
|
|
insertMedia fp mbMime contents (MediaBag mediamap) =
|
2015-09-26 22:40:58 -07:00
|
|
|
MediaBag (M.insert (splitDirectories fp) (mime, contents) mediamap)
|
2014-08-17 20:42:30 +04:00
|
|
|
where mime = fromMaybe fallback mbMime
|
2014-07-31 12:00:21 -07:00
|
|
|
fallback = case takeExtension fp of
|
2017-03-04 13:03:41 +01:00
|
|
|
".gz" -> getMimeTypeDef $ dropExtension fp
|
|
|
|
_ -> getMimeTypeDef fp
|
2014-07-31 12:00:21 -07:00
|
|
|
|
|
|
|
-- | Lookup a media item in a 'MediaBag', returning mime type and contents.
|
|
|
|
lookupMedia :: FilePath
|
|
|
|
-> MediaBag
|
2014-08-17 20:42:30 +04:00
|
|
|
-> Maybe (MimeType, BL.ByteString)
|
2015-09-26 22:40:58 -07:00
|
|
|
lookupMedia fp (MediaBag mediamap) = M.lookup (splitDirectories fp) mediamap
|
2014-07-31 12:00:21 -07:00
|
|
|
|
|
|
|
-- | Get a list of the file paths stored in a 'MediaBag', with
|
|
|
|
-- their corresponding mime types and the lengths in bytes of the contents.
|
2014-08-17 20:42:30 +04:00
|
|
|
mediaDirectory :: MediaBag -> [(String, MimeType, Int)]
|
2014-07-31 12:00:21 -07:00
|
|
|
mediaDirectory (MediaBag mediamap) =
|
|
|
|
M.foldWithKey (\fp (mime,contents) ->
|
2015-09-26 22:40:58 -07:00
|
|
|
(((Posix.joinPath fp), mime, fromIntegral $ BL.length contents):)) [] mediamap
|