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