-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefref.ts
615 lines (524 loc) · 15.4 KB
/
defref.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import * as Colors from "https://deno.land/[email protected]/fmt/colors.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import {
Context,
ExpandedMacro,
Expression,
format_location,
Invocation,
new_macro,
style_file,
} from "./tsgen.ts";
import { new_name, PerNameState, resolve_name, try_resolve_name } from "./names.ts";
import { Attributes, dfn, h1, h2, h3, h4, h5, h6 } from "./h.ts";
import {
build_actual_link_url,
get_root_directory,
link_name,
} from "./linkname.ts";
import { html5_dependency_js } from "./html5.ts";
import { out_file_absolute, out_state, write_file_absolute } from "./out.ts";
import { createHash } from "npm:sha256-uint8array";
const global_preview_state_key = Symbol("GlobalPreviews");
interface GlobalPreviewState {
// paths of all output files that contain previews
files: Set<string>;
// all previews to create
previews: Map<string /* id */, string /* preview text */>,
}
function global_preview_state(ctx: Context): GlobalPreviewState {
const state = ctx.state.get(global_preview_state_key);
if (state) {
return <GlobalPreviewState> state;
} else {
ctx.state.set(
global_preview_state_key,
<GlobalPreviewState> {
files: new Set(),
previews: new Map(),
},
);
return global_preview_state(ctx);
}
}
export function id_to_preview_data(id: string): string {
return `ßä§${id}üÖö`;
}
const preview_data_regex = /(data-preview=")(ßä§)([^"]*)(üÖö)(")/g;
function replace_all_preview_data(source: string, ctx: Context): string {
return source.replaceAll(
preview_data_regex,
(
_match,
preamble,
_marker_prefix,
id,
_marker_postfix,
postamble
) => {
const name = resolve_name(id, "def", ctx)!;
const hash = get_hash(name);
return `${preamble}/previews/${hash}.html${postamble}`;
}
);
}
function tsgenReadTextFile(path: string, ctx: Context): string | undefined {
try {
const content = Deno.readTextFileSync(path);
return content;
} catch (err) {
ctx.error(`Could not read file ${style_file(path)}`);
ctx.error(err);
ctx.halt();
return undefined;
}
}
function tsgenWriteTextFile(path: string, content: string, ctx: Context): boolean {
try {
Deno.writeTextFileSync(path, content);
return true;
} catch (err) {
ctx.error(`Could not write file ${style_file(path)}`);
ctx.error(err);
ctx.halt();
return false;
}
}
// All macros that use defref must be descendents of an invocation of this.
// Postprocesses all preview-data to contain content-determined links,
// and processes and renames the preview files to work with these links.
export function enable_previews(...expressions: Expression[]): Invocation {const macro = new_macro(
undefined,
(expanded, ctx) => {
const preview_state = global_preview_state(ctx);
// Write all previews to the file system.
for (const [id, preview_content] of preview_state.previews) {
const name = resolve_name(id, "def", ctx)!;
const hash = get_hash(name);
const replaced_preview_content = replace_all_preview_data(preview_content, ctx);
if (!tsgenWriteTextFile(join(...preview_path(hash, ctx)), replaced_preview_content, ctx)) {
return "";
}
}
// In all files containing defref invocations, replace the preview-data with a hash-based preview url
for (const path of preview_state.files) {
const content = tsgenReadTextFile(path, ctx);
if (!content) {
return "";
}
const replaced = replace_all_preview_data(content, ctx);
if (!tsgenWriteTextFile(path, replaced, ctx)) {
return "";
}
}
return expanded;
},
);
return new Invocation(macro, expressions);
}
export function previewable_link(
is_def: boolean,
name: PerNameState,
is_tex: boolean,
surpress_preview: boolean,
display_text: Expression,
): Expression {
const macro = new_macro(
(args, ctx) => {
// get some metadata for rendering
const the_def = get_def(name);
const id = the_def.id;
const clazz = the_def.clazz;
const preview_data = id_to_preview_data(id);
// Register the current output file as containing previewable links.
const current_path = out_state(ctx).current_path;
global_preview_state(ctx).files.add(join(...current_path));
if (is_tex) {
const link_url = build_actual_link_url(id, name, ctx);
const link_tex =
`\\href{${link_url}}{${
<string>display_text // all calls of previewable_link with is_tex = true must set display_text to a string
}}`;
const data_tex = surpress_preview ? link_tex : `\\htmlData{preview=${preview_data}}{${link_tex}}`;
const id_tex = is_def ? `\\htmlId{${id}}{${data_tex}}` : data_tex;
const classy_tex = `\\htmlClass{normal_text${
clazz ? ` ${clazz}` : ""
}}{${id_tex}}`;
return [
html5_dependency_js("/named_assets/floating-ui.core.min.js"),
html5_dependency_js("/named_assets/floating-ui.dom.min.js"),
html5_dependency_js("/named_assets/tooltips.js"),
html5_dependency_js("/named_assets/previews.js"),
classy_tex,
];
} else {
const attributes: Attributes = {};
if (is_def) {
attributes.id = id;
}
if (!surpress_preview) {
attributes["data-preview"] = preview_data;
}
if (clazz && !is_def) {
attributes.class = `ref ${clazz}`;
} else if (!is_def) {
attributes.class = "ref";
} else if (clazz) {
attributes.class = clazz;
}
const the_link = link_name(
id,
attributes,
args[0],
);
const the_final_thingy = is_def ? dfn(the_link) : the_link;
return [
html5_dependency_js("/named_assets/floating-ui.core.min.js"),
html5_dependency_js("/named_assets/floating-ui.dom.min.js"),
html5_dependency_js("/named_assets/tooltips.js"),
html5_dependency_js("/named_assets/previews.js"),
the_final_thingy,
];
}
},
);
return new Invocation(macro, [display_text]);
}
const previewkey = Symbol("Preview");
interface PreviewState {
ids: Set<string> | null;
}
function preview_state(ctx: Context): PreviewState {
const state = ctx.state.get(previewkey);
if (state) {
return <PreviewState> state;
} else {
ctx.state.set(previewkey, {
ids: null,
});
return preview_state(ctx);
}
}
export function add_preview_id_to_preview_scope(id: string, ctx: Context): boolean {
const ids = preview_state(ctx).ids;
if (ids) {
ids.add(id);
return true;
} else {
ctx.warn(
`Did not create a preview for id ${style_def(id)} at ${
format_location(ctx.stack.peek()!)
}`,
);
ctx.warn(` There was no containing preview scope.`);
return false;
}
}
export function preview_path(basename: string, ctx: Context): string[] {
return [...get_root_directory(ctx), "previews", `${basename}.html`];
}
export function create_preview_from_string(id: string, content: string, ctx: Context) {
global_preview_state(ctx).previews.set(id, content);
const name = resolve_name(id, "def", ctx)!;
const hash = createHash().update(content).digest("hex");
set_hash(name, hash);
}
export function preview_scope(...expressions: Expression[]): Invocation {
let previous_scope: Set<string> | null = null;
const my_scope = new Set<string>();
const macro = new_macro(
undefined,
(expanded, ctx) => {
const ids = preview_state(ctx).ids;
if (ids != null) {
for (const id of ids) {
const regex = new RegExp(
`(<a[^>]*id="${id}"[^>]*class=")([^"]*)("{1}[^>]*>)`,
);
const withDefinedHereClassAlmost = expanded.replace(
regex,
`$1$2 defined_here$3`,
);
const katexRegex = new RegExp(
`(class="enclosing normal_text[^"]*)("><span class="enclosing" id="${id}"><span class="enclosing" data-preview="ßä§${id}üÖö">)`
);
const withDefinedHereClass = withDefinedHereClassAlmost.replace(
katexRegex,
`$1 defined_here$2`,
);
create_preview_from_string(id, withDefinedHereClass, ctx);
}
}
return expanded;
},
(ctx) => {
const state = preview_state(ctx);
previous_scope = state.ids;
state.ids = my_scope;
},
(ctx) => {
preview_state(ctx).ids = previous_scope;
},
);
return new Invocation(macro, expressions);
}
export interface Def {
id: string;
singular?: string;
plural?: string;
Singular?: string;
Plural?: string;
math?: string;
clazz?: string;
}
export const def_key = Symbol("Def");
export function get_def(name_state: PerNameState): Def {
return name_state.get(def_key)!;
}
export function set_def(name_state: PerNameState, def: Def) {
name_state.set(def_key, def);
}
// store a content-derived hash with each name
export const hash_key = Symbol("PreviewHash");
export function get_hash(name_state: PerNameState): string {
return name_state.get(hash_key)!;
}
export function set_hash(name_state: PerNameState, hash: string) {
name_state.set(hash_key, hash);
}
export function create_manual_preview(id: string, preview?: Expression): Expression {
const macro = new_macro(
undefined,
(expanded, ctx) => {
global_preview_state(ctx).previews.set(id, expanded);
const name = resolve_name(id, "def", ctx)!;
const hash = createHash().update(expanded).digest("hex");
set_hash(name, hash);
return "";
}
);
return preview ? new Invocation(macro, [preview]) : "";
}
export function def_truly_generic(
is_tex: boolean,
info: string | Def,
is_fake: boolean,
add_to_preview_scope: boolean,
surpress_preview: boolean,
text?: Expression,
preview?: Expression,
): Expression {
const info_: Def = (typeof info === "string") ? { id: info } : info;
const macro = new_macro(
(args, ctx) => {
const name = is_fake ? try_resolve_name(info_.id, "def", ctx) : new_name(info_.id, "def", ctx);
if (!name) {
return "";
} else if (!is_fake) {
name.set(def_key, info_);
}
const link = previewable_link(
true,
name,
is_tex,
surpress_preview,
text ? args[0] : (is_tex ? get_math(info_) : get_singular(info_)),
);
if (add_to_preview_scope && !is_fake && !preview) {
add_preview_id_to_preview_scope(info_.id, ctx);
}
return [
create_manual_preview(info_.id, preview),
link,
];
},
undefined,
undefined,
undefined,
3,
);
return new Invocation(macro, [text ? text : "never used"]);
}
export function def_generic(
info: string | Def,
is_fake: boolean,
text?: Expression,
preview?: Expression,
): Expression {
return def_truly_generic(false, info, is_fake, true, false, text, preview);
}
export function def_generic$(
info: string | Def,
is_fake: boolean,
preview?: Expression,
): Expression {
return def_truly_generic(true, info, is_fake, true, false, undefined, preview);
}
export function def(
info: string | Def,
text?: Expression,
preview?: Expression,
): Expression {
const info_ = typeof info === "string"
? { id: info, clazz: "def" }
: { ...info, clazz: "def" };
return def_generic(info_, false, text, preview);
}
export function def_fake(
info: string | Def,
text?: Expression,
preview?: Expression,
): Expression {
const info_ = typeof info === "string"
? { id: info, clazz: "def defined_here" }
: { ...info, clazz: "def defined_here" };
return def_generic(info_, true, text, preview);
}
/** Refer to a definition using its singular label. */
export function r(
id: string,
text?: Expression,
): Expression {
const macro = new_macro(
(args, _ctx) => {
return ref_invocation(get_singular, id, args[0] ? args[0] : undefined);
},
);
return new Invocation(macro, text ? [text] : []);
}
/** Refer to a definition in its plural label. */
export function rs(
id: string,
text?: Expression,
): Expression {
const macro = new_macro(
(args, _ctx) => {
return ref_invocation(get_plural, id, args[0] ? args[0] : undefined);
},
);
return new Invocation(macro, text ? [text] : []);
}
export function R(
id: string,
text?: Expression,
): Expression {
const macro = new_macro(
(args, _ctx) => {
return ref_invocation(get_Singular, id, args[0] ? args[0] : undefined);
},
);
return new Invocation(macro, text ? [text] : []);
}
export function Rs(
id: string,
text?: Expression,
): Expression {
const macro = new_macro(
(args, _ctx) => {
return ref_invocation(get_Plural, id, args[0] ? args[0] : undefined);
},
);
return new Invocation(macro, text ? [text] : []);
}
function ref_invocation_generic(
is_tex: boolean,
noun_form: (d: Def) => string,
id: string,
text?: Expression,
): Expression {
const macro = new_macro(
(args, ctx) => {
const name = try_resolve_name(id, "def", ctx);
if (name) {
return [
html5_dependency_js("/named_assets/floating-ui.core.min.js"),
html5_dependency_js("/named_assets/floating-ui.dom.min.js"),
html5_dependency_js("/named_assets/tooltips.js"),
html5_dependency_js("/named_assets/previews.js"),
previewable_link(
false,
name,
is_tex,
false,
text ? args[0] : noun_form(get_def(name)),
),
];
} else if (ctx.must_make_progress) {
ctx.warn(`Did not create a preview for id ${style_def(id)} at ${
format_location(ctx.stack.peek()!)
}`);
return "UndefinedReference";
} else {
return null;
}
},
undefined,
undefined,
undefined,
2,
);
return new Invocation(macro, [text ? text : "never used"]);
}
function ref_invocation(
noun_form: (d: Def) => string,
id: string,
text?: Expression,
): Expression {
const macro = new_macro(
(args, _ctx) => {
return ref_invocation_generic(false, noun_form, id, args[0] ? args[0] : undefined);
},
);
return new Invocation(macro, text ? [text] : []);
}
export function r$(
id: string,
): Expression {
const macro = new_macro(
(_args, _ctx) => {
return ref_invocation_generic(true, get_math, id);
},
);
return new Invocation(macro, []);
}
export function get_singular(d: Def): string {
if (d.singular === undefined) {
return d.id;
} else {
return d.singular;
}
}
export function get_plural(d: Def): string {
if (d.plural === undefined) {
return `${get_singular(d)}s`;
} else {
return d.plural;
}
}
export function get_Singular(d: Def): string {
if (d.Singular === undefined) {
return naive_capitalize(get_singular(d));
} else {
return d.Singular;
}
}
export function get_Plural(d: Def): string {
if (d.Plural === undefined) {
return naive_capitalize(get_plural(d));
} else {
return d.Plural;
}
}
export function get_math(d: Def): string {
if (d.math === undefined) {
return `\\text{${get_singular(d)}}`;
} else {
return d.math;
}
}
function naive_capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
export function style_def(s: string): string {
return Colors.green(s);
}