Browse Source

Resolve #20: Na katex-server čekáme pomocí stdoutu.

pull/28/head
Jan Černohorský 1 year ago
parent
commit
410c2852b2
  1. 1
      src/formatitko/katex-server/index.mjs
  2. 16
      src/formatitko/katex.py

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

@ -41,6 +41,7 @@ if (!unixSocketPath) {
// This server listens on a Unix socket at /var/run/mysocket // This server listens on a Unix socket at /var/run/mysocket
var unixServer = net.createServer(handleClient); var unixServer = net.createServer(handleClient);
unixServer.listen(unixSocketPath); unixServer.listen(unixSocketPath);
console.log("OK")
function handleExit(signal) { function handleExit(signal) {
// unixServer.emit('close') // unixServer.emit('close')

16
src/formatitko/katex.py

@ -28,22 +28,16 @@ class KatexClient:
raise Exception("npm not found. Node.js is required to use KaTeX.") raise Exception("npm not found. Node.js is required to use KaTeX.")
else: else:
raise e raise e
self._server_process = subprocess.Popen(["node", srcdir + "/katex-server/index.mjs", self._socket_file])
self._server_process = subprocess.Popen(["node", srcdir + "/katex-server/index.mjs", self._socket_file], stdout=subprocess.PIPE)
self._client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Wait for the node program to create the socket file ok = self._server_process.stdout.readline()
while not os.path.exists(self._socket_file): if ok != b"OK\n":
time.sleep(0.01) raise Exception("Failed to connect to katex-server")
# Wait for the node program to start accepting connections
while True:
try:
self._client.connect(self._socket_file) self._client.connect(self._socket_file)
time.sleep(0.01)
except ConnectionRefusedError:
continue
break
def render(self, tex: str, options: Dict={}): def render(self, tex: str, options: Dict={}):
# Send formulas to translate # Send formulas to translate

Loading…
Cancel
Save