| 1 |
"""Docutils helper functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import importlib |
| 6 |
import logging |
| 7 |
import typing as t |
| 8 |
|
| 9 |
import docutils.nodes |
| 10 |
import docutils.parsers.rst.directives |
| 11 |
import docutils.parsers.rst.roles |
| 12 |
import docutils.writers |
| 13 |
|
| 14 |
from . import _extras |
| 15 |
|
| 16 |
logger = logging.getLogger(__name__) |
| 17 |
|
| 18 |
|
| 19 |
class IgnoredDirective(docutils.parsers.rst.Directive): |
| 20 |
"""Stub for unknown directives.""" |
| 21 |
|
| 22 |
has_content = True |
| 23 |
|
| 24 |
def run(self) -> list: |
| 25 |
"""Do nothing.""" |
| 26 |
return [] |
| 27 |
|
| 28 |
|
| 29 |
def ignore_role( |
| 30 |
name: str, |
| 31 |
rawtext: str, |
| 32 |
text: str, |
| 33 |
lineno: int, |
| 34 |
inliner: docutils.parsers.rst.states.Inliner, |
| 35 |
options: t.Mapping[str, t.Any] | None = None, |
| 36 |
content: t.Sequence[str] | None = None, |
| 37 |
) -> tuple[ |
| 38 |
t.Sequence[docutils.nodes.reference], t.Sequence[docutils.nodes.reference] |
| 39 |
]: |
| 40 |
"""Stub for unknown roles.""" |
| 41 |
return ([], []) |
| 42 |
|
| 43 |
|
| 44 |
def clean_docutils_directives_and_roles_cache() -> None: |
| 45 |
"""Clean docutils' directives and roles cache by reloading their modules. |
| 46 |
|
| 47 |
Reloads: |
| 48 |
- :py:mod:`docutils.parsers.rst.directives` |
| 49 |
- :py:mod:`docutils.parsers.rst.roles` |
| 50 |
""" |
| 51 |
logger.info("Reload module docutils.parsers.rst.directives/roles") |
| 52 |
importlib.reload(docutils.parsers.rst.directives) |
| 53 |
importlib.reload(docutils.parsers.rst.roles) |
| 54 |
|
| 55 |
|
| 56 |
def ignore_directives_and_roles(directives: list[str], roles: list[str]) -> None: |
| 57 |
"""Ignore directives and roles in docutils. |
| 58 |
|
| 59 |
:param directives: Directives to ignore |
| 60 |
:param roles: Roles to ignore |
| 61 |
""" |
| 62 |
for directive in directives: |
| 63 |
docutils.parsers.rst.directives.register_directive(directive, IgnoredDirective) |
| 64 |
|
| 65 |
for role in roles: |
| 66 |
docutils.parsers.rst.roles.register_local_role(role, ignore_role) |
| 67 |
|
| 68 |
|
| 69 |
class CodeBlockDirective(docutils.parsers.rst.Directive): |
| 70 |
"""Code block directive.""" |
| 71 |
|
| 72 |
has_content = True |
| 73 |
optional_arguments = 1 |
| 74 |
|
| 75 |
def run(self) -> list[docutils.nodes.literal_block]: |
| 76 |
"""Run directive. |
| 77 |
|
| 78 |
:return: Literal block |
| 79 |
""" |
| 80 |
try: |
| 81 |
language = self.arguments[0] |
| 82 |
except IndexError: |
| 83 |
language = "" |
| 84 |
code = "\n".join(self.content) |
| 85 |
literal = docutils.nodes.literal_block(code, code) |
| 86 |
literal["classes"].append("code-block") |
| 87 |
literal["language"] = language |
| 88 |
return [literal] |
| 89 |
|
| 90 |
|
| 91 |
def register_code_directive( |
| 92 |
*, |
| 93 |
ignore_code_directive: bool = False, |
| 94 |
ignore_codeblock_directive: bool = False, |
| 95 |
ignore_sourcecode_directive: bool = False, |
| 96 |
) -> None: |
| 97 |
"""Optionally register code directives. |
| 98 |
|
| 99 |
:param ignore_code_directive: If "code" directive should be ignored, |
| 100 |
so that the code block will not be checked; defaults to :py:obj:`False` |
| 101 |
:param ignore_codeblock_directive: If "code-block" directive should be ignored, |
| 102 |
so that the code block will not be checked; defaults to :py:obj:`False` |
| 103 |
:param ignore_sourcecode_directive: If "sourcecode" directive should be ignored, |
| 104 |
so that the code block will not be checked; defaults to :py:obj:`False` |
| 105 |
""" |
| 106 |
if not _extras.SPHINX_INSTALLED: |
| 107 |
if ignore_code_directive is False: |
| 108 |
logger.debug("Register custom directive for 'code'.") |
| 109 |
docutils.parsers.rst.directives.register_directive("code", CodeBlockDirective) |
| 110 |
|
| 111 |
if ignore_codeblock_directive is False: |
| 112 |
logger.debug("Register custom directive for 'code-block'.") |
| 113 |
docutils.parsers.rst.directives.register_directive("code-block", CodeBlockDirective) |
| 114 |
if ignore_sourcecode_directive is False: |
| 115 |
logger.debug("Register custom directive for 'sourcecode'.") |
| 116 |
docutils.parsers.rst.directives.register_directive("sourcecode", CodeBlockDirective) |