// Precompiled renderers (ADR 0054), issue #333: a tree passed to a // component that matches on it, and a node whose walk stops partway. // The components are recursive, so the simplifier never inlines them // (as for a component from another module): the precompile pass // translates their bodies with the call's arguments. import std/html.{type Html, Attribute, Fragment, Node, Text, text} import std/io import std/list // Matched on, then placed as a child in the fallback: the argument's // tree is reached from more than one place, so it stays built and is // rendered as it is. fn wrap :: id: String skeleton: Html -> Html { match skeleton { Node(tag, attributes, children) -> Node(tag, [Attribute("id", id), ..attributes], children) , Fragment([only]) -> wrap id only , _ -> Node("div", [Attribute("id", id)], [skeleton]) } } // One attribute list for two arms: the fallback's walk stops at the // shared list, and the node is rendered as it is from the builder it // started at. fn mark :: id: String skeleton: Html -> Html { let marks = [Attribute("id", id)] match skeleton { Node(tag, attributes, children) -> Node(tag, list.append attributes marks, children) , Fragment([only]) -> mark id only , _ -> Node("div", marks, [skeleton]) } } pub fn main :: -> () / { // The fallback: the fragment is matched on, then a child. io.println (html.render (wrap "a" (Fragment([Node("p", [], []), Node("br", [], [])])))) // The first arm: the node's parts are taken apart and rebuilt. io.println ( html.render (wrap "b" (Node("p", [Attribute("class", "x")], [Text("hi")]))) ) // The recursion: a fragment of one element marks the element. io.println (html.render (wrap "c" (Fragment([Node("em", [], [])])))) io.println (html.render (mark "d" (Fragment([Node("p", [], []), Node("br", [], [])])))) io.println ( html.render (mark "e" (Node("p", [Attribute("class", "x")], [Text("hi")]))) ) io.println (html.render (mark "f" (Fragment([Node("em", [], [])])))) // No component: after a hole, a node whose walk stops at a shared // attribute list is rendered from the builder the hole left. let shared = [Attribute("class", "x")] let name = "n" io.println ( html.render (
{text name}{Node("p", shared, [])}{Node("i", shared, [])}
) ) } //>


//>

hi

//> //>


//>

hi

//> //>
n