forked from InsightSoftwareConsortium/ITKWikiExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertDoxygenLink
179 lines (163 loc) · 6.24 KB
/
InsertDoxygenLink
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
import sys, os, re, glob, cStringIO
def setLinkInFile( fname, link, comment ):
# print >> sys.stderr, "Processing", fname
bname = os.path.basename(fname)
bname = bname[3:-2] # remove itk prefix and .h suffix
f = open( fname, "r" )
out = cStringIO.StringIO()
# load everything in memory
fcontent = f.read()
f.close()
# now parse all the doxygen fields
last = 0
for m in re.finditer(r"^( *)/\*\* *(.*?)\*/", fcontent, re.DOTALL | re.MULTILINE):
# write what is before the doxygen field to the output
out.write(fcontent[last:m.start(0)])
last = m.end(0)
# we don't care about doxygen fields not about a class
m2 = re.search(r"\class +([^ ]*)", m.group(2))
if m2 and m2.group(1).strip() == bname:
indent = m.group(1)
# drop the space and the * to make the processing easier
dcontent = m.group(2)
emptyFirstLine = dcontent.splitlines()[0].strip() == ""
dcontent = re.compile(r"^ *\* ?", re.MULTILINE).sub("",dcontent).rstrip()
dcontent2 = setLinkInDoxygenComment(dcontent, link, comment)
dcontent2 = re.sub("\n\n+", "\n\n", dcontent2)
# if emptyFirstLine:
# dcontent2 = "\n"+dcontent2
dcontent2 = "/** "+dcontent2.replace("\n", "\n"+indent+" * ")+"\n"+indent+" */"
dcontent2 = re.compile(r" +$", re.MULTILINE).sub("", dcontent2)
out.write(dcontent2)
else:
# not a class
out.write(m.group(0))
out.write(fcontent[last:])
# we can save the content to the original file
f = open( fname, "w" )
f.write( out.getvalue() )
f.close()
def setLinkInDoxygenComment(dcontent, link, comment):
m = re.search(r"(^.*\\wiki(?:\n| ))(.+)(\\endwiki.*$)", dcontent, re.DOTALL)
if m:
# the wiki-endwiki is already there - good
return m.group(1)+setLinkInWikiBlock(m.group(2), link, comment)+m.group(3)
else:
# get the \wikiexample which may be there, so we can put them in the new wiki block
examples = "\n".join(re.findall(r"\\wikiexample{.+?,.+?}", dcontent))
if examples != "":
examples += "\n"
cleanedDContent = re.sub(r" *\\wikiexample{.+?,.+?} *", "", dcontent)
return cleanedDContent+"\n\n\\wiki\n"+setLinkInWikiBlock(examples, link, comment)+"\\endwiki"
def setLinkInWikiBlock(wikiBlock, link, comment):
stag = r"\wikiexample{"+link+","
tag = r"\wikiexample{%s,%s}" % (link, comment)
if tag in wikiBlock:
return wikiBlock
elif stag in wikiBlock:
# comment has changed
return re.sub(r"\\wikiexample{"+re.escape(link)+",.+?}", tag, wikiBlock)
else:
return wikiBlock+tag+"\n"
def checkLink( fname, link, comment ):
# print >> sys.stderr, "Checking", fname
bname = os.path.basename(fname)
bname = bname[3:-2] # remove itk prefix and .h suffix
f = open( fname, "r" )
# load everything in memory
fcontent = f.read()
f.close()
# now parse all the doxygen fields
ret = 0
for m in re.finditer(r"/\*\*(.*?)\*/", fcontent, re.DOTALL):
dcontent = m.group(1)
# we don't care about doxygen fields not about a class
if r"\class" in dcontent and dcontent != " \class classname ":
m2 = re.search(r"(^.*\\wiki(?:\n| ))(.+)(\\endwiki.*$)", dcontent, re.DOTALL)
if m2:
wikiBlock = m2.group(2)
# do we have a line with the expected content?
tag = r"\wikiexample{%s,%s}" % (link, comment)
if not tag in wikiBlock:
# get class name and the line for debug output
cname = re.search(r"\class +([^ ]*)", dcontent).group(1).strip()
if cname == bname:
line = len(fcontent[:m.start(1)].splitlines())
print >> sys.stderr, r'%s:%s: error: "%s" not set in class %s.' % (fname, line, tag, cname)
ret = 1
else:
# get class name and the line for debug output
cname = re.search(r"\class +([^ ]*)", dcontent).group(1).strip()
if cname == bname:
line = len(fcontent[:m.start(1)].splitlines())
print >> sys.stderr, r'%s:%s: error: no wiki block in class %s.' % (fname, line, cname)
ret = 1
return ret
def getLinksAndComments():
res = {}
import mwclient
site = mwclient.Site('itk.org', '/Wiki/')
page = site.Pages['ITK/Examples']
content = page.edit()
for l in content.splitlines():
if re.search(r'{{ITKDoxygenURL\|([^}]+)}}', l):
m = re.match(r"\| *\[\[.*ITK/Examples/(.+)\|(.+)\]\] *\|\|", l)
link = m.group(1).strip()
comment = m.group(2).strip()
comment = comment.replace(",", r"\,")
classes = re.findall(r'{{ITKDoxygenURL\|([^}]+)}}', l)
if not 'Broken/' in link and not 'WishList/' in link:
for c in classes:
ps = res.get(c , [])
ps.append((link, comment))
res[c] = ps
return res
def main():
# first arg is the command
command = sys.argv[1]
if command == "set":
link = sys.argv[2]
comment = sys.argv[3]
files = sys.argv[4:]
for fname in files:
setLinkInFile(fname, link, comment)
return 0
elif command == "check":
link = sys.argv[2]
comment = sys.argv[3]
files = sys.argv[4:]
ret = 0
for fname in files:
ret = max( ret, checkLink(fname, link, comment) )
return ret
elif command == "massive-check":
lcs = getLinksAndComments()
cmm = os.path.abspath(sys.argv[2]+"/*/*/*/itk-module.cmake")
ret = 0
for fname in glob.glob(cmm):
dname = os.path.dirname(fname)
for fname2 in glob.glob(dname+"/include/*.h"):
bname2 = os.path.basename(fname2)
bname2 = bname2[3:-2] # remove itk prefix and .h suffix
if lcs.has_key(bname2):
for link, comment in lcs[bname2]:
ret = max( ret, checkLink(fname2, link, comment) )
return ret
elif command == "massive-set":
lcs = getLinksAndComments()
cmm = os.path.abspath(sys.argv[2]+"/*/*/*/itk-module.cmake")
for fname in glob.glob(cmm):
dname = os.path.dirname(fname)
for fname2 in glob.glob(dname+"/include/*.h"):
bname2 = os.path.basename(fname2)
bname2 = bname2[3:-2] # remove itk prefix and .h suffix
if lcs.has_key(bname2):
for link, comment in lcs[bname2]:
setLinkInFile(fname2, link, comment)
else:
print >> sys.stderr, "Unknown command", command
return 1
if __name__ == "__main__":
ret = main()
sys.exit(ret)