diff --git a/sphinx/apidoc.py b/sphinx/apidoc.py index 19a711370..a8e819acd 100644 --- a/sphinx/apidoc.py +++ b/sphinx/apidoc.py @@ -25,6 +25,7 @@ from fnmatch import fnmatch from sphinx.util.osutil import FileAvoidWrite, walk from sphinx import __display_version__ +from sphinx.util import rst # automodule options if 'SPHINX_APIDOC_OPTIONS' in os.environ: @@ -67,8 +68,10 @@ def write_file(name, text, opts): f.write(text) -def format_heading(level, text): +def format_heading(level, text, escape=True): """Create a heading of [1, 2 or 3 supported].""" + if escape: + text = rst.escape(text) underlining = ['=', '-', '~', ][level - 1] * len(text) 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'): """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 += ' :maxdepth: %s\n\n' % opts.maxdepth diff --git a/tests/root/trailing_underscore/package_/__init__.py b/tests/root/trailing_underscore/package_/__init__.py new file mode 100644 index 000000000..b09612b83 --- /dev/null +++ b/tests/root/trailing_underscore/package_/__init__.py @@ -0,0 +1 @@ +""" A package with trailing underscores """ diff --git a/tests/root/trailing_underscore/package_/module_.py b/tests/root/trailing_underscore/package_/module_.py new file mode 100644 index 000000000..9902551ee --- /dev/null +++ b/tests/root/trailing_underscore/package_/module_.py @@ -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 """ diff --git a/tests/test_apidoc.py b/tests/test_apidoc.py index d44868aeb..d161fcd05 100644 --- a/tests/test_apidoc.py +++ b/tests/test_apidoc.py @@ -19,6 +19,8 @@ from sphinx.apidoc import main as apidoc_main from util import rootdir, remove_unicode_literals +from sphinx.util.rst import escape as rst_escape + @pytest.fixture() 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: 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\n" in rst with open(outdir / 'a.b.x.rst') as f: 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\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._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( coderoot=(rootdir / 'root'),