diff --git a/.gitignore b/.gitignore index 52c9e2a..1bbf6a5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ _build .python-version .vscode tests/__pycache__ +_readthedocs diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 38c7a37..c554653 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -18,6 +18,7 @@ build: # Build documentation in the docs/ directory with Sphinx sphinx: configuration: docs/conf.py + builder: dirhtml # If using Sphinx, optionally build your docs in additional formats such as PDF # formats: @@ -26,4 +27,4 @@ sphinx: # Optionally declare the Python requirements required to build your docs python: install: - - requirements: requirements.txt \ No newline at end of file + - requirements: requirements.txt diff --git a/docs/Makefile b/docs/Makefile index 9cedfd9..7b056eb 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -5,7 +5,7 @@ SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = -BUILDDIR = _build +BUILDDIR = _readthedocs # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 @@ -56,9 +56,9 @@ html: .PHONY: dirhtml dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." .PHONY: singlehtml singlehtml: @@ -226,6 +226,6 @@ dummy: .PHONY: autobuild autobuild: - sphinx-autobuild -nW -q -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + sphinx-autobuild --re-ignore='docs/_readthedocs/*' -nW -q -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo - @echo "Auto-build finished. The HTML pages are in $(BUILDDIR)/dirhtml." \ No newline at end of file + @echo "Auto-build finished. The HTML pages are in $(BUILDDIR)/html." diff --git a/docs/conf.py b/docs/conf.py index 49dc5bf..ada85e3 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,6 +20,9 @@ # import os # import sys # sys.path.insert(0, os.path.abspath('.')) +import json +import os +import shutil # -- General configuration ------------------------------------------------ @@ -98,7 +101,7 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store','_static/docson'] +exclude_patterns = ['_build', '_readthedocs', 'Thumbs.db', '.DS_Store','_static/docson'] # The reST default role (used for this markup: `text`) to use for all # documents. @@ -369,4 +372,69 @@ linkcheck_ignore = [ 'https://linux.die.net/man/3/libuuid', # 403 Client Error: Forbidden for url -] \ No newline at end of file +] + +# Substitute branch name placeholders in schema. + +def create_directory(path): + output_dir = os.path.dirname(path) + os.makedirs(output_dir, exist_ok=True) + +def replace_substring_in_json(file_path, search_substring, replace_string, output_path=None): + # Read the JSON file + with open(file_path, 'r') as file: + data = json.load(file) + + # Recursively search and replace the substring in the JSON data + _replace_substring_in_json(data, search_substring, replace_string) + + # Set output path + if output_path is None: + output_path = file_path + + # Create the directory if it does not exist + create_directory(output_path) + + # Write the modified JSON data to the output file + with open(output_path, 'w') as file: + json.dump(data, file, indent=4) + +def _replace_substring_in_json(data, search_substring, replace_string): + if isinstance(data, dict): + for key, value in data.items(): + if isinstance(value, str): + data[key] = value.replace(search_substring, replace_string) + else: + _replace_substring_in_json(value, search_substring, replace_string) + elif isinstance(data, list): + for i, item in enumerate(data): + if isinstance(item, str): + data[i] = item.replace(search_substring, replace_string) + else: + _replace_substring_in_json(item, search_substring, replace_string) + + +def setup(app): + # Connect handlers to events + app.connect('config-inited', config_inited) + app.connect('env-before-read-docs', env_before_read_docs) + app.connect('build-finished', build_finished) + + +def config_inited(app, config): + shutil.copytree('../schema', '../.temp', dirs_exist_ok=True) + + rtd_version = os.getenv('READTHEDOCS_VERSION') + + # Replace {{version}} placeholders + if rtd_version is not None: + replace_substring_in_json('../.temp/network-schema.json', '{{version}}', rtd_version) + + +def env_before_read_docs(app, env, docnames): + create_directory('_readthedocs/html/') + shutil.copyfile('../.temp/network-schema.json', '_readthedocs/html/network-schema.json') + + +def build_finished(app, exception): + shutil.rmtree('../.temp')