| 1 |
"""Test various Sphinx-specific markup extensions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
from types import SimpleNamespace |
| 7 |
|
| 8 |
import pytest |
| 9 |
from docutils import nodes, utils |
| 10 |
from docutils.parsers.rst import Parser as RstParser |
| 11 |
|
| 12 |
from sphinx import addnodes |
| 13 |
from sphinx.builders.latex import LaTeXBuilder |
| 14 |
from sphinx.environment import default_settings |
| 15 |
from sphinx.roles import XRefRole |
| 16 |
from sphinx.testing.util import assert_node |
| 17 |
from sphinx.transforms import SphinxSmartQuotes |
| 18 |
from sphinx.util import texescape |
| 19 |
from sphinx.util.docutils import _get_settings, sphinx_domains |
| 20 |
from sphinx.writers.html import HTMLWriter |
| 21 |
from sphinx.writers.html5 import HTML5Translator |
| 22 |
from sphinx.writers.latex import LaTeXTranslator, LaTeXWriter |
| 23 |
|
| 24 |
from tests.utils import extract_node |
| 25 |
|
| 26 |
TYPE_CHECKING = False |
| 27 |
if TYPE_CHECKING: |
| 28 |
from sphinx.environment import BuildEnvironment |
| 29 |
from sphinx.testing.util import SphinxTestApp |
| 30 |
from sphinx.util.docutils import _DocutilsSettings |
| 31 |
|
| 32 |
|
| 33 |
def new_settings(env: BuildEnvironment) -> _DocutilsSettings: |
| 34 |
texescape.init() # otherwise done by the latex builder |
| 35 |
settings = _get_settings( |
| 36 |
RstParser, HTMLWriter, LaTeXWriter, defaults=default_settings |
| 37 |
) |
| 38 |
settings.smart_quotes = True |
| 39 |
settings.env = env |
| 40 |
settings.env.current_document.docname = 'dummy' |
| 41 |
settings.contentsname = 'dummy' |
| 42 |
return settings |
| 43 |
|
| 44 |
|
| 45 |
def new_document(env: BuildEnvironment) -> nodes.document: |
| 46 |
settings = new_settings(env) |
| 47 |
document = utils.new_document('test data', settings) |
| 48 |
document['file'] = 'dummy' |
| 49 |
return document |
| 50 |
|
| 51 |
|
| 52 |
def new_inliner(env: BuildEnvironment) -> SimpleNamespace: |
| 53 |
document = new_document(env) |
| 54 |
|
| 55 |
def _get_source_and_line(line: int | None = 1) -> tuple[str, int | None]: |
| 56 |
return 'dummy.rst', line |
| 57 |
|
| 58 |
document.reporter.get_source_and_line = _get_source_and_line |
| 59 |
return SimpleNamespace(document=document, reporter=document.reporter) |
| 60 |
|
| 61 |
|
| 62 |
def parse_rst(rst: str, *, env: BuildEnvironment) -> nodes.document: |
| 63 |
document = new_document(env) |
| 64 |
parser = RstParser() |
| 65 |
domain_context = sphinx_domains(env) |
| 66 |
domain_context.enable() |
| 67 |
parser.parse(rst, document) |
| 68 |
domain_context.disable() |
| 69 |
SphinxSmartQuotes(document, startnode=None).apply() |
| 70 |
for msg in list(document.findall(nodes.system_message)): |
| 71 |
if msg['level'] == 1: |
| 72 |
msg.replace_self([]) |
| 73 |
return document |
| 74 |
|
| 75 |
|
| 76 |
# since we're not resolving the markup afterwards, these nodes may remain |
| 77 |
class ForgivingTranslator: |
| 78 |
def visit_pending_xref(self, node: nodes.Element) -> None: |
| 79 |
pass |
| 80 |
|
| 81 |
def depart_pending_xref(self, node: nodes.Element) -> None: |
| 82 |
pass |
| 83 |
|
| 84 |
|
| 85 |
class ForgivingHTMLTranslator(HTML5Translator, ForgivingTranslator): |
| 86 |
pass |
| 87 |
|
| 88 |
|
| 89 |
class ForgivingLaTeXTranslator(LaTeXTranslator, ForgivingTranslator): |
| 90 |
pass |
| 91 |
|
| 92 |
|
| 93 |
def rst_to_html(rst: str, *, app: SphinxTestApp) -> str: |
| 94 |
document = parse_rst(rst, env=app.env) |
| 95 |
html_translator = ForgivingHTMLTranslator(document, app.builder) |
| 96 |
document.walkabout(html_translator) |
| 97 |
html_translated = ''.join(html_translator.fragment).strip() |
| 98 |
return html_translated |
| 99 |
|
| 100 |
|
| 101 |
def rst_to_latex(rst: str, *, app: SphinxTestApp) -> str: |
| 102 |
document = parse_rst(rst, env=app.env) |
| 103 |
app.builder = LaTeXBuilder(app, app.env) |
| 104 |
app.builder.init() |
| 105 |
theme = app.builder.themes.get('manual') |
| 106 |
latex_translator = ForgivingLaTeXTranslator(document, app.builder, theme) |
| 107 |
latex_translator.first_document = -1 # don't write \begin{document} |
| 108 |
document.walkabout(latex_translator) |
| 109 |
latex_translated = ''.join(latex_translator.body).strip() |
| 110 |
return latex_translated |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.parametrize( |
| 114 |
('rst', 'html_expected', 'latex_expected'), |
| 115 |
[ |
| 116 |
( |
| 117 |
# cve role |
| 118 |
':cve:`2020-10735`', |
| 119 |
( |
| 120 |
'<p><span class="target" id="index-0"></span><a class="cve reference external" ' |
| 121 |
'href="https://www.cve.org/CVERecord?id=CVE-2020-10735">' |
| 122 |
'<strong>CVE 2020-10735</strong></a></p>' |
| 123 |
), |
| 124 |
( |
| 125 |
'\\sphinxAtStartPar\n' |
| 126 |
'\\index{Common Vulnerabilities and Exposures@\\spxentry{Common Vulnerabilities and Exposures}' |
| 127 |
'!CVE 2020\\sphinxhyphen{}10735@\\spxentry{CVE 2020\\sphinxhyphen{}10735}}' |
| 128 |
'\\sphinxhref{https://www.cve.org/CVERecord?id=CVE-2020-10735}' |
| 129 |
'{\\sphinxstylestrong{CVE 2020\\sphinxhyphen{}10735}}' |
| 130 |
), |
| 131 |
), |
| 132 |
( |
| 133 |
# cve role with anchor |
| 134 |
':cve:`2020-10735#id1`', |
| 135 |
( |
| 136 |
'<p><span class="target" id="index-0"></span><a class="cve reference external" ' |
| 137 |
'href="https://www.cve.org/CVERecord?id=CVE-2020-10735#id1">' |
| 138 |
'<strong>CVE 2020-10735#id1</strong></a></p>' |
| 139 |
), |
| 140 |
( |
| 141 |
'\\sphinxAtStartPar\n' |
| 142 |
'\\index{Common Vulnerabilities and Exposures@\\spxentry{Common Vulnerabilities and Exposures}' |
| 143 |
'!CVE 2020\\sphinxhyphen{}10735\\#id1@\\spxentry{CVE 2020\\sphinxhyphen{}10735\\#id1}}' |
| 144 |
'\\sphinxhref{https://www.cve.org/CVERecord?id=CVE-2020-10735\\#id1}' |
| 145 |
'{\\sphinxstylestrong{CVE 2020\\sphinxhyphen{}10735\\#id1}}' |
| 146 |
), |
| 147 |
), |
| 148 |
( |
| 149 |
# cwe role |
| 150 |
':cwe:`787`', |
| 151 |
( |
| 152 |
'<p><span class="target" id="index-0"></span><a class="cwe reference external" ' |
| 153 |
'href="https://cwe.mitre.org/data/definitions/787.html">' |
| 154 |
'<strong>CWE 787</strong></a></p>' |
| 155 |
), |
| 156 |
( |
| 157 |
'\\sphinxAtStartPar\n' |
| 158 |
'\\index{Common Weakness Enumeration@\\spxentry{Common Weakness Enumeration}' |
| 159 |
'!CWE 787@\\spxentry{CWE 787}}' |
| 160 |
'\\sphinxhref{https://cwe.mitre.org/data/definitions/787.html}' |
| 161 |
'{\\sphinxstylestrong{CWE 787}}' |
| 162 |
), |
| 163 |
), |
| 164 |
( |
| 165 |
# cwe role with anchor |
| 166 |
':cwe:`787#id1`', |
| 167 |
( |
| 168 |
'<p><span class="target" id="index-0"></span><a class="cwe reference external" ' |
| 169 |
'href="https://cwe.mitre.org/data/definitions/787.html#id1">' |
| 170 |
'<strong>CWE 787#id1</strong></a></p>' |
| 171 |
), |
| 172 |
( |
| 173 |
'\\sphinxAtStartPar\n' |
| 174 |
'\\index{Common Weakness Enumeration@\\spxentry{Common Weakness Enumeration}' |
| 175 |
'!CWE 787\\#id1@\\spxentry{CWE 787\\#id1}}' |
| 176 |
'\\sphinxhref{https://cwe.mitre.org/data/definitions/787.html\\#id1}' |
| 177 |
'{\\sphinxstylestrong{CWE 787\\#id1}}' |
| 178 |
), |
| 179 |
), |
| 180 |
( |
| 181 |
# pep role |
| 182 |
':pep:`8`', |
| 183 |
( |
| 184 |
'<p><span class="target" id="index-0"></span><a class="pep reference external" ' |
| 185 |
'href="https://peps.python.org/pep-0008/"><strong>PEP 8</strong></a></p>' |
| 186 |
), |
| 187 |
( |
| 188 |
'\\sphinxAtStartPar\n' |
| 189 |
'\\index{Python Enhancement Proposals@\\spxentry{Python Enhancement Proposals}' |
| 190 |
'!PEP 8@\\spxentry{PEP 8}}\\sphinxhref{https://peps.python.org/pep-0008/}' |
| 191 |
'{\\sphinxstylestrong{PEP 8}}' |
| 192 |
), |
| 193 |
), |
| 194 |
( |
| 195 |
# pep role with anchor |
| 196 |
':pep:`8#id1`', |
| 197 |
( |
| 198 |
'<p><span class="target" id="index-0"></span><a class="pep reference external" ' |
| 199 |
'href="https://peps.python.org/pep-0008/#id1">' |
| 200 |
'<strong>PEP 8#id1</strong></a></p>' |
| 201 |
), |
| 202 |
( |
| 203 |
'\\sphinxAtStartPar\n' |
| 204 |
'\\index{Python Enhancement Proposals@\\spxentry{Python Enhancement Proposals}' |
| 205 |
'!PEP 8\\#id1@\\spxentry{PEP 8\\#id1}}\\sphinxhref' |
| 206 |
'{https://peps.python.org/pep-0008/\\#id1}' |
| 207 |
'{\\sphinxstylestrong{PEP 8\\#id1}}' |
| 208 |
), |
| 209 |
), |
| 210 |
( |
| 211 |
# rfc role |
| 212 |
':rfc:`2324`', |
| 213 |
( |
| 214 |
'<p><span class="target" id="index-0"></span><a class="rfc reference external" ' |
| 215 |
'href="https://datatracker.ietf.org/doc/html/rfc2324.html"><strong>RFC 2324</strong></a></p>' |
| 216 |
), |
| 217 |
( |
| 218 |
'\\sphinxAtStartPar\n' |
| 219 |
'\\index{RFC@\\spxentry{RFC}!RFC 2324@\\spxentry{RFC 2324}}' |
| 220 |
'\\sphinxhref{https://datatracker.ietf.org/doc/html/rfc2324.html}' |
| 221 |
'{\\sphinxstylestrong{RFC 2324}}' |
| 222 |
), |
| 223 |
), |
| 224 |
( |
| 225 |
# rfc role with anchor |
| 226 |
':rfc:`2324#section-1`', |
| 227 |
( |
| 228 |
'<p><span class="target" id="index-0"></span><a class="rfc reference external" ' |
| 229 |
'href="https://datatracker.ietf.org/doc/html/rfc2324.html#section-1">' |
| 230 |
'<strong>RFC 2324 Section 1</strong></a></p>' |
| 231 |
), |
| 232 |
( |
| 233 |
'\\sphinxAtStartPar\n' |
| 234 |
'\\index{RFC@\\spxentry{RFC}!RFC 2324 Section 1@\\spxentry{RFC 2324 Section 1}}' |
| 235 |
'\\sphinxhref{https://datatracker.ietf.org/doc/html/rfc2324.html\\#section-1}' |
| 236 |
'{\\sphinxstylestrong{RFC 2324 Section 1}}' |
| 237 |
), |
| 238 |
), |
| 239 |
( |
| 240 |
# interpolation of arrows in menuselection |
| 241 |
':menuselection:`a --> b`', |
| 242 |
'<p><span class="menuselection">a \N{TRIANGULAR BULLET} b</span></p>', |
| 243 |
'\\sphinxAtStartPar\n\\sphinxmenuselection{a \\(\\rightarrow\\) b}', |
| 244 |
), |
| 245 |
( |
| 246 |
# interpolation of ampersands in menuselection |
| 247 |
':menuselection:`&Foo -&&- &Bar`', |
| 248 |
( |
| 249 |
'<p><span class="menuselection"><span class="accelerator">F</span>oo ' |
| 250 |
'-&- <span class="accelerator">B</span>ar</span></p>' |
| 251 |
), |
| 252 |
( |
| 253 |
'\\sphinxAtStartPar\n' |
| 254 |
r'\sphinxmenuselection{\sphinxaccelerator{F}oo \sphinxhyphen{}' |
| 255 |
r'\&\sphinxhyphen{} \sphinxaccelerator{B}ar}' |
| 256 |
), |
| 257 |
), |
| 258 |
( |
| 259 |
# interpolation of ampersands in guilabel |
| 260 |
':guilabel:`&Foo -&&- &Bar`', |
| 261 |
( |
| 262 |
'<p><span class="guilabel"><span class="accelerator">F</span>oo ' |
| 263 |
'-&- <span class="accelerator">B</span>ar</span></p>' |
| 264 |
), |
| 265 |
( |
| 266 |
'\\sphinxAtStartPar\n' |
| 267 |
r'\sphinxguilabel{\sphinxaccelerator{F}oo \sphinxhyphen{}\&\sphinxhyphen{} \sphinxaccelerator{B}ar}' |
| 268 |
), |
| 269 |
), |
| 270 |
( |
| 271 |
# no ampersands in guilabel |
| 272 |
':guilabel:`Foo`', |
| 273 |
'<p><span class="guilabel">Foo</span></p>', |
| 274 |
'\\sphinxAtStartPar\n\\sphinxguilabel{Foo}', |
| 275 |
), |
| 276 |
( |
| 277 |
# kbd role |
| 278 |
':kbd:`space`', |
| 279 |
'<p><kbd class="kbd docutils literal notranslate">space</kbd></p>', |
| 280 |
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{space}}', |
| 281 |
), |
| 282 |
( |
| 283 |
# kbd role |
| 284 |
':kbd:`Control+X`', |
| 285 |
( |
| 286 |
'<p>' |
| 287 |
'<kbd class="kbd docutils literal notranslate">Control</kbd>' |
| 288 |
'+' |
| 289 |
'<kbd class="kbd docutils literal notranslate">X</kbd>' |
| 290 |
'</p>' |
| 291 |
), |
| 292 |
( |
| 293 |
'\\sphinxAtStartPar\n' |
| 294 |
'\\sphinxkeyboard{\\sphinxupquote{Control}}' |
| 295 |
'+' |
| 296 |
'\\sphinxkeyboard{\\sphinxupquote{X}}' |
| 297 |
), |
| 298 |
), |
| 299 |
( |
| 300 |
# kbd role |
| 301 |
':kbd:`Alt+^`', |
| 302 |
( |
| 303 |
'<p>' |
| 304 |
'<kbd class="kbd docutils literal notranslate">Alt</kbd>' |
| 305 |
'+' |
| 306 |
'<kbd class="kbd docutils literal notranslate">^</kbd>' |
| 307 |
'</p>' |
| 308 |
), |
| 309 |
( |
| 310 |
'\\sphinxAtStartPar\n' |
| 311 |
'\\sphinxkeyboard{\\sphinxupquote{Alt}}' |
| 312 |
'+' |
| 313 |
'\\sphinxkeyboard{\\sphinxupquote{\\textasciicircum{}}}' |
| 314 |
), |
| 315 |
), |
| 316 |
( |
| 317 |
# kbd role |
| 318 |
':kbd:`M-x M-s`', |
| 319 |
( |
| 320 |
'<p>' |
| 321 |
'<kbd class="kbd docutils literal notranslate">M</kbd>' |
| 322 |
'-' |
| 323 |
'<kbd class="kbd docutils literal notranslate">x</kbd>' |
| 324 |
' ' |
| 325 |
'<kbd class="kbd docutils literal notranslate">M</kbd>' |
| 326 |
'-' |
| 327 |
'<kbd class="kbd docutils literal notranslate">s</kbd>' |
| 328 |
'</p>' |
| 329 |
), |
| 330 |
( |
| 331 |
'\\sphinxAtStartPar\n' |
| 332 |
'\\sphinxkeyboard{\\sphinxupquote{M}}' |
| 333 |
'\\sphinxhyphen{}' |
| 334 |
'\\sphinxkeyboard{\\sphinxupquote{x}}' |
| 335 |
' ' |
| 336 |
'\\sphinxkeyboard{\\sphinxupquote{M}}' |
| 337 |
'\\sphinxhyphen{}' |
| 338 |
'\\sphinxkeyboard{\\sphinxupquote{s}}' |
| 339 |
), |
| 340 |
), |
| 341 |
( |
| 342 |
# kbd role |
| 343 |
':kbd:`-`', |
| 344 |
'<p><kbd class="kbd docutils literal notranslate">-</kbd></p>', |
| 345 |
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{\\sphinxhyphen{}}}', |
| 346 |
), |
| 347 |
( |
| 348 |
# kbd role |
| 349 |
':kbd:`Caps Lock`', |
| 350 |
'<p><kbd class="kbd docutils literal notranslate">Caps Lock</kbd></p>', |
| 351 |
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{Caps Lock}}', |
| 352 |
), |
| 353 |
( |
| 354 |
# kbd role |
| 355 |
':kbd:`sys rq`', |
| 356 |
'<p><kbd class="kbd docutils literal notranslate">sys rq</kbd></p>', |
| 357 |
'\\sphinxAtStartPar\n\\sphinxkeyboard{\\sphinxupquote{sys rq}}', |
| 358 |
), |
| 359 |
( |
| 360 |
# kbd role |
| 361 |
':kbd:`⌘+⇧+M`', |
| 362 |
( |
| 363 |
'<p>' |
| 364 |
'<kbd class="kbd docutils literal notranslate">⌘</kbd>' |
| 365 |
'+' |
| 366 |
'<kbd class="kbd docutils literal notranslate">⇧</kbd>' |
| 367 |
'+' |
| 368 |
'<kbd class="kbd docutils literal notranslate">M</kbd>' |
| 369 |
'</p>' |
| 370 |
), |
| 371 |
( |
| 372 |
'\\sphinxAtStartPar\n' |
| 373 |
'\\sphinxkeyboard{\\sphinxupquote{⌘}}' |
| 374 |
'+' |
| 375 |
'\\sphinxkeyboard{\\sphinxupquote{⇧}}' |
| 376 |
'+' |
| 377 |
'\\sphinxkeyboard{\\sphinxupquote{M}}' |
| 378 |
), |
| 379 |
), |
| 380 |
( |
| 381 |
# verify smarty-pants quotes |
| 382 |
'"John"', |
| 383 |
'<p>“John”</p>', |
| 384 |
'\\sphinxAtStartPar\n“John”', |
| 385 |
), |
| 386 |
( |
| 387 |
# ... but not in literal text |
| 388 |
'``"John"``', |
| 389 |
( |
| 390 |
'<p><code class="docutils literal notranslate"><span class="pre">' |
| 391 |
'"John"</span></code></p>' |
| 392 |
), |
| 393 |
'\\sphinxAtStartPar\n\\sphinxcode{\\sphinxupquote{"John"}}', |
| 394 |
), |
| 395 |
( |
| 396 |
# verify classes for inline roles |
| 397 |
':manpage:`mp(1)`', |
| 398 |
'<p><em class="manpage">mp(1)</em></p>', |
| 399 |
'\\sphinxAtStartPar\n\\sphinxstyleliteralemphasis{\\sphinxupquote{mp(1)}}', |
| 400 |
), |
| 401 |
( |
| 402 |
# correct escaping in normal mode |
| 403 |
'Γ\\\\∞$', |
| 404 |
None, |
| 405 |
'\\sphinxAtStartPar\nΓ\\textbackslash{}\\(\\infty\\)\\$', |
| 406 |
), |
| 407 |
( |
| 408 |
# in verbatim code fragments |
| 409 |
'::\n\n @Γ\\∞${}', |
| 410 |
None, |
| 411 |
( |
| 412 |
'\\begin{sphinxVerbatim}[commandchars=\\\\\\{\\}]\n' |
| 413 |
'@Γ\\PYGZbs{}\\(\\infty\\)\\PYGZdl{}\\PYGZob{}\\PYGZcb{}\n' |
| 414 |
'\\end{sphinxVerbatim}' |
| 415 |
), |
| 416 |
), |
| 417 |
( |
| 418 |
# description list: simple |
| 419 |
'term\n description', |
| 420 |
'<dl class="simple">\n<dt>term</dt><dd><p>description</p>\n</dd>\n</dl>', |
| 421 |
None, |
| 422 |
), |
| 423 |
( |
| 424 |
# description list: with classifiers |
| 425 |
'term : class1 : class2\n description', |
| 426 |
( |
| 427 |
'<dl class="simple">\n<dt>term<span class="classifier">class1</span>' |
| 428 |
'<span class="classifier">class2</span></dt><dd><p>description</p>\n</dd>\n</dl>' |
| 429 |
), |
| 430 |
None, |
| 431 |
), |
| 432 |
( |
| 433 |
# glossary (description list): multiple terms |
| 434 |
'.. glossary::\n\n term1\n term2\n description', |
| 435 |
( |
| 436 |
'<dl class="simple glossary">\n' |
| 437 |
'<dt id="term-term1">term1<a class="headerlink" href="#term-term1"' |
| 438 |
' title="Link to this term">¶</a></dt>' |
| 439 |
'<dt id="term-term2">term2<a class="headerlink" href="#term-term2"' |
| 440 |
' title="Link to this term">¶</a></dt>' |
| 441 |
'<dd><p>description</p>\n</dd>\n</dl>' |
| 442 |
), |
| 443 |
None, |
| 444 |
), |
| 445 |
( |
| 446 |
# backslash escaping (docutils 0.16) |
| 447 |
r'4 backslashes \\\\', |
| 448 |
r'<p>4 backslashes \\</p>', |
| 449 |
None, |
| 450 |
), |
| 451 |
], |
| 452 |
) |
| 453 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 454 |
def test_inline( |
| 455 |
app: SphinxTestApp, rst: str, html_expected: str, latex_expected: str |
| 456 |
) -> None: |
| 457 |
if html_expected: |
| 458 |
html_translated = rst_to_html(rst, app=app) |
| 459 |
assert html_expected == html_translated, f'from {rst!r}' |
| 460 |
if latex_expected: |
| 461 |
latex_translated = rst_to_latex(rst, app=app) |
| 462 |
assert latex_expected == latex_translated, f'from {rst!r}' |
| 463 |
|
| 464 |
|
| 465 |
@pytest.mark.parametrize( |
| 466 |
('rst', 'html_expected', 'latex_expected'), |
| 467 |
[ |
| 468 |
( |
| 469 |
# correct interpretation of code with whitespace |
| 470 |
'``code sample``', |
| 471 |
( |
| 472 |
'<p><code class="(samp )?docutils literal notranslate"><span class="pre">' |
| 473 |
'code</span>   <span class="pre">sample</span></code></p>' |
| 474 |
), |
| 475 |
r'\\sphinxAtStartPar\n\\sphinxcode{\\sphinxupquote{code sample}}', |
| 476 |
), |
| 477 |
( |
| 478 |
# non-interpolation of dashes in option role |
| 479 |
':option:`--with-option`', |
| 480 |
( |
| 481 |
'<p><code( class="xref std std-option docutils literal notranslate")?>' |
| 482 |
'<span class="pre">--with-option</span></code></p>$' |
| 483 |
), |
| 484 |
( |
| 485 |
r'\\sphinxAtStartPar\n' |
| 486 |
r'\\sphinxcode{\\sphinxupquote{\\sphinxhyphen{}\\sphinxhyphen{}with\\sphinxhyphen{}option}}$' |
| 487 |
), |
| 488 |
), |
| 489 |
( |
| 490 |
# in URIs |
| 491 |
'`test <https://www.google.com/~me/>`_', |
| 492 |
None, |
| 493 |
r'\\sphinxAtStartPar\n\\sphinxhref{https://www.google.com/~me/}{test}.*', |
| 494 |
), |
| 495 |
], |
| 496 |
) |
| 497 |
@pytest.mark.sphinx('html', testroot='_blank') |
| 498 |
def test_inline_regex( |
| 499 |
app: SphinxTestApp, rst: str, html_expected: str, latex_expected: str |
| 500 |
) -> None: |
| 501 |
if html_expected: |
| 502 |
html_translated = rst_to_html(rst, app=app) |
| 503 |
assert re.match(html_expected, html_translated), f'from {rst!r}' |
| 504 |
if latex_expected: |
| 505 |
latex_translated = rst_to_latex(rst, app=app) |
| 506 |
assert re.match(latex_expected, latex_translated), f'from {rst!r}' |
| 507 |
|
| 508 |
|
| 509 |
@pytest.mark.sphinx( |
| 510 |
'dummy', |
| 511 |
testroot='_blank', |
| 512 |
confoverrides={'latex_engine': 'xelatex'}, |
| 513 |
) |
| 514 |
@pytest.mark.parametrize( |
| 515 |
('rst', 'latex_expected'), |
| 516 |
[ |
| 517 |
( |
| 518 |
# in verbatim code fragments |
| 519 |
'::\n\n @Γ\\∞${}', |
| 520 |
( |
| 521 |
'\\begin{sphinxVerbatim}[commandchars=\\\\\\{\\}]\n' |
| 522 |
'@Γ\\PYGZbs{}∞\\PYGZdl{}\\PYGZob{}\\PYGZcb{}\n' |
| 523 |
'\\end{sphinxVerbatim}' |
| 524 |
), |
| 525 |
), |
| 526 |
], |
| 527 |
) |
| 528 |
def test_inline_for_unicode_latex_engine( |
| 529 |
app: SphinxTestApp, rst: str, latex_expected: str |
| 530 |
) -> None: |
| 531 |
latex_translated = rst_to_latex(rst, app=app) |
| 532 |
assert latex_expected == latex_translated, f'from {rst!r}' |
| 533 |
|
| 534 |
|
| 535 |
@pytest.mark.sphinx('dummy', testroot='_blank') |
| 536 |
def test_samp_role(app: SphinxTestApp) -> None: |
| 537 |
# no braces |
| 538 |
text = ':samp:`a{b}c`' |
| 539 |
doctree = parse_rst(text, env=app.env) |
| 540 |
assert_node( |
| 541 |
doctree[0], [nodes.paragraph, nodes.literal, ('a', [nodes.emphasis, 'b'], 'c')] |
| 542 |
) |
| 543 |
# nested braces |
| 544 |
text = ':samp:`a{{b}}c`' |
| 545 |
doctree = parse_rst(text, env=app.env) |
| 546 |
assert_node( |
| 547 |
doctree[0], |
| 548 |
[nodes.paragraph, nodes.literal, ('a', [nodes.emphasis, '{b'], '}c')], |
| 549 |
) |
| 550 |
|
| 551 |
# half-opened braces |
| 552 |
text = ':samp:`a{bc`' |
| 553 |
doctree = parse_rst(text, env=app.env) |
| 554 |
assert_node(doctree[0], [nodes.paragraph, nodes.literal, 'a{bc']) |
| 555 |
|
| 556 |
# escaped braces |
| 557 |
text = ':samp:`a\\\\{b}c`' |
| 558 |
doctree = parse_rst(text, env=app.env) |
| 559 |
assert_node(doctree[0], [nodes.paragraph, nodes.literal, 'a{b}c']) |
| 560 |
|
| 561 |
# no braces (whitespaces are keeped as is) |
| 562 |
text = ':samp:`code sample`' |
| 563 |
doctree = parse_rst(text, env=app.env) |
| 564 |
assert_node(doctree[0], [nodes.paragraph, nodes.literal, 'code sample']) |
| 565 |
|
| 566 |
|
| 567 |
@pytest.mark.sphinx('dummy', testroot='_blank') |
| 568 |
def test_download_role(app: SphinxTestApp) -> None: |
| 569 |
# implicit |
| 570 |
text = ':download:`sphinx.rst`' |
| 571 |
doctree = parse_rst(text, env=app.env) |
| 572 |
assert_node( |
| 573 |
doctree[0], |
| 574 |
[nodes.paragraph, addnodes.download_reference, nodes.literal, 'sphinx.rst'], |
| 575 |
) |
| 576 |
assert_node( |
| 577 |
extract_node(doctree, 0, 0), |
| 578 |
refdoc='dummy', |
| 579 |
refdomain='', |
| 580 |
reftype='download', |
| 581 |
refexplicit=False, |
| 582 |
reftarget='sphinx.rst', |
| 583 |
refwarn=False, |
| 584 |
) |
| 585 |
assert_node(extract_node(doctree, 0, 0, 0), classes=['xref', 'download']) |
| 586 |
|
| 587 |
# explicit |
| 588 |
text = ':download:`reftitle <sphinx.rst>`' |
| 589 |
doctree = parse_rst(text, env=app.env) |
| 590 |
assert_node( |
| 591 |
doctree[0], |
| 592 |
[nodes.paragraph, addnodes.download_reference, nodes.literal, 'reftitle'], |
| 593 |
) |
| 594 |
assert_node( |
| 595 |
extract_node(doctree, 0, 0), |
| 596 |
refdoc='dummy', |
| 597 |
refdomain='', |
| 598 |
reftype='download', |
| 599 |
refexplicit=True, |
| 600 |
reftarget='sphinx.rst', |
| 601 |
refwarn=False, |
| 602 |
) |
| 603 |
assert_node(extract_node(doctree, 0, 0, 0), classes=['xref', 'download']) |
| 604 |
|
| 605 |
|
| 606 |
@pytest.mark.sphinx('dummy', testroot='_blank') |
| 607 |
def test_XRefRole(app: SphinxTestApp) -> None: |
| 608 |
inliner = new_inliner(app.env) |
| 609 |
role = XRefRole() |
| 610 |
|
| 611 |
# implicit |
| 612 |
doctrees, errors = role('ref', 'rawtext', 'text', 5, inliner, {}, []) # type: ignore[arg-type] |
| 613 |
assert len(doctrees) == 1 |
| 614 |
assert_node(doctrees[0], [addnodes.pending_xref, nodes.literal, 'text']) |
| 615 |
assert_node( |
| 616 |
doctrees[0], |
| 617 |
refdoc='dummy', |
| 618 |
refdomain='', |
| 619 |
reftype='ref', |
| 620 |
reftarget='text', |
| 621 |
refexplicit=False, |
| 622 |
refwarn=False, |
| 623 |
) |
| 624 |
assert errors == [] |
| 625 |
|
| 626 |
# explicit |
| 627 |
doctrees, errors = role('ref', 'rawtext', 'title <target>', 5, inliner, {}, []) # type: ignore[arg-type] |
| 628 |
assert_node(doctrees[0], [addnodes.pending_xref, nodes.literal, 'title']) |
| 629 |
assert_node( |
| 630 |
doctrees[0], |
| 631 |
refdoc='dummy', |
| 632 |
refdomain='', |
| 633 |
reftype='ref', |
| 634 |
reftarget='target', |
| 635 |
refexplicit=True, |
| 636 |
refwarn=False, |
| 637 |
) |
| 638 |
|
| 639 |
# bang |
| 640 |
doctrees, errors = role('ref', 'rawtext', '!title <target>', 5, inliner, {}, []) # type: ignore[arg-type] |
| 641 |
assert_node(doctrees[0], [nodes.literal, 'title <target>']) |
| 642 |
|
| 643 |
# refdomain |
| 644 |
doctrees, errors = role('test:doc', 'rawtext', 'text', 5, inliner, {}, []) # type: ignore[arg-type] |
| 645 |
assert_node(doctrees[0], [addnodes.pending_xref, nodes.literal, 'text']) |
| 646 |
assert_node( |
| 647 |
doctrees[0], |
| 648 |
refdoc='dummy', |
| 649 |
refdomain='test', |
| 650 |
reftype='doc', |
| 651 |
reftarget='text', |
| 652 |
refexplicit=False, |
| 653 |
refwarn=False, |
| 654 |
) |
| 655 |
|
| 656 |
# fix_parens |
| 657 |
role = XRefRole(fix_parens=True) |
| 658 |
doctrees, errors = role('ref', 'rawtext', 'text()', 5, inliner, {}, []) # type: ignore[arg-type] |
| 659 |
assert_node(doctrees[0], [addnodes.pending_xref, nodes.literal, 'text()']) |
| 660 |
assert_node( |
| 661 |
doctrees[0], |
| 662 |
refdoc='dummy', |
| 663 |
refdomain='', |
| 664 |
reftype='ref', |
| 665 |
reftarget='text', |
| 666 |
refexplicit=False, |
| 667 |
refwarn=False, |
| 668 |
) |
| 669 |
|
| 670 |
# lowercase |
| 671 |
role = XRefRole(lowercase=True) |
| 672 |
doctrees, errors = role('ref', 'rawtext', 'TEXT', 5, inliner, {}, []) # type: ignore[arg-type] |
| 673 |
assert_node(doctrees[0], [addnodes.pending_xref, nodes.literal, 'TEXT']) |
| 674 |
assert_node( |
| 675 |
doctrees[0], |
| 676 |
refdoc='dummy', |
| 677 |
refdomain='', |
| 678 |
reftype='ref', |
| 679 |
reftarget='text', |
| 680 |
refexplicit=False, |
| 681 |
refwarn=False, |
| 682 |
) |
| 683 |
|
| 684 |
|
| 685 |
@pytest.mark.sphinx('dummy', testroot='prolog') |
| 686 |
def test_rst_prolog(app: SphinxTestApp) -> None: |
| 687 |
app.build(force_all=True) |
| 688 |
rst = app.env.get_doctree('restructuredtext') |
| 689 |
md = app.env.get_doctree('markdown') |
| 690 |
|
| 691 |
# rst_prolog |
| 692 |
assert isinstance(rst[0], nodes.paragraph) |
| 693 |
assert_node(extract_node(rst, 0, 0), nodes.emphasis) |
| 694 |
assert_node(extract_node(rst, 0, 0, 0), nodes.Text) |
| 695 |
assert extract_node(rst, 0, 0, 0) == 'Hello world' |
| 696 |
|
| 697 |
# rst_epilog |
| 698 |
assert isinstance(rst[-1], nodes.section) |
| 699 |
assert_node(extract_node(rst, -1, -1), nodes.paragraph) |
| 700 |
assert_node(extract_node(rst, -1, -1, 0), nodes.emphasis) |
| 701 |
assert_node(extract_node(rst, -1, -1, 0, 0), nodes.Text) |
| 702 |
assert extract_node(rst, -1, -1, 0, 0) == 'Good-bye world' |
| 703 |
|
| 704 |
# rst_prolog & rst_epilog on exlucding reST parser |
| 705 |
assert not md.rawsource.startswith('*Hello world*.') |
| 706 |
assert not md.rawsource.endswith('*Good-bye world*.\n') |
| 707 |
|
| 708 |
|
| 709 |
@pytest.mark.sphinx('dummy', testroot='keep_warnings') |
| 710 |
def test_keep_warnings_is_True(app: SphinxTestApp) -> None: |
| 711 |
app.build(force_all=True) |
| 712 |
doctree = app.env.get_doctree('index') |
| 713 |
assert isinstance(doctree[0], nodes.section) |
| 714 |
assert len(doctree[0]) == 2 |
| 715 |
assert_node(extract_node(doctree, 0, 1), nodes.system_message) |
| 716 |
|
| 717 |
|
| 718 |
@pytest.mark.sphinx( |
| 719 |
'dummy', |
| 720 |
testroot='keep_warnings', |
| 721 |
confoverrides={'keep_warnings': False}, |
| 722 |
) |
| 723 |
def test_keep_warnings_is_False(app: SphinxTestApp) -> None: |
| 724 |
app.build(force_all=True) |
| 725 |
doctree = app.env.get_doctree('index') |
| 726 |
assert isinstance(doctree[0], nodes.section) |
| 727 |
assert len(doctree[0]) == 1 |
| 728 |
|
| 729 |
|
| 730 |
@pytest.mark.sphinx('dummy', testroot='refonly_bullet_list') |
| 731 |
def test_compact_refonly_bullet_list(app: SphinxTestApp) -> None: |
| 732 |
app.build(force_all=True) |
| 733 |
doctree = app.env.get_doctree('index') |
| 734 |
assert isinstance(doctree[0], nodes.section) |
| 735 |
assert len(doctree[0]) == 5 |
| 736 |
|
| 737 |
assert extract_node(doctree, 0, 1).astext() == 'List A:' |
| 738 |
assert_node(extract_node(doctree, 0, 2), nodes.bullet_list) |
| 739 |
assert_node(extract_node(doctree, 0, 2, 0, 0), addnodes.compact_paragraph) |
| 740 |
assert extract_node(doctree, 0, 2, 0, 0).astext() == 'genindex' |
| 741 |
|
| 742 |
assert extract_node(doctree, 0, 3).astext() == 'List B:' |
| 743 |
assert_node(extract_node(doctree, 0, 4), nodes.bullet_list) |
| 744 |
assert_node(extract_node(doctree, 0, 4, 0, 0), nodes.paragraph) |
| 745 |
assert extract_node(doctree, 0, 4, 0, 0).astext() == 'Hello' |
| 746 |
|
| 747 |
|
| 748 |
@pytest.mark.sphinx('dummy', testroot='default_role') |
| 749 |
def test_default_role1(app: SphinxTestApp) -> None: |
| 750 |
app.build(force_all=True) |
| 751 |
|
| 752 |
# default-role: pep |
| 753 |
doctree = app.env.get_doctree('index') |
| 754 |
assert isinstance(doctree[0], nodes.section) |
| 755 |
assert_node(extract_node(doctree, 0, 1), nodes.paragraph) |
| 756 |
assert_node(extract_node(doctree, 0, 1, 0), addnodes.index) |
| 757 |
assert_node(extract_node(doctree, 0, 1, 1), nodes.target) |
| 758 |
assert_node(extract_node(doctree, 0, 1, 2), nodes.reference, classes=['pep']) |
| 759 |
|
| 760 |
# no default-role |
| 761 |
doctree = app.env.get_doctree('foo') |
| 762 |
assert isinstance(doctree[0], nodes.section) |
| 763 |
assert_node(extract_node(doctree, 0, 1), nodes.paragraph) |
| 764 |
assert_node(extract_node(doctree, 0, 1, 0), nodes.title_reference) |
| 765 |
assert_node(extract_node(doctree, 0, 1, 1), nodes.Text) |
| 766 |
|
| 767 |
|
| 768 |
@pytest.mark.sphinx( |
| 769 |
'dummy', |
| 770 |
testroot='default_role', |
| 771 |
confoverrides={'default_role': 'guilabel'}, |
| 772 |
) |
| 773 |
def test_default_role2(app: SphinxTestApp) -> None: |
| 774 |
app.build(force_all=True) |
| 775 |
|
| 776 |
# default-role directive is stronger than configratuion |
| 777 |
doctree = app.env.get_doctree('index') |
| 778 |
assert isinstance(doctree[0], nodes.section) |
| 779 |
assert_node(extract_node(doctree, 0, 1), nodes.paragraph) |
| 780 |
assert_node(extract_node(doctree, 0, 1, 0), addnodes.index) |
| 781 |
assert_node(extract_node(doctree, 0, 1, 1), nodes.target) |
| 782 |
assert_node(extract_node(doctree, 0, 1, 2), nodes.reference, classes=['pep']) |
| 783 |
|
| 784 |
# default_role changes the default behavior |
| 785 |
doctree = app.env.get_doctree('foo') |
| 786 |
assert isinstance(doctree[0], nodes.section) |
| 787 |
assert_node(extract_node(doctree, 0, 1), nodes.paragraph) |
| 788 |
assert_node(extract_node(doctree, 0, 1, 0), nodes.inline, classes=['guilabel']) |
| 789 |
assert_node(extract_node(doctree, 0, 1, 1), nodes.Text) |