summaryrefslogtreecommitdiff
path: root/registry/vulkan/scripts/translate_math.js
blob: c2fd6d267cbfa442dfc826777445cdb1860a3cdd (plain)
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
// Copyright 2020-2021 The Khronos Group, Inc.
//
// SPDX-License-Identifier: Apache-2.0

// Translates the latexmath in html on build time using KaTeX
// Usage: nodejs translate_math.js katex/katex.min.js vkspec.html

const katex = require(process.argv[2]);
const fs = require("fs");
const escapeRegex = require("escape-string-regexp");
const he = require('he');

const filepath = process.argv[3];

var html = fs.readFileSync(filepath, "utf8");

const delimiters = [
                     //{ left: "$$", right: "$$", display: true},
                     { left: "\\[", right: "\\]", display: true},
                     //{ left: "$", right: "$", display: false},
                     { left: "\\(", right: "\\)", display: false}
                   ]

for( var delim of delimiters ) {
    const regex = new RegExp( escapeRegex(delim.left) + "([\\S\\s]*?)" + escapeRegex(delim.right), "g");
    html = html.replace( regex,
        function(match, g1) {
            return katex.renderToString( he.decode(g1, {'strict': true}), {displayMode: delim.display, output: 'html', strict: true} );
        }
    );
}

fs.writeFileSync(filepath, html, 'utf8');