module Heist.Splices.BindStrict where

------------------------------------------------------------------------------
import           Data.Text (Text)
import qualified Data.Text as T
import qualified Text.XmlHtml as X

------------------------------------------------------------------------------
import           Heist.Common
import           Heist.Interpreted.Internal
import           Heist.Splices.Apply
import           Heist.Splices.Bind
import           Heist.Internal.Types.HeistState

-- | Default name for the bind splice.
bindStrictTag :: Text
bindStrictTag :: Text
bindStrictTag = Text
"bindStrict"


------------------------------------------------------------------------------
-- | Implementation of the bind splice.
bindStrictImpl :: Monad n => Splice n
bindStrictImpl :: forall (n :: * -> *). Monad n => Splice n
bindStrictImpl = do
    Node
node <- forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
    Template
cs <- forall (n :: * -> *). Monad n => Splice n
runChildren
    let err :: [Char]
err = [Char]
"must supply \"" forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
bindAttr forall a. [a] -> [a] -> [a]
++
              [Char]
"\" attribute in <" forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack (Node -> Text
X.elementTag Node
node) forall a. [a] -> [a] -> [a]
++ [Char]
">"
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. Monad m => a -> m a
return () forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
HeistT n m b -> [Char] -> HeistT n m b
`orError` [Char]
err) (forall {m :: * -> *} {n :: * -> *}.
(Monad m, Monad n) =>
Template -> Text -> HeistT n m ()
add Template
cs)
          (Text -> Node -> Maybe Text
X.getAttribute Text
bindAttr Node
node)
    forall (m :: * -> *) a. Monad m => a -> m a
return []

  where
    add :: Template -> Text -> HeistT n m ()
add Template
cs Text
nm = forall (m :: * -> *) (n :: * -> *).
Monad m =>
(HeistState n -> HeistState n) -> HeistT n m ()
modifyHS forall a b. (a -> b) -> a -> b
$ forall (n :: * -> *).
Text -> Splice n -> HeistState n -> HeistState n
bindSplice Text
nm forall a b. (a -> b) -> a -> b
$ do
        Node
caller <- forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
        TPath
ctx <- forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m TPath
getContext
        forall (n :: * -> *).
Monad n =>
Text -> Template -> Maybe [Char] -> TPath -> Template -> Splice n
rawApply Text
"bindstrict-content" Template
cs forall a. Maybe a
Nothing TPath
ctx (Node -> Template
X.childNodes Node
caller)