-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_styler.ts
More file actions
95 lines (86 loc) · 2.54 KB
/
Copy pathsyntax_styler.ts
File metadata and controls
95 lines (86 loc) · 2.54 KB
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
import {
lex_syntax,
render_syntax_html,
token_types_global,
TokenTypeRegistry,
type LexedSyntax,
type SyntaxLang
} from './lexer.ts';
export interface SyntaxStylerOptions {
/**
* Token-type id space used by the registered lexers. Defaults to the shared
* `token_types_global`, which the built-in lexers intern into — inject a
* separate registry only when every registered lexer interns into that same
* registry.
*/
token_types?: TokenTypeRegistry;
}
/**
* A no-op lexer — `plaintext` emits no events, so its text renders escaped but
* unstyled. Registered on every styler as the explicit "highlight nothing"
* language.
*/
const lexer_plaintext: SyntaxLang = {
id: 'plaintext',
lex: (l) => {
l.pos = l.end;
}
};
/**
* Registry and facade over the hand-written lexer engine (`lexer.ts`): registers
* languages, lexes text to a flat token event stream, and renders that stream to
* syntax-highlighted HTML.
*
* @example
* ```ts
* import {syntax_styler_global} from '@fuzdev/fuz_code/syntax_styler_global.ts';
*
* const html = syntax_styler_global.stylize('const x = 1;', 'ts');
* ```
*/
export class SyntaxStyler {
/**
* Registered languages, keyed by id and by each alias.
*/
langs: Map<string, SyntaxLang> = new Map();
/**
* Token-type id space for this styler's languages.
* See `SyntaxStylerOptions.token_types`.
*/
readonly token_types: TokenTypeRegistry;
constructor(options: SyntaxStylerOptions = {}) {
this.token_types = options.token_types ?? token_types_global;
this.add_lang(lexer_plaintext);
}
/**
* Registers a language (and its aliases).
*/
add_lang(lang: SyntaxLang): void {
this.langs.set(lang.id, lang);
if (lang.aliases) {
for (const alias of lang.aliases) this.langs.set(alias, lang);
}
}
/**
* Whether a language is registered under `id` (its primary id or an alias).
*/
has_lang(id: string): boolean {
return this.langs.has(id);
}
/**
* Lexes `text` with the language registered as `lang`, returning the flat
* token event stream. Throws when `lang` is not registered — see `has_lang`.
*/
lex(text: string, lang: string): LexedSyntax {
const l = this.langs.get(lang);
if (l === undefined) throw Error(`The language "${lang}" is not registered.`);
return lex_syntax(text, l, this.langs, this.token_types);
}
/**
* Generates syntax-highlighted HTML (spans with `.token_*` classes) from
* `text`. Throws when `lang` is not registered — see `has_lang`.
*/
stylize(text: string, lang: string): string {
return render_syntax_html(this.lex(text, lang));
}
}