-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatch_qthelp.py
94 lines (71 loc) · 2.57 KB
/
patch_qthelp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
import re
import os
import os.path
def fix_qhp(cwd):
keyword = re.compile('^(\s*)\<keyword name="(.*\(C\+\+.+\))" ref="\(u\'(.+)\', u\'(.+)\'\)"\/>\s*$')
refclean = re.compile('[^0-9a-zA-Z._\-#/\\\\~\[\]\=]')
refrepl = '_'
files = {} # files to fix
def repl(m):
indent, name, id, ref = m.groups()
if '#' in ref:
filename, refhash = ref.split('#', 1)
refhash = refhash.replace('&', '&')
cleanhash = refclean.sub('_', refhash)
ref = '#'.join([filename, cleanhash])
if filename in files:
files[filename] |= {(refhash, cleanhash)}
else:
files[filename] = {(refhash, cleanhash)}
id = id.replace('<', '<')
id = id.replace('>', '>')
ret = '{0}<keyword name="{1}" id="cv::{2}" ref="{3}"/>\n'.format(indent, name, id, ref)
ret += '{0}<keyword name="{1}" id="{2}" ref="{3}"/>\n'.format(indent, name, id, ref)
return ret
ret = ''
qhp_path = os.path.join(cwd, 'OpenCV.qhp')
with open(qhp_path, 'r') as f:
for line in f:
if '<keyword ' in line:
line, n = keyword.subn(repl, line)
if not n:
continue
ret += line
with open(qhp_path, 'w') as f:
f.write(ret)
for fn, fixset in files.items():
fn = os.path.join(cwd, fn)
tfn = fn + '.tmp'
with open(fn, 'r') as f:
with open(tfn, 'w') as out:
data = f.read()
for refhash, cleanhash in fixset:
data = data.replace(refhash, cleanhash)
out.write(data)
os.rename(tfn, fn)
def merge_css(src, dst, skip):
if not os.path.exists(src):
return
with open(src, 'a') as fsrc:
with open(dst, 'r') as fdst:
# skip header and @import url('basic.css');
for x in range(skip):
fdst.readline()
fsrc.write('\n')
fsrc.write(fdst.read())
os.rename(src, dst)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('targets',
metavar = 'path',
nargs = 1,
help = 'path to generated qthelp docs')
options = parser.parse_args()
target = options.targets[0]
docsdir = os.path.abspath(target)
if os.path.exists(docsdir):
bcss, dcss = [os.path.join(docsdir, '_static', f) for f in ['basic.css', 'default.css']]
merge_css(bcss, dcss, skip=6)
fix_qhp(docsdir)