-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathfind-broken-xrefs.py
executable file
·55 lines (43 loc) · 1.5 KB
/
find-broken-xrefs.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
#!/usr/bin/python
# This is a very crude script to find broken xrefs.
# Usage: <script name> <directory to search>
import re
import sys
import os
from fnmatch import fnmatch
root = sys.argv[1]
pattern = "*.asciidoc"
def findFiles(root):
allfiles = []
for path, subdirs, files in os.walk(root):
for name in files:
if fnmatch(name, pattern):
filename = os.path.join(path, name)
allfiles.append(filename)
return allfiles
def inspectFile(filename):
with open(filename) as fin:
content = fin.read()
xrefre = re.compile(r"<<([^#,>]+)#([^,>]*),([^>]+)>>")
xrefs = xrefre.findall(content)
path = filename[0:filename.rfind("/")]
fileHasErrors = False
for xref in xrefs:
(target, anchor, caption) = xref
if target.find("{articles}") != -1:
trueTarget = target.replace("{articles}", "articles/")
else:
trueTarget = path + "/" + target
trueTarget = trueTarget
if not os.path.exists(trueTarget + ".asciidoc") and \
not os.path.exists(trueTarget + "/index.asciidoc"):
if not fileHasErrors:
print "--------------\nFile has errors: %s" % (filename)
fileHasErrors = True
else:
print
print ("INVALID XREF: <<%s#%s,%s>>" % (target,anchor,caption))
print "File does not exist: %s" % (trueTarget)
files = findFiles(root)
for filename in files:
inspectFile(filename)