forked from fKunstner/latex-to-utf8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
341 lines (304 loc) · 11.3 KB
/
index.html
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
<!DOCTYPE doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<title>
Typst2UTF8
</title>
<script src="https://kit.fontawesome.com/7194e22a27.js">
</script>
<script src="map.js">
</script>
<script src="samples.js">
</script>
<link href="style.css" rel="stylesheet"/>
</meta>
</meta>
</head>
</html>
<body>
<div class="main">
<h1>
Typst ⟶ UTF-8
</h1>
<div id="mode-buttons">
<a href="#snippet" id="snippet-button" class="button active">
Snippet
</a><a href="#text" id="text-button" class="button inactive">
Text
</a>
</div>
<div id="snippet-area">
<div id="typstInput" contenteditable="true" spellcheck="false" placeholder="Typst formula" style="border: 1px solid #ccc; padding: 5px;"></div>
<!-- <input autofocus="" id="typstInput" placeholder="Typst formula" required="" type="typst"></input> -->
<span style="font-size:2em">
↓
</span>
<div>
<input placeholder="UTF8 output" id="utf8Output"></input>
<a id="copy" class="button active">
Copy
</a>
</div>
</div>
<div id="text-area">
<div id="text-control-area">
<label>
<input type="checkbox" id="markdown-checkbox" name="markdown" checked>
Use <span style="font-family:monospace">`backticks`</span>
</label>
<a id="copy-text" class="button active">
Copy
</a>
</div>
<textarea placeholder="Text with $...$ and $ ... $ typst math" id="typstInput-text"></textarea>
<span style="font-size:2.5em;
margin:.25em;
vertical-align: top;
display: inline-block;">
→
</span>
<textarea placeholder="UTF8 output" id="utf8Output-text"></textarea>
</div>
<div id="links">
<a href="https://github.com/eloitor/typst-to-utf8" title="GitHub page">
<i class="fab fa-github">
</i>
</a>
<a href="https://github.com/eloitor/typst-to-utf8/issues/new/choose" title="Report a bug">
<i class="fas fa-bug">
</i>
</a>
</div>
</div>
</body>
<script>
var typstInput = document.getElementById("typstInput");
var utf8Output = document.getElementById("utf8Output");
var typstInputText = document.getElementById("typstInput-text");
var utf8OutputText = document.getElementById("utf8Output-text");
var copyButton = document.getElementById("copy");
var copyTextButton = document.getElementById("copy-text");
var snippetButton = document.getElementById("snippet-button");
var textButton = document.getElementById("text-button");
var snippetArea = document.getElementById("snippet-area");
var textArea = document.getElementById("text-area");
var markdownCheckbox = document.getElementById("markdown-checkbox");
var autoExpand = function (field, field2) {
// Reset field height
field.style.height = 'inherit';
// Get the computed styles for the element
var computed = window.getComputedStyle(field);
// Calculate the height
var height = parseInt(computed.getPropertyValue('border-top-width'), 10)
+ parseInt(computed.getPropertyValue('padding-top'), 10)
+ field.scrollHeight
+ parseInt(computed.getPropertyValue('padding-bottom'), 10)
+ parseInt(computed.getPropertyValue('border-bottom-width'), 10);
height = Math.max(height, 300);
field.style.height = height + 'px';
field2.style.height = height + 'px';
};
function activateMode(mode) {
window.location.hash = mode;
if (mode === "text") {
update(typstInputText, utf8OutputText);
textButton.classList.add("active");
snippetButton.classList.add("inactive");
textButton.classList.remove("inactive");
snippetButton.classList.remove("active");
snippetArea.style.display = "none";
textArea.style.display = "block";
} else if (mode === "snippet") {
update(typstInput, utf8Output);
textButton.classList.add("inactive");
snippetButton.classList.add("active");
textButton.classList.remove("active");
snippetButton.classList.remove("inactive");
snippetArea.style.display = "block";
textArea.style.display = "none";
}
}
function clickTextButton() {
activateMode("text")
}
function clickSnippetButton() {
activateMode("snippet")
}
function processSubAndSuperscripts(input) {
let subscript = /(.*)\_\(([^\(\)]*)\)(.*)/ms;
let superscript = /(.*)\^\(([^\(\)]*)\)(.*)/ms;
while (true) {
let results = input.match(subscript);
if (results === null) {
break;
}
input = results[1] + "_" + results[2].split("").join("_") + results[3];
}
while (true) {
let results = input.match(superscript);
if (results === null) {
break;
}
input = results[1] + "^" + results[2].split("").join("^") + results[3];
}
return input
}
function processMathContent(content, mode) {
// Here you can process the math content, including handling inline vs block math for markdown
if (markdownCheckbox.checked && mode === "#text") {
if (content.startsWith(" ") && content.endsWith(" ")) {
return "```" + content.slice(1, -1) + "```";
} else {
return "`" + content + "`";
}
}
return content;
}
function typst2utf8(input, mode) {
if (input === undefined) {
return "";
}
let result = "";
let state = mode === "#text" ? "text" : "math";
let mathContent = "";
let i = 0;
while (i < input.length) {
if (state === "text") {
if (input[i] === '$') {
state = "math";
} else {
result += input[i];
}
i++;
} else if (state === "math") {
if (input[i] === '$') {
state = "text";
result += processMathContent(mathContent, mode);
mathContent = "";
i++;
} else if (input[i] === '"') {
state = "literal";
mathContent += input[i]; // Include the " in the output
i++;
while (i < input.length && state === "literal") {
mathContent += input[i];
if (input[i] === '"') {
state = "math";
}
i++;
}
} else {
mathContent += input[i];
i++;
}
}
}
if (mathContent) {
result += processMathContent(mathContent, mode);
}
return result;
}
function update(from, to) {
to.value = typst2utf8(from.value, mode = window.location.hash);
}
function setRandomSample() {
typstInput.innerText = samples[Math.floor(Math.random() * samples.length)];
typstInputText.value = samplesText[Math.floor(Math.random() * samplesText.length)];
update(typstInput, utf8Output);
update(typstInputText, utf8OutputText);
}
function restoreSelection(containerEl, start, end) {
const range = document.createRange();
const sel = window.getSelection();
let charIndex = 0;
let startNode = null;
let startOffset = 0;
let endNode = null;
let endOffset = 0;
function traverseNodes(node) {
if (node.nodeType === 3) {
const textLength = node.textContent.length;
if (startNode === null && charIndex + textLength >= start) {
startNode = node;
startOffset = start - charIndex;
}
if (endNode === null && charIndex + textLength >= end) {
endNode = node;
endOffset = end - charIndex;
}
charIndex += textLength;
} else {
for (let i = 0; i < node.childNodes.length; i++) {
traverseNodes(node.childNodes[i]);
if (startNode !== null && endNode !== null) {
return;
}
}
}
}
traverseNodes(containerEl);
if (startNode !== null && endNode !== null) {
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
sel.removeAllRanges();
sel.addRange(range);
}
}
function parseSnippet() {
const typstInput = document.getElementById('typstInput');
const text = typstInput.innerHTML;
// Define your error pattern here
const errorPattern = /error/g;
// Highlight errors
console.log(text);
let highlightedText = text.replace('<span class="error">', '');
highlightedText = highlightedText.replace('</span>', '');
highlightedText = highlightedText.replace(errorPattern, '<span class="error">$&</span>');
console.log(highlightedText);
// Set the new HTML content
if (highlightedText !== typstInput.innerHTML) {
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const startOffset = range.startOffset;
const endOffset = range.endOffset;
typstInput.innerHTML = highlightedText;
const sel = window.getSelection();
sel.removeAllRanges();
restoreSelection(typstInput, startOffset, endOffset);
}
update(typstInput, document.getElementById('utf8Output'));
}
function parseText() {
autoExpand(typstInputText, utf8OutputText)
update(typstInputText, utf8OutputText)
}
function copyToClipboard(from) {
from.select();
document.execCommand("copy");
}
function copyToClipboardSnippet() {
copyToClipboard(utf8Output)
}
function copyToClipboardText() {
copyToClipboard(utf8OutputText)
}
function load() {
var currhash = window.location.hash;
if (currhash === "#snippet") {
activateMode("text")
} else if (currhash === "#text") {
activateMode("text")
}
typstInput.addEventListener("keyup", parseSnippet);
typstInputText.addEventListener("keyup", parseText);
copyButton.addEventListener("click", copyToClipboardSnippet);
copyTextButton.addEventListener("click", copyToClipboardText);
snippetButton.addEventListener("click", clickSnippetButton);
textButton.addEventListener("click", clickTextButton);
markdownCheckbox.addEventListener("click", parseText);
setRandomSample()
}
window.addEventListener('DOMContentLoaded', load, false);
</script>