1#!/usr/bin/env python 2 3from __future__ import print_function 4 5import fileinput 6import re 7import sys 8 9refs = {} 10complete_file = "" 11 12for line in open(sys.argv[1], 'r'): 13 complete_file += line 14 15for m in re.findall('\[\[(.+)\]\]\n=+ ([^\n]+)', complete_file): 16 ref, title = m 17 refs["<<" + ref + ">>"] = "<<" + ref + ", " + title + ">>" 18 19def translate(match): 20 try: 21 return refs[match.group(0)] 22 except KeyError: 23 return "" 24 25rc = re.compile('|'.join(map(re.escape, sorted(refs, reverse=True)))) 26for line in open(sys.argv[1], 'r'): 27 print(rc.sub(translate, line), end='') 28