35
35
# + /docs/bar : is a redirect entry, or
36
36
# + /docs/bar : is something we don't understand
37
37
#
38
+ # + {{ < api-reference page="" anchor="" ... > }}
39
+ # + {{ < api-reference page="" > }}
38
40
39
41
import argparse
40
42
import glob
72
74
RESULT = {}
73
75
# Cached redirect entries
74
76
REDIRECTS = {}
75
-
77
+ # Cached anchors in target pages
78
+ ANCHORS = {}
76
79
77
80
def new_record (level , message , target ):
78
81
"""Create new checking record.
@@ -330,6 +333,44 @@ def check_target(page, anchor, target):
330
333
msg = "Link may be wrong for the anchor [%s]" % anchor
331
334
return new_record ("WARNING" , msg , target )
332
335
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 )
333
374
334
375
def validate_links (page ):
335
376
"""Find and validate links on a content page.
@@ -355,6 +396,27 @@ def validate_links(page):
355
396
r = check_target (page , m [0 ], m [1 ])
356
397
if r :
357
398
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
+
358
420
if len (records ):
359
421
RESULT [page ] = records
360
422
0 commit comments