diff --git a/.gitmodules b/.gitmodules index 611d4fc..4f484b8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "ucwmac"] path = ucwmac 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 diff --git a/src/formatitko/katex-server/index.mjs b/src/formatitko/katex-server/index.mjs old mode 100644 new mode 100755 diff --git a/src/formatitko/katex-server/package-lock.json b/src/formatitko/katex-server/package-lock.json index 3b5bee3..05246dc 100644 --- a/src/formatitko/katex-server/package-lock.json +++ b/src/formatitko/katex-server/package-lock.json @@ -1,15 +1,18 @@ { - "name": "ksp-katex-server", + "name": "formatitko-katex-server", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "ksp-katex-server", + "name": "formatitko-katex-server", "version": "1.0.0", "license": "ISC", "dependencies": { "katex": "^0.16.3" + }, + "bin": { + "formatitko-katex-server": "index.mjs" } }, "node_modules/commander": { diff --git a/src/formatitko/katex-server/package.json b/src/formatitko/katex-server/package.json index 3d68121..adbb8ed 100644 --- a/src/formatitko/katex-server/package.json +++ b/src/formatitko/katex-server/package.json @@ -1,8 +1,11 @@ { - "name": "ksp-katex-server", + "name": "formatitko-katex-server", "version": "1.0.0", "description": "", "main": "index.mjs", + "bin": { + "formatitko-katex-server":"index.mjs" + }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/src/formatitko/katex-server/yarn.lock b/src/formatitko/katex-server/yarn.lock new file mode 100644 index 0000000..2760613 --- /dev/null +++ b/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" diff --git a/src/formatitko/katex.py b/src/formatitko/katex.py index 39e521f..f81db24 100644 --- a/src/formatitko/katex.py +++ b/src/formatitko/katex.py @@ -3,6 +3,7 @@ import subprocess import tempfile import json import os +import shutil class KatexError(Exception): pass @@ -20,8 +21,10 @@ class KatexClient: _socket_file: str _temp_dir: tempfile.TemporaryDirectory[str] _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: self._socket_file = socket else: @@ -38,20 +41,21 @@ class KatexClient: self._temp_dir = tempfile.TemporaryDirectory(prefix='formatitko') self._socket_file = self._temp_dir.name + "/katex-socket" - srcdir = os.path.dirname(os.path.realpath(__file__)) + if self._katex_server_path is None: + + srcdir = os.path.dirname(os.path.realpath(__file__)) - # Test if `node_modules` directory exists and if not, run `npm install` - if not os.path.isdir(srcdir + "/katex-server/node_modules"): - print("Installing node dependencies for the first time...") - try: - subprocess.run(["npm", "install"], cwd=srcdir+"/katex-server", check=True) - except subprocess.CalledProcessError as e: - if e.returncode == 127: + # Test if `node_modules` directory exists and if not, run `npm install` + if not os.path.isdir(srcdir + "/katex-server/node_modules"): + print("Installing node dependencies for the first time...") + npm = shutil.which("npm") or shutil.which("yarnpkg") + if npm is None: raise NPMNotFoundError("npm not found. Node.js is required to use KaTeX.") - else: - raise e + subprocess.run([npm, "install"], cwd=srcdir+"/katex-server", check=True) + + 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() if ok != b"OK\n":