Last active
August 26, 2020 01:23
-
-
Save lierdakil/1b73eed72ef8fea7085d409fe9b7746d to your computer and use it in GitHub Desktop.
A simple script to make madoko play with pipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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