""" test_markup ~~~~~~~~~~~ Test various Sphinx-specific markup extensions. :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ import pickle import re import pytest from docutils import frontend, utils, nodes from docutils.parsers.rst import Parser as RstParser from docutils.transforms.universal import SmartQuotes from sphinx import addnodes from sphinx.builders.latex import LaTeXBuilder from sphinx.testing.util import assert_node from sphinx.util import texescape from sphinx.util.docutils import sphinx_domains from sphinx.writers.html import HTMLWriter, HTMLTranslator from sphinx.writers.latex import LaTeXWriter, LaTeXTranslator @pytest.fixture def settings(app): texescape.init() # otherwise done by the latex builder optparser = frontend.OptionParser( components=(RstParser, HTMLWriter, LaTeXWriter)) settings = optparser.get_default_values() settings.smart_quotes = True settings.env = app.builder.env settings.env.temp_data['docname'] = 'dummy' settings.contentsname = 'dummy' settings.rfc_base_url = 'http://tools.ietf.org/html/' domain_context = sphinx_domains(settings.env) domain_context.enable() yield settings domain_context.disable() @pytest.fixture def parse(settings): def parse_(rst): document = utils.new_document(b'test data', settings) document['file'] = 'dummy' parser = RstParser() parser.parse(rst, document) SmartQuotes(document, startnode=None).apply() for msg in document.traverse(nodes.system_message): if msg['level'] == 1: msg.replace_self([]) return document return parse_ # since we're not resolving the markup afterwards, these nodes may remain class ForgivingTranslator: def visit_pending_xref(self, node): pass def depart_pending_xref(self, node): pass class ForgivingHTMLTranslator(HTMLTranslator, ForgivingTranslator): pass class ForgivingLaTeXTranslator(LaTeXTranslator, ForgivingTranslator): pass @pytest.fixture def verify_re_html(app, parse): def verify(rst, html_expected): document = parse(rst) html_translator = ForgivingHTMLTranslator(document, app.builder) document.walkabout(html_translator) html_translated = ''.join(html_translator.fragment).strip() assert re.match(html_expected, html_translated), 'from ' + rst return verify @pytest.fixture def verify_re_latex(app, parse): def verify(rst, latex_expected): document = parse(rst) app.builder = LaTeXBuilder(app) app.builder.set_environment(app.env) app.builder.init() latex_translator = ForgivingLaTeXTranslator(document, app.builder) latex_translator.first_document = -1 # don't write \begin{document} document.walkabout(latex_translator) latex_translated = ''.join(latex_translator.body).strip() assert re.match(latex_expected, latex_translated), 'from ' + repr(rst) return verify @pytest.fixture def verify_re(verify_re_html, verify_re_latex): def verify_re_(rst, html_expected, latex_expected): if html_expected: verify_re_html(rst, html_expected) if latex_expected: verify_re_latex(rst, latex_expected) return verify_re_ @pytest.fixture def verify(verify_re_html, verify_re_latex): def verify_(rst, html_expected, latex_expected): if html_expected: verify_re_html(rst, re.escape(html_expected) + '$') if latex_expected: verify_re_latex(rst, re.escape(latex_expected) + '$') return verify_ @pytest.fixture def get_verifier(verify, verify_re): v = { 'verify': verify, 'verify_re': verify_re, } def get(name): return v[name] return get @pytest.mark.parametrize('type,rst,html_expected,latex_expected', [ ( # pep role 'verify', ':pep:`8`', ('
'), ('\\index{Python Enhancement Proposals@\\spxentry{Python Enhancement Proposals}' '!PEP 8@\\spxentry{PEP 8}}\\sphinxhref{http://www.python.org/dev/peps/pep-0008}' '{\\sphinxstylestrong{PEP 8}}') ), ( # pep role with anchor 'verify', ':pep:`8#id1`', (''), ('\\index{Python Enhancement Proposals@\\spxentry{Python Enhancement Proposals}' '!PEP 8\\#id1@\\spxentry{PEP 8\\#id1}}\\sphinxhref' '{http://www.python.org/dev/peps/pep-0008\\#id1}' '{\\sphinxstylestrong{PEP 8\\#id1}}') ), ( # rfc role 'verify', ':rfc:`2324`', (''), ('\\index{RFC@\\spxentry{RFC}!RFC 2324@\\spxentry{RFC 2324}}' '\\sphinxhref{http://tools.ietf.org/html/rfc2324.html}' '{\\sphinxstylestrong{RFC 2324}}') ), ( # rfc role with anchor 'verify', ':rfc:`2324#id1`', (''), ('\\index{RFC@\\spxentry{RFC}!RFC 2324\\#id1@\\spxentry{RFC 2324\\#id1}}' '\\sphinxhref{http://tools.ietf.org/html/rfc2324.html\\#id1}' '{\\sphinxstylestrong{RFC 2324\\#id1}}') ), ( # correct interpretation of code with whitespace 'verify_re', '``code sample``', (''
'code sample
'), '\\sphinxmenuselection{a \\(\\rightarrow\\) b}', ), ( # interpolation of ampersands in menuselection 'verify', ':menuselection:`&Foo -&&- &Bar`', ('
'), r'\sphinxmenuselection{\sphinxaccelerator{F}oo -\&- \sphinxaccelerator{B}ar}', ), ( # interpolation of ampersands in guilabel 'verify', ':guilabel:`&Foo -&&- &Bar`', ('
Foo ' '-&- Bar
'), r'\sphinxguilabel{\sphinxaccelerator{F}oo -\&- \sphinxaccelerator{B}ar}', ), ( # no ampersands in guilabel 'verify', ':guilabel:`Foo`', 'Foo
', r'\sphinxguilabel{Foo}', ), ( # non-interpolation of dashes in option role 'verify_re', ':option:`--with-option`', (''
'--with-option
“John”
', "“John”", ), ( # ... but not in literal text 'verify', '``"John"``', (''
'"John"
mp(1)
', '\\sphinxstyleliteralemphasis{\\sphinxupquote{mp(1)}}', ), ( # correct escaping in normal mode 'verify', 'Γ\\\\∞$', None, 'Γ\\textbackslash{}\\(\\infty\\)\\$', ), ( # in verbatim code fragments 'verify', '::\n\n @Γ\\∞${}', None, ('\\begin{sphinxVerbatim}[commandchars=\\\\\\{\\}]\n' '@Γ\\PYGZbs{}\\(\\infty\\)\\PYGZdl{}\\PYGZob{}\\PYGZcb{}\n' '\\end{sphinxVerbatim}'), ), ( # in URIs 'verify_re', '`test