forked from kubernetes/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkchecker.py
executable file
·540 lines (447 loc) · 17.6 KB
/
linkchecker.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env python3
#
# This a link checker for Kubernetes documentation website.
#
# If the language to check is not English (`en`), we check if you are actually
# using the localized links. For example, if you checking
# `content/zh-cn/docs/foo/bar`, we check if the English version exists AND if the
# Chinese version exists as well. A checking record is produced if the link
# can use the localized version.
#
# Usage: linkchecker.py -h
#
# Cases handled:
#
# - [foo](#bar) : ignored currently
# + [foo](http://bar) : insecure links to external site
# + [foo](https://k8s.io/website/...) : hardcoded site domain name
#
# + [foo](/<lang>/docs/bar/...) : where <lang> is not 'en'
# + /<lang>/docs/bar : contains shortcode, so ignore, or
# + /<lang>/docs/bar : is a image link (ignore currently), or
# + /<lang>/docs/bar : points to shared (non-localized) page, or
# + /<lang>/docs/bar.md : exists for current lang, or
# + /<lang>/docs/bar/_index.md : exists for current lang, or
# + /<lang>/docs/bar/ : is a redirect entry, or
# + /<lang>/docs/bar : is something we don't understand, then ERR
#
# + [foo](/docs/bar/...)
# + /docs/bar : contains shortcode, so ignore, or
# + /docs/bar : is a image link (ignore currently), or
# + /docs/bar : points to a shared (non-localized) page, or
# + /docs/bar.md : exists for current lang, or
# + /docs/bar/_index.md : exists for current lang, or
# + /docs/bar : is a redirect entry, or
# + /docs/bar : is something we don't understand
#
# + [foo](<lang>/docs/bar/...) : leading slash missing for absolute path
#
# + [foo](docs/bar/...) : leading slash missing for absolute path
#
# + {{ < api-reference page="" anchor="" ... > }}
# + {{ < api-reference page="" > }}
import argparse
import glob
import os
import re
import sys
# These are the bad links that doesn't hurt, though good to fix
BAD_LINK_TYPES = {
"B01": {
"reason": "Using bad protocol",
"level": "WARNING",
},
"B02": {
"reason": "Link target is a redirect entry",
"level": "WARNING",
},
"B03": {
"reason": "Intra-site linkes should use relative path",
"level": "WARNING",
},
}
# Constants for colored printing
C_RED = "\033[31m"
C_GREEN = "\033[32m"
C_YELLOW = "\033[33m"
C_GRAY = "\033[90m"
C_CYAN = "\033[36m"
C_END = "\033[0m"
# Command line arguments shared across functions
ARGS = None
# Command line parser
PARSER = None
# Language as parsed from the file path
LANG = None
# Global result dictionary keyed by page examined
RESULT = {}
# Cached redirect entries
REDIRECTS = {}
# Cached anchors in target pages
ANCHORS = {}
def new_record(level, message, target):
"""Create new checking record.
:param level: Record severity level, one of 'INFO', 'WARNING' and 'ERROR'
:param message: Error message string
:param target: The link target in question
:returns: A string representation the checking result, may contain ASCII
coded terminal colors, or None if the record is suppressed.
"""
global ARGS
# Skip info when verbose
if ARGS.verbose is False and level == "INFO":
return None
result = None
if ARGS.no_color:
result = target + ": " + message
else:
target = C_GRAY + target + C_END
if level == "INFO":
result = target + ": " + C_GREEN + message + C_END
elif level == "WARNING":
result = target + ": " + C_YELLOW + message + C_END
else: # default to error
result = target + ": " + C_RED + message + C_END
return result
def dump_result():
"""Dump result to stdout."""
global RESULT, ARGS
for path, path_output in RESULT.items():
norm_path = os.path.normpath(path)
if ARGS.no_color:
print("File: " + norm_path)
else:
print(C_CYAN + "File: " + norm_path + C_END)
for p in path_output:
print(" "*4 + p)
return
def strip_comments(content):
"""Manual striping of comments from file content.
Many localized content pages contain original English content in comments.
These comments have to be stripped out before analyzing the links.
Doing this using regular expression is difficult. Even the grep tool is
not suitable for this use case.
NOTE: We strived to preserve line numbers when producing the resulted
text. This can be useful in future if we want to print out the line
numbers for bad links.
"""
result = []
in_comment = False
for line in content:
idx1 = line.find("<!--")
idx2 = line.find("-->")
if not in_comment:
# only care if new comment started
if idx1 < 0:
result.append(line)
continue
# single line comment
if idx2 > 0:
result.append(line[:idx1] + line[idx2+4:])
continue
result.append(line[:idx1])
in_comment = True
continue
# already in comment block
if idx2 < 0: # ignore whole line
result.append("")
continue
result.append(line[idx2+4:])
in_comment = False
return result
def normalize_filename(name, ftype="markdown"):
"""Guess the filename based on a link target.
This function only deals with regular files.
"""
if name.endswith("/"):
name = name[:-1]
if ftype == "markdown":
name += ".md"
else:
name += ".html"
return name
def check_file_exists(base, path, ftype="markdown"):
"""Check if the target file exists.
NOTE: We build a normalized path using 'base' and 'path' values. Suppose
the resulted path string is 'foo/bar', we check if 'foo/bar.md' exists,
AND we check if 'foo/bar/_index.md' exists.
:param base: The base directory to begin with
:param path: The link target which is a relative path string
:returns: A boolean indicating whether the target file exists.
"""
# NOTE: anchor is ignored, can be a todo item
parts = path.split("#")
fn = normalize_filename(parts[0], ftype=ftype)
target = base + fn
if os.path.isfile(target):
return True
dir_name = base + parts[0]
if os.path.isdir(dir_name):
if os.path.isfile(dir_name + "/_index.md"):
return True
if os.path.isfile(dir_name + "/_index.html"):
return True
# /docs/contribute/style/hugo-shortcodes/ has this
if os.path.isfile(dir_name + "/index.md"):
return True
return False
def get_redirect(path):
"""Check if the path exists in the redirect database.
NOTE: We do NOT check if the redirect target is there or not. We do an
**exact** matching for redirection entries.
:returns: The redirect target if any, or None if not found.
"""
global REDIRECTS
def _check_redirect(t):
for key, value in REDIRECTS.items():
if key == t: # EXACT MATCH
return value
return None
# NOTE: anchor is ignored, can be a future todo
parts = path.split("#")
target = parts[0]
if not target.endswith("/"):
target += "/"
new_target = _check_redirect(target)
last_target = new_target
while new_target:
new_target = _check_redirect(new_target)
if new_target is None:
break
last_target = new_target
return last_target
def check_target(page, anchor, target):
"""Check a link from anchor to target on provided page.
:param page: Currently not used. Passed here in case we want to check the
in-page links in the future.
:param anchor: Anchor string from the content page. This is provided to
help handle cases where target is empty.
:param target: The link target string to check
:returns: A checking record (string) if errors found, or None if we can
find the target link.
"""
target = target.strip()
# B01: bad protocol
if target.startswith("http://"):
return new_record("WARNING", "Use HTTPS rather than HTTP", target)
# full link
if target.startswith("https://"):
# B03: self link, should revise to relative path
if (target.startswith("https://k8s.io/docs") or
target.startswith("https://kubernetes.io/docs")):
return new_record("ERROR", "Should use relative paths", target)
# external link, skip
return new_record("INFO", "External link, skipped", target)
# in-page link
# TODO: check if the target anchor does exists
if target.startswith("#"):
return new_record("INFO", "In-page link, skipped", target)
# Link has shortcode
if target.find("{{") > 0:
return new_record("INFO", "Link has shortcode, skipped", target)
# TODO: check links to examples
if target.startswith("/examples/"):
return new_record("WARNING", "Examples link, skipped", target)
# it is an embedded image
# TODO: an image might get translated as well
if target.endswith(".png") or target.endswith(".svg"):
return new_record("INFO", "Link to image, skipped", target)
# link to English or localized page
if (target.startswith("/docs/") or
target.startswith("/" + LANG + "/docs/")):
# target is shared reference (kubectl or kubernetes-api?
if (target.find("/docs/reference/generated/kubectl/") >= 0 or
target.find("/docs/reference/generated/kubernetes-api/") >= 0):
if check_file_exists(ROOT + "/static", target, "html"):
return None
return new_record("ERROR", "Missing shared reference", target)
# target is a markdown (.md) or a "<dir>/_index.md"?
if target.startswith("/docs/"):
base = os.path.join(ROOT, "content", "en")
else:
# localized target
base = os.path.join(ROOT, "content")
ok = check_file_exists(base, target)
if ok:
# We do't do additional checks for English site even if it has
# links to a non-English page
if LANG == "en":
return None
# If we are already checking localized link, fine
if target.startswith("/" + LANG + "/docs/"):
return None
# additional check for localization even if English target exists
base = os.path.join(ROOT, "content", LANG)
found = check_file_exists(base, target)
if not found:
# Still to be translated
return None
msg = ("Localized page detected, please append '/%s' to the target"
% LANG)
return new_record("ERROR", msg, target)
# taget might be a redirect entry
real_target = get_redirect(target)
if real_target:
msg = ("Link using redirect records, should use %s instead" %
real_target)
return new_record("WARNING", msg, target)
return new_record("ERROR", "Missing link for [%s]" % anchor, target)
# absolute link missing leading slash
if (target.startswith("docs/") or target.startswith(LANG + "/docs/")):
return new_record("ERROR", "Missing leading slash. Try \"/%s\"" %
target, target)
msg = "Link may be wrong for the anchor [%s]" % anchor
return new_record("WARNING", msg, target)
def check_anchor(target, anchor):
"""Check if an anchor is defined in the target page
:param target: The target page to check
:param anchor: Anchor string to find in the target page
"""
if target not in ANCHORS:
try:
with open(target, "r") as f:
data = f.readlines()
except Exception as ex:
print("[Error] failed in reading markdown file: " + str(ex))
return
content = "\n".join(strip_comments(data))
anchor_pattern1 = r"<a name=\"(.*?)\""
regex1 = re.compile(anchor_pattern1)
anchor_pattern2 = r"{#(.*?)}"
regex2 = re.compile(anchor_pattern2)
ANCHORS[target] = regex1.findall(content) + regex2.findall(content)
return anchor in ANCHORS[target]
def check_apiref_target(target, anchor):
"""Check a link to an API reference page.
:param target: The link target string to check
:param anchor: Anchor string from the content page
"""
base = os.path.join(ROOT, "content", "en", "docs", "reference",
"kubernetes-api")
ok = check_file_exists(base + "/", target)
if not ok:
return new_record("ERROR", "API reference page not found", target)
if anchor is None:
return
target_page = os.path.join(base, target)+".md"
if not check_anchor(target_page, anchor):
return new_record("ERROR", "Anchor not found in API reference page",
target+"#"+anchor)
def validate_links(page, in_place_edit):
"""Find and validate links on a content page.
The checking records are consolidated into the global variable RESULT.
"""
try:
with open(page, "r") as f:
data = f.readlines()
except Exception as ex:
print("[Error] failed in reading markdown file: " + str(ex))
return
content = "\n".join(strip_comments(data))
# Single results: searches for pattern: []()
link_pattern = r"\[([`/\w\s\n]*)\]\(([^\)]*)\)"
regex = re.compile(link_pattern)
matches = regex.findall(content)
records = []
target_records = []
for m in matches:
r = check_target(page, m[0], m[1])
if r:
records.append(r)
target_records.append(m[1])
# if multiple records are the same they need not be checked repeatedly
# remove paths that are not relative too
target_records = {item for item in target_records
if not item.startswith("http") and
not item.startswith(f"/{LANG}")}
# English-language pages don't have "en" in their path
if in_place_edit and target_records and LANG != "en":
updated_data = []
for line in data:
if any(rec in line for rec in target_records):
for rec in target_records:
line = line.replace(
f"({rec})",
# assumes unlocalized links are in "/docs/..." format
f"(/{LANG}{rec})")
updated_data.append(line)
with open(page, "w") as f:
for line in updated_data:
f.write(line)
# searches for pattern: {{< api-reference page="" anchor=""
apiref_re = r"{{ *< *api-reference page=\"([^\"]*?)\" *anchor=\"(.*?)\""
regex = re.compile(apiref_re)
matches = regex.findall(content)
for m in matches:
r = check_apiref_target(m[0], m[1])
if r:
records.append(r)
# searches for pattern: {{< api-reference page=""
apiref_re = r"{{ *< *api-reference page=\"([^\"]*?)\""
regex = re.compile(apiref_re)
matches = regex.findall(content)
for m in matches:
r = check_apiref_target(m, None)
if r:
records.append(r)
if len(records):
RESULT[page] = records
def parse_arguments():
"""Argument parser.
Result is returned and saved into global variable ARGS.
"""
global PARSER
PARSER = argparse.ArgumentParser(description="Links checker for docs.")
PARSER.add_argument("-v", dest="verbose", action="store_true",
help="switch on verbose level")
PARSER.add_argument("-n", "--no-color", action="store_true",
help="Suppress colored printing.")
PARSER.add_argument("-f", dest="filter", default="content/en/docs/**/*.md",
metavar="<FILTER>",
help=("File pattern to scan. "
"(default='content/en/docs/**/*.md')"))
PARSER.add_argument("-w", dest="in_place_edit", action="store_true",
help="[EXPERIMENTAL] Turns on in-place replacement "
"for localized content.")
return PARSER.parse_args()
def main():
"""The main entry of the program."""
global ARGS, ROOT, REDIRECTS, PARSER, LANG
ARGS = parse_arguments()
ROOT = os.path.join(os.path.dirname(__file__), '..')
print(ARGS.filter)
parts = ARGS.filter.split("/", 2)
if len(parts) != 3 or parts[0] != "content":
print("ERROR:\nPlease specify file pattern in the format "
"'content/<lang>/<path-pattern>', for example:\n"
"'content/zh-cn/docs/concepts/**/*.md'\n")
PARSER.print_help()
sys.exit(-1)
LANG = parts[1]
# read redirects data
redirects_fn = os.path.join(ROOT, "static", "_redirects")
try:
with open(redirects_fn, "r") as f:
data = f.readlines()
for item in data:
parts = item.split()
# There are entries without 301 specified
if len(parts) < 2:
continue
entry = parts[0]
# There are some entries not ended with "/"
if entry.endswith("/"):
REDIRECTS[entry] = parts[1]
else:
REDIRECTS[entry + "/"] = parts[1]
except Exception as ex:
print("[Error] failed in reading redirects file: " + str(ex))
return
folders = [f for f in glob.glob(ARGS.filter, recursive=True)]
for page in folders:
validate_links(page, ARGS.in_place_edit)
dump_result()
# Done
print("Completed link validation.")
if __name__ == '__main__':
sys.exit(main())