    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 = "bind"

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

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