    {-|
    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 :: forall α. [α] -> Maybe (α, [α])
    uncons []     = Maybe (α, [α])
    forall a. Maybe a
    Nothing
    uncons (α
    x:[α]
    xs) = (α, [α]) -> Maybe (α, [α])
    forall a. a -> Maybe a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (α
    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
    $ \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 -> String
    forall a. [a] -> [a] -> [a]
    ++String
    aString -> String -> String
    forall a. [a] -> [a] -> [a]
    ++String
    ";") | (String
    a,[Char
    b]) <- [(String, String)]
    xmlEntities]

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