type Splice = Node -> m [Node]

runNode :: Monad m => X.Node -> Splice m runNode (X.Element nm at ch) = do newAtts <- mapM attSubst at let n = X.Element nm newAtts ch s <- liftM (lookupSplice nm) getTS maybe (runKids newAtts) (recurseSplice n) s where runKids newAtts = do newKids <- runNodeList ch return [X.Element nm newAtts newKids] runNode n = return [n]

recurseSplice :: Monad m => X.Node -> Splice m -> Splice m recurseSplice node splice = do result <- localParamNode (const node) splice ts' <- getTS if _recurse ts' && _recursionDepth ts' < mAX_RECURSION_DEPTH then do modRecursionDepth (+1) res <- runNodeList result restoreTS ts' return res else return result where modRecursionDepth :: Monad m => (Int -> Int) -> TemplateMonad m () modRecursionDepth f = modifyTS (\st -> st { _recursionDepth = f (_recursionDepth st) })