3 # This tool will hack the doxygen generated documentation to fix link
4 # errors produced by doxygen.
6 # This works because most anchors doxygen generates are unique 32 char
7 # hash values. To speed up the operation, the tool will not check all
8 # the files itself but will let 'linklint' do the grunt
9 # work. fix-links.py reads the 'errorX.txt' and 'errorAX.txt' files
10 # generated by linklint. These files list links to missing files
11 # (errorX.html) and links to missing anchors
12 # (errorAX.html). fix-links.py works in the following way:
14 # - Build a complete index of all unique anchors found in any html
15 # file. The index will only include *unique* anchors. Anchors found
16 # multiple times are removed from the index
18 # - The index is extended to include all unique names of html files
20 # - Scn the linklint result and check the bad links against the
21 # index. If the file or anchor is found in the index, an accoringly
22 # corrected link is generated otherwise the link is removed.
24 # One additional twak is, that fix-links.py will successively remove
25 # initial 'g' charachters from anchors until the link is found in the
26 # index. Doxygen seems to create links with the wrong number of 'g'
27 # charachters in front sometimes.
29 import sys,os.path,fnmatch, HTMLParser, getopt, re, codecs
31 class HTMLFilter(HTMLParser.HTMLParser):
33 def __init__(self, out=None):
34 HTMLParser.HTMLParser.__init__(self)
39 def startCollect(self):
50 def handle_starttag(self,tag,attrs):
51 m = getattr(self,'_s_'+tag.upper(),None)
55 self.emit_starttag(tag,attrs)
57 def handle_endtag(self,tag):
58 m = getattr(self,'_e_'+tag.upper(),None)
64 def handle_data(self,data):
70 def handle_charref(self,name):
71 self.handle_data('&#%s;' % name)
73 def handle_entityref(self,name):
74 self.handle_data('&%s;' % name)
76 def emit_starttag(self,tag,attrs):
77 self.handle_data('<%s%s>' % (tag, "".join([' %s="%s"' % attr for attr in attrs])))
79 def emit_endtag(self,tag):
80 self.handle_data('</%s>' % tag)
85 def __init__(self, skipdirs = ('.svn',)):
87 self._skipdirs = skipdirs
90 sys.stderr.write("Building anchor index ")
92 for dirname, subdirs, files in os.walk('.'):
93 for d in self._skipdirs:
96 for f in fnmatch.filter(files,'*.html'):
98 path = os.path.normpath(os.path.join(dirname, f))
99 self._addAnchor(f, path)
100 self._extractAnchors(path)
101 sys.stderr.write(" Done.\n")
103 for k in self._anchors.keys():
104 if not self._anchors[k]:
107 sys.stderr.write("%d unique anchors in %d files (%d duplicates)\n"
108 % (len(self._anchors), nf, dups))
110 def _addAnchor(self, anchor, path):
111 if self._anchors.has_key(anchor):
112 self._anchors[anchor] = None
114 self._anchors[anchor] = path
115 if len(self._anchors) % 100 == 0:
116 sys.stderr.write('.')
118 def __getitem__(self, key):
119 return self._anchors.get(key)
121 class AnchorExtractor(HTMLFilter):
124 HTMLFilter.__init__(self)
127 def _s_A(self,attrs):
129 if attrs.has_key('name'):
130 self._anchors[attrs['name']] = None
133 return self._anchors.keys()
135 def _extractAnchors(self, f):
136 extractor = AnchorIndex.AnchorExtractor()
137 extractor.feed(file(f).read())
139 for anchor in extractor.anchors():
140 self._addAnchor(anchor, f)
142 TAG_RE = re.compile("<[^>]*>")
143 REF_RE = re.compile("&[^;]*;")
147 s = s.replace(" "," ").replace("\n"," ")
148 s = REF_RE.sub("?",s)
153 def __init__(self, skipdirs=('.svn',)):
154 self._index = AnchorIndex(skipdirs)
159 class LinkFilter(HTMLFilter):
161 def __init__(self, index, topdir, out):
162 HTMLFilter.__init__(self, out)
164 self._topdir = topdir
166 def _check(self, tag, linkAttr, attrs):
168 for i,(k,v) in enumerate(attrs):
175 if '#' in target : target, anchor = target.rsplit('#',1)
176 if '/' in target : tdir, target = target.rsplit('/', 1)
179 newTarget = self.anchorLookup(anchor)
180 if newTarget is None:
181 newTarget = self.fileLookup(target)
182 if newTarget and anchor:
183 newTarget = '%s#%s' % (newTarget, anchor)
185 attrs[ix] = (attrs[ix][0], '/'.join((self._topdir, newTarget)))
186 self.emit_starttag(tag, attrs)
188 def anchorLookup(self,anchor):
191 target = self._index[anchor]
193 target = '%s#%s' % (target, anchor)
194 elif anchor.startswith('g'):
200 def fileLookup(self,target):
201 return self._index[target]
203 def _s_A(self, attrs):
204 self._check('a', 'href', attrs)
206 def _s_AREA(self, attrs):
207 self._check('area', 'href', attrs)
210 data = codecs.open(path, "r", "utf-8").read()
211 filt = LinkFixer.LinkFilter(self._index,
212 ("../" * (len(os.path.split(path)[0].split("/"))))[:-1],
213 codecs.open(path, "w", "utf-8") )
217 (opts, args) = getopt.getopt(sys.argv[1:], "vs:")
219 sys.stderr.write("""Usage: fix-links.py [-s skip-dir]...
221 Check all links and try to find the correct target. If a target is
222 found, the link is changed accordingly, otherwise the link is removed.
224 To find anchors, fix-links.py generates a complete index of all
225 anchors defined in any HTML file in the current directory or some
226 subdirectory. The directories named 'skiped-dir' (at any level) will
227 not be scanned for '*.html' files.
231 skipdirs = [ val for opt, val in opts if opt == '-s' ]
232 verbose = ( '-v', '' ) in opts
234 fixer = LinkFixer(skipdirs)
237 for dirname, subdirs, files in os.walk('.'):
241 for f in fnmatch.filter(files,'*.html'):
242 path = os.path.normpath(os.path.join(dirname, f))