add extinclude warning in directive
This commit is contained in:
parent
9c2885ce94
commit
2b0dd5e5bd
4 changed files with 76 additions and 10 deletions
15
docs/conf.py
15
docs/conf.py
|
|
@ -34,6 +34,21 @@ extensions = [
|
||||||
'ext.xref', 'ext.extinclude'
|
'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 ----
|
#---- xref links ----
|
||||||
#import the xref.py extension
|
#import the xref.py extension
|
||||||
xref_links = {"link_name" : ("user text", "url")}
|
xref_links = {"link_name" : ("user text", "url")}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
from docutils import nodes
|
from docutils import nodes
|
||||||
from sphinx.application import Sphinx
|
from sphinx.application import Sphinx
|
||||||
from sphinx.util.docutils import SphinxDirective, SphinxRole
|
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
|
from sphinx.directives.code import LiteralInclude, container_wrapper
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from requests.exceptions import RequestException
|
||||||
|
from docutils.parsers.rst import directives
|
||||||
|
|
||||||
class ExtInclude(LiteralInclude):
|
class ExtInclude(LiteralInclude):
|
||||||
"""A directive to include code that comes from an url
|
"""A directive to include code that comes from an url
|
||||||
|
|
@ -29,12 +30,40 @@ class ExtInclude(LiteralInclude):
|
||||||
|
|
||||||
def run(self) -> list[nodes.Node]:
|
def run(self) -> list[nodes.Node]:
|
||||||
url = self.arguments[0]
|
url = self.arguments[0]
|
||||||
headers = {
|
|
||||||
'accept': 'application/text',
|
try:
|
||||||
'Content-Type': 'application/text',
|
headers = {
|
||||||
|
'accept': 'application/text',
|
||||||
|
'Content-Type': 'application/text',
|
||||||
}
|
}
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
#paragraph_node = nodes.paragraph(text=f'hello {self.arguments[0]}!')
|
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
|
code = response.text
|
||||||
|
|
||||||
literal = nodes.literal_block(code, code)
|
literal = nodes.literal_block(code, code)
|
||||||
|
|
@ -54,12 +83,9 @@ class ExtInclude(LiteralInclude):
|
||||||
self.add_name(literal)
|
self.add_name(literal)
|
||||||
|
|
||||||
return [literal]
|
return [literal]
|
||||||
#paragraph_node = nodes.paragraph(text=content.text)
|
|
||||||
#return [paragraph_node]
|
|
||||||
|
|
||||||
|
|
||||||
def setup(app: Sphinx) -> ExtensionMetadata:
|
def setup(app: Sphinx) -> ExtensionMetadata:
|
||||||
|
|
||||||
app.add_directive('extinclude', ExtInclude)
|
app.add_directive('extinclude', ExtInclude)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,29 @@
|
||||||
"""adds link url in the global scope
|
"""adds link url in the global scope
|
||||||
|
|
||||||
|
sample use:
|
||||||
|
|
||||||
|
:xref:`Tiramisu <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
|
from docutils import nodes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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:
|
First of create, let's create a structural file like this:
|
||||||
|
|
||||||
.. code-block:: yaml
|
.. code-block:: yaml
|
||||||
:caption: the :file:`dist/00-base.yml` file content
|
:caption: the :file:`dist/00-base.yml` file content
|
||||||
|
|
||||||
%YAML 1.2
|
%YAML 1.2
|
||||||
---
|
---
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue