Skip to content

Commit

Permalink
check internal doc links
Browse files Browse the repository at this point in the history
  • Loading branch information
nelliemckesson committed Sep 7, 2023
1 parent c8689ba commit 9bd0683
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions scripts/create_nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,47 @@ def collect_xref_internal_inks(line, filepath, output_dir, adoc_dir, needed_inte
needed_internal_links[filepath].append(linkinfo)
return

def collect_simple_internal_links(line, filepath, output_dir, adoc_dir, needed_internal_links):
def collect_simple_internal_links(line, filepath, mainfile, output_dir, adoc_dir, needed_internal_links):
# <<overlay_prefix,overlay_prefix>>
for m in re.finditer(r'<<(.+?),(.+?)>>', line):
print("-------NEW LINK")
anchor = m.group(1)
link_path = os.path.normpath(filepath)
print(link_path)
link_path = re.sub(adoc_dir, "", mainfile)
link_path = re.sub("^/", "", link_path)
link_path = os.path.normpath(link_path)
link_relpath = os.path.relpath(link_path, adoc_dir)
print(link_relpath)
# linkinfo = {'url': link_relpath}
linkinfo = {'url': link_path}
if anchor:
linkinfo['anchor'] = anchor
needed_internal_links[filepath].append(linkinfo)
print("-------END")
return

def collect_all_internal_links(line, filepath, output_dir, adoc_dir, needed_internal_links):
def collect_all_internal_links(line, filepath, mainfile, output_dir, adoc_dir, needed_internal_links):
collect_xref_internal_inks(line, filepath, output_dir, adoc_dir, needed_internal_links)
collect_simple_internal_links(line, filepath, output_dir, adoc_dir, needed_internal_links)
collect_simple_internal_links(line, filepath, mainfile, output_dir, adoc_dir, needed_internal_links)
return

# need to get the main file path somehow...
def read_file_with_includes(filepath, output_dir=None):
def read_file_with_includes(filepath, filelevel, mainfile, output_dir=None):
if output_dir is None:
output_dir = os.path.dirname(filepath)
content = ''
if filelevel == 1:
mainfile = filepath
with open(filepath) as adoc_fh:
if filepath not in needed_internal_links:
needed_internal_links[filepath] = []
parent_dir = os.path.dirname(filepath)
for line in adoc_fh.readlines():
collect_all_internal_links(line, filepath, output_dir, adoc_dir, needed_internal_links)
collect_all_internal_links(line, filepath, mainfile, output_dir, adoc_dir, needed_internal_links)
m = re.match(r'^include::(.*)\[\]\s*$', line)
filelevel += 1
if m:
content += read_file_with_includes(os.path.join(parent_dir, m.group(1)), output_dir)
new_content, filelevel = read_file_with_includes(os.path.join(parent_dir, m.group(1)), filelevel, mainfile, output_dir)
content += new_content
else:
content += line
return content
return content, filelevel

min_level = 2 # this has to be 2
max_level = 3 # this can be 2 or 3
Expand All @@ -100,8 +102,6 @@ def read_file_with_includes(filepath, output_dir=None):
for subitem in tab['subitems']:
if 'subpath' in subitem:
fullpath = os.path.join(tab['path'], subitem['subpath'])
print("------NEW FILE")
print(fullpath)
if fullpath in available_anchors:
raise Exception("{} occurs twice in {}".format(fullpath, index_json))
available_anchors[fullpath] = set()
Expand All @@ -113,7 +113,7 @@ def read_file_with_includes(filepath, output_dir=None):
level = min_level
adjusted_path = re.sub("^/", "", fullpath)
top_level_file = os.path.join(adoc_dir, adjusted_path)
adoc_content = read_file_with_includes(top_level_file)
adoc_content, filelevel = read_file_with_includes(top_level_file, 1, top_level_file)
last_line_was_discrete = False
header_id = None
for line in adoc_content.split('\n'):
Expand Down Expand Up @@ -159,12 +159,7 @@ def read_file_with_includes(filepath, output_dir=None):
for filepath in sorted(needed_internal_links):
for linkinfo in needed_internal_links[filepath]:
if not linkinfo['url'].startswith('pico-sdk/'): # these pages aren't created by a non-doxygen build
print("CHECKING:")
print(linkinfo['url'])
adjusted_url = "/" + linkinfo['url']
print(adjusted_url)
if 'anchor' in linkinfo:
print(linkinfo['anchor'])
if adjusted_url not in available_anchors:
raise Exception("{} has an internal-link to {} but that destination doesn't exist".format(filepath, adjusted_url))
if 'anchor' in linkinfo:
Expand Down

0 comments on commit 9bd0683

Please sign in to comment.