aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-02-22 13:28:47 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2021-02-22 13:28:47 -0800
commit4617f229ea051fea50bce6307fe8221b246a23fe (patch)
tree3b37895b613cae9a3eeed0efe90b03ddc337eb2c
parent80fde18fb1d983b938476ed5b3771ed5d6158d44 (diff)
downloadpandoc-4617f229ea051fea50bce6307fe8221b246a23fe.tar.gz
Text.Pandoc.MIME: add exported function getCharset.
[API change]
-rw-r--r--src/Text/Pandoc/MIME.hs17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/Text/Pandoc/MIME.hs b/src/Text/Pandoc/MIME.hs
index 53c5cd018..3d06e1579 100644
--- a/src/Text/Pandoc/MIME.hs
+++ b/src/Text/Pandoc/MIME.hs
@@ -10,8 +10,13 @@
Mime type lookup.
-}
-module Text.Pandoc.MIME ( MimeType, getMimeType, getMimeTypeDef,
- extensionFromMimeType, mediaCategory ) where
+module Text.Pandoc.MIME (
+ MimeType,
+ getMimeType,
+ getMimeTypeDef,
+ getCharset,
+ extensionFromMimeType,
+ mediaCategory ) where
import Data.List (isPrefixOf, isSuffixOf)
import qualified Data.Map as M
import qualified Data.Text as T
@@ -54,6 +59,14 @@ reverseMimeTypes = M.fromList $ map swap mimeTypesList
mimeTypes :: M.Map T.Text MimeType
mimeTypes = M.fromList mimeTypesList
+-- | Get the charset from a mime type, if one is present.
+getCharset :: MimeType -> Maybe T.Text
+getCharset mt =
+ let (_,y) = T.breakOn "charset=" mt
+ in if T.null y
+ then Nothing
+ else Just $ T.toUpper $ T.takeWhile (/= ';') $ T.drop 8 y
+
-- | Collection of common mime types.
-- Except for first entry, list borrowed from
-- <https://github.com/Happstack/happstack-server/blob/master/src/Happstack/Server/FileServe/BuildingBlocks.hs happstack-server>