Skip to content

Instantly share code, notes, and snippets.

@lierdakil
Last active August 26, 2020 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lierdakil/1b73eed72ef8fea7085d409fe9b7746d to your computer and use it in GitHub Desktop.
Save lierdakil/1b73eed72ef8fea7085d409fe9b7746d to your computer and use it in GitHub Desktop.
A simple script to make madoko play with pipes
#!/usr/bin/env python
import tempfile
import sys
import subprocess
import os
import errno
# Make temp. file for input
inp = tempfile.NamedTemporaryFile(suffix='.mdk',mode='wb',dir=os.getcwd())
# Make temp. dir for output
outdir = tempfile.TemporaryDirectory()
# Put stdin into a file
inp.write(sys.stdin.buffer.read())
# Run madoko; Uses API introduced in Python 3.5
subprocess.run(['madoko', '--no-tex', '--no-pdf', f'--odir={outdir.name}', inp.name])
# Construct path to madoko's output HTML file
outfile=os.path.join(outdir.name,os.path.splitext(os.path.basename(inp.name))[0]+'.html')
# Now just ouptut the result:
with open(outfile, mode='rb') as f:
# Node's child_process stdout is non-blocking, but Python expects blocking stdout
# Hence we need this song and dance
s = f.read()
len_s = len(s)
written = 0
while written < len_s:
try:
written = written + os.write(sys.stdout.fileno(), s[written:])
except OSError as e:
if e.errno == errno.EAGAIN:
pass
else:
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment