Skip to content

Commit 216b34a

Browse files
committed
Update script to check api-reference links
1 parent 787e703 commit 216b34a

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

scripts/linkchecker.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
# + /docs/bar : is a redirect entry, or
3636
# + /docs/bar : is something we don't understand
3737
#
38+
# + {{ < api-reference page="" anchor="" ... > }}
39+
# + {{ < api-reference page="" > }}
3840

3941
import argparse
4042
import glob
@@ -72,7 +74,8 @@
7274
RESULT = {}
7375
# Cached redirect entries
7476
REDIRECTS = {}
75-
77+
# Cached anchors in target pages
78+
ANCHORS = {}
7679

7780
def new_record(level, message, target):
7881
"""Create new checking record.
@@ -330,6 +333,44 @@ def check_target(page, anchor, target):
330333
msg = "Link may be wrong for the anchor [%s]" % anchor
331334
return new_record("WARNING", msg, target)
332335

336+
def check_anchor(target_page, anchor):
337+
"""Check if an anchor is defined in the target page
338+
339+
:param target_page: The target page to check
340+
:param anchor: Anchor string to find in the target page
341+
"""
342+
if target_page not in ANCHORS:
343+
try:
344+
with open(target_page, "r") as f:
345+
data = f.readlines()
346+
except Exception as ex:
347+
print("[Error] failed in reading markdown file: " + str(ex))
348+
return
349+
content = "\n".join(strip_comments(data))
350+
anchor_pattern1 = r"<a name=\"(.*?)\""
351+
regex1 = re.compile(anchor_pattern1)
352+
anchor_pattern2 = r"{#(.*?)}"
353+
regex2 = re.compile(anchor_pattern2)
354+
ANCHORS[target_page] = regex1.findall(content) + regex2.findall(content)
355+
return anchor in ANCHORS[target_page]
356+
357+
def check_apiref_target(target, anchor):
358+
"""Check a link to an API reference page.
359+
360+
:param target: The link target string to check
361+
:param anchor: Anchor string from the content page
362+
"""
363+
base = os.path.join(ROOT, "content", "en", "docs", "reference", "kubernetes-api")
364+
ok = check_file_exists(base + "/", target)
365+
if not ok:
366+
return new_record("ERROR", "API reference page not found", target)
367+
368+
if anchor is None:
369+
return
370+
371+
target_page = os.path.join(base, target)+".md"
372+
if not check_anchor(target_page, anchor):
373+
return new_record("ERROR", "Anchor not found in API reference page", target+"#"+anchor)
333374

334375
def validate_links(page):
335376
"""Find and validate links on a content page.
@@ -355,6 +396,27 @@ def validate_links(page):
355396
r = check_target(page, m[0], m[1])
356397
if r:
357398
records.append(r)
399+
400+
# searches for pattern: {{< api-reference page="" anchor=""
401+
apiref_pattern = r"{{ *< *api-reference page=\"([^\"]*?)\" *anchor=\"(.*?)\""
402+
regex = re.compile(apiref_pattern)
403+
404+
matches = regex.findall(content)
405+
for m in matches:
406+
r = check_apiref_target(m[0], m[1])
407+
if r:
408+
records.append(r)
409+
410+
# searches for pattern: {{< api-reference page=""
411+
apiref_pattern = r"{{ *< *api-reference page=\"([^\"]*?)\""
412+
regex = re.compile(apiref_pattern)
413+
414+
matches = regex.findall(content)
415+
for m in matches:
416+
r = check_apiref_target(m, None)
417+
if r:
418+
records.append(r)
419+
358420
if len(records):
359421
RESULT[page] = records
360422

0 commit comments

Comments
 (0)