in scripts/linkchecker.py [0:0]
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("/" + ARGS.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 ARGS.lang == "en":
return None
# If we are already checking localized link, fine
if target.startswith("/" + ARGS.lang + "/docs/"):
return None
# additional check for localization even if English target exists
base = os.path.join(ROOT, "content", ARGS.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"
% ARGS.lang)
return new_record("ERROR", "Link not using localized page", 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)
msg = "Link may be wrong for the anchor [%s]" % anchor
return new_record("WARNING", msg, target)