#!/usr/bin/env python3 import argparse import chardet import os.path import re import subprocess import sys import tempfile URL_REGEX = '(https?://\S*)' ANCHOR_SUB = '\\1' HTML_PARAGRAPH = '
' BROWSER_CMD = ['lynx', '-force_html', '-hiddenlinks=merge', '-display_charset=utf-8', '-dump'] def main(): parser = argparse.ArgumentParser() parser.add_argument('input_file') args = parser.parse_args() with open(args.input_file, 'rb') as input_file: text = input_file.read() with tempfile.NamedTemporaryFile(mode='rb+') as temp_file: encoding = chardet.detect(text)['encoding'] temp_file.write(bytes(str.encode(re.sub( URL_REGEX, ANCHOR_SUB, text.decode(encoding).replace('\n\n', HTML_PARAGRAPH)), encoding))) BROWSER_CMD.append(str(temp_file.name)) temp_file.seek(0) sys.stdout.write(subprocess.check_output(BROWSER_CMD, universal_newlines=True)) if __name__ == '__main__': main()