Update tinyparser.js for graphs post
Lonami Exo totufals@hotmail.com
Sat, 06 Feb 2021 17:02:26 +0100
1 files changed,
58 insertions(+),
57 deletions(-)
M
content/blog/graphs/tinyparser.js
→
content/blog/graphs/tinyparser.js
@@ -1,63 +1,64 @@
-// Custom tiny parser for csv tables. They're more comfortable. -var tables = document.getElementsByClassName('csv'); -for (var i = 0; i != tables.length; ++i) { - var lines = tables[i].innerHTML.split('\n'); - var result = ['<table>']; - var line = null; - for (var j = 0; j != lines.length; ++j) { - line = lines[j].trim(); - if (line) { - result.push('<tr><td>'); - result.push(lines[j].replace(/\s*;\s*/g, '</td><td>')); - result.push('</td></tr>'); - } - } - result.push('</table>'); - tables[i].innerHTML = result.join(''); -} - -// Custom tiny math parser. I like writing parsers. -// 'x^2n ' becomes 'x<sup>2n</sup> ' -// 'x_5n ' becomes 'x<sub>5n</sub> ' -// 'a * b' becomes 'a × b' -// ' ' becomes ' ' -function mathify(element) { - var c = null; - var closeOnSpace = null; - var content = element.innerHTML.split(''); - for (var j = 0; j != content.length; ++j) { - c = content[j]; - if (c == '*') { - content[j] = '×'; - } else if (c == '^') { - content[j] = '<sup>'; - closeOnSpace = '</sup>'; - } else if (c == '_') { - content[j] = '<sub>'; - closeOnSpace = '</sub>'; - } else if (c == ' ') { - if (closeOnSpace == null) { - content[j] = ' '; +const mathify = content => { + let result = '' + let close = null; + let symbol = null; + Array.from(content).forEach(c => { + if (symbol !== null) { + if (c === ' ') { + if (symbol === 'rightarrow') { + result += '→' + } else if (symbol === 'geq') { + result += '≥' + } else { + result += '?' + } + symbol = null; + result += ' ' + } else { + symbol += c + } + } else if (c == '$') { + symbol = '' + } else if (c === '*') { + result += '×' + } else if (c === '^') { + result += '<sup>' + close = '</sup>' + } else if (c === '_') { + result += '<sub>'; + close = '</sub>'; + } else if (c === ' ' || c == '\n') { + if (close !== null) { + result += close + close = null + } + result += ' ' } else { - content[j] = closeOnSpace + ' '; - closeOnSpace = null; + result += c } - } else if (c == '\n' && closeOnSpace != null) { - content[j] = closeOnSpace + '\n'; - closeOnSpace = null; - } + }) + if (close !== null) { + result += close; } - if (closeOnSpace != null) { - content.push(closeOnSpace.trim()); - } + return `<em class="math">${result.trim()}</em>` +} - element.innerHTML = content.join(''); -} +const parse_maths = html => html.replaceAll(/\\\([^\\]+\\\)/g, match => + mathify(match.slice(2, -2)) +); -var maths = document.getElementsByTagName('mark'); -for (var i = 0; i != maths.length; ++i) - mathify(maths[i]); +Array.from(document.getElementsByClassName('matrix')).forEach(matrix => { + let result = '<table>' + matrix.innerHTML.trim().split('\\').forEach(row => { + result += '<tr>' + row.trim().split("'").forEach(elem => { + result += `<td>${mathify(elem.trim())}</td>` + }) + result += '</tr>' + }) + matrix.innerHTML = result +}) -var maths = document.getElementsByClassName('math'); -for (var i = 0; i != maths.length; ++i) - mathify(maths[i]); +Array.from(document.getElementsByTagName('p')).forEach(paragraph => { + paragraph.innerHTML = parse_maths(paragraph.innerHTML) +})