diff --git a/sphinx/.gitignore b/sphinx/.gitignore new file mode 100644 index 0000000..eba74f4 --- /dev/null +++ b/sphinx/.gitignore @@ -0,0 +1 @@ +venv/ \ No newline at end of file diff --git a/sphinx/sphinx_to_html.py b/sphinx/sphinx_to_html.py index f7b1c49..6747e51 100755 --- a/sphinx/sphinx_to_html.py +++ b/sphinx/sphinx_to_html.py @@ -8,7 +8,6 @@ import re import shutil import subprocess import sys -import tempfile page_contents = sys.stdin.read() @@ -42,9 +41,15 @@ else: doc_url = "" image_url = "" -# Set up temporary directory with sphinx configuration. -with tempfile.TemporaryDirectory() as tmp_dir: - work_dir = pathlib.Path(tmp_dir) / "work" +# Create a temporary directory manually +tmp_dir = pathlib.Path("./tmp_sphinx") +if tmp_dir.exists(): + shutil.rmtree(tmp_dir) # Remove if it already exists +tmp_dir.mkdir(parents=True) + +try: + work_dir = tmp_dir / "work" + work_dir.mkdir(parents=True) script_dir = pathlib.Path(__file__).parent.resolve() shutil.copytree(script_dir / "template", work_dir) @@ -69,14 +74,14 @@ with tempfile.TemporaryDirectory() 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(":doc:`([\w\s\n-]+?)\n?<(.+?)>`", doc_label_link, page_contents) - page_contents = re.sub(":ref:`([\w\s\n-]+?)\n?<(.+?)>`", ref_label_link, page_contents) - page_contents = re.sub(":ref:`([\w\s-]+?)`", ref_link, page_contents) - page_contents = re.sub(":term:`([\w\s\n-]+?)\n?<(.+?)>`", 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) + page_contents = re.sub(r":doc:`/(.+?)`", doc_link, page_contents) + page_contents = re.sub(r":doc:`([\w\s\n-]+?)\n?<(.+?)>`", doc_label_link, page_contents) + page_contents = re.sub(r":ref:`([\w\s\n-]+?)\n?<(.+?)>`", ref_label_link, page_contents) + page_contents = re.sub(r":ref:`([\w\s-]+?)`", ref_link, page_contents) + page_contents = re.sub(r":term:`([\w\s\n-]+?)\n?<(.+?)>`", term_link, page_contents) + page_contents = re.sub(r":term:`([\w\s-]+?)`", term_link, page_contents) + page_contents = re.sub(r"figure:: (.+?)", figure_link, page_contents) + page_contents = re.sub(r"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. @@ -84,11 +89,11 @@ with tempfile.TemporaryDirectory() as tmp_dir: return f"warning:: include not available in preview: {html.escape(matchobj.group(1))}" def raw_directive(matchobj): return f"warning:: raw not available in preview: {html.escape(matchobj.group(1))}" - page_contents = re.sub("literalinclude::(.*)", include_directive, page_contents) - page_contents = re.sub("include::(.*)", include_directive, page_contents) - page_contents = re.sub("raw::(.*)", raw_directive, page_contents) - page_contents = re.sub(".. toctree::(.*)", ".. code-block:: none", page_contents) - page_contents = re.sub(":maxdepth:(.*)", "", page_contents) + page_contents = re.sub(r"literalinclude::(.*)", include_directive, page_contents) + page_contents = re.sub(r"include::(.*)", include_directive, page_contents) + page_contents = re.sub(r"raw::(.*)", raw_directive, page_contents) + page_contents = re.sub(r".. toctree::(.*)", ".. code-block:: none", page_contents) + page_contents = re.sub(r":maxdepth:(.*)", "", page_contents) page_contents = page_contents.replace("|BLENDER_VERSION|", "BLENDER_VERSION") page_filepath.write_text(page_contents) @@ -99,10 +104,11 @@ with tempfile.TemporaryDirectory() as tmp_dir: # Run sphinx-build. out_dir = work_dir / "out" + out_dir.mkdir(parents=True) out_filepath = out_dir / "contents.html" - sphinx_build = script_dir / "venv" / "bin" / "sphinx-build" - sphinx_cmd = [sphinx_build, "-b", "html", work_dir, out_dir] + sphinx_build = str(script_dir / "venv" / "bin" / "sphinx-build") + sphinx_cmd = [sphinx_build, "-b", "html", str(work_dir), str(out_dir)] result = subprocess.run(sphinx_cmd, capture_output=True) # Output errors. @@ -122,3 +128,7 @@ with tempfile.TemporaryDirectory() as tmp_dir: body = body.replace('href="http', 'target="_blank" href="http') body = '