From 968618dac3d326b87a1f06c4a1380289b70a23a3 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Thu, 6 Apr 2023 19:36:58 +0200 Subject: [PATCH] Fix various issues in Sphinx .rst rendering for the blender-manual repo --- sphinx/sphinx_to_html.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/sphinx/sphinx_to_html.py b/sphinx/sphinx_to_html.py index 1107b30..7686109 100755 --- a/sphinx/sphinx_to_html.py +++ b/sphinx/sphinx_to_html.py @@ -28,10 +28,17 @@ if gitea_prefix.startswith(local_url): gitea_prefix = gitea_prefix[len(local_url):] if len(gitea_prefix): - org, repo, view, ref, branch = gitea_prefix.strip('/').split('/')[:5] + path_tokens = gitea_prefix.strip('/').split('/') + org, repo, view, ref, branch = path_tokens[:5] doc_url = f"{base_url}/{org}/{repo}/{view}/{ref}/{branch}" - image_url = f"{base_url}/{org}/{repo}/raw/{ref}/{branch}" + image_url = f"{base_url}/{org}/{repo}/media/{ref}/{branch}" + + # Hardcoded exception for blender-manual, that has links relative + # to manual/ folder. + if len(path_tokens) > 5 and path_tokens[5] == 'manual': + doc_url += "/manual" + image_url += "/manual" else: doc_url = "" image_url = "" @@ -48,10 +55,17 @@ with tempfile.TemporaryDirectory(dir=args.user_work_dir) as tmp_dir: page_contents = page_filepath.read_text() # Turn links into external links since internal links are not found and stripped. - def doc_link(matchobj): + def path_to_label(path): + path = path.removesuffix('/index') + return path.split('/')[-1].replace('_', ' ').replace('-', ' ').capitalize() + def doc_label_link(matchobj): return f"`{matchobj.group(1)}<{doc_url}/{matchobj.group(2).strip('/')}.rst>`_" - def ref_link(matchobj): + def doc_link(matchobj): + return f"`{path_to_label(matchobj.group(1))} <{doc_url}/{matchobj.group(1).strip('/')}.rst>`_" + def ref_label_link(matchobj): return f"`{matchobj.group(1)} <{placeholder_url}>`_" + def ref_link(matchobj): + return f"`{path_to_label(matchobj.group(1))} <{placeholder_url}>`_" def term_link(matchobj): return f"`{matchobj.group(1)} <{placeholder_url}>`_" def figure_link(matchobj): @@ -59,12 +73,14 @@ with tempfile.TemporaryDirectory(dir=args.user_work_dir) as tmp_dir: def image_link(matchobj): return f"image:: {image_url}/{matchobj.group(1).strip('/')}" - page_contents = re.sub(":doc:`(.*)<(.+)>`", doc_link, page_contents) - page_contents = re.sub(":ref:`(.+)<(.+)>`", ref_link, page_contents) - page_contents = re.sub(":ref:`(.+)`", ref_link, page_contents) - page_contents = re.sub(":term:`(.+)`", term_link, page_contents) - page_contents = re.sub("figure:: (.+)", figure_link, page_contents) - page_contents = re.sub("image:: (.+)", image_link, page_contents) + page_contents = re.sub(":doc:`/(.+?)`", doc_link, page_contents) + page_contents = re.sub(":doc:`(.+?)<(.+?)>`", doc_label_link, page_contents) + page_contents = re.sub(":ref:`(.+?)<(.+?)>`", ref_label_link, page_contents) + page_contents = re.sub(":ref:`([\w\s-]+?)`", ref_link, page_contents) + page_contents = re.sub(":term:`(.+?)<(.+?)>`", term_link, page_contents) + page_contents = re.sub(":term:`([\w\s-]+?)`", term_link, page_contents) + page_contents = re.sub("figure:: (.+?)", figure_link, page_contents) + page_contents = re.sub("image:: (.+?)", image_link, page_contents) # Disable include directives and raw for security. They are already disabled # by docutils.py, this is just to be extra careful.