Fix issues with trailing underscores in heading names

Fixes #1451, using the approach in 8d96c90fc6
This commit is contained in:
Eric Wieser 2017-03-23 00:21:04 +00:00
parent 895014cd71
commit 3e7bee5836
4 changed files with 26 additions and 4 deletions

View File

@ -25,6 +25,7 @@ from fnmatch import fnmatch
from sphinx.util.osutil import FileAvoidWrite, walk from sphinx.util.osutil import FileAvoidWrite, walk
from sphinx import __display_version__ from sphinx import __display_version__
from sphinx.util import rst
# automodule options # automodule options
if 'SPHINX_APIDOC_OPTIONS' in os.environ: if 'SPHINX_APIDOC_OPTIONS' in os.environ:
@ -67,8 +68,10 @@ def write_file(name, text, opts):
f.write(text) f.write(text)
def format_heading(level, text): def format_heading(level, text, escape=True):
"""Create a heading of <level> [1, 2 or 3 supported].""" """Create a heading of <level> [1, 2 or 3 supported]."""
if escape:
text = rst.escape(text)
underlining = ['=', '-', '~', ][level - 1] * len(text) underlining = ['=', '-', '~', ][level - 1] * len(text)
return '%s\n%s\n\n' % (text, underlining) return '%s\n%s\n\n' % (text, underlining)
@ -149,7 +152,7 @@ def create_package_file(root, master_package, subroot, py_files, opts, subs, is_
def create_modules_toc_file(modules, opts, name='modules'): def create_modules_toc_file(modules, opts, name='modules'):
"""Create the module's index.""" """Create the module's index."""
text = format_heading(1, '%s' % opts.header) text = format_heading(1, '%s' % opts.header, escape=False)
text += '.. toctree::\n' text += '.. toctree::\n'
text += ' :maxdepth: %s\n\n' % opts.maxdepth text += ' :maxdepth: %s\n\n' % opts.maxdepth

View File

@ -0,0 +1 @@
""" A package with trailing underscores """

View File

@ -0,0 +1,7 @@
""" A module with a trailing underscore """
class SomeClass_:
""" A class with a trailing underscore """
def some_function_(some_arg_):
""" A function with a trailing underscore in name and argument """

View File

@ -19,6 +19,8 @@ from sphinx.apidoc import main as apidoc_main
from util import rootdir, remove_unicode_literals from util import rootdir, remove_unicode_literals
from sphinx.util.rst import escape as rst_escape
@pytest.fixture() @pytest.fixture()
def apidoc(tempdir, apidoc_params): def apidoc(tempdir, apidoc_params):
@ -71,13 +73,13 @@ def test_pep_0420_enabled(make_app, apidoc):
with open(outdir / 'a.b.c.rst') as f: with open(outdir / 'a.b.c.rst') as f:
rst = f.read() rst = f.read()
assert "a.b.c package\n" in rst assert rst_escape("a.b.c package\n") in rst
assert "automodule:: a.b.c.d\n" in rst assert "automodule:: a.b.c.d\n" in rst
assert "automodule:: a.b.c\n" in rst assert "automodule:: a.b.c\n" in rst
with open(outdir / 'a.b.x.rst') as f: with open(outdir / 'a.b.x.rst') as f:
rst = f.read() rst = f.read()
assert "a.b.x namespace\n" in rst assert rst_escape("a.b.x namespace\n") in rst
assert "automodule:: a.b.x.y\n" in rst assert "automodule:: a.b.x.y\n" in rst
assert "automodule:: a.b.x\n" not in rst assert "automodule:: a.b.x\n" not in rst
@ -118,6 +120,15 @@ def test_pep_0420_disabled_top_level_verify(make_app, apidoc):
print(app._status.getvalue()) print(app._status.getvalue())
print(app._warning.getvalue()) print(app._warning.getvalue())
@pytest.mark.apidoc(coderoot=(rootdir / 'root' / 'trailing_underscore'))
def test_trailing_underscore(make_app, apidoc):
outdir = apidoc.outdir
assert (outdir / 'conf.py').isfile()
assert (outdir / 'package_.rst').isfile()
with open(outdir / 'package_.rst') as f:
rst = f.read()
assert rst_escape("package_ package\n") in rst
assert rst_escape("package_.module_ module\n") in rst
@pytest.mark.apidoc( @pytest.mark.apidoc(
coderoot=(rootdir / 'root'), coderoot=(rootdir / 'root'),