9
9
from docutils import nodes
10
10
from docutils .core import Publisher
11
11
import pytest
12
+ from sphinx import version_info as sphinx_version_info
12
13
from sphinx .environment import BuildEnvironment
13
- from sphinx .testing .path import path
14
14
from sphinx .testing .util import SphinxTestApp
15
15
16
16
from .builders import DoctreeBuilder
@@ -90,12 +90,22 @@ def doctrees(self) -> dict[str, nodes.document] | Doctrees:
90
90
except AttributeError :
91
91
return Doctrees (self .env )
92
92
93
- def pformat (self , docname : str = "index" ) -> str :
93
+ def pformat (
94
+ self , docname : str = "index" , pop_doc_attrs = ("translation_progress" ,)
95
+ ) -> str :
94
96
"""Return an indented pseudo-XML representation.
95
97
96
98
The src directory is replaced with <src>, for reproducibility.
99
+
100
+ :param pop_doc_attrs: Remove these attributes of the doctree node,
101
+ before converting to text.
102
+ By default, ``translation_progress`` is removed for compatibility
103
+ (added in sphinx 7.1).
97
104
"""
98
- text = self .doctrees [docname ].pformat ()
105
+ doctree = self .doctrees [docname ].deepcopy ()
106
+ for attr_name in pop_doc_attrs :
107
+ doctree .attributes .pop (attr_name , None )
108
+ text = doctree .pformat ()
99
109
return text .replace (str (self ._app .srcdir ) + os .sep , "<src>/" ).rstrip ()
100
110
101
111
def get_resolved_doctree (self , docname : str = "index" ) -> nodes .document :
@@ -109,9 +119,22 @@ def get_resolved_doctree(self, docname: str = "index") -> nodes.document:
109
119
# https://github.com/sphinx-doc/sphinx/blob/05a898ecb4ff8e654a053a1ba5131715a4514812/sphinx/environment/__init__.py#L538
110
120
return doctree
111
121
112
- def get_resolved_pformat (self , docname : str = "index" ) -> str :
113
- """Return the pformat of the doctree after post-transforms."""
114
- text = self .get_resolved_doctree (docname ).pformat ()
122
+ def get_resolved_pformat (
123
+ self , docname : str = "index" , pop_doc_attrs = ("translation_progress" ,)
124
+ ) -> str :
125
+ """Return an indented pseudo-XML representation, after post-transforms.
126
+
127
+ The src directory is replaced with <src>, for reproducibility.
128
+
129
+ :param pop_doc_attrs: Remove these attributes of the doctree node,
130
+ before converting to text.
131
+ By default, ``translation_progress`` is removed for compatibility
132
+ (added in sphinx 7.1).
133
+ """
134
+ doctree = self .get_resolved_doctree (docname )
135
+ for attr_name in pop_doc_attrs :
136
+ doctree .attributes .pop (attr_name , None )
137
+ text = doctree .pformat ()
115
138
return text .replace (str (self ._app .srcdir ) + os .sep , "<src>/" ).rstrip ()
116
139
117
140
@@ -141,9 +164,17 @@ def __call__(
141
164
self .srcdir .joinpath (filename ).parent .mkdir (parents = True , exist_ok = True )
142
165
self .srcdir .joinpath (filename ).write_text (content , encoding = "utf8" )
143
166
167
+ srcdir : Any
168
+ if sphinx_version_info >= (7 , 2 ):
169
+ srcdir = self .srcdir
170
+ else :
171
+ from sphinx .testing .path import path
172
+
173
+ srcdir = path (str (self .srcdir ))
174
+
144
175
return AppWrapper (
145
176
self ._app_cls (
146
- srcdir = path ( str ( self . srcdir )) ,
177
+ srcdir = srcdir ,
147
178
buildername = self .buildername ,
148
179
confoverrides = self ._confoverrides ,
149
180
** kwargs ,
0 commit comments