From 2b0dd5e5bdaf605e10efc3b67787e1bb5f663674 Mon Sep 17 00:00:00 2001 From: gwen Date: Sat, 22 Nov 2025 08:04:51 +0100 Subject: [PATCH] add extinclude warning in directive --- docs/conf.py | 15 ++++++++++++++ docs/ext/extinclude.py | 44 ++++++++++++++++++++++++++++++++--------- docs/ext/xref.py | 25 +++++++++++++++++++++++ docs/library/output.rst | 2 +- 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 8d3861dfc..3748e7567 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -34,6 +34,21 @@ extensions = [ 'ext.xref', 'ext.extinclude' ] +#---- disable highlight warnings with yaml new version ---- +# Configuration pour les blocs de code +highlight_language = 'yaml' + +# Options spécifiques pour YAML +highlight_options = { + 'yaml': { + 'startinline': True + } +} + +suppress_warnings = [ + 'misc.highlighting_failure' +] + #---- xref links ---- #import the xref.py extension xref_links = {"link_name" : ("user text", "url")} diff --git a/docs/ext/extinclude.py b/docs/ext/extinclude.py index 19218d4dc..f5f1915f0 100644 --- a/docs/ext/extinclude.py +++ b/docs/ext/extinclude.py @@ -1,6 +1,5 @@ from __future__ import annotations - from docutils import nodes from sphinx.application import Sphinx from sphinx.util.docutils import SphinxDirective, SphinxRole @@ -8,6 +7,8 @@ from sphinx.util.typing import ExtensionMetadata from sphinx.directives.code import LiteralInclude, container_wrapper import requests +from requests.exceptions import RequestException +from docutils.parsers.rst import directives class ExtInclude(LiteralInclude): """A directive to include code that comes from an url @@ -29,12 +30,40 @@ class ExtInclude(LiteralInclude): def run(self) -> list[nodes.Node]: url = self.arguments[0] - headers = { - 'accept': 'application/text', - 'Content-Type': 'application/text', + + try: + headers = { + 'accept': 'application/text', + 'Content-Type': 'application/text', } - response = requests.get(url, headers=headers) - #paragraph_node = nodes.paragraph(text=f'hello {self.arguments[0]}!') + response = requests.get(url, headers=headers) + response.raise_for_status() # This will raise an exception for 4xx/5xx status codes + + except requests.exceptions.HTTPError as e: + if response.status_code == 404: + error_msg = f"extinclude: URL not found (404): {url}" + else: + error_msg = f"extinclude: HTTP error {response.status_code}: {url}" + + # Create an error node that will be displayed in the documentation + error_node = nodes.error() + para = nodes.paragraph() + para += nodes.Text(error_msg) + error_node += para + self.state.document.reporter.warning(error_msg, line=self.lineno) + return [error_node] + + except requests.exceptions.RequestException as e: + error_msg = f"extinclude: Failed to fetch URL {url}: {str(e)}" + + # Create an error node that will be displayed in the documentation + error_node = nodes.error() + para = nodes.paragraph() + para += nodes.Text(error_msg) + error_node += para + self.state.document.reporter.warning(error_msg, line=self.lineno) + return [error_node] + code = response.text literal = nodes.literal_block(code, code) @@ -54,12 +83,9 @@ class ExtInclude(LiteralInclude): self.add_name(literal) return [literal] - #paragraph_node = nodes.paragraph(text=content.text) - #return [paragraph_node] def setup(app: Sphinx) -> ExtensionMetadata: - app.add_directive('extinclude', ExtInclude) return { diff --git a/docs/ext/xref.py b/docs/ext/xref.py index 09606efd8..71948a3fa 100644 --- a/docs/ext/xref.py +++ b/docs/ext/xref.py @@ -1,4 +1,29 @@ """adds link url in the global scope + +sample use: + +:xref:`Tiramisu ` + +You must declare in the `conf.py` + +:: + + #---- xref links ---- + #import the xref.py extension + xref_links = {"link_name" : ("user text", "url")} + #link_name = "Sphinx External Links" + #user_text = "modified External Links Extension" + #url = "http://www.sphinx-doc.org/en/stable/ext/extlinks.html" + #enables syntax like: + " :xref:`tiramisu` " + links = { + 'tiramisu': ('Tiramisu', 'https://tiramisu.readthedocs.io/en/latest/'), + 'tiramisu library': ('Tiramisu library homepage', 'https://forge.cloud.silique.fr/stove/tiramisu'), + } + xref_links.update(links) + + + """ from docutils import nodes diff --git a/docs/library/output.rst b/docs/library/output.rst index b39cd0a1e..af7c88658 100644 --- a/docs/library/output.rst +++ b/docs/library/output.rst @@ -6,7 +6,7 @@ After construct a configuration, loads user datas, you can choose this configura First of create, let's create a structural file like this: .. code-block:: yaml - :caption: the :file:`dist/00-base.yml` file content + :caption: the :file:`dist/00-base.yml` file content %YAML 1.2 ---