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