forked from adventuregamestudio/ags-manual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checklinks
executable file
·96 lines (77 loc) · 1.81 KB
/
checklinks
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
#!/usr/bin/awk -f
function get_page_name(path) {
split_count = split(path, parts, "/")
filename = parts[split_count]
split_count = split(filename, parts, ".")
if (split_count == 1)
return filename
ext_length = length(parts[split_count]) + 1
return substr(filename, 1, length(filename) - ext_length)
}
function make_link(page, anchor) {
link = anchor
# remove leading spaces and hashes
gsub("^ *##* *", "", link)
# remove punctuation
gsub("[\.\?\(\)\+\*!,#]", "", link)
# convert spaces to dashes
gsub(" ", "-", link)
return sprintf("%s#%s", page, tolower(link))
}
BEGIN {
count = 0
bad = 0
good = 0
for (i = 0; i < ARGC; i++) {
pagename = get_page_name(ARGV[i])
# ignore the symlinked index file
if (pagename == "index") {
ARGV[i] = null
continue
}
targets[pagename] = null
count ++
while ((getline line < ARGV[i]) > 0) {
if (match(line, "^ *##.*") != 0) {
link = make_link(pagename, substr(line, RSTART, RLENGTH))
if (!(link in targets)) {
targets[link] = null
count ++
}
}
}
close(ARGV[i])
}
}
{
if (FNR == 1)
pagename = get_page_name(FILENAME)
if (match($0, "\\[[^\\(\\)\\]]*\\]\\([^\\[\\]\\)]*\\)") != 0) {
line = substr($0, RSTART, RLENGTH)
start = index(line, "(") + 1
end = index(line, ")")
link = substr(line, start, end - start)
# ignore links to images
if (tolower(link) ~ "\\.(jpg|gif|png)$")
next
# ignore actual web links
if (tolower(link) ~ "^https?://")
next
if (index(link, "#") == 1)
link = pagename link
if (link in targets) {
good ++
} else {
bad ++
printf("Bad link %s on page %s (line %d)\n", link, FILENAME, FNR)
}
}
}
END {
printf("\nFound %d pages/anchors\n", count)
printf("Total links: %d\n", good + bad)
printf("Bad links: %d\n", bad)
if (bad > 0)
exit 1
exit 0
}