trebol 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +8 -2
  2. package/dist/cli.js +55 -20
  3. package/dist/lib/core/access.d.ts +10 -0
  4. package/dist/lib/core/access.js +210 -0
  5. package/dist/lib/core/base.d.ts +4 -0
  6. package/dist/lib/core/base.js +50 -0
  7. package/dist/lib/core/bitset.d.ts +9 -0
  8. package/dist/lib/core/bitset.js +75 -0
  9. package/dist/lib/core/boot.d.ts +18 -0
  10. package/dist/lib/core/boot.js +72 -0
  11. package/dist/lib/core/compare.d.ts +9 -0
  12. package/dist/lib/core/compare.js +156 -0
  13. package/dist/lib/core/ctx.d.ts +36 -0
  14. package/dist/lib/core/ctx.js +251 -0
  15. package/dist/lib/core/dec.d.ts +28 -0
  16. package/dist/lib/core/dec.js +158 -0
  17. package/dist/lib/core/errors.d.ts +15 -0
  18. package/dist/lib/core/errors.js +44 -0
  19. package/dist/lib/core/escapes.d.ts +14 -0
  20. package/dist/lib/core/escapes.js +80 -0
  21. package/dist/lib/core/eval.d.ts +220 -0
  22. package/dist/lib/core/eval.js +666 -0
  23. package/dist/lib/core/func.d.ts +64 -0
  24. package/dist/lib/core/func.js +129 -0
  25. package/dist/lib/core/hash.d.ts +5 -0
  26. package/dist/lib/core/hash.js +155 -0
  27. package/dist/lib/core/index.d.ts +18 -0
  28. package/dist/lib/core/index.js +20 -0
  29. package/dist/lib/core/int64.d.ts +22 -0
  30. package/dist/lib/core/int64.js +113 -0
  31. package/dist/lib/core/kinds.d.ts +67 -0
  32. package/dist/lib/core/kinds.js +134 -0
  33. package/dist/lib/core/lexer.d.ts +13 -0
  34. package/dist/lib/core/lexer.js +702 -0
  35. package/dist/lib/core/mezz.d.ts +1 -0
  36. package/dist/lib/core/mezz.js +360 -0
  37. package/dist/lib/core/mold.d.ts +20 -0
  38. package/dist/lib/core/mold.js +434 -0
  39. package/dist/lib/core/natives/control.d.ts +1 -0
  40. package/dist/lib/core/natives/control.js +374 -0
  41. package/dist/lib/core/natives/csv.d.ts +15 -0
  42. package/dist/lib/core/natives/csv.js +315 -0
  43. package/dist/lib/core/natives/data.d.ts +11 -0
  44. package/dist/lib/core/natives/data.js +367 -0
  45. package/dist/lib/core/natives/define.d.ts +17 -0
  46. package/dist/lib/core/natives/define.js +47 -0
  47. package/dist/lib/core/natives/encode.d.ts +3 -0
  48. package/dist/lib/core/natives/encode.js +187 -0
  49. package/dist/lib/core/natives/json.d.ts +5 -0
  50. package/dist/lib/core/natives/json.js +273 -0
  51. package/dist/lib/core/natives/math.d.ts +3 -0
  52. package/dist/lib/core/natives/math.js +500 -0
  53. package/dist/lib/core/natives/parse.d.ts +1 -0
  54. package/dist/lib/core/natives/parse.js +600 -0
  55. package/dist/lib/core/natives/series.d.ts +4 -0
  56. package/dist/lib/core/natives/series.js +509 -0
  57. package/dist/lib/core/natives/text.d.ts +1 -0
  58. package/dist/lib/core/natives/text.js +484 -0
  59. package/dist/lib/core/natives/xml.d.ts +9 -0
  60. package/dist/lib/core/natives/xml.js +242 -0
  61. package/dist/lib/core/series.d.ts +26 -0
  62. package/dist/lib/core/series.js +73 -0
  63. package/dist/lib/core/symbols.d.ts +9 -0
  64. package/dist/lib/core/symbols.js +44 -0
  65. package/dist/lib/core/values.d.ts +206 -0
  66. package/dist/lib/core/values.js +223 -0
  67. package/dist/lib/host/browser.d.ts +4 -0
  68. package/dist/lib/host/browser.js +4081 -0
  69. package/dist/lib/host/chart.d.ts +26 -0
  70. package/dist/lib/host/chart.js +158 -0
  71. package/dist/lib/host/compress.d.ts +1 -0
  72. package/dist/lib/host/compress.js +37 -0
  73. package/dist/lib/host/draw.d.ts +31 -0
  74. package/dist/lib/host/draw.js +348 -0
  75. package/dist/lib/host/icons.d.ts +3 -0
  76. package/dist/lib/host/icons.js +97 -0
  77. package/dist/lib/host/index.d.ts +38 -0
  78. package/dist/lib/host/index.js +190 -0
  79. package/dist/lib/host/locale.d.ts +40 -0
  80. package/dist/lib/host/locale.js +241 -0
  81. package/dist/lib/host/markdown.d.ts +28 -0
  82. package/dist/lib/host/markdown.js +151 -0
  83. package/dist/lib/host/modules.d.ts +10 -0
  84. package/dist/lib/host/modules.js +262 -0
  85. package/dist/lib/host/natives.d.ts +6 -0
  86. package/dist/lib/host/natives.js +98 -0
  87. package/dist/lib/host/net.d.ts +12 -0
  88. package/dist/lib/host/net.js +541 -0
  89. package/dist/lib/host/page.d.ts +9 -0
  90. package/dist/lib/host/page.js +55 -0
  91. package/dist/lib/host/rows.d.ts +15 -0
  92. package/dist/lib/host/rows.js +80 -0
  93. package/dist/lib/host/scheduler.d.ts +45 -0
  94. package/dist/lib/host/scheduler.js +173 -0
  95. package/dist/lib/host/theme.d.ts +58 -0
  96. package/dist/lib/host/theme.js +1462 -0
  97. package/dist/lib/ui/i18n.trb +111 -0
  98. package/dist/lib/ui/ui.trb +1237 -0
  99. package/dist/trebol.js +123 -56
  100. package/package.json +17 -5
  101. package/src/host/browser.ts +274 -33
  102. package/src/host/index.ts +2 -2
  103. package/src/host/modules.ts +8 -1
  104. package/src/host/theme.ts +63 -4
  105. package/src/ui/i18n.trb +4 -0
  106. package/src/ui/ui.trb +48 -15
@@ -0,0 +1,97 @@
1
+ // The built-in icons: 24 px outlines drawn with the current text color, so
2
+ // they follow the role of the face that shows them. `icon #search`,
3
+ // `button #plus "New"`, `field #search "Search"`.
4
+ const PATHS = {
5
+ search: 'M11 4a7 7 0 1 0 0 14a7 7 0 1 0 0-14zM16.5 16.5L21 21',
6
+ plus: 'M12 5v14M5 12h14',
7
+ minus: 'M5 12h14',
8
+ close: 'M6 6l12 12M18 6L6 18',
9
+ check: 'M5 12.5l4.5 4.5L19 7',
10
+ 'chevron-left': 'M15 6l-6 6 6 6',
11
+ 'chevron-right': 'M9 6l6 6-6 6',
12
+ 'chevron-up': 'M6 15l6-6 6 6',
13
+ 'chevron-down': 'M6 9l6 6 6-6',
14
+ 'chevrons-left': 'M11 6l-6 6 6 6M19 6l-6 6 6 6',
15
+ 'chevrons-right': 'M5 6l6 6-6 6M13 6l6 6-6 6',
16
+ 'arrow-left': 'M19 12H5M11 6l-6 6 6 6',
17
+ 'arrow-right': 'M5 12h14M13 6l6 6-6 6',
18
+ 'arrow-up': 'M12 19V5M6 11l6-6 6 6',
19
+ 'arrow-down': 'M12 5v14M6 13l6 6 6-6',
20
+ edit: 'M4 20h4L19 9l-4-4L4 16v4zM13.5 6.5l4 4',
21
+ trash: 'M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13M10 11v6M14 11v6',
22
+ user: 'M12 4a4 4 0 1 0 0 8a4 4 0 1 0 0-8zM4 21c0-4 3.5-7 8-7s8 3 8 7',
23
+ mail: 'M3 6h18v12H3zM3 7l9 6 9-6',
24
+ phone: 'M5 4h4l2 5-2.5 1.5a11 11 0 0 0 5 5L15 13l5 2v4a1 1 0 0 1-1 1A16 16 0 0 1 4 5a1 1 0 0 1 1-1z',
25
+ info: 'M12 3a9 9 0 1 0 0 18a9 9 0 1 0 0-18zM12 11v6M12 7.5v.5',
26
+ warning: 'M12 3L2 20h20L12 3zM12 10v4M12 17v.5',
27
+ menu: 'M4 6h16M4 12h16M4 18h16',
28
+ home: 'M3 11l9-7 9 7M5 9.5V20h5v-6h4v6h5V9.5',
29
+ folder: 'M3 6.5A1.5 1.5 0 0 1 4.5 5H9l2 2.5h8.5A1.5 1.5 0 0 1 21 9v9.5a1.5 1.5 0 0 1-1.5 1.5h-15A1.5 1.5 0 0 1 3 18.5z',
30
+ door: 'M6 21V4a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v17M3 21h18M14.5 12v.5',
31
+ building: 'M5 21V4h14v17M3 21h18M9 8h.01M15 8h.01M9 12h.01M15 12h.01M10 21v-5h4v5',
32
+ landmark: 'M3 21h18M5 18h14M12 3l9 5H3zM6 8v10M10 8v10M14 8v10M18 8v10',
33
+ shield: 'M12 3l8 3v6c0 5-3.5 8-8 9c-4.5-1-8-4-8-9V6z',
34
+ fireworks: 'M12 22v-9M12 3v3M5 7l2.5 2M19 7l-2.5 2M3.5 13h3M17.5 13h3M8 3.5l1 2.5M16 3.5l-1 2.5',
35
+ coins: 'M9 4a6 6 0 1 0 0 12a6 6 0 1 0 0-12zM15.5 8.3a6 6 0 1 1-7.2 7.2',
36
+ book: 'M4 5a2 2 0 0 1 2-2h14v15H6a2 2 0 0 0-2 2zM4 20a2 2 0 0 0 2 1h14v-3',
37
+ scale: 'M12 4v17M7 21h10M5 7h14M5 7l-3 7a3.5 3.5 0 0 0 6 0zM19 7l-3 7a3.5 3.5 0 0 0 6 0z',
38
+ people: 'M9 5a3.5 3.5 0 1 0 0 7a3.5 3.5 0 1 0 0-7zM2.5 20c0-3.5 3-6 6.5-6s6.5 2.5 6.5 6M16 5.3a3.5 3.5 0 0 1 0 6.4M18 14.5c2 .8 3.5 2.8 3.5 5.5',
39
+ tree: 'M12 3l6 7h-3l4 5H5l4-5H6zM12 15v6',
40
+ truck: 'M2 6h12v11H2zM14 10h4l4 4v3h-8zM6.5 15a2 2 0 1 0 0 4a2 2 0 1 0 0-4zM17.5 15a2 2 0 1 0 0 4a2 2 0 1 0 0-4z',
41
+ box: 'M12 3l8 4.5v9L12 21l-8-4.5v-9zM4 7.5l8 4.5 8-4.5M12 12v9',
42
+ file: 'M6 3h8l4 4v14H6zM14 3v4h4M9 12h6M9 16h6',
43
+ image: 'M4 5h16v14H4zM4 16l5-5 4 4 2-2 5 5M15 8.5a1.5 1.5 0 1 0 0 3a1.5 1.5 0 1 0 0-3z',
44
+ settings: 'M4 7h10M18 7h2M4 17h4M12 17h8M16 5v4M10 15v4',
45
+ filter: 'M4 5h16l-6 8v6l-4-2v-4L4 5z',
46
+ refresh: 'M20 12a8 8 0 1 1-2.3-5.7M20 4v5h-5',
47
+ download: 'M12 4v11M7 10l5 5 5-5M5 20h14',
48
+ upload: 'M12 16V4M7 9l5-5 5 5M5 20h14',
49
+ external: 'M14 4h6v6M20 4l-9 9M18 14v6H4V6h6',
50
+ eye: 'M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12zM12 9a3 3 0 1 0 0 6a3 3 0 1 0 0-6z',
51
+ lock: 'M5 11h14v10H5zM8 11V7a4 4 0 0 1 8 0v4',
52
+ calendar: 'M4 6h16v15H4zM4 10h16M8 3v4M16 3v4',
53
+ star: 'M12 3l2.8 5.8 6.2.9-4.5 4.4 1 6.3L12 17.5l-5.5 2.9 1-6.3L3 9.7l6.2-.9L12 3z',
54
+ heart: 'M12 20s-8-5-8-11a4.5 4.5 0 0 1 8-2.8A4.5 4.5 0 0 1 20 9c0 6-8 11-8 11z',
55
+ type: 'M5 7V4h14v3M12 4v16M9 20h6',
56
+ lines: 'M4 6h16M4 12h16M4 18h10',
57
+ rows: 'M3 5h18v6H3zM3 13h18v6H3z',
58
+ bold: 'M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z',
59
+ italic: 'M10 5h8M6 19h8M14 5l-4 14',
60
+ heading: 'M6 5v14M18 5v14M6 12h12',
61
+ list: 'M9 6h11M9 12h11M9 18h11M4 6h.01M4 12h.01M4 18h.01',
62
+ 'list-ordered': 'M10 6h10M10 12h10M10 18h10M4 5l1.5-1v5M4 14.5a1.5 1.5 0 0 1 3 0c0 1.5-3 2-3 4h3',
63
+ link: 'M10 14a4 4 0 0 0 6 0l3-3a4 4 0 0 0-6-6l-1 1M14 10a4 4 0 0 0-6 0l-3 3a4 4 0 0 0 6 6l1-1',
64
+ hash: 'M5 9h14M5 15h14M10 4L8 20M16 4l-2 16',
65
+ banknote: 'M3 7h18v10H3zM12 9.5a2.5 2.5 0 1 0 0 5a2.5 2.5 0 1 0 0-5zM6.5 10v4M17.5 10v4',
66
+ 'check-square': 'M4 4h16v16H4zM8 12l3 3 5-6',
67
+ toggle: 'M8 7h8a5 5 0 0 1 0 10H8A5 5 0 0 1 8 7zM16 9.5a2.5 2.5 0 1 0 0 5a2.5 2.5 0 1 0 0-5z',
68
+ radio: 'M12 3a9 9 0 1 0 0 18a9 9 0 1 0 0-18zM12 9a3 3 0 1 0 0 6a3 3 0 1 0 0-6z',
69
+ dropdown: 'M3 7h18v10H3zM14 11l2 2 2-2',
70
+ panel: 'M4 4h16v16H4zM4 9h16',
71
+ columns: 'M3 5h7v14H3zM14 5h7v14h-7z',
72
+ grid: 'M4 4h7v7H4zM13 4h7v7h-7zM4 13h7v7H4zM13 13h7v7h-7z',
73
+ tabs: 'M3 9h18v11H3zM3 9V5h7v4M10 5h6v4',
74
+ pointer: 'M6 4l13 6-5.5 2L11 18z',
75
+ };
76
+ export const ICONS = Object.keys(PATHS);
77
+ const SVG = 'http://www.w3.org/2000/svg';
78
+ /** An icon element, or null when the name is unknown. Icons are decorative unless labelled. */
79
+ export function iconNode(name, label = null) {
80
+ const d = PATHS[name];
81
+ if (d === undefined)
82
+ return null;
83
+ const svg = document.createElementNS(SVG, 'svg');
84
+ svg.setAttribute('viewBox', '0 0 24 24');
85
+ svg.setAttribute('class', 'tb-icon');
86
+ if (label === null) {
87
+ svg.setAttribute('aria-hidden', 'true');
88
+ }
89
+ else {
90
+ svg.setAttribute('role', 'img');
91
+ svg.setAttribute('aria-label', label);
92
+ }
93
+ const path = document.createElementNS(SVG, 'path');
94
+ path.setAttribute('d', d);
95
+ svg.append(path);
96
+ return svg;
97
+ }
@@ -0,0 +1,38 @@
1
+ import './natives.ts';
2
+ import './net.ts';
3
+ import './page.ts';
4
+ import './compress.ts';
5
+ import './locale.ts';
6
+ import { type Loader } from './modules.ts';
7
+ import { type Cell, type ObjectCell, type Runtime } from '../core/index.ts';
8
+ import { type Scheduler } from './scheduler.ts';
9
+ export { output } from './natives.ts';
10
+ export { fileHandle } from './net.ts';
11
+ export { page } from './page.ts';
12
+ export { mapAt, formatLocal, activeLanguage, shown, translate, uiText, editLocal, readLocal, readDate, weekStart, fileSize, LOCAL_KINDS } from './locale.ts';
13
+ export type { Loader } from './modules.ts';
14
+ export * from './scheduler.ts';
15
+ export interface Host {
16
+ readonly rt: Runtime;
17
+ readonly sch: Scheduler;
18
+ }
19
+ /** Rebol-style multi-line report of an error. */
20
+ export declare function report(e: ObjectCell): string;
21
+ export interface HostOptions {
22
+ /** Reports errors of tasks nobody waits for. */
23
+ onError?: (e: ObjectCell) => void;
24
+ /** How `import` finds files; defaults to fetch relative to the page. */
25
+ loader?: Loader;
26
+ }
27
+ export declare function startHost(opts?: HostOptions): Host;
28
+ /** URL of the UI dialect source, next to this file. */
29
+ export declare const UI_SOURCE: string;
30
+ /** URL of the languages module (i18n, tr, translated). */
31
+ export declare const I18N_SOURCE: string;
32
+ /** Loads languages and the UI dialect (view, layout, stylize, theme...) into the user context. */
33
+ export declare function startUi(host: Host): Promise<Cell>;
34
+ /**
35
+ * Loads source into the user context and runs it as a task. Resolves with
36
+ * its value; rejects with the error! if it fails. Load errors throw LoadError.
37
+ */
38
+ export declare function runSource(host: Host, src: string, file?: string): Promise<Cell>;
@@ -0,0 +1,190 @@
1
+ // The host: registers console, time, task and network natives, builds a
2
+ // runtime and a scheduler, and runs programs as tasks.
3
+ import "./natives.js";
4
+ import "./net.js";
5
+ import "./page.js";
6
+ import "./compress.js";
7
+ import "./locale.js";
8
+ import { setLoader, defaultLoader } from "./modules.js";
9
+ import { K, makeRuntime, load, bindSeries, doSource, scriptStart, errorMessage, form, mold, LIBRARY_FILES, } from "../core/index.js";
10
+ import { makeScheduler, spawn, taskResult } from "./scheduler.js";
11
+ export { output } from "./natives.js";
12
+ export { fileHandle } from "./net.js";
13
+ export { page } from "./page.js";
14
+ export { mapAt, formatLocal, activeLanguage, shown, translate, uiText, editLocal, readLocal, readDate, weekStart, fileSize, LOCAL_KINDS } from "./locale.js";
15
+ export * from "./scheduler.js";
16
+ /** Host mezzanines: the Trebol side of the host natives. */
17
+ const HOST_MEZZANINES = String.raw `
18
+ print: func ["Prints a value; a block is reduced first." value [any-type!]] [
19
+ write-line either block? :value [reduce value] [:value]
20
+ ]
21
+
22
+ fetch: func [
23
+ "Requests a URL. JSON answers come back as maps and blocks."
24
+ url [url! string!]
25
+ /with "method, headers, body, form, multipart, timeout, full"
26
+ options [block! object!]
27
+ ][
28
+ http-request url either with [either block? options [make object! options] [options]] [none]
29
+ ]
30
+
31
+ show-progress: func [
32
+ "Shows how far a request went on a face: a progress bar's value, or the bar of an upload or a photo."
33
+ face [object!]
34
+ done [integer! none!] "Bytes done; none once the request is over"
35
+ total [integer! none!] "Bytes in all, when known"
36
+ /local part
37
+ ][
38
+ part: either all [done total total > 0] [done / to decimal! total] [none]
39
+ either find [upload photo] select face 'style [face/sent: part] [if done [face/value: part]]
40
+ ]
41
+
42
+ save: func [
43
+ "Gives the user a file with a value written so load reads it back; .json, .csv and .xml are written as such."
44
+ where [file! none!] "The name of the file, or none for the bytes"
45
+ value [any-type!]
46
+ /all "Kept for R3 code: values are always written so load gives them back"
47
+ /local data
48
+ ][
49
+ ; /all names the refinement here, not the native
50
+ data: switch/default either file? where [suffix? where] [none] [
51
+ %.json [to-json :value]
52
+ %.csv [to-csv :value]
53
+ %.xml [to-xml :value]
54
+ ] [
55
+ append either block? :value [mold/all/only value] [mold/all :value] newline
56
+ ]
57
+ either where [download where data] [to binary! data]
58
+ ]
59
+
60
+ help: func [
61
+ "Shows how to call a function, or what a word holds."
62
+ 'word [any-type!] "The word to explain"
63
+ /local value name about entry args refs ret target usage describe kind
64
+ ][
65
+ unless word? :word [
66
+ print "Type help and a word: help print, help round"
67
+ exit
68
+ ]
69
+ name: uppercase form word
70
+ unless value? word [
71
+ print [name "has no value."]
72
+ exit
73
+ ]
74
+ value: get word
75
+ ; "a block!", "an object!"
76
+ kind: mold type? :value
77
+ kind: rejoin [either find "aeiou" first kind ["an "] ["a "] kind]
78
+ unless any-function? :value [
79
+ about: mold :value
80
+ if 60 < length? about [about: append copy/part about 57 "..."]
81
+ print rejoin [name " is " kind " of value: " about]
82
+ exit
83
+ ]
84
+ ; read the spec: a description, then arguments, refinements and their
85
+ ; arguments, each with its types and its own description
86
+ about: none args: copy [] refs: copy [] ret: none target: args entry: none
87
+ foreach item spec-of :value [
88
+ case [
89
+ item = /local [break]
90
+ string? item [either entry [entry/doc: item] [about: item]]
91
+ block? item [if entry [entry/types: item]]
92
+ refinement? item [
93
+ append refs entry: make object! [name: item doc: none types: none args: copy []]
94
+ target: entry/args
95
+ ]
96
+ set-word? item [entry: ret: make object! [name: item doc: none types: none]]
97
+ any [word? item lit-word? item get-word? item] [
98
+ append target entry: make object! [name: item doc: none types: none]
99
+ ]
100
+ ]
101
+ ]
102
+ describe: func [e] [
103
+ rejoin [
104
+ mold e/name
105
+ either e/doc [rejoin [" -- " e/doc]] [""]
106
+ either e/types [rejoin [" (" form e/types ")"]] [""]
107
+ ]
108
+ ]
109
+ usage: copy name
110
+ foreach a args [append append usage " " mold a/name]
111
+ ; an operator goes between its arguments
112
+ if op? :value [usage: rejoin [mold args/1/name " " name " " mold args/2/name]]
113
+ print "USAGE:"
114
+ print rejoin [" " usage]
115
+ print ""
116
+ print "DESCRIPTION:"
117
+ if about [print rejoin [" " about]]
118
+ print rejoin [" " name " is " kind " value."]
119
+ unless empty? args [
120
+ print ""
121
+ print "ARGUMENTS:"
122
+ foreach a args [print rejoin [" " describe a]]
123
+ ]
124
+ unless empty? refs [
125
+ print ""
126
+ print "REFINEMENTS:"
127
+ foreach r refs [
128
+ print rejoin [" " describe r]
129
+ foreach a r/args [print rejoin [" " describe a]]
130
+ ]
131
+ ]
132
+ if all [ret ret/types] [
133
+ print ""
134
+ print "RETURNS:"
135
+ print rejoin [" " form ret/types]
136
+ ]
137
+ exit
138
+ ]
139
+
140
+ ?: :help
141
+
142
+ ??: func ["Prints a word and its value." 'word [any-word!]] [
143
+ print rejoin [form word ": " mold get word]
144
+ ]
145
+
146
+ source: func ["Prints the source of a function." 'word [any-word!] /local value] [
147
+ value: get word
148
+ print rejoin [
149
+ form word ": " case [
150
+ function? :value [rejoin ["func " mold spec-of :value " " mold body-of :value]]
151
+ any-function? :value [rejoin ["native " mold spec-of :value]]
152
+ true [mold :value]
153
+ ]
154
+ ]
155
+ ]
156
+ `;
157
+ /** Rebol-style multi-line report of an error. */
158
+ export function report(e) {
159
+ return form(e);
160
+ }
161
+ export function startHost(opts = {}) {
162
+ const onError = opts.onError ?? (e => console.error(report(e)));
163
+ const rt = makeRuntime();
164
+ setLoader(rt, opts.loader ?? defaultLoader());
165
+ const r = doSource(rt, HOST_MEZZANINES, 'host');
166
+ // Host mezzanines failing to load is a bug in Trebol itself.
167
+ if (r.k === 'error')
168
+ throw new Error(`host mezzanines failed: ${errorMessage(r.e)}`);
169
+ const sch = makeScheduler(rt, e => onError(e));
170
+ return { rt, sch };
171
+ }
172
+ /** URL of the UI dialect source, next to this file. */
173
+ export const UI_SOURCE = new URL('../ui/ui.trb', import.meta.url).href;
174
+ /** URL of the languages module (i18n, tr, translated). */
175
+ export const I18N_SOURCE = new URL('../ui/i18n.trb', import.meta.url).href;
176
+ // Trebol's own code: its errors point at the program's call (see LIBRARY_FILES).
177
+ LIBRARY_FILES.add('host').add(UI_SOURCE).add(I18N_SOURCE);
178
+ /** Loads languages and the UI dialect (view, layout, stylize, theme...) into the user context. */
179
+ export function startUi(host) {
180
+ return runSource(host, `import ${I18N_SOURCE} import ${UI_SOURCE}`);
181
+ }
182
+ /**
183
+ * Loads source into the user context and runs it as a task. Resolves with
184
+ * its value; rejects with the error! if it fails. Load errors throw LoadError.
185
+ */
186
+ export function runSource(host, src, file) {
187
+ const code = load(src, file !== undefined ? { file } : {});
188
+ bindSeries(code.s, host.rt.user, true);
189
+ return taskResult(spawn(host.sch, code.s, scriptStart(code).start));
190
+ }
@@ -0,0 +1,40 @@
1
+ import { type Cell, type MapCell, type Runtime } from '../core/index.ts';
2
+ /** The kinds `localize` formats; anything else shows as `form` gives it. */
3
+ export declare const LOCAL_KINDS: ReadonlySet<number>;
4
+ /** A value in the format of a language tag (`es`, `en-GB`); null when it has no local format. */
5
+ export declare function formatLocal(c: Cell, lang: string): string | null;
6
+ /** An amount as it is edited in a language: no thousands, the local decimal separator (`1234,50`). */
7
+ export declare function editLocal(c: Cell, lang: string): string;
8
+ /**
9
+ * An amount typed in a language as Trebol reads it (`1234.5`), or null when it is not one.
10
+ * Currency signs and spaces are dropped. Thousands only count in groups of three; a single
11
+ * other separator is the decimal one, so `12.5` is twelve and a half in `es` too.
12
+ */
13
+ export declare function readLocal(s: string, lang: string): string | null;
14
+ /**
15
+ * A date typed in a language as Trebol reads it (`2026-11-03`), or null when it is not one.
16
+ * Day, month and year go in the order of the language with any separator; `2026-11-03`
17
+ * reads in every language, and a two-digit year is in this century.
18
+ */
19
+ export declare function readDate(s: string, lang: string): string | null;
20
+ /** The first day of the week in a language, 1 for Monday to 7 for Sunday. */
21
+ export declare function weekStart(lang: string): number;
22
+ /** The active language: `i18n/language` of the user context, `en` if there is none. */
23
+ export declare function activeLanguage(rt: Runtime): string;
24
+ /** The value under a key of a map (a word or a string key, any case), or null. */
25
+ export declare function mapAt(m: MapCell, key: string): Cell | null;
26
+ /**
27
+ * A text of the interface in the active language, as `tr` gives it: a string
28
+ * as is, a text per language (a map) in the active language, then the main
29
+ * one, then any, and a word through the dictionary `i18n/texts`.
30
+ */
31
+ export declare function translate(c: Cell, rt: Runtime): string;
32
+ /**
33
+ * One of Trebol's own texts (the map `i18n/ui`) in the active language, with
34
+ * `#1`, `#2`... replaced by the arguments. A key that is missing shows as is.
35
+ */
36
+ export declare function uiText(rt: Runtime, key: string, ...args: string[]): string;
37
+ /** A value as it shows on screen in a language: local format, or `form`. */
38
+ export declare function shown(c: Cell, lang: string): string;
39
+ /** A size in bytes as it reads in a language: 89 bytes, 12 kB, 3.4 MB. */
40
+ export declare function fileSize(bytes: number, lang: string): string;
@@ -0,0 +1,241 @@
1
+ // Values in the format of a language: dates, times, numbers, percents and
2
+ // money, through Intl. It works wherever Intl does (browser or Node); the
3
+ // core keeps its own language-neutral form/mold.
4
+ import { K, native, value, string, form, text, formatDec, spell, ctxFind, intern, } from "../core/index.js";
5
+ /** The kinds `localize` formats; anything else shows as `form` gives it. */
6
+ export const LOCAL_KINDS = new Set([K.Date, K.Time, K.Integer, K.Decimal, K.Percent, K.Money]);
7
+ const NS_PER_MS = 1e6;
8
+ const DAY_NS = 24 * 3600 * 1e9;
9
+ function safe(fn) {
10
+ try {
11
+ return fn();
12
+ }
13
+ catch {
14
+ return null;
15
+ }
16
+ }
17
+ /** A value in the format of a language tag (`es`, `en-GB`); null when it has no local format. */
18
+ export function formatLocal(c, lang) {
19
+ switch (c.t) {
20
+ case K.Integer:
21
+ return safe(() => new Intl.NumberFormat(lang).format(c.v));
22
+ case K.Decimal:
23
+ return safe(() => new Intl.NumberFormat(lang, { maximumSignificantDigits: 15 }).format(c.v));
24
+ case K.Percent:
25
+ return safe(() => new Intl.NumberFormat(lang, { style: 'percent', maximumFractionDigits: 2 }).format(c.v));
26
+ case K.Money: {
27
+ // a decimal string keeps every digit; Intl never goes through a float
28
+ const amount = formatDec(c.v);
29
+ const frac = { minimumFractionDigits: 2, maximumFractionDigits: Math.max(2, c.v.e) };
30
+ return safe(() => new Intl.NumberFormat(lang, c.cur !== ''
31
+ ? { style: 'currency', currency: c.cur, ...frac }
32
+ : frac).format(amount));
33
+ }
34
+ case K.Date: {
35
+ const d = c.v;
36
+ const ms = Date.UTC(d.y, d.m - 1, d.d) + (d.time ?? 0) / NS_PER_MS;
37
+ const opts = { year: 'numeric', month: 'numeric', day: 'numeric', timeZone: 'UTC' };
38
+ if (d.time !== null)
39
+ Object.assign(opts, { hour: 'numeric', minute: '2-digit' });
40
+ // years before 100 would be read as 19xx
41
+ if (d.y < 100)
42
+ return null;
43
+ return safe(() => new Intl.DateTimeFormat(lang, opts).format(ms));
44
+ }
45
+ case K.Time: {
46
+ if (c.v < 0 || c.v >= DAY_NS)
47
+ return null;
48
+ const opts = { hour: 'numeric', minute: '2-digit', timeZone: 'UTC' };
49
+ if (c.v % 6e10 !== 0)
50
+ opts.second = '2-digit';
51
+ return safe(() => new Intl.DateTimeFormat(lang, opts).format(c.v / NS_PER_MS));
52
+ }
53
+ }
54
+ return null;
55
+ }
56
+ /** The decimal and group separators of a language: `,` and `.` in `es`. */
57
+ function separators(lang) {
58
+ let parts = [];
59
+ try {
60
+ parts = new Intl.NumberFormat(lang).formatToParts(12345.6);
61
+ }
62
+ catch { /* unknown language tag */ }
63
+ const find = (type) => parts.find(p => p.type === type)?.value;
64
+ return { decimal: find('decimal') ?? '.', group: find('group') ?? ',' };
65
+ }
66
+ /** An amount as it is edited in a language: no thousands, the local decimal separator (`1234,50`). */
67
+ export function editLocal(c, lang) {
68
+ if (c.t !== K.Money)
69
+ return c.t === K.None ? '' : form(c);
70
+ const amount = formatDec(c.v);
71
+ const opts = { useGrouping: false, minimumFractionDigits: 2, maximumFractionDigits: Math.max(2, c.v.e) };
72
+ return safe(() => new Intl.NumberFormat(lang, opts).format(amount)) ?? formatDec(c.v);
73
+ }
74
+ /**
75
+ * An amount typed in a language as Trebol reads it (`1234.5`), or null when it is not one.
76
+ * Currency signs and spaces are dropped. Thousands only count in groups of three; a single
77
+ * other separator is the decimal one, so `12.5` is twelve and a half in `es` too.
78
+ */
79
+ export function readLocal(s, lang) {
80
+ const { decimal, group } = separators(lang);
81
+ const sep = (ch) => ch === decimal || ch === group || ch === '.' || ch === ',';
82
+ let t = [...s].filter(ch => /[0-9+-]/.test(ch) || sep(ch)).join('');
83
+ if (t.includes(decimal)) {
84
+ const [whole, ...rest] = t.split(decimal);
85
+ if (rest.length > 1)
86
+ return null;
87
+ t = `${grouped(whole, group)}.${rest[0]}`;
88
+ }
89
+ else if (/^[+-]?\d{1,3}([.,]\d{3})+$/.test(t) && [...t].filter(sep).every(ch => ch === group)) {
90
+ t = grouped(t, group);
91
+ }
92
+ else if ([...t].filter(sep).length === 1) {
93
+ t = t.replace(/[.,]/, '.');
94
+ }
95
+ return /^[+-]?(\d+\.?\d*|\.\d+)$/.test(t) ? t : null;
96
+ }
97
+ /** The whole part of an amount without its thousands separators. */
98
+ function grouped(whole, group) {
99
+ return whole.split(group).join('');
100
+ }
101
+ /** The order of day, month and year in the dates of a language: `dmy` in `es`, `mdy` in `en`. */
102
+ function dateOrder(lang) {
103
+ let parts = [];
104
+ try {
105
+ parts = new Intl.DateTimeFormat(lang, { timeZone: 'UTC' }).formatToParts(Date.UTC(2026, 10, 23));
106
+ }
107
+ catch { /* unknown language tag */ }
108
+ const order = parts.map(p => p.type[0]).filter(c => c === 'd' || c === 'm' || c === 'y').join('');
109
+ return order.length === 3 ? order : 'dmy';
110
+ }
111
+ /**
112
+ * A date typed in a language as Trebol reads it (`2026-11-03`), or null when it is not one.
113
+ * Day, month and year go in the order of the language with any separator; `2026-11-03`
114
+ * reads in every language, and a two-digit year is in this century.
115
+ */
116
+ export function readDate(s, lang) {
117
+ const nums = s.trim().split(/[^0-9]+/).filter(n => n !== '');
118
+ if (nums.length !== 3)
119
+ return null;
120
+ const order = nums[0].length === 4 ? 'ymd' : dateOrder(lang);
121
+ const at = (c) => Number(nums[order.indexOf(c)]);
122
+ let y = at('y');
123
+ const m = at('m'), d = at('d');
124
+ if (nums[order.indexOf('y')].length <= 2)
125
+ y += 2000;
126
+ const t = new Date(Date.UTC(y, m - 1, d));
127
+ if (y < 100 || t.getUTCFullYear() !== y || t.getUTCMonth() !== m - 1 || t.getUTCDate() !== d)
128
+ return null;
129
+ return `${y}-${String(m).padStart(2, '0')}-${String(d).padStart(2, '0')}`;
130
+ }
131
+ /** The first day of the week in a language, 1 for Monday to 7 for Sunday. */
132
+ export function weekStart(lang) {
133
+ try {
134
+ const loc = new Intl.Locale(lang);
135
+ const first = (loc.getWeekInfo?.() ?? loc.weekInfo)?.firstDay;
136
+ if (first !== undefined)
137
+ return first;
138
+ // without week data, only regions known to start on Sunday
139
+ return ['US', 'CA', 'MX', 'BR', 'JP', 'IL', 'PH'].includes(loc.maximize().region ?? '') ? 7 : 1;
140
+ }
141
+ catch {
142
+ return 1;
143
+ }
144
+ }
145
+ /** The active language: `i18n/language` of the user context, `en` if there is none. */
146
+ export function activeLanguage(rt) {
147
+ const i = ctxFind(rt.user, intern('i18n'));
148
+ const obj = i < 0 ? null : rt.user.vals[i];
149
+ if (obj === null || obj.t !== K.Object)
150
+ return 'en';
151
+ const o = obj;
152
+ const j = ctxFind(o.ctx, intern('language'));
153
+ const lang = j < 0 ? null : o.ctx.vals[j];
154
+ if (lang?.t === K.Word || lang?.t === K.LitWord)
155
+ return spell(lang.sym);
156
+ if (lang?.t === K.String)
157
+ return text(lang);
158
+ return 'en';
159
+ }
160
+ /** A field of the user's `i18n` object, or null. */
161
+ function i18nField(rt, name) {
162
+ const i = ctxFind(rt.user, intern('i18n'));
163
+ const obj = i < 0 ? null : rt.user.vals[i];
164
+ if (obj === null || obj.t !== K.Object)
165
+ return null;
166
+ const j = ctxFind(obj.ctx, intern(name));
167
+ return j < 0 ? null : obj.ctx.vals[j];
168
+ }
169
+ /** The value under a key of a map (a word or a string key, any case), or null. */
170
+ export function mapAt(m, key) {
171
+ const d = m.s.data;
172
+ const k = key.toLowerCase();
173
+ for (let i = 0; i + 1 < d.length; i += 2) {
174
+ const c = d[i];
175
+ const name = c.t === K.String ? text(c) : 'sym' in c ? spell(c.sym) : null;
176
+ if (name !== null && name.toLowerCase() === k)
177
+ return d[i + 1];
178
+ }
179
+ return null;
180
+ }
181
+ /**
182
+ * A text of the interface in the active language, as `tr` gives it: a string
183
+ * as is, a text per language (a map) in the active language, then the main
184
+ * one, then any, and a word through the dictionary `i18n/texts`.
185
+ */
186
+ export function translate(c, rt) {
187
+ if (c.t === K.String)
188
+ return text(c);
189
+ if (c.t === K.Map) {
190
+ const main = i18nField(rt, 'main');
191
+ const t = mapAt(c, activeLanguage(rt))
192
+ ?? (main !== null && 'sym' in main ? mapAt(c, spell(main.sym)) : null)
193
+ ?? c.s.data.find((v, i) => i % 2 === 1 && v.t === K.String)
194
+ ?? null;
195
+ return t === null ? '' : translate(t, rt);
196
+ }
197
+ if (c.t === K.Word || c.t === K.LitWord) {
198
+ const texts = i18nField(rt, 'texts');
199
+ const t = texts?.t === K.Map ? mapAt(texts, spell(c.sym)) : null;
200
+ return t === null ? spell(c.sym) : translate(t, rt);
201
+ }
202
+ return form(c);
203
+ }
204
+ /**
205
+ * One of Trebol's own texts (the map `i18n/ui`) in the active language, with
206
+ * `#1`, `#2`... replaced by the arguments. A key that is missing shows as is.
207
+ */
208
+ export function uiText(rt, key, ...args) {
209
+ const ui = i18nField(rt, 'ui');
210
+ const t = ui?.t === K.Map ? mapAt(ui, key) : null;
211
+ let out = t === null ? key : translate(t, rt);
212
+ args.forEach((a, i) => { out = out.replaceAll(`#${i + 1}`, a); });
213
+ return out;
214
+ }
215
+ /** A value as it shows on screen in a language: local format, or `form`. */
216
+ export function shown(c, lang) {
217
+ return formatLocal(c, lang) ?? form(c);
218
+ }
219
+ /** A size in bytes as it reads in a language: 89 bytes, 12 kB, 3.4 MB. */
220
+ export function fileSize(bytes, lang) {
221
+ const units = ['byte', 'kilobyte', 'megabyte', 'gigabyte'];
222
+ let n = bytes;
223
+ let u = 0;
224
+ while (n >= 1000 && u < units.length - 1) {
225
+ n /= 1000;
226
+ u++;
227
+ }
228
+ try {
229
+ // short bytes read "89 byte" in English: whole words for bytes, symbols above
230
+ const display = u === 0 ? 'long' : 'short';
231
+ return new Intl.NumberFormat(lang, { style: 'unit', unit: units[u], unitDisplay: display, maximumFractionDigits: u === 0 ? 0 : 1 }).format(n);
232
+ }
233
+ catch {
234
+ return `${bytes} B`;
235
+ }
236
+ }
237
+ native('format-local', '["The value as text in the way of a language: dates, numbers, money." value [any-type!] language [word! string!]]', (_t, f) => {
238
+ const l = f.args[1];
239
+ const lang = l.t === K.String ? text(l) : spell(l.sym);
240
+ return value(string(shown(f.args[0], lang)));
241
+ });
@@ -0,0 +1,28 @@
1
+ export type Inline = {
2
+ k: 'text';
3
+ text: string;
4
+ } | {
5
+ k: 'br';
6
+ } | {
7
+ k: 'b' | 'i';
8
+ kids: Inline[];
9
+ } | {
10
+ k: 'a';
11
+ href: string;
12
+ kids: Inline[];
13
+ };
14
+ export type Block = {
15
+ k: 'p';
16
+ kids: Inline[];
17
+ } | {
18
+ k: 'h';
19
+ level: number;
20
+ kids: Inline[];
21
+ } | {
22
+ k: 'ul' | 'ol';
23
+ items: Inline[][];
24
+ };
25
+ export declare function parseMd(src: string): Block[];
26
+ export declare function printMd(blocks: Block[]): string;
27
+ /** Only web and mail links open; anything else (javascript:...) is shown as plain text. */
28
+ export declare function safeHref(href: string): boolean;