From b0ff66abd19675e0b45e6fabe842cca2902aae00 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Tue, 20 Feb 2024 16:04:35 +0100 Subject: [PATCH] Separate katex-server into own repo. --- .gitmodules | 3 + katex-server | 1 + src/formatitko/katex-server/.gitignore | 1 - src/formatitko/katex-server/README.md | 1 - src/formatitko/katex-server/index.js | 1 - src/formatitko/katex-server/index.mjs | 131 ------------------ src/formatitko/katex-server/package-lock.json | 39 ------ src/formatitko/katex-server/package.json | 14 -- 8 files changed, 4 insertions(+), 187 deletions(-) create mode 160000 katex-server delete mode 100644 src/formatitko/katex-server/.gitignore delete mode 100644 src/formatitko/katex-server/README.md delete mode 100644 src/formatitko/katex-server/index.js delete mode 100644 src/formatitko/katex-server/index.mjs delete mode 100644 src/formatitko/katex-server/package-lock.json delete mode 100644 src/formatitko/katex-server/package.json diff --git a/.gitmodules b/.gitmodules index 611d4fc..c82be69 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "ucwmac"] path = ucwmac url = git://git.ucw.cz/ucwmac.git +[submodule "katex-server"] + path = katex-server + url = https://gitea.ks.matfyz.cz/KSP/formatitko-katex-server.git diff --git a/katex-server b/katex-server new file mode 160000 index 0000000..9a122d9 --- /dev/null +++ b/katex-server @@ -0,0 +1 @@ +Subproject commit 9a122d9ee999a4e14f3543fd9f01d38de40a7706 diff --git a/src/formatitko/katex-server/.gitignore b/src/formatitko/katex-server/.gitignore deleted file mode 100644 index 3c3629e..0000000 --- a/src/formatitko/katex-server/.gitignore +++ /dev/null @@ -1 +0,0 @@ -node_modules diff --git a/src/formatitko/katex-server/README.md b/src/formatitko/katex-server/README.md deleted file mode 100644 index 082cd55..0000000 --- a/src/formatitko/katex-server/README.md +++ /dev/null @@ -1 +0,0 @@ -This was made by Standa Lukeš @exyi diff --git a/src/formatitko/katex-server/index.js b/src/formatitko/katex-server/index.js deleted file mode 100644 index d6754c8..0000000 --- a/src/formatitko/katex-server/index.js +++ /dev/null @@ -1 +0,0 @@ -console.log(require('katex').renderToString('\\frac{2a}{b}')) diff --git a/src/formatitko/katex-server/index.mjs b/src/formatitko/katex-server/index.mjs deleted file mode 100644 index 2c1c05c..0000000 --- a/src/formatitko/katex-server/index.mjs +++ /dev/null @@ -1,131 +0,0 @@ -// KaTeX rendering server -// Listens on unix socket, path is provided as first argument -// Expects JSON lines, each line is a query with the following schema: -// { -// formulas: [ -// { -// tex: string, -// options?: object -// } -// ], -// options?: object -// } - -// see https://katex.org/docs/options.html for list of available options -// If options formulas[].options field is used, the global options field is ignored. - -// For each line, returns one JSON line with the following schema: -// { -// results: [ -// { html?: string } | { error?: string } -// ] -// } | { error?: string } - - -// If one formula is invalid, the error in results is used -// If the entire query is invalid (couldn't parse JSON, for example), the outer error field is used - - -import katex from 'katex' -import net from 'net' -import * as readline from 'readline' - -const myArgs = process.argv.slice(2) - -const unixSocketPath = myArgs[0] -if (!unixSocketPath) { - console.error('you must specify socket path') - process.exit(1) -} - -// This server listens on a Unix socket at /var/run/mysocket -var unixServer = net.createServer(handleClient); -unixServer.listen(unixSocketPath); -console.log("OK") - -function handleExit(signal) { - // unixServer.emit('close') - unixServer.close(function () { - - }); - process.exit(0); // put this into the callback to avoid closing open connections -} -process.on('SIGINT', handleExit); -process.on('SIGQUIT', handleExit); -process.on('SIGTERM', handleExit); -process.on('exit', handleExit); - -const defaultOptions = {} - -/** - * @param {net.Socket} socket - * @returns {Promise} - * */ -function socketWrite(socket, data) { - return new Promise((resolve, reject) => { - socket.write(data, (err) => { - if (err) { - reject(err) - } else { - resolve() - } - }) - }) -} - -/** - * @param {net.Socket} client - * */ -async function handleClient(client) { - const rl = readline.createInterface({ input: client }) - - /* Added by GS: A stack of katex's `macros` objects, each group inherits - * the one from the parent group and can add its own stuff without - * affecting the parent. - */ - let macroStack = [{}] - for await (const line of rl) { - try { - // The custom commands for pushing and popping the macro stack. - if (line === "begingroup") { - // Copy the current state of macros and push it onto the stack. - macroStack.push({...macroStack.slice(-1)[0]}) - continue - } else if (line === "endgroup") { - macroStack.pop() - continue - } else if (line === "init") { - macroStack = [{}] - continue - } - const query = JSON.parse(line) - const results = [] - for (const input of query.formulas) { - const options = input.options ?? query.options ?? defaultOptions - // Add macros from the macros option - if (options.macros) { - for (const macro of Object.keys(options.macros)) { - macroStack.slice(-1)[macro] = options.macros[macro] - } - } - options.macros = macroStack.slice(-1)[0] - // Enforce globalGroup option, katex then saves created macros - // into the options.macros object. - options.globalGroup = true - try { - const html = katex.renderToString(input.tex, options) - results.push({ html }) - } catch (e) { - results.push({ error: String(e) }) - } - } - await socketWrite(client, JSON.stringify({ results }, null, query.debug ? ' ' : undefined)) - await socketWrite(client, '\n') - } catch (e) { - console.error(e) - await socketWrite(client, JSON.stringify({ error: String(e) })) - await socketWrite(client, '\n') - } - } -} - diff --git a/src/formatitko/katex-server/package-lock.json b/src/formatitko/katex-server/package-lock.json deleted file mode 100644 index 3b5bee3..0000000 --- a/src/formatitko/katex-server/package-lock.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "ksp-katex-server", - "version": "1.0.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "ksp-katex-server", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "katex": "^0.16.3" - } - }, - "node_modules/commander": { - "version": "8.3.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", - "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==", - "engines": { - "node": ">= 12" - } - }, - "node_modules/katex": { - "version": "0.16.3", - "resolved": "https://registry.npmjs.org/katex/-/katex-0.16.3.tgz", - "integrity": "sha512-3EykQddareoRmbtNiNEDgl3IGjryyrp2eg/25fHDEnlHymIDi33bptkMv6K4EOC2LZCybLW/ZkEo6Le+EM9pmA==", - "funding": [ - "https://opencollective.com/katex", - "https://github.com/sponsors/katex" - ], - "dependencies": { - "commander": "^8.0.0" - }, - "bin": { - "katex": "cli.js" - } - } - } -} diff --git a/src/formatitko/katex-server/package.json b/src/formatitko/katex-server/package.json deleted file mode 100644 index 3d68121..0000000 --- a/src/formatitko/katex-server/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "ksp-katex-server", - "version": "1.0.0", - "description": "", - "main": "index.mjs", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "dependencies": { - "katex": "^0.16.3" - } -}