    {-|
    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 :: [α] -> Maybe (α, [α])
    uncons []     = Maybe (α, [α])
    forall a. Maybe a
    Nothing
    uncons (x :: α
    x:xs :: [α]
    xs) = (α, [α]) -> Maybe (α, [α])
    forall (m :: * -> *) a. Monad m => a -> m a
    return (α
    x, [α]
    xs)

    escapeXMLText :: T.Text -> T.Text
    escapeXMLText :: Text -> Text
    escapeXMLText = String -> Text
    T.pack (String -> Text) -> (Text -> String) -> Text -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> String
    escapeXML (String -> String) -> (Text -> String) -> Text -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> String
    T.unpack

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

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