general/markup/ops.bend

near-v-framework@0.6.0.0 · 38 lines · 0 definitions

1import Base
2import near-v-framework@0.6.0.0/general/markup/type.bend as M
3
4# Printing markup: text and attribute values are escaped; Raw is printed as it is (a stylesheet or a
5# script, whose own language has no markup in it).
6
7def escape.go(cs: List<&2, Char>) -> String:
8  match cs:
9    case []:
10      ""
11    case +c <> rest:
12      Bool.pick(String, Char.is_eq(c, '&'), "&amp;",
13        Bool.pick(String, Char.is_eq(c, '<'), "&lt;",
14          Bool.pick(String, Char.is_eq(c, '>'), "&gt;",
15            Bool.pick(String, Char.is_eq(c, '"'), "&quot;", String.from_list([c]))))) ++ escape.go(rest)
16
17def escape(s: String) -> String:
18  escape.go(String.to_list(s))
19
20def attributes(xs: List<&2, M.Attribute>) -> String:
21  match xs:
22    case []:
23      ""
24    case M.Attribute{n, v} <> rest:
25      " " ++ n ++ "=\"" ++ escape(v) ++ "\"" ++ attributes(rest)
26
27def print(ns: List<&2, M.Node>) -> String:
28  match ns:
29    case []:
30      ""
31    case M.Element{+tag, xs, children} <> rest:
32      "<" ++ tag ++ attributes(xs) ++ ">" ++ print(children) ++ "</" ++ tag ++ ">" ++ print(rest)
33    case M.Void{tag, xs} <> rest:
34      "<" ++ tag ++ attributes(xs) ++ ">" ++ print(rest)
35    case M.Text{t} <> rest:
36      escape(t) ++ print(rest)
37    case M.Raw{t} <> rest:
38      t ++ print(rest)