1#!/usr/bin/env python 2 3from __future__ import print_function 4import re 5import sys 6 7 8rc_script = re.compile(r"\s*(.*\S)?\s*") 9 10 11def parse_dict(filename): 12 links = {} 13 for line in open(filename, "r"): 14 m = re.match("^([^=]+)=([^\n]+)$", line) 15 if not m: 16 continue 17 name = m.group(1) 18 value = m.group(2) 19 20 # strip leading and trailing whitespace 21 m = rc_script.match(name) 22 if m: 23 name = m.group(1) 24 25 # skip special names 26 if name == "": 27 continue 28 if name == "\\": 29 continue 30 31 links[name] = '<a href="' + value + '" class="dg">' + name + "</a>" 32 return links 33 34 35links = parse_dict(sys.argv[1]) 36 37 38def translate(match): 39 return links[match.group(1)] 40 41 42# match for all names, with word boundaries \b 43rc = re.compile(r"\b(" + "|".join(map(re.escape, sorted(links, reverse=True))) + r")\b") 44 45for line in open(sys.argv[2], "r"): 46 if links: 47 line = rc.sub(translate, line) 48 print(line, end="") 49