louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Compiled templaters (ADR 0057): a page written as an HTML literal, as |
| 2 | // `html`…`` and as `pug`…`` renders byte-identical output. Runs on both |
| 3 | // backends, optimised and not. |
| 4 | |
| 5 | import std/html.{type Html} |
| 6 | import std/io |
| 7 | import std/list |
| 8 | import std/string |
| 9 | import std/template.{html, pug} |
| 10 | import std/test.{assert} |
| 11 | |
| 12 | // http_demo's page skeleton. |
| 13 | fn page_literal :: title: String body: Html -> Html |
| 14 | { |
| 15 | <html lang="en"> |
| 16 | <head> |
| 17 | <meta charset="utf-8"/> |
| 18 | <link rel="stylesheet" href="/static/style.css"/> |
| 19 | <title>{title}</title> |
| 20 | </head> |
| 21 | <body> |
| 22 | <main class="shell"> |
| 23 | <h1>{title}</h1> |
| 24 | {body} |
| 25 | </main> |
| 26 | </body> |
| 27 | </html> |
| 28 | } |
| 29 | |
| 30 | fn page_html :: title: String body: Html -> Html |
| 31 | { |
| 32 | html`<html lang="en"><head><meta charset="utf-8"><link rel="stylesheet" href="/static/style.css"><title>{title}</title></head><body><main class="shell"><h1>{title}</h1>{body}</main></body></html>` |
| 33 | } |
| 34 | |
| 35 | fn page_pug :: title: String body: Html -> Html |
| 36 | { |
| 37 | pug` |
| 38 | html(lang="en") |
| 39 | head |
| 40 | meta(charset="utf-8") |
| 41 | link(rel="stylesheet" href="/static/style.css") |
| 42 | title {title} |
| 43 | body |
| 44 | main.shell |
| 45 | h1 {title} |
| 46 | {body} |
| 47 | ` |
| 48 | } |
| 49 | |
| 50 | // A card with an escaped text hole and an embedded element. |
| 51 | fn card_literal :: title: String body: Html -> Html |
| 52 | { |
| 53 | <section class="card"><h2>{title}</h2>{body}</section> |
| 54 | } |
| 55 | |
| 56 | fn card_html :: title: String body: Html -> Html |
| 57 | { |
| 58 | html`<section class="card"><h2>{title}</h2>{body}</section>` |
| 59 | } |
| 60 | |
| 61 | fn card_pug :: title: String body: Html -> Html |
| 62 | { |
| 63 | pug` |
| 64 | section.card |
| 65 | h2 {title} |
| 66 | {body} |
| 67 | ` |
| 68 | } |
| 69 | |
| 70 | // A list hole and inline holes in text. |
| 71 | fn list_literal :: name: String items: List<Html> -> Html |
| 72 | { |
| 73 | <div class="list"><p>Welcome, {name}! Count: {list.length |
| 74 | items}.</p><ul>{items}</ul></div> |
| 75 | } |
| 76 | |
| 77 | fn list_html :: name: String items: List<Html> -> Html |
| 78 | { |
| 79 | html`<div class="list"><p>Welcome, {name}! Count: {list.length items}.</p><ul>{items}</ul></div>` |
| 80 | } |
| 81 | |
| 82 | fn list_pug :: name: String items: List<Html> -> Html |
| 83 | { |
| 84 | pug` |
| 85 | .list |
| 86 | p Welcome, {name}! Count: {list.length items}. |
| 87 | ul |
| 88 | {items} |
| 89 | ` |
| 90 | } |
| 91 | |
| 92 | // Typed attribute holes (literal and pug; `html`'s are raw text). |
| 93 | fn attrs_literal :: on: Bool off: Bool rows: Int value: Option<String> -> Html |
| 94 | { |
| 95 | <input required={on} autofocus={off} rows={rows} value={value}/> |
| 96 | } |
| 97 | |
| 98 | fn attrs_pug :: on: Bool off: Bool rows: Int value: Option<String> -> Html |
| 99 | { |
| 100 | pug`input(required={on} autofocus={off} rows={rows} value={value})` |
| 101 | } |
| 102 | |
| 103 | fn same :: name: String a: String b: String -> () / <io> |
| 104 | { |
| 105 | assert (a == b) name |
| 106 | io.println ("ok: " <> name) |
| 107 | } |
| 108 | |
| 109 | pub fn main :: -> () / <io> |
| 110 | { |
| 111 | let body = html`<p>body & soul</p>` |
| 112 | same |
| 113 | "page" |
| 114 | (html.render (page_literal "T & <" body)) |
| 115 | (html.render (page_html "T & <" body)) |
| 116 | same |
| 117 | "page" |
| 118 | (html.render (page_literal "T & <" body)) |
| 119 | (html.render (page_pug "T & <" body)) |
| 120 | let inner = pug`p body` |
| 121 | same |
| 122 | "card" |
| 123 | (html.render (card_literal "T <&" inner)) |
| 124 | (html.render (card_html "T <&" inner)) |
| 125 | same |
| 126 | "card" |
| 127 | (html.render (card_literal "T <&" inner)) |
| 128 | (html.render (card_pug "T <&" inner)) |
| 129 | let items: List<Html> = [pug`li one`, html`<li>two</li>`, <li>three</li>] |
| 130 | same |
| 131 | "list" |
| 132 | (html.render (list_literal "Ada" items)) |
| 133 | (html.render (list_html "Ada" items)) |
| 134 | same |
| 135 | "list" |
| 136 | (html.render (list_literal "Ada" items)) |
| 137 | (html.render (list_pug "Ada" items)) |
| 138 | same |
| 139 | "attrs" |
| 140 | (html.render (attrs_literal True False 3 Some("v"))) |
| 141 | (html.render (attrs_pug True False 3 Some("v"))) |
| 142 | same |
| 143 | "attrs" |
| 144 | (html.render (attrs_literal False True 0 None)) |
| 145 | (html.render (attrs_pug False True 0 None)) |
| 146 | io.println (html.render (attrs_pug True False 3 None)) |
| 147 | } |
| 148 | //> ok: page |
| 149 | //> ok: page |
| 150 | //> ok: card |
| 151 | //> ok: card |
| 152 | //> ok: list |
| 153 | //> ok: list |
| 154 | //> ok: attrs |
| 155 | //> ok: attrs |
| 156 | //> <input required rows="3"> |