Skip to content

Commit

Permalink
docs: [Python] 型エイリアス系へのリンクについてワークアラウンド (#952)
Browse files Browse the repository at this point in the history
Sphinx AutoAPIが`NewType`や`TypeAlias`へのリンクを張ってくれないという
問題について、`missing-reference`から宛先を張りなおすというワークアラウ
ンドを入れる。

以下が要課題。
#952 (review)

Resolves: #951
  • Loading branch information
qryxip authored Jan 28, 2025
1 parent 3ab4a6c commit 5de33e2
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/ghpages/apis/python_api/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

import typing

import sphinx.util.logging
from docutils.nodes import TextElement, reference
from sphinx.addnodes import pending_xref
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment

project = "voicevox_core_python_api"
# copyright = '2022, _'
# author = '_'
Expand Down Expand Up @@ -40,3 +48,37 @@

html_theme = "pydata_sphinx_theme"
# html_static_path = ['_static']


def setup(sphinx: Sphinx) -> None:
sphinx.connect("missing-reference", _on_missing_reference)


def _on_missing_reference(
app: Sphinx, env: BuildEnvironment, node: pending_xref, contnode: TextElement
) -> reference | None:
"""
``NewType`` や ``TypeAlias`` について ``class``
宛てにリンクしようとしてmissingになったものを、 ``data`` 宛てに修正する。
"""
# 参考: https://github.com/sphinx-doc/sphinx/issues/10785#issue-1348601826
TARGETS = {
"CharacterVersion",
"StyleId",
"VoiceModelId",
}
if (
node["refdomain"] == "py"
and node["reftype"] == "class"
and node["reftarget"].split(".")[-1] in TARGETS
):
xref = app.env.get_domain("py").resolve_xref(
env, node["refdoc"], app.builder, "data", node["reftarget"], node, contnode
)
xref = typing.cast(reference | None, xref) # ?
if not xref:
_logger.error("unresolved link to `%s`", node["reftarget"])
return xref


_logger = sphinx.util.logging.getLogger(__name__)

0 comments on commit 5de33e2

Please sign in to comment.