git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.MIME: add exported function getCharset.

[API change]

commit4617f229ea051fea50bce6307fe8221b246a23fe authorJohn MacFarlane <jgm@berkeley.edu> dateMon 22 Feb 2021, 13:28 (6 years ago) parent80fde18
1 file changed+15−2
src/Text/Pandoc/MIME.hs+15−2
src/Text/Pandoc/MIME.hs+15−2
@@ -10,8 +10,13 @@
1010
1111Mime type lookup.
1212-}
13−module Text.Pandoc.MIME ( MimeType, getMimeType, getMimeTypeDef,
14− extensionFromMimeType, mediaCategory ) where
13+module Text.Pandoc.MIME (
14+ MimeType,
15+ getMimeType,
16+ getMimeTypeDef,
17+ getCharset,
18+ extensionFromMimeType,
19+ mediaCategory ) where
1520import Data.List (isPrefixOf, isSuffixOf)
1621import qualified Data.Map as M
1722import qualified Data.Text as T
@@ -54,6 +59,14 @@ reverseMimeTypes = M.fromList $ map swap mimeTypesList
5459mimeTypes :: M.Map T.Text MimeType
5560mimeTypes = M.fromList mimeTypesList
5661
62+-- | Get the charset from a mime type, if one is present.
63+getCharset :: MimeType -> Maybe T.Text
64+getCharset mt =
65+ let (_,y) = T.breakOn "charset=" mt
66+ in if T.null y
67+ then Nothing
68+ else Just $ T.toUpper $ T.takeWhile (/= ';') $ T.drop 8 y
69+
5770-- | Collection of common mime types.
5871-- Except for first entry, list borrowed from
5972-- <https://github.com/Happstack/happstack-server/blob/master/src/Happstack/Server/FileServe/BuildingBlocks.hs happstack-server>