| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
from docutils.parsers.rst import directives |
| 6 |
|
| 7 |
from ..utils import derive_doc_url_base |
| 8 |
from .add_content_directive import AddContentDirective |
| 9 |
|
| 10 |
|
| 11 |
class ExternalDocLink(AddContentDirective): |
| 12 |
has_content = False |
| 13 |
required_arguments = 1 |
| 14 |
optional_arguments = 0 |
| 15 |
# allow for spaces in the argument string |
| 16 |
final_argument_whitespace = True |
| 17 |
option_spec = { |
| 18 |
"base_url": directives.unchanged, |
| 19 |
"service": directives.unchanged, |
| 20 |
"ref": directives.unchanged_required, |
| 21 |
} |
| 22 |
|
| 23 |
def gen_rst(self) -> t.Iterator[str]: |
| 24 |
message = self.arguments[0].strip() |
| 25 |
|
| 26 |
service: str | None = self.options.get("service") |
| 27 |
default_base_url: str = derive_doc_url_base(service) |
| 28 |
|
| 29 |
base_url = self.options.get("base_url", default_base_url) |
| 30 |
relative_link = self.options["ref"] |
| 31 |
# use a trailing `__` to make the hyperlink an "anonymous hyperlink" |
| 32 |
# |
| 33 |
# the reason for this is that sphinx will warn (error with -W) if we generate |
| 34 |
# rst with duplicate target names, as when an autodoc method name matches the |
| 35 |
# (snake_cased) message for a hyperlink, a common scenario |
| 36 |
# the conflicts are stipulated by docutils to cause warnings, along with a |
| 37 |
# conflict resolution procedure specified under the implicit hyperlink section |
| 38 |
# of the spec: |
| 39 |
# https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#implicit-hyperlink-targets |
| 40 |
# |
| 41 |
# for details on anonymous hyperlinks, see also: |
| 42 |
# https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#anonymous-hyperlinks |
| 43 |
yield ( |
| 44 |
f"See `{message} <{base_url}/{relative_link}>`__ in the " |
| 45 |
"API documentation for details." |
| 46 |
) |