module Heist.Splices.Bind 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.Internal.Types.HeistState

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


------------------------------------------------------------------------------
-- | Default attribute name for the bind tag.
bindAttr :: Text
bindAttr :: Text
bindAttr = Text
"tag"


------------------------------------------------------------------------------
-- | Implementation of the bind splice.
bindImpl :: Monad n => Splice n
bindImpl :: forall (n :: * -> *). Monad n => Splice n
bindImpl = do
    node <- HeistT n n Node
forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
    let err = String
"must supply \"" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
bindAttr String -> String -> String
forall a. [a] -> [a] -> [a]
++
              String
"\" attribute in <" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack (Node -> Text
X.elementTag Node
node) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
">"
    maybe (return () `orError` err)
          (add node)
          (X.getAttribute bindAttr node)
    return []
  where
    add :: Node -> Text -> HeistT n m ()
add Node
node Text
nm = (HeistState n -> HeistState n) -> HeistT n m ()
forall (m :: * -> *) (n :: * -> *).
Monad m =>
(HeistState n -> HeistState n) -> HeistT n m ()
modifyHS ((HeistState n -> HeistState n) -> HeistT n m ())
-> (HeistState n -> HeistState n) -> HeistT n m ()
forall a b. (a -> b) -> a -> b
$ Text -> Splice n -> HeistState n -> HeistState n
forall (n :: * -> *).
Text -> Splice n -> HeistState n -> HeistState n
bindSplice Text
nm (Splice n -> HeistState n -> HeistState n)
-> Splice n -> HeistState n -> HeistState n
forall a b. (a -> b) -> a -> b
$ do
        caller <- HeistT n n Node
forall (m :: * -> *) (n :: * -> *). Monad m => HeistT n m Node
getParamNode
        ctx <- getContext
        rawApply "bind-content" (X.childNodes node)
                 Nothing ctx (X.childNodes caller)