aboutsummaryrefslogtreecommitdiff
path: root/mustache/src/Text/Mustache/Internal.hs
blob: c1bf8b7528ecccee051d401fc4859b742b5baba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-|
Module      : $Header$
Description : Types and conversions
Copyright   : (c) Justus Adam, 2015
License     : BSD3
Maintainer  : dev@justus.science
Stability   : experimental
Portability : POSIX

escapeXML and xmlEntities curtesy to the tagsoup library.
-}
module Text.Mustache.Internal (uncons, escapeXMLText) where


import           Data.Char   (ord)
import qualified Data.IntMap as IntMap
import qualified Data.Text   as T


uncons :: [α] -> Maybe (α, [α])
uncons []     = Nothing
uncons (x:xs) = return (x, xs)


escapeXMLText :: T.Text -> T.Text
escapeXMLText = T.pack . escapeXML . T.unpack


escapeXML :: String -> String
escapeXML = concatMap $ \x -> IntMap.findWithDefault [x] (ord x) mp
    where mp = IntMap.fromList [(ord b, "&"++a++";") | (a,[b]) <- xmlEntities]


xmlEntities :: [(String, String)]
xmlEntities =
  [ ("quot", "\"")
  , ("#39", "'")
  , ("amp" , "&")
  , ("lt"  , "<")
  , ("gt"  , ">")
  ]