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