Compare commits

...

2 Commits

  1. 3
      .gitmodules
  2. 0
      src/formatitko/katex-server/index.mjs
  3. 7
      src/formatitko/katex-server/package-lock.json
  4. 5
      src/formatitko/katex-server/package.json
  5. 15
      src/formatitko/katex-server/yarn.lock
  6. 20
      src/formatitko/katex.py

3
.gitmodules

@ -1,3 +1,6 @@
[submodule "ucwmac"] [submodule "ucwmac"]
path = ucwmac path = ucwmac
url = git://git.ucw.cz/ucwmac.git url = git://git.ucw.cz/ucwmac.git
[submodule "src/formatitko/katex-server"]
path = src/formatitko/katex-server
url = https://gitea.ks.matfyz.cz/KSP/formatitko-katex-server.git

0
src/formatitko/katex-server/index.mjs

7
src/formatitko/katex-server/package-lock.json

@ -1,15 +1,18 @@
{ {
"name": "ksp-katex-server", "name": "formatitko-katex-server",
"version": "1.0.0", "version": "1.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "ksp-katex-server", "name": "formatitko-katex-server",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"katex": "^0.16.3" "katex": "^0.16.3"
},
"bin": {
"formatitko-katex-server": "index.mjs"
} }
}, },
"node_modules/commander": { "node_modules/commander": {

5
src/formatitko/katex-server/package.json

@ -1,8 +1,11 @@
{ {
"name": "ksp-katex-server", "name": "formatitko-katex-server",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.mjs", "main": "index.mjs",
"bin": {
"formatitko-katex-server":"index.mjs"
},
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },

15
src/formatitko/katex-server/yarn.lock

@ -0,0 +1,15 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
commander@^8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==
katex@^0.16.3:
version "0.16.9"
resolved "https://registry.yarnpkg.com/katex/-/katex-0.16.9.tgz#bc62d8f7abfea6e181250f85a56e4ef292dcb1fa"
integrity sha512-fsSYjWS0EEOwvy81j3vRA8TEAhQhKiqO+FQaKWp0m39qwOzHVBgAUBIXWj1pB+O2W3fIpNa6Y9KSKCVbfPhyAQ==
dependencies:
commander "^8.3.0"

20
src/formatitko/katex.py

@ -3,6 +3,7 @@ import subprocess
import tempfile import tempfile
import json import json
import os import os
import shutil
class KatexError(Exception): class KatexError(Exception):
pass pass
@ -20,8 +21,10 @@ class KatexClient:
_socket_file: str _socket_file: str
_temp_dir: tempfile.TemporaryDirectory[str] _temp_dir: tempfile.TemporaryDirectory[str]
_connected: bool _connected: bool
_katex_server_path: str
def __init__(self, socket: str=None, connect: bool=True): def __init__(self, socket: str=None, connect: bool=True, katex_server_path: str=None):
self._katex_server_path = katex_server_path
if socket is not None: if socket is not None:
self._socket_file = socket self._socket_file = socket
else: else:
@ -38,20 +41,21 @@ class KatexClient:
self._temp_dir = tempfile.TemporaryDirectory(prefix='formatitko') self._temp_dir = tempfile.TemporaryDirectory(prefix='formatitko')
self._socket_file = self._temp_dir.name + "/katex-socket" self._socket_file = self._temp_dir.name + "/katex-socket"
if self._katex_server_path is None:
srcdir = os.path.dirname(os.path.realpath(__file__)) srcdir = os.path.dirname(os.path.realpath(__file__))
# Test if `node_modules` directory exists and if not, run `npm install` # Test if `node_modules` directory exists and if not, run `npm install`
if not os.path.isdir(srcdir + "/katex-server/node_modules"): if not os.path.isdir(srcdir + "/katex-server/node_modules"):
print("Installing node dependencies for the first time...") print("Installing node dependencies for the first time...")
try: npm = shutil.which("npm") or shutil.which("yarnpkg")
subprocess.run(["npm", "install"], cwd=srcdir+"/katex-server", check=True) if npm is None:
except subprocess.CalledProcessError as e:
if e.returncode == 127:
raise NPMNotFoundError("npm not found. Node.js is required to use KaTeX.") raise NPMNotFoundError("npm not found. Node.js is required to use KaTeX.")
else: subprocess.run([npm, "install"], cwd=srcdir+"/katex-server", check=True)
raise e
self._katex_server_path = srcdir + "/katex-server/index.mjs"
self._server_process = subprocess.Popen(["node", srcdir + "/katex-server/index.mjs", self._socket_file], stdout=subprocess.PIPE) self._server_process = subprocess.Popen(["node", self._katex_server_path, self._socket_file], stdout=subprocess.PIPE)
ok = self._server_process.stdout.readline() ok = self._server_process.stdout.readline()
if ok != b"OK\n": if ok != b"OK\n":

Loading…
Cancel
Save