all repos — gemini-redirect @ fcd5db0bbb4bb9fe310c1507e91664de87765514

content/graphs/tinyparser.js (view raw)

 1// Custom tiny parser for csv tables. They're more comfortable.
 2var tables = document.getElementsByClassName('csv');
 3for (var i = 0; i != tables.length; ++i) {
 4  var lines = tables[i].innerHTML.split('\n');
 5  var result = ['<table>'];
 6  var line = null;
 7  for (var j = 0; j != lines.length; ++j) {
 8    line = lines[j].trim();
 9    if (line) {
10      result.push('<tr><td>');
11      result.push(lines[j].replace(/\s*;\s*/g, '</td><td>'));
12      result.push('</td></tr>');
13    }
14  }
15  result.push('</table>');
16  tables[i].innerHTML = result.join('');
17}
18
19// Custom tiny math parser. I like writing parsers.
20// 'x^2n ' becomes 'x<sup>2n</sup> '
21// 'x_5n ' becomes 'x<sub>5n</sub> '
22// 'a * b' becomes 'a × b'
23// ' '     becomes '&nbsp;'
24function mathify(element) {
25  var c = null;
26  var closeOnSpace = null;
27  var content = element.innerHTML.split('');
28  for (var j = 0; j != content.length; ++j) {
29    c = content[j];
30    if (c == '*') {
31      content[j] = '×';
32    } else if (c == '^') {
33      content[j] = '<sup>';
34      closeOnSpace = '</sup>';
35    } else if (c == '_') {
36      content[j] = '<sub>';
37      closeOnSpace = '</sub>';
38    } else if (c == ' ') {
39      if (closeOnSpace == null) {
40        content[j] = '&nbsp;';
41      } else {
42        content[j] = closeOnSpace + ' ';
43        closeOnSpace = null;
44      }
45    } else if (c == '\n' && closeOnSpace != null) {
46        content[j] = closeOnSpace + '\n';
47        closeOnSpace = null;
48    }
49  }
50  if (closeOnSpace != null) {
51    content.push(closeOnSpace.trim());
52  }
53
54  element.innerHTML = content.join('');
55}
56
57var maths = document.getElementsByTagName('mark');
58for (var i = 0; i != maths.length; ++i)
59  mathify(maths[i]);
60
61var maths = document.getElementsByClassName('math');
62for (var i = 0; i != maths.length; ++i)
63  mathify(maths[i]);