webtex-cn 0.1.0
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/LICENSE +201 -0
- package/README.md +2 -0
- package/bin/webtex-cn.js +195 -0
- package/dist/base.css +513 -0
- package/dist/honglou.css +35 -0
- package/dist/minimal.css +26 -0
- package/dist/siku-quanshu-colored.css +31 -0
- package/dist/siku-quanshu.css +33 -0
- package/dist/webtex-cn.css +653 -0
- package/dist/webtex-cn.esm.js +1788 -0
- package/dist/webtex-cn.js +1812 -0
- package/dist/webtex-cn.min.js +46 -0
- package/package.json +59 -0
- package/src/config/templates.js +49 -0
- package/src/index.js +129 -0
- package/src/layout/grid-layout.js +484 -0
- package/src/model/nodes.js +94 -0
- package/src/parser/commands.js +156 -0
- package/src/parser/index.js +27 -0
- package/src/parser/parser.js +472 -0
- package/src/parser/tokenizer.js +228 -0
- package/src/renderer/html-renderer.js +643 -0
- package/src/templates/base.css +513 -0
- package/src/templates/honglou.css +35 -0
- package/src/templates/minimal.css +26 -0
- package/src/templates/siku-quanshu-colored.css +31 -0
- package/src/templates/siku-quanshu.css +33 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TeX Tokenizer for luatex-cn compatible files.
|
|
3
|
+
* Splits TeX source into a stream of tokens.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const TokenType = {
|
|
7
|
+
COMMAND: 'COMMAND',
|
|
8
|
+
OPEN_BRACE: 'OPEN_BRACE',
|
|
9
|
+
CLOSE_BRACE: 'CLOSE_BRACE',
|
|
10
|
+
OPEN_BRACKET: 'OPEN_BRACKET',
|
|
11
|
+
CLOSE_BRACKET: 'CLOSE_BRACKET',
|
|
12
|
+
TEXT: 'TEXT',
|
|
13
|
+
NEWLINE: 'NEWLINE', // \\
|
|
14
|
+
COMMENT: 'COMMENT',
|
|
15
|
+
BEGIN: 'BEGIN',
|
|
16
|
+
END: 'END',
|
|
17
|
+
MATH: 'MATH',
|
|
18
|
+
PARAGRAPH_BREAK: 'PARAGRAPH_BREAK',
|
|
19
|
+
EOF: 'EOF',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function isLetter(ch) {
|
|
23
|
+
return /[a-zA-Z]/.test(ch);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isCJK(ch) {
|
|
27
|
+
const code = ch.codePointAt(0);
|
|
28
|
+
// CJK Unified Ideographs
|
|
29
|
+
if (code >= 0x4E00 && code <= 0x9FFF) return true;
|
|
30
|
+
// CJK Extension A
|
|
31
|
+
if (code >= 0x3400 && code <= 0x4DBF) return true;
|
|
32
|
+
// CJK Compatibility Ideographs
|
|
33
|
+
if (code >= 0xF900 && code <= 0xFAFF) return true;
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isCommandChar(ch) {
|
|
38
|
+
return isLetter(ch) || isCJK(ch) || ch === '@' || ch === '*';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class Tokenizer {
|
|
42
|
+
constructor(source) {
|
|
43
|
+
this.source = source;
|
|
44
|
+
this.pos = 0;
|
|
45
|
+
this.tokens = [];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
peek() {
|
|
49
|
+
if (this.pos >= this.source.length) return null;
|
|
50
|
+
return this.source[this.pos];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
advance() {
|
|
54
|
+
const ch = this.source[this.pos];
|
|
55
|
+
this.pos++;
|
|
56
|
+
return ch;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
tokenize() {
|
|
60
|
+
while (this.pos < this.source.length) {
|
|
61
|
+
const ch = this.peek();
|
|
62
|
+
|
|
63
|
+
if (ch === '%') {
|
|
64
|
+
// Comment: skip to end of line
|
|
65
|
+
this.skipComment();
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (ch === '$') {
|
|
70
|
+
this.readMath();
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (ch === '\\') {
|
|
75
|
+
this.readCommand();
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (ch === '{') {
|
|
80
|
+
this.tokens.push({ type: TokenType.OPEN_BRACE, value: '{' });
|
|
81
|
+
this.advance();
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (ch === '}') {
|
|
86
|
+
this.tokens.push({ type: TokenType.CLOSE_BRACE, value: '}' });
|
|
87
|
+
this.advance();
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (ch === '[') {
|
|
92
|
+
this.tokens.push({ type: TokenType.OPEN_BRACKET, value: '[' });
|
|
93
|
+
this.advance();
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (ch === ']') {
|
|
98
|
+
this.tokens.push({ type: TokenType.CLOSE_BRACKET, value: ']' });
|
|
99
|
+
this.advance();
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Regular text
|
|
104
|
+
this.readText();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.tokens.push({ type: TokenType.EOF, value: '' });
|
|
108
|
+
return this.tokens;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
skipComment() {
|
|
112
|
+
while (this.pos < this.source.length && this.source[this.pos] !== '\n') {
|
|
113
|
+
this.pos++;
|
|
114
|
+
}
|
|
115
|
+
// Skip the newline too
|
|
116
|
+
if (this.pos < this.source.length) this.pos++;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
readCommand() {
|
|
120
|
+
this.advance(); // skip '\'
|
|
121
|
+
|
|
122
|
+
if (this.pos >= this.source.length) {
|
|
123
|
+
this.tokens.push({ type: TokenType.TEXT, value: '\\' });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const nextCh = this.peek();
|
|
128
|
+
|
|
129
|
+
// \\ = forced newline
|
|
130
|
+
if (nextCh === '\\') {
|
|
131
|
+
this.advance();
|
|
132
|
+
this.tokens.push({ type: TokenType.NEWLINE, value: '\\\\' });
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// \{ \} \[ \] \% \$ \& \# \_ \~ \^ - escaped characters
|
|
137
|
+
if ('{}[]%$&#_~^'.includes(nextCh)) {
|
|
138
|
+
this.advance();
|
|
139
|
+
this.tokens.push({ type: TokenType.TEXT, value: nextCh });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// \ (backslash + space) = control space
|
|
144
|
+
if (nextCh === ' ' || nextCh === '\n') {
|
|
145
|
+
this.advance();
|
|
146
|
+
this.tokens.push({ type: TokenType.TEXT, value: ' ' });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Read command name: sequence of letters or CJK chars
|
|
151
|
+
if (!isCommandChar(nextCh)) {
|
|
152
|
+
// Single non-letter character command (like \,)
|
|
153
|
+
this.advance();
|
|
154
|
+
this.tokens.push({ type: TokenType.COMMAND, value: nextCh });
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Determine if we're reading ASCII letters or CJK characters
|
|
159
|
+
const isAsciiStart = isLetter(nextCh) || nextCh === '@' || nextCh === '*';
|
|
160
|
+
const isCJKStart = isCJK(nextCh);
|
|
161
|
+
|
|
162
|
+
let name = '';
|
|
163
|
+
if (isAsciiStart) {
|
|
164
|
+
// ASCII command: read letters, @, *
|
|
165
|
+
while (this.pos < this.source.length && (isLetter(this.peek()) || this.peek() === '@' || this.peek() === '*')) {
|
|
166
|
+
name += this.advance();
|
|
167
|
+
}
|
|
168
|
+
// Skip trailing spaces after ASCII commands
|
|
169
|
+
while (this.pos < this.source.length && this.source[this.pos] === ' ') {
|
|
170
|
+
this.pos++;
|
|
171
|
+
}
|
|
172
|
+
} else if (isCJKStart) {
|
|
173
|
+
// CJK command: read CJK characters
|
|
174
|
+
while (this.pos < this.source.length && isCJK(this.peek())) {
|
|
175
|
+
name += this.advance();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Special handling for \begin and \end
|
|
180
|
+
if (name === 'begin') {
|
|
181
|
+
this.tokens.push({ type: TokenType.BEGIN, value: 'begin' });
|
|
182
|
+
} else if (name === 'end') {
|
|
183
|
+
this.tokens.push({ type: TokenType.END, value: 'end' });
|
|
184
|
+
} else {
|
|
185
|
+
this.tokens.push({ type: TokenType.COMMAND, value: name });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
readMath() {
|
|
190
|
+
this.advance(); // skip opening $
|
|
191
|
+
let content = '';
|
|
192
|
+
while (this.pos < this.source.length) {
|
|
193
|
+
const ch = this.peek();
|
|
194
|
+
if (ch === '$') {
|
|
195
|
+
this.advance(); // skip closing $
|
|
196
|
+
this.tokens.push({ type: TokenType.MATH, value: content });
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
content += this.advance();
|
|
200
|
+
}
|
|
201
|
+
// Unclosed $: treat as text
|
|
202
|
+
this.tokens.push({ type: TokenType.TEXT, value: '$' + content });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
readText() {
|
|
206
|
+
let text = '';
|
|
207
|
+
while (this.pos < this.source.length) {
|
|
208
|
+
const ch = this.peek();
|
|
209
|
+
if (ch === '\\' || ch === '{' || ch === '}' || ch === '[' || ch === ']' || ch === '%' || ch === '$') {
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
text += this.advance();
|
|
213
|
+
}
|
|
214
|
+
if (text) {
|
|
215
|
+
// Split on blank lines (paragraph breaks): \n followed by whitespace-only line
|
|
216
|
+
const parts = text.split(/\n[ \t]*\n/);
|
|
217
|
+
for (let i = 0; i < parts.length; i++) {
|
|
218
|
+
if (i > 0) {
|
|
219
|
+
this.tokens.push({ type: TokenType.PARAGRAPH_BREAK, value: '' });
|
|
220
|
+
}
|
|
221
|
+
const collapsed = parts[i].replace(/[ \t]+/g, ' ');
|
|
222
|
+
if (collapsed.trim() || collapsed === ' ') {
|
|
223
|
+
this.tokens.push({ type: TokenType.TEXT, value: collapsed });
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|