@linxiraos/pi-tui 1.4.13 → 1.5.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.
- package/CHANGELOG.md +33 -34
- package/dist/types/chat/messages.d.ts +2 -0
- package/dist/types/chat/spotlight.d.ts +8 -3
- package/dist/types/chat/transcript-render-helpers.d.ts +5 -0
- package/dist/types/components/editor.d.ts +4 -1
- package/dist/types/html-form/data.d.ts +56 -0
- package/dist/types/html-form/dialog.d.ts +51 -0
- package/dist/types/html-form/media.d.ts +28 -0
- package/dist/types/html-form/paginate.d.ts +43 -0
- package/dist/types/html-form/parse.d.ts +99 -0
- package/dist/types/overlays/ask-dialog.d.ts +11 -6
- package/dist/types/overlays/copy-targets.d.ts +8 -6
- package/dist/types/overlays/directory-picker.d.ts +62 -0
- package/dist/types/overlays/hook-editor.d.ts +3 -1
- package/dist/types/overlays/hub-frame.d.ts +2 -0
- package/dist/types/overlays/model-hub.d.ts +2 -0
- package/dist/types/overlays/team-hub.d.ts +150 -0
- package/dist/types/render/row-moves.d.ts +41 -0
- package/dist/types/status-line/component.d.ts +5 -0
- package/dist/types/status-line/host.d.ts +2 -2
- package/dist/types/status-line/schema.d.ts +1 -1
- package/dist/types/status-line/types.d.ts +8 -0
- package/dist/types/tools/ask.d.ts +11 -49
- package/dist/types/tools/autoresearch.d.ts +2 -2
- package/dist/types/tools/eval.d.ts +4 -0
- package/dist/types/tools/grouped-file-output.d.ts +2 -0
- package/dist/types/tools/shell-viz.d.ts +6 -2
- package/dist/types/tools/xd-url.d.ts +6 -0
- package/dist/types/windows-input-mode.d.ts +8 -0
- package/package.json +9 -9
- package/src/charts/table-data.ts +18 -5
- package/src/chat/chat-transcript-builder.ts +27 -10
- package/src/chat/image-loading.ts +55 -0
- package/src/chat/messages.d.ts +2 -0
- package/src/chat/messages.ts +3 -0
- package/src/chat/spotlight.d.ts +8 -3
- package/src/chat/spotlight.ts +23 -11
- package/src/chat/tool-execution.ts +5 -0
- package/src/chat/transcript-render-helpers.d.ts +5 -0
- package/src/chat/transcript-render-helpers.ts +13 -0
- package/src/chrome/transcript-container.ts +1 -1
- package/src/components/editor.d.ts +4 -1
- package/src/components/editor.ts +3 -2
- package/src/components/markdown.ts +70 -12
- package/src/html-form/data.d.ts +56 -0
- package/src/html-form/data.ts +177 -0
- package/src/html-form/dialog.ts +1450 -0
- package/src/html-form/media.ts +105 -0
- package/src/html-form/paginate.ts +99 -0
- package/src/html-form/parse.d.ts +99 -0
- package/src/html-form/parse.ts +568 -0
- package/src/lang-from-path.ts +1 -0
- package/src/latex-block.ts +3 -3
- package/src/latex-to-unicode.ts +18 -5
- package/src/overlays/ask-dialog.ts +69 -52
- package/src/overlays/copy-targets.d.ts +8 -6
- package/src/overlays/copy-targets.ts +25 -46
- package/src/overlays/directory-picker.ts +387 -0
- package/src/overlays/hook-editor.ts +16 -15
- package/src/overlays/hub-frame.ts +8 -1
- package/src/overlays/model-hub.ts +12 -2
- package/src/overlays/spotlight-sheet.ts +18 -18
- package/src/overlays/team-hub.ts +901 -0
- package/src/render/row-moves.d.ts +41 -0
- package/src/render/row-moves.ts +109 -0
- package/src/setup/wizard-overlay.ts +7 -3
- package/src/status-line/component.ts +19 -0
- package/src/status-line/footer.ts +2 -2
- package/src/status-line/host.ts +2 -2
- package/src/status-line/presets.ts +6 -6
- package/src/status-line/schema.ts +2 -1
- package/src/status-line/segments.ts +31 -4
- package/src/status-line/startup.ts +1 -1
- package/src/status-line/types.ts +8 -0
- package/src/stdin-buffer.ts +15 -1
- package/src/terminal-capabilities.ts +12 -3
- package/src/terminal.ts +41 -3
- package/src/tools/ask.d.ts +11 -49
- package/src/tools/ask.ts +201 -606
- package/src/tools/autoresearch.ts +2 -2
- package/src/tools/debug.ts +31 -38
- package/src/tools/eval.d.ts +4 -0
- package/src/tools/eval.ts +4 -0
- package/src/tools/grep.ts +38 -16
- package/src/tools/grouped-file-output.d.ts +2 -0
- package/src/tools/grouped-file-output.ts +5 -0
- package/src/tools/shell-viz-render.ts +37 -6
- package/src/tools/shell-viz.d.ts +6 -2
- package/src/tools/shell-viz.ts +19 -6
- package/src/tools/xd-url.d.ts +6 -0
- package/src/tools/xd-url.ts +19 -1
- package/src/tui.ts +129 -1
- package/src/windows-input-mode.d.ts +8 -0
- package/src/windows-input-mode.ts +13 -0
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reduces an agent-written HTML `<form>` to what a terminal form draws.
|
|
3
|
+
*
|
|
4
|
+
* The model is deliberately small: titled sections (one per top-level
|
|
5
|
+
* `<fieldset>`, plus untitled runs of loose content between them), context
|
|
6
|
+
* blocks (paragraphs, headings, lists, preformatted text, tables) and fields.
|
|
7
|
+
* Radios and checkboxes join one {@link ChoiceField} per `type` + `name`
|
|
8
|
+
* wherever they appear, so a group split across table cells or list items is
|
|
9
|
+
* still one question. Layout-only markup (`<br>` between labels, wrapper
|
|
10
|
+
* `<div>`s, `style`, buttons) is dropped: the renderer owns layout, and the
|
|
11
|
+
* UI owns Submit/Cancel.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const form = parseHtmlForm(`<form><fieldset><legend>Database</legend>
|
|
15
|
+
* <label><input type="radio" name="db" value="sqlite" checked> SQLite</label>
|
|
16
|
+
* <label><input type="radio" name="db" value="pg"> Postgres</label>
|
|
17
|
+
* </fieldset></form>`);
|
|
18
|
+
* form.sections[0].title; // [{ text: "Database" }]
|
|
19
|
+
* form.fields[0]; // { kind: "choice", name: "db", multi: false, options: [sqlite ✓, pg] }
|
|
20
|
+
*/
|
|
21
|
+
import { Comment, type Document, Element, type Node, parseHTML, Text } from "@linxiraos/pi-utils/dom";
|
|
22
|
+
|
|
23
|
+
/** Inline emphasis a span keeps from `<code>`, `<strong>`/`<b>` and `<em>`/`<i>`. */
|
|
24
|
+
export type FormSpanStyle = "code" | "strong" | "em";
|
|
25
|
+
|
|
26
|
+
/** A run of inline text; `"\n"` inside `text` is a hard line break (`<br>`). */
|
|
27
|
+
export interface FormSpan {
|
|
28
|
+
text: string;
|
|
29
|
+
style?: FormSpanStyle;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One answer of a {@link ChoiceField}. */
|
|
33
|
+
export interface ChoiceOption {
|
|
34
|
+
value: string;
|
|
35
|
+
label: FormSpan[];
|
|
36
|
+
checked: boolean;
|
|
37
|
+
disabled: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A radio group (`multi: false`), checkbox group (`multi: true`) or `<select>` (`select: true`). */
|
|
41
|
+
export interface ChoiceField {
|
|
42
|
+
kind: "choice";
|
|
43
|
+
name: string;
|
|
44
|
+
multi: boolean;
|
|
45
|
+
/** From a `<select>`: a single select draws as a one-row dropdown instead of option rows. */
|
|
46
|
+
select: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The UI adds a free-text "Other" answer: set for radio and checkbox groups of
|
|
49
|
+
* two or more options that don't already offer one of their own.
|
|
50
|
+
*/
|
|
51
|
+
other: boolean;
|
|
52
|
+
/** The control's own label; empty for radio/checkbox groups, whose question is the surrounding text. */
|
|
53
|
+
label: FormSpan[];
|
|
54
|
+
options: ChoiceOption[];
|
|
55
|
+
required: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A single-line `<input>` (`multiline: false`) or a `<textarea>`. */
|
|
59
|
+
export interface TextField {
|
|
60
|
+
kind: "text";
|
|
61
|
+
name: string;
|
|
62
|
+
/** The `<input type>` (`text`, `number`, `url`, …); `textarea` for textareas. */
|
|
63
|
+
type: string;
|
|
64
|
+
multiline: boolean;
|
|
65
|
+
label: FormSpan[];
|
|
66
|
+
value: string;
|
|
67
|
+
placeholder: string;
|
|
68
|
+
required: boolean;
|
|
69
|
+
min?: number;
|
|
70
|
+
max?: number;
|
|
71
|
+
pattern?: string;
|
|
72
|
+
minLength?: number;
|
|
73
|
+
maxLength?: number;
|
|
74
|
+
/** Visible rows a textarea asked for (`rows`), 1 for inputs. */
|
|
75
|
+
rows: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Any control the user answers. */
|
|
79
|
+
export type FormField = ChoiceField | TextField;
|
|
80
|
+
|
|
81
|
+
/** A renderable unit of a section, in document order. */
|
|
82
|
+
export type FormBlock =
|
|
83
|
+
| { kind: "text"; spans: FormSpan[]; heading: boolean }
|
|
84
|
+
| { kind: "list"; items: FormSpan[][]; ordered: boolean }
|
|
85
|
+
| { kind: "pre"; text: string }
|
|
86
|
+
| { kind: "table"; rows: FormSpan[][][]; headerRows: number }
|
|
87
|
+
/** An `<img>`: `src` is an http(s) or `data:` URL, a `file:` URL or a local path (see `media.ts`). */
|
|
88
|
+
| { kind: "image"; src: string; alt: string }
|
|
89
|
+
/** An inline `<svg>`, serialized whole. */
|
|
90
|
+
| { kind: "svg"; markup: string; alt: string }
|
|
91
|
+
| { kind: "field"; field: FormField };
|
|
92
|
+
|
|
93
|
+
/** A `<fieldset>` (titled by its `<legend>`) or loose content between fieldsets (untitled). */
|
|
94
|
+
export interface FormSection {
|
|
95
|
+
title: FormSpan[];
|
|
96
|
+
blocks: FormBlock[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A parsed form: what to draw, the fields in document order, and `<input type="hidden">` pairs. */
|
|
100
|
+
export interface HtmlForm {
|
|
101
|
+
sections: FormSection[];
|
|
102
|
+
fields: FormField[];
|
|
103
|
+
hidden: [name: string, value: string][];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const SKIPPED: Record<string, true> = {
|
|
107
|
+
button: true,
|
|
108
|
+
head: true,
|
|
109
|
+
iframe: true,
|
|
110
|
+
link: true,
|
|
111
|
+
meta: true,
|
|
112
|
+
noscript: true,
|
|
113
|
+
script: true,
|
|
114
|
+
style: true,
|
|
115
|
+
template: true,
|
|
116
|
+
title: true,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const BLOCK: Record<string, true> = {
|
|
120
|
+
address: true,
|
|
121
|
+
article: true,
|
|
122
|
+
aside: true,
|
|
123
|
+
blockquote: true,
|
|
124
|
+
dd: true,
|
|
125
|
+
details: true,
|
|
126
|
+
div: true,
|
|
127
|
+
dl: true,
|
|
128
|
+
dt: true,
|
|
129
|
+
figure: true,
|
|
130
|
+
figcaption: true,
|
|
131
|
+
footer: true,
|
|
132
|
+
header: true,
|
|
133
|
+
li: true,
|
|
134
|
+
main: true,
|
|
135
|
+
nav: true,
|
|
136
|
+
p: true,
|
|
137
|
+
section: true,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const HEADING: Record<string, true> = { h1: true, h2: true, h3: true, h4: true, h5: true, h6: true, summary: true };
|
|
141
|
+
|
|
142
|
+
const STYLE_OF: Record<string, FormSpanStyle> = {
|
|
143
|
+
b: "strong",
|
|
144
|
+
code: "code",
|
|
145
|
+
em: "em",
|
|
146
|
+
i: "em",
|
|
147
|
+
kbd: "code",
|
|
148
|
+
samp: "code",
|
|
149
|
+
strong: "strong",
|
|
150
|
+
tt: "code",
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const IGNORED_INPUTS: Record<string, true> = { button: true, file: true, image: true, reset: true, submit: true };
|
|
154
|
+
|
|
155
|
+
const CONTROL_SELECTOR = "input, select, textarea";
|
|
156
|
+
|
|
157
|
+
/** Parse agent-written form HTML; content outside the first `<form>` is ignored when one exists. */
|
|
158
|
+
export function parseHtmlForm(html: string): HtmlForm {
|
|
159
|
+
const document = parseHTML(html).document;
|
|
160
|
+
return new FormBuilder(document.querySelector("form") ?? document).build();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Append `text` to `spans`, merging into the previous span when the style matches. */
|
|
164
|
+
function appendSpan(spans: FormSpan[], text: string, style: FormSpanStyle | undefined): void {
|
|
165
|
+
if (!text) return;
|
|
166
|
+
const last = spans[spans.length - 1];
|
|
167
|
+
if (last && last.style === style) last.text += text;
|
|
168
|
+
else spans.push(style ? { text, style } : { text });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Collapse whitespace like HTML layout does: runs become one space, spaces
|
|
173
|
+
* next to a hard break vanish, and the ends are trimmed. Empty spans drop.
|
|
174
|
+
*/
|
|
175
|
+
function normalizeSpans(spans: FormSpan[]): FormSpan[] {
|
|
176
|
+
const out: FormSpan[] = [];
|
|
177
|
+
for (const span of spans) {
|
|
178
|
+
const text = span.text.replace(/[^\S\n]+/g, " ").replace(/ ?\n ?/g, "\n");
|
|
179
|
+
appendSpan(out, text, span.style);
|
|
180
|
+
}
|
|
181
|
+
const first = out[0];
|
|
182
|
+
if (first) first.text = first.text.replace(/^\s+/, "");
|
|
183
|
+
const last = out[out.length - 1];
|
|
184
|
+
if (last) last.text = last.text.replace(/\s+$/, "");
|
|
185
|
+
return out.filter(span => span.text !== "");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Plain text of spans, for labels that fall back to a single string. */
|
|
189
|
+
export function spansText(spans: readonly FormSpan[]): string {
|
|
190
|
+
return spans.map(span => span.text).join("");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** An inline svg's accessible name: its `aria-label`, else its `<title>`. */
|
|
194
|
+
function svgTitle(svg: Element): string {
|
|
195
|
+
const label = svg.getAttribute("aria-label");
|
|
196
|
+
if (label) return label.trim();
|
|
197
|
+
const title = svg.children.find(child => child.localName === "title");
|
|
198
|
+
return (title?.textContent ?? "").replace(/\s+/g, " ").trim();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function numberAttr(element: Element, name: string): number | undefined {
|
|
202
|
+
const raw = element.getAttribute(name);
|
|
203
|
+
if (raw === null || raw.trim() === "") return undefined;
|
|
204
|
+
const value = Number(raw);
|
|
205
|
+
return Number.isFinite(value) ? value : undefined;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
class FormBuilder {
|
|
209
|
+
readonly #root: Document | Element;
|
|
210
|
+
readonly #sections: FormSection[] = [];
|
|
211
|
+
readonly #fields: FormField[] = [];
|
|
212
|
+
readonly #hidden: [string, string][] = [];
|
|
213
|
+
/** Radio/checkbox groups by `type\0name`. */
|
|
214
|
+
readonly #groups = new Map<string, ChoiceField>();
|
|
215
|
+
/** `<label for>` elements by the id of the control they name. */
|
|
216
|
+
readonly #labelFor = new Map<string, Element>();
|
|
217
|
+
#section: FormSection = { title: [], blocks: [] };
|
|
218
|
+
#inline: FormSpan[] = [];
|
|
219
|
+
#fieldsetDepth = 0;
|
|
220
|
+
|
|
221
|
+
constructor(root: Document | Element) {
|
|
222
|
+
this.#root = root;
|
|
223
|
+
this.#sections.push(this.#section);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
build(): HtmlForm {
|
|
227
|
+
const ids = new Set<string>();
|
|
228
|
+
for (const control of this.#root.querySelectorAll(CONTROL_SELECTOR)) {
|
|
229
|
+
if (control.id) ids.add(control.id);
|
|
230
|
+
}
|
|
231
|
+
for (const label of this.#root.querySelectorAll("label")) {
|
|
232
|
+
const target = label.getAttribute("for");
|
|
233
|
+
if (target && ids.has(target) && !this.#labelFor.has(target)) this.#labelFor.set(target, label);
|
|
234
|
+
}
|
|
235
|
+
this.#children(this.#root, undefined);
|
|
236
|
+
this.#flush(false);
|
|
237
|
+
for (const field of this.#fields) {
|
|
238
|
+
if (field.kind !== "choice" || field.select || field.options.length < 2) continue;
|
|
239
|
+
field.other = !field.options.some(option => /^\s*other\b/i.test(spansText(option.label)));
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
sections: this.#sections.filter(section => section.blocks.length > 0 || section.title.length > 0),
|
|
243
|
+
fields: this.#fields,
|
|
244
|
+
hidden: this.#hidden,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
#children(node: Node, style: FormSpanStyle | undefined): void {
|
|
249
|
+
for (const child of node.childNodes) this.#walk(child, style);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
#walk(node: Node, style: FormSpanStyle | undefined): void {
|
|
253
|
+
if (node instanceof Comment) return;
|
|
254
|
+
if (node instanceof Text) {
|
|
255
|
+
appendSpan(this.#inline, node.data, style);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (!(node instanceof Element)) return;
|
|
259
|
+
const tag = node.localName;
|
|
260
|
+
if (SKIPPED[tag]) return;
|
|
261
|
+
switch (tag) {
|
|
262
|
+
case "br":
|
|
263
|
+
appendSpan(this.#inline, "\n", undefined);
|
|
264
|
+
return;
|
|
265
|
+
case "hr":
|
|
266
|
+
this.#flush(false);
|
|
267
|
+
return;
|
|
268
|
+
case "input":
|
|
269
|
+
case "select":
|
|
270
|
+
case "textarea":
|
|
271
|
+
this.#control(node, undefined);
|
|
272
|
+
return;
|
|
273
|
+
case "label":
|
|
274
|
+
this.#label(node, style);
|
|
275
|
+
return;
|
|
276
|
+
case "fieldset":
|
|
277
|
+
this.#fieldset(node);
|
|
278
|
+
return;
|
|
279
|
+
case "legend":
|
|
280
|
+
this.#flush(false);
|
|
281
|
+
this.#children(node, style);
|
|
282
|
+
this.#flush(true);
|
|
283
|
+
return;
|
|
284
|
+
case "ul":
|
|
285
|
+
case "ol":
|
|
286
|
+
this.#list(node, tag === "ol");
|
|
287
|
+
return;
|
|
288
|
+
case "pre":
|
|
289
|
+
this.#flush(false);
|
|
290
|
+
this.#section.blocks.push({ kind: "pre", text: (node.textContent ?? "").replace(/^\n/, "").trimEnd() });
|
|
291
|
+
return;
|
|
292
|
+
case "table":
|
|
293
|
+
this.#table(node);
|
|
294
|
+
return;
|
|
295
|
+
case "img": {
|
|
296
|
+
const src = node.getAttribute("src")?.trim();
|
|
297
|
+
if (!src) return;
|
|
298
|
+
this.#flush(false);
|
|
299
|
+
this.#section.blocks.push({ kind: "image", src, alt: (node.getAttribute("alt") ?? "").trim() });
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
case "svg":
|
|
303
|
+
this.#flush(false);
|
|
304
|
+
this.#section.blocks.push({ kind: "svg", markup: node.outerHTML, alt: svgTitle(node) });
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (HEADING[tag]) {
|
|
308
|
+
this.#flush(false);
|
|
309
|
+
this.#children(node, style);
|
|
310
|
+
this.#flush(true);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (BLOCK[tag]) {
|
|
314
|
+
this.#flush(false);
|
|
315
|
+
this.#children(node, style);
|
|
316
|
+
this.#flush(false);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
this.#children(node, STYLE_OF[tag] ?? style);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Emit buffered inline content as a text block. */
|
|
323
|
+
#flush(heading: boolean): void {
|
|
324
|
+
const spans = normalizeSpans(this.#inline);
|
|
325
|
+
this.#inline = [];
|
|
326
|
+
if (spans.length > 0) this.#section.blocks.push({ kind: "text", spans, heading });
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Inline spans of `element`, skipping controls (their label is the text around them). */
|
|
330
|
+
#spansOf(element: Element): FormSpan[] {
|
|
331
|
+
const spans: FormSpan[] = [];
|
|
332
|
+
const visit = (node: Node, style: FormSpanStyle | undefined): void => {
|
|
333
|
+
if (node instanceof Comment) return;
|
|
334
|
+
if (node instanceof Text) {
|
|
335
|
+
appendSpan(spans, node.data, style);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (!(node instanceof Element)) return;
|
|
339
|
+
const tag = node.localName;
|
|
340
|
+
if (SKIPPED[tag] || tag === "input" || tag === "select" || tag === "textarea" || tag === "svg") return;
|
|
341
|
+
if (tag === "br") {
|
|
342
|
+
appendSpan(spans, "\n", undefined);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
// Labels stay text: a picture inside one reads as its alt text.
|
|
346
|
+
if (tag === "img") {
|
|
347
|
+
appendSpan(spans, node.getAttribute("alt") ?? "", style);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (BLOCK[tag] || HEADING[tag]) appendSpan(spans, " ", undefined);
|
|
351
|
+
for (const child of node.childNodes) visit(child, STYLE_OF[tag] ?? style);
|
|
352
|
+
};
|
|
353
|
+
for (const child of element.childNodes) visit(child, undefined);
|
|
354
|
+
return normalizeSpans(spans);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
#label(label: Element, style: FormSpanStyle | undefined): void {
|
|
358
|
+
const target = label.getAttribute("for");
|
|
359
|
+
if (target && this.#labelFor.get(target) === label) return;
|
|
360
|
+
const controls = label.querySelectorAll(CONTROL_SELECTOR);
|
|
361
|
+
if (controls.length === 0) {
|
|
362
|
+
this.#children(label, style);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
this.#flush(false);
|
|
366
|
+
const spans = this.#spansOf(label);
|
|
367
|
+
for (const control of controls) this.#control(control, spans);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
#fieldset(fieldset: Element): void {
|
|
371
|
+
this.#flush(false);
|
|
372
|
+
const legend = fieldset.children.find(child => child.localName === "legend");
|
|
373
|
+
if (this.#fieldsetDepth > 0) {
|
|
374
|
+
// Nested fieldsets stay in the outer section; their legend becomes a heading.
|
|
375
|
+
if (legend) this.#walk(legend, undefined);
|
|
376
|
+
} else {
|
|
377
|
+
this.#startSection(legend ? this.#spansOf(legend) : []);
|
|
378
|
+
}
|
|
379
|
+
this.#fieldsetDepth++;
|
|
380
|
+
for (const child of fieldset.childNodes) {
|
|
381
|
+
if (child !== legend) this.#walk(child, undefined);
|
|
382
|
+
}
|
|
383
|
+
this.#flush(false);
|
|
384
|
+
this.#fieldsetDepth--;
|
|
385
|
+
if (this.#fieldsetDepth === 0) this.#startSection([]);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
#startSection(title: FormSpan[]): void {
|
|
389
|
+
if (this.#section.blocks.length === 0 && this.#section.title.length === 0) {
|
|
390
|
+
this.#section.title = title;
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
this.#section = { title, blocks: [] };
|
|
394
|
+
this.#sections.push(this.#section);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
#list(list: Element, ordered: boolean): void {
|
|
398
|
+
this.#flush(false);
|
|
399
|
+
if (list.querySelector(CONTROL_SELECTOR)) {
|
|
400
|
+
// A list of controls is layout, not content: walk it as flow.
|
|
401
|
+
this.#children(list, undefined);
|
|
402
|
+
this.#flush(false);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const items = list.children
|
|
406
|
+
.filter(child => child.localName === "li")
|
|
407
|
+
.map(item => this.#spansOf(item))
|
|
408
|
+
.filter(spans => spans.length > 0);
|
|
409
|
+
if (items.length > 0) this.#section.blocks.push({ kind: "list", items, ordered });
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
#table(table: Element): void {
|
|
413
|
+
this.#flush(false);
|
|
414
|
+
const rows = table.querySelectorAll("tr").filter(row => row.closest("table") === table);
|
|
415
|
+
const cellsOf = (row: Element) => row.children.filter(cell => cell.localName === "td" || cell.localName === "th");
|
|
416
|
+
if (!table.querySelector(CONTROL_SELECTOR)) {
|
|
417
|
+
const cells = rows.map(row => cellsOf(row).map(cell => this.#spansOf(cell)));
|
|
418
|
+
let headerRows = 0;
|
|
419
|
+
while (
|
|
420
|
+
headerRows < rows.length &&
|
|
421
|
+
(rows[headerRows]!.closest("thead") !== null ||
|
|
422
|
+
cellsOf(rows[headerRows]!).every(cell => cell.localName === "th"))
|
|
423
|
+
) {
|
|
424
|
+
headerRows++;
|
|
425
|
+
}
|
|
426
|
+
if (cells.some(row => row.length > 0)) this.#section.blocks.push({ kind: "table", rows: cells, headerRows });
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
// A table of controls (a decision matrix): each row's text becomes a line and each
|
|
430
|
+
// unlabeled control borrows its column header as its label.
|
|
431
|
+
let header: FormSpan[][] = [];
|
|
432
|
+
for (const row of rows) {
|
|
433
|
+
const cells = cellsOf(row);
|
|
434
|
+
if (cells.every(cell => cell.localName === "th") && !row.querySelector(CONTROL_SELECTOR)) {
|
|
435
|
+
header = cells.map(cell => this.#spansOf(cell));
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
const text: FormSpan[] = [];
|
|
439
|
+
for (const cell of cells) {
|
|
440
|
+
if (cell.querySelector(CONTROL_SELECTOR)) continue;
|
|
441
|
+
const spans = this.#spansOf(cell);
|
|
442
|
+
if (spans.length === 0) continue;
|
|
443
|
+
if (text.length > 0) appendSpan(text, " · ", undefined);
|
|
444
|
+
for (const span of spans) appendSpan(text, span.text, span.style);
|
|
445
|
+
}
|
|
446
|
+
if (text.length > 0) this.#section.blocks.push({ kind: "text", spans: text, heading: false });
|
|
447
|
+
cells.forEach((cell, column) => {
|
|
448
|
+
for (const control of cell.querySelectorAll(CONTROL_SELECTOR)) {
|
|
449
|
+
const own = control.closest("label");
|
|
450
|
+
const spans = own && cell.contains(own) ? this.#spansOf(own) : this.#spansOf(cell);
|
|
451
|
+
this.#control(control, spans.length > 0 ? spans : (header[column] ?? []));
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** Label of a control outside any wrapping `<label>`: `<label for>`, then `aria-label`, `title`. */
|
|
458
|
+
#detachedLabel(control: Element): FormSpan[] {
|
|
459
|
+
const label = control.id ? this.#labelFor.get(control.id) : undefined;
|
|
460
|
+
if (label) return this.#spansOf(label);
|
|
461
|
+
const text = control.getAttribute("aria-label") ?? control.getAttribute("title");
|
|
462
|
+
return text ? normalizeSpans([{ text }]) : [];
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
#control(control: Element, wrapping: FormSpan[] | undefined): void {
|
|
466
|
+
this.#flush(false);
|
|
467
|
+
const label = wrapping && wrapping.length > 0 ? wrapping : this.#detachedLabel(control);
|
|
468
|
+
const name = control.getAttribute("name") ?? "";
|
|
469
|
+
const required = control.hasAttribute("required");
|
|
470
|
+
const tag = control.localName;
|
|
471
|
+
if (tag === "select") {
|
|
472
|
+
const multi = control.hasAttribute("multiple");
|
|
473
|
+
const options: ChoiceOption[] = control.querySelectorAll("option").map(option => {
|
|
474
|
+
const text = (option.textContent ?? "").replace(/\s+/g, " ").trim();
|
|
475
|
+
return {
|
|
476
|
+
value: option.getAttribute("value") ?? text,
|
|
477
|
+
label: [{ text }],
|
|
478
|
+
checked: option.hasAttribute("selected"),
|
|
479
|
+
disabled: option.hasAttribute("disabled"),
|
|
480
|
+
};
|
|
481
|
+
});
|
|
482
|
+
// A single select always has a selection: the first enabled option unless one is marked.
|
|
483
|
+
if (!multi && !options.some(option => option.checked)) {
|
|
484
|
+
const first = options.find(option => !option.disabled);
|
|
485
|
+
if (first) first.checked = true;
|
|
486
|
+
}
|
|
487
|
+
this.#addField({ kind: "choice", name, multi, select: true, other: false, label, options, required });
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (tag === "textarea") {
|
|
491
|
+
this.#addField({
|
|
492
|
+
kind: "text",
|
|
493
|
+
name,
|
|
494
|
+
type: "textarea",
|
|
495
|
+
multiline: true,
|
|
496
|
+
label: label.length > 0 ? label : this.#nameLabel(name),
|
|
497
|
+
value: (control.textContent ?? "").replace(/^\r?\n/, ""),
|
|
498
|
+
placeholder: control.getAttribute("placeholder") ?? "",
|
|
499
|
+
required,
|
|
500
|
+
minLength: numberAttr(control, "minlength"),
|
|
501
|
+
maxLength: numberAttr(control, "maxlength"),
|
|
502
|
+
rows: numberAttr(control, "rows") ?? 4,
|
|
503
|
+
});
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const type = (control.getAttribute("type") ?? "text").toLowerCase();
|
|
507
|
+
if (IGNORED_INPUTS[type]) return;
|
|
508
|
+
const value = control.getAttribute("value");
|
|
509
|
+
if (type === "hidden") {
|
|
510
|
+
if (name) this.#hidden.push([name, value ?? ""]);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
if (type === "radio" || type === "checkbox") {
|
|
514
|
+
const option: ChoiceOption = {
|
|
515
|
+
value: value ?? "on",
|
|
516
|
+
label: label.length > 0 ? label : [{ text: value ?? name }],
|
|
517
|
+
checked: control.hasAttribute("checked"),
|
|
518
|
+
disabled: control.hasAttribute("disabled"),
|
|
519
|
+
};
|
|
520
|
+
const key = `${type}\0${name}`;
|
|
521
|
+
const group = this.#groups.get(key);
|
|
522
|
+
if (group) {
|
|
523
|
+
group.options.push(option);
|
|
524
|
+
group.required ||= required;
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const field: ChoiceField = {
|
|
528
|
+
kind: "choice",
|
|
529
|
+
name,
|
|
530
|
+
multi: type === "checkbox",
|
|
531
|
+
select: false,
|
|
532
|
+
other: false,
|
|
533
|
+
label: [],
|
|
534
|
+
options: [option],
|
|
535
|
+
required,
|
|
536
|
+
};
|
|
537
|
+
this.#groups.set(key, field);
|
|
538
|
+
this.#addField(field);
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
this.#addField({
|
|
542
|
+
kind: "text",
|
|
543
|
+
name,
|
|
544
|
+
type,
|
|
545
|
+
multiline: false,
|
|
546
|
+
label: label.length > 0 ? label : this.#nameLabel(name),
|
|
547
|
+
value: value ?? "",
|
|
548
|
+
placeholder: control.getAttribute("placeholder") ?? "",
|
|
549
|
+
required,
|
|
550
|
+
min: numberAttr(control, "min"),
|
|
551
|
+
max: numberAttr(control, "max"),
|
|
552
|
+
pattern: control.getAttribute("pattern") ?? undefined,
|
|
553
|
+
minLength: numberAttr(control, "minlength"),
|
|
554
|
+
maxLength: numberAttr(control, "maxlength"),
|
|
555
|
+
rows: 1,
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Last-resort label from the control name: `sentry_dsn` → `sentry dsn`. */
|
|
560
|
+
#nameLabel(name: string): FormSpan[] {
|
|
561
|
+
return name ? [{ text: name.replace(/[_-]+/g, " ") }] : [];
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
#addField(field: FormField): void {
|
|
565
|
+
this.#fields.push(field);
|
|
566
|
+
this.#section.blocks.push({ kind: "field", field });
|
|
567
|
+
}
|
|
568
|
+
}
|
package/src/lang-from-path.ts
CHANGED
|
@@ -37,6 +37,7 @@ const EXTENSION_LANG: Record<string, readonly [string, string]> = {
|
|
|
37
37
|
rb: ["ruby", "ruby"],
|
|
38
38
|
rbw: ["ruby", "ruby"],
|
|
39
39
|
gemspec: ["ruby", "ruby"],
|
|
40
|
+
erb: ["erb", "erb"],
|
|
40
41
|
lua: ["lua", "lua"],
|
|
41
42
|
sh: ["bash", "shellscript"],
|
|
42
43
|
bash: ["bash", "shellscript"],
|
package/src/latex-block.ts
CHANGED
|
@@ -372,14 +372,14 @@ function binomBox(top: Box, bottom: Box): Box {
|
|
|
372
372
|
* A drawn radical for a multi-line radicand: overline row on top, bar column
|
|
373
373
|
* on the left, hook at the bottom. Single-line radicands stay flat (`√x̄`).
|
|
374
374
|
*/
|
|
375
|
-
function radicalBox(inner: Box, degree: string | null): Box {
|
|
375
|
+
function radicalBox(inner: Box, degree: string | null, wrap: (run: string) => string): Box {
|
|
376
376
|
const lines: string[] = [` ┌${BAR.repeat(inner.width + 1)}`];
|
|
377
377
|
for (let y = 0; y < inner.lines.length; y++) {
|
|
378
378
|
lines.push((y === inner.lines.length - 1 ? "╲│ " : " │ ") + inner.lines[y]);
|
|
379
379
|
}
|
|
380
380
|
const box: Box = { lines, baseline: inner.baseline + 1, width: inner.width + 3 };
|
|
381
381
|
if (!degree) return box;
|
|
382
|
-
const deg = latexToUnicode(`^{${degree}}`);
|
|
382
|
+
const deg = latexToUnicode(wrap(`^{${degree}}`));
|
|
383
383
|
// Degree sits one row above the baseline, at the radical's upper left.
|
|
384
384
|
return hconcat([{ lines: [deg, spaces(visibleWidth(deg))], baseline: 1, width: visibleWidth(deg) }, box]);
|
|
385
385
|
}
|
|
@@ -1020,7 +1020,7 @@ function parseExpr(src: string, ctx: Ctx = ROOT_CTX): Box {
|
|
|
1020
1020
|
// Display style always draws the roof (like LaTeX); inline math
|
|
1021
1021
|
// keeps the flat `√(…)` form via latexToUnicode.
|
|
1022
1022
|
flush();
|
|
1023
|
-
boxes.push(paint(radicalBox(parseExpr(arg.text, inner()), degree)));
|
|
1023
|
+
boxes.push(paint(radicalBox(parseExpr(arg.text, inner()), degree, ctx.wrap)));
|
|
1024
1024
|
i = arg.end;
|
|
1025
1025
|
continue;
|
|
1026
1026
|
}
|
package/src/latex-to-unicode.ts
CHANGED
|
@@ -371,7 +371,6 @@ export const MATH_FONT_COMMANDS: ReadonlySet<string> = new Set([
|
|
|
371
371
|
const TEXT_COMMANDS: Record<string, true> = {
|
|
372
372
|
text: true,
|
|
373
373
|
textrm: true,
|
|
374
|
-
textnormal: true,
|
|
375
374
|
textsc: true,
|
|
376
375
|
mathrm: true,
|
|
377
376
|
mathnormal: true,
|
|
@@ -1532,6 +1531,12 @@ class LatexParser {
|
|
|
1532
1531
|
return attribute === null ? text : scopeRendered(text, attribute, style[attribute]);
|
|
1533
1532
|
}
|
|
1534
1533
|
|
|
1534
|
+
if (name === "textnormal") {
|
|
1535
|
+
return styledText(
|
|
1536
|
+
unescapeText(this.#rawArgument()),
|
|
1537
|
+
this.#renderStyle({ ...style, bold: false, italic: false }),
|
|
1538
|
+
);
|
|
1539
|
+
}
|
|
1535
1540
|
if (TEXT_COMMANDS[name]) return styledText(unescapeText(this.#rawArgument()), current);
|
|
1536
1541
|
if (name === "operatorname") {
|
|
1537
1542
|
const fn = unescapeText(this.#rawArgument());
|
|
@@ -1893,7 +1898,10 @@ class LatexParser {
|
|
|
1893
1898
|
#optionalArgument(style: ParseStyle): Argument | null {
|
|
1894
1899
|
const source = this.#optionalRawArgument();
|
|
1895
1900
|
if (source === null) return null;
|
|
1896
|
-
|
|
1901
|
+
const parser = new LatexParser(source);
|
|
1902
|
+
parser.#foreground = this.#foreground;
|
|
1903
|
+
parser.#background = this.#background;
|
|
1904
|
+
return { text: parser.parse(style, false), group: true };
|
|
1897
1905
|
}
|
|
1898
1906
|
|
|
1899
1907
|
#optionalRawArgument(): string | null {
|
|
@@ -1933,12 +1941,13 @@ class LatexParser {
|
|
|
1933
1941
|
const index = this.#optionalArgument(style);
|
|
1934
1942
|
if (index !== null) {
|
|
1935
1943
|
const indexText = plainText(index.text);
|
|
1944
|
+
const indexMatchesStyle = index.text.every(chunk => sameRenderStyle(chunk.style, current));
|
|
1936
1945
|
radical =
|
|
1937
|
-
indexText === "2"
|
|
1946
|
+
indexMatchesStyle && indexText === "2"
|
|
1938
1947
|
? styledText("√", current)
|
|
1939
|
-
: indexText === "3"
|
|
1948
|
+
: indexMatchesStyle && indexText === "3"
|
|
1940
1949
|
? styledText("∛", current)
|
|
1941
|
-
: indexText === "4"
|
|
1950
|
+
: indexMatchesStyle && indexText === "4"
|
|
1942
1951
|
? styledText("∜", current)
|
|
1943
1952
|
: concatRendered(toSuperscript(index.text, true, current), styledText("√", current));
|
|
1944
1953
|
}
|
|
@@ -1964,6 +1973,8 @@ class LatexParser {
|
|
|
1964
1973
|
this.#optionalRawArgument();
|
|
1965
1974
|
if (this.#s[this.#i] === "{") this.#rawArgument(); // column count
|
|
1966
1975
|
}
|
|
1976
|
+
const outerForeground = this.#foreground;
|
|
1977
|
+
const outerBackground = this.#background;
|
|
1967
1978
|
const body: Rendered = [];
|
|
1968
1979
|
while (this.#i < this.#s.length) {
|
|
1969
1980
|
if (this.#s.startsWith("\\end", this.#i)) {
|
|
@@ -1973,6 +1984,8 @@ class LatexParser {
|
|
|
1973
1984
|
}
|
|
1974
1985
|
appendRendered(body, this.#node(style));
|
|
1975
1986
|
}
|
|
1987
|
+
this.#foreground = outerForeground;
|
|
1988
|
+
this.#background = outerBackground;
|
|
1976
1989
|
let renderedBody = trimRendered(body);
|
|
1977
1990
|
if (
|
|
1978
1991
|
env === "cases" ||
|