From 216b34a3d10ad82c1b76eb75db29aa9399da5322 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Thu, 8 Jul 2021 18:11:57 +0200 Subject: [PATCH] Update script to check api-reference links --- scripts/linkchecker.py | 64 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/scripts/linkchecker.py b/scripts/linkchecker.py index 6a5ea7d089..5bc63e1d7f 100755 --- a/scripts/linkchecker.py +++ b/scripts/linkchecker.py @@ -35,6 +35,8 @@ # + /docs/bar : is a redirect entry, or # + /docs/bar : is something we don't understand # +# + {{ < api-reference page="" anchor="" ... > }} +# + {{ < api-reference page="" > }} import argparse import glob @@ -72,7 +74,8 @@ ARGS = None RESULT = {} # Cached redirect entries REDIRECTS = {} - +# Cached anchors in target pages +ANCHORS = {} def new_record(level, message, target): """Create new checking record. @@ -330,6 +333,44 @@ def check_target(page, anchor, target): msg = "Link may be wrong for the anchor [%s]" % anchor return new_record("WARNING", msg, target) +def check_anchor(target_page, anchor): + """Check if an anchor is defined in the target page + + :param target_page: The target page to check + :param anchor: Anchor string to find in the target page + """ + if target_page not in ANCHORS: + try: + with open(target_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)) + anchor_pattern1 = r"