Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit 8adfe97

Browse files
authored
Merge pull request #63 from readthedocs/humitos/ignore-nonhtml-builders
2 parents 0655561 + 283c6be commit 8adfe97

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

hoverxref/domains.py

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ def _get_docpath(self, builder, docname):
5252
return docpath
5353

5454
def _is_ignored_ref(self, env, target):
55+
# HACK: skip all references if the builder is non-html. We shouldn't
56+
# have overridden the Domain in first instance at ``setup_domains``
57+
# function, but at that time ``app.builder`` is not yet initialized. If
58+
# we suscribe ourselves to ``builder-initied`` it's too late and our
59+
# override does not take effect. Other builders (e.g. LatexBuilder) may
60+
# fail with internal functions we use (e.g. builder.get_outfilename).
61+
# So, we are skipping it here :(
62+
if env.app.builder.format != 'html':
63+
return True
64+
5565
if target in env.config.hoverxref_ignore_refs:
5666
logger.info(
5767
'Ignoring reference in hoverxref_ignore_refs. target=%s',

tests/examples/default/index.rst

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ Using ``hoverxref`` (or ``ref`` if ``hoverxref_auto_ref=True``) should add an ``
99
:ref:`This a :ref: to Chapter I <Chapter I>`.
1010

1111
:hoverxref:`This a :hoverxref: to Chapter I, Section I <Section I>`.
12+
13+
.. _example-reference:
14+
15+
Example Reference
16+
-----------------
17+
18+
This is a reference to :ref:`example-reference`.

tests/test_internals.py

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import pytest
44
import shutil
5+
from unittest import mock
56

67
from hoverxref.translators import HoverXRefHTMLTranslatorMixin
78

@@ -44,3 +45,36 @@ def test_override_translator_non_html_builder(app, status, warning):
4445
assert app.builder.format == 'html'
4546
for name, klass in app.registry.translators.items():
4647
assert issubclass(klass, HoverXRefHTMLTranslatorMixin)
48+
49+
50+
@pytest.mark.sphinx(
51+
srcdir=srcdir,
52+
buildername='latex',
53+
confoverrides={
54+
'hoverxref_project': 'myproject',
55+
'hoverxref_version': 'myversion',
56+
'hoverxref_auto_ref': True,
57+
},
58+
)
59+
def test_dont_fail_non_html_builder(app, status, warning):
60+
"""
61+
Test our resolver is not used by non-HTML builder.
62+
63+
When running the build with ``latex`` as builder and
64+
``hoverxref_auto_ref=True`` it should not fail with
65+
66+
def _get_docpath(self, builder, docname):
67+
docpath = builder.get_outfilename(docname)
68+
AttributeError: 'LaTeXBuilder' object has no attribute 'get_outfilename'
69+
70+
LaTeXBuilder should never use our resolver.
71+
"""
72+
73+
with mock.patch('hoverxref.domains.HoverXRefBaseDomain._get_docpath') as _get_docpath:
74+
app.build()
75+
assert not _get_docpath.called
76+
path = app.outdir / 'test.tex'
77+
assert path.exists() is True
78+
content = open(path).read()
79+
80+
assert app.builder.format == 'latex'

0 commit comments

Comments
 (0)