tobogganSe connecter

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.

90 lignes · 3285 octetsBrut · Historique
1/// Templaters: the tags of tagged template literals (ADR 0048).
2///
3/// `css`body { color: {c} }`` calls `css` with the literal's `Template`:
4/// its static text and the values of its `{...}` holes. A templater is a
5/// plain function from a `Template`; write your own for other languages.
6///
7/// Holes are safe by default: a `String` or a value with `Show` is text
8/// the templater escapes; an `Element` is embedded as is.
9///
10/// std's own `pug`, `html` and `xml` are not functions: the compiler
11/// lowers a template tagged with one of them to the HTML tree it denotes
12/// (ADR 0057), so their holes are typed by position and a malformed
13/// template is a compile error. The declarations below exist only as the
14/// tags' resolution targets; they are never called.
15
16import std/html
17
18/// A template literal's static text and holes. `parts` holds one item
19/// more than `holes`: text, hole, text, ..., hole, text.
20pub type Template<msg> { Template(parts: List<String>, holes: List<Hole<msg>>) }
21
22/// A hole's value: `Text` is untrusted text a templater escapes; `Embed`
23/// is an element inserted as is.
24pub type Hole<msg> { Text(String), Embed(html.Element<msg>) }
25
26/// `html`<p class="{cls}">{body}</p>``: the markup as written, with text
27/// holes escaped when rendered and element holes embedded.
28///
29/// Compiled (ADR 0057): this declaration is the tag's resolution target
30/// and is never called.
31pub fn html :: template: Template<msg> -> html.Element<msg>
32{
33 panic "unreachable: std's templaters are compiled syntax (ADR 0057)"
34}
35
36/// XML built by the `xml` templater; `render_xml` returns the markup.
37pub opaque type Xml { Xml(String) }
38
39/// `xml`<item id="{id}">{name}</item>``: the markup as written, with the
40/// holes escaped.
41///
42/// Compiled (ADR 0057): this declaration is the tag's resolution target
43/// and is never called.
44pub fn xml :: template: Template<msg> -> Xml
45{
46 panic "unreachable: std's templaters are compiled syntax (ADR 0057)"
47}
48
49/// The markup of an `Xml` value.
50pub fn render_xml :: x: Xml -> String
51{
52 let Xml(s) = x
53 s
54}
55
56/// The compiled `xml` templater's runtime part (ADR 0057): the compiler
57/// lowers `xml`…`` to the HTML fragment the template denotes (raw parts,
58/// typed holes) and calls this to render it into an `Xml` — the runtime
59/// `xml`'s output, byte for byte.
60pub fn xml_of_element :: element: html.Element<msg> -> Xml
61{
62 Xml(html.render element)
63}
64
65/// Pug markup, the indentation-based shorthand:
66///
67/// ```louise
68/// pug`
69/// .card
70/// h2 {title}
71/// p.body-text Hello, {name}!
72/// ul
73/// {items}
74/// `
75/// ```
76///
77/// A line is an element (a `tag`, `.class`es, an `#id` and attributes in
78/// parentheses, then inline content after a space), a `| text` line, a
79/// `//` comment, or a hole on its own (the element embedded there). Deeper
80/// lines are children. `.card` and `#id` shorthands are `div`s, void
81/// elements and escaping are as in `std/html`, an uppercase tag calls a
82/// component, and a malformed template is a compile error naming the
83/// line.
84///
85/// Compiled (ADR 0057): this declaration is the tag's resolution target
86/// and is never called.
87pub fn pug :: template: Template<msg> -> html.Element<msg>
88{
89 panic "unreachable: std's templaters are compiled syntax (ADR 0057)"
90}