tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

72 lignes · 2589 octetsBrut · Historique
1// Precompiled renderers (ADR 0054), issue #333: a tree passed to a
2// component that matches on it, and a node whose walk stops partway.
3// The components are recursive, so the simplifier never inlines them
4// (as for a component from another module): the precompile pass
5// translates their bodies with the call's arguments.
6
7import std/html.{type Html, Attribute, Fragment, Node, Text, text}
8import std/io
9import std/list
10
11// Matched on, then placed as a child in the fallback: the argument's
12// tree is reached from more than one place, so it stays built and is
13// rendered as it is.
14fn wrap :: id: String skeleton: Html -> Html
15{
16 match skeleton
17 { Node(tag, attributes, children) ->
18 Node(tag, [Attribute("id", id), ..attributes], children)
19 , Fragment([only]) -> wrap id only
20 , _ -> Node("div", [Attribute("id", id)], [skeleton])
21 }
22}
23
24// One attribute list for two arms: the fallback's walk stops at the
25// shared list, and the node is rendered as it is from the builder it
26// started at.
27fn mark :: id: String skeleton: Html -> Html
28{
29 let marks = [Attribute("id", id)]
30 match skeleton
31 { Node(tag, attributes, children) ->
32 Node(tag, list.append attributes marks, children)
33 , Fragment([only]) -> mark id only
34 , _ -> Node("div", marks, [skeleton])
35 }
36}
37
38pub fn main :: -> () / <io>
39{
40 // The fallback: the fragment is matched on, then a child.
41 io.println
42 (html.render (wrap "a" (Fragment([Node("p", [], []), Node("br", [], [])]))))
43 // The first arm: the node's parts are taken apart and rebuilt.
44 io.println
45 ( html.render
46 (wrap "b" (Node("p", [Attribute("class", "x")], [Text("hi")])))
47 )
48 // The recursion: a fragment of one element marks the element.
49 io.println (html.render (wrap "c" (Fragment([Node("em", [], [])]))))
50 io.println
51 (html.render (mark "d" (Fragment([Node("p", [], []), Node("br", [], [])]))))
52 io.println
53 ( html.render
54 (mark "e" (Node("p", [Attribute("class", "x")], [Text("hi")])))
55 )
56 io.println (html.render (mark "f" (Fragment([Node("em", [], [])]))))
57 // No component: after a hole, a node whose walk stops at a shared
58 // attribute list is rendered from the builder the hole left.
59 let shared = [Attribute("class", "x")]
60 let name = "n"
61 io.println
62 ( html.render
63 (<div>{text name}{Node("p", shared, [])}{Node("i", shared, [])}</div>)
64 )
65}
66//> <div id="a"><p></p><br></div>
67//> <p id="b" class="x">hi</p>
68//> <em id="c"></em>
69//> <div id="d"><p></p><br></div>
70//> <p class="x" id="e">hi</p>
71//> <em id="f"></em>
72//> <div>n<p class="x"></p><i class="x"></i></div>