I have the below python script, which accepts text input, format it (to html) and produces output.
It works ok when piping a static text input. However, it freezes (or probably waits for some kind of EOF) when the input is a stream of text.
I would like it to stream the output, instead of waiting the text to finish.
EDIT: It seems that I wasn't patient enough, and it waits on some buffer to fill before dumping the output. Any idea on how to dump the output for every line
Thanks
#!/usr/bin/env python
"""
Convert a piped in text input to html, while replacing file/path names with URLs
"""
import sys
import os
import re
def file_html(field):
field = field.group()
if os.path.isfile(field) or os.path.isdir(field):
return """<a href="file://{0}" target="_blank">{0} </a>""".format(field)
else:
return field
if __name__ == "__main__":
for line in sys.stdin:
line = '<p><tt>' + re.sub('/[^\s]*', file_html, line) + '</tt></p>'
sys.stdout.write(line)