Skip to content

Commit

Permalink
Added check for relative links to online documentation files (#2635)
Browse files Browse the repository at this point in the history
* Added extra check.

* Fixed non-documentation md files processing.

* Fixed RuntimeError description.

* Removed unused variable.
  • Loading branch information
nignatovsky authored Jul 27, 2021
1 parent 1697960 commit 0a70225
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions ci/prepare-documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import omzdocs

all_images_paths = {}
all_md_paths = {}
documentation_md_paths = set()

XML_ID_ATTRIBUTE = '{http://www.w3.org/XML/1998/namespace}id'

Expand Down Expand Up @@ -91,6 +93,8 @@ def add_page(output_root, parent, *, id=None, path=None, title=None, index=-1):
element.attrib['title'] = title
return element

documentation_md_paths.add(Path(path))

output_path = output_root / path

output_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -140,23 +144,25 @@ def add_page(output_root, parent, *, id=None, path=None, title=None, index=-1):
(output_root / image_rel_path.parent).mkdir(parents=True, exist_ok=True)
shutil.copyfile(image_abs_path, output_root / image_rel_path)

non_md_links = [ref.url for ref in page.external_references()
if ref.type == 'link' and not ref.url.endswith('.md')]
links = [ref.url for ref in page.external_references() if ref.type == 'link']

for non_md_link in non_md_links:
parsed_non_md_link = urllib.parse.urlparse(non_md_link)
for link in links:
parsed_link = urllib.parse.urlparse(link)

if parsed_non_md_link.scheme or parsed_non_md_link.netloc:
if parsed_link.scheme or parsed_link.netloc:
continue # not a relative URL

if parsed_non_md_link.fragment:
if parsed_link.fragment:
continue # link to markdown section

relative_path = (OMZ_ROOT / Path(path).parent / non_md_link).resolve().relative_to(OMZ_ROOT)
suggested_path = OMZ_PREFIX + Path(relative_path).as_posix()
relative_path = (OMZ_ROOT / Path(path).parent / link).resolve().relative_to(OMZ_ROOT)

raise RuntimeError(f'{path}: Relative link to non-markdown file "{non_md_link}". '
f'Replace it by `{suggested_path}`')
if link.endswith('.md'):
all_md_paths[relative_path] = Path(path)
else:
suggested_path = OMZ_PREFIX + Path(relative_path).as_posix()
raise RuntimeError(f'{path}: Relative link to non-markdown file "{link}". '
f'Replace it by `{suggested_path}`')

return element

Expand Down Expand Up @@ -363,6 +369,12 @@ def main():

sort_titles(demos_group_element)

for md_path in all_md_paths:
if md_path not in documentation_md_paths:
raise RuntimeError(f'{all_md_paths[md_path]}: '
f'Relative link to non-online documentation file "{md_path}". '
f'Replace it by `{OMZ_PREFIX + md_path.as_posix()}`')

with (output_root / 'DoxygenLayout.xml').open('wb') as layout_file:
ET.ElementTree(doxygenlayout_element).write(layout_file)

Expand Down

0 comments on commit 0a70225

Please sign in to comment.