-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
247 lines (223 loc) · 8.13 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
<!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>
TeX2UTF8
</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>
LaTeX ⟶ 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">
<input autofocus="" id="latexInput" placeholder="Latex formula" required="" type="latex"></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 $$...$$ latex math" id="latexInput-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/fKunstner/latex-to-utf8" title="GitHub page">
<i class="fab fa-github">
</i>
</a>
<a href="https://github.com/fKunstner/latex-to-utf8/issues/new/choose" title="Report a bug">
<i class="fas fa-bug">
</i>
</a>
</div>
</div>
</body>
<script>
var latexInput = document.getElementById("latexInput");
var utf8Output = document.getElementById("utf8Output");
var latexInputText = document.getElementById("latexInput-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(latexInputText, 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(latexInput, 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 latex2utf8(input, mode) {
if (input === undefined) {
return "";
}
if (mode === "#text") {
let displaymath_regex = /(.*)\$\$([^$]*)\$\$(.*)/sm;
let inlinemath_regex = /(.*)\$([^$]*)\$(.*)/sm;
while (true) {
let results = input.match(displaymath_regex);
if (results === null) {
break;
}
let delim = markdownCheckbox.checked ? "```" : "";
let processed = delim + latex2utf8(results[2], mode = "") + delim;
input = results[1] + processed + results[3];
}
while (true) {
let results = input.match(inlinemath_regex);
if (results === null) {
break;
}
let delim = markdownCheckbox.checked ? "`" : "";
let processed = delim + latex2utf8(results[2], mode = "") + delim;
input = results[1] + processed + results[3];
}
} else {
input = processSubAndSuperscripts(input);
Object.keys(map).forEach(function (key) {
input = input.split(key).join(map[key]);
});
}
return input
}
function update(from, to) {
to.value = latex2utf8(from.value, mode = window.location.hash);
}
function setRandomSample() {
latexInput.value = samples[Math.floor(Math.random() * samples.length)];
latexInputText.value = samplesText[Math.floor(Math.random() * samplesText.length)];
update(latexInput, utf8Output);
update(latexInputText, utf8OutputText);
}
function parseSnippet() {
update(latexInput, utf8Output)
}
function parseText() {
autoExpand(latexInputText, utf8OutputText)
update(latexInputText, 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")
}
latexInput.addEventListener("keyup", parseSnippet);
latexInputText.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>