From ffb2130b55d63b0db40144dd8eee6bd4bfa7181d Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 4 Nov 2016 15:17:50 -0700 Subject: [PATCH 1/6] Implement prepare_docstring filter that wraps Sphinx's This allows us to output docstrings in a reasonable fashion. --- autoapi/mappers/base.py | 6 ++++++ autoapi/templates/python/class.rst | 3 +-- autoapi/templates/python/function.rst | 2 +- autoapi/templates/python/member.rst | 2 +- autoapi/templates/python/method.rst | 2 +- autoapi/templates/python/module.rst | 2 +- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/autoapi/mappers/base.py b/autoapi/mappers/base.py index db439bb..3a86fa5 100644 --- a/autoapi/mappers/base.py +++ b/autoapi/mappers/base.py @@ -7,6 +7,7 @@ import unidecode from jinja2 import Environment, FileSystemLoader, TemplateNotFound from sphinx.util.console import darkgreen, bold from sphinx.util.osutil import ensuredir +from sphinx.util.docstrings import prepare_docstring from ..settings import API_ROOT @@ -153,6 +154,10 @@ class PythonMapperBase(object): return '.'.join(pieces) +def _wrapped_prepare(value): + return '\n'.join(prepare_docstring(value)) + + class SphinxMapperBase(object): '''Base class for mapping `PythonMapperBase` objects to Sphinx. @@ -177,6 +182,7 @@ class SphinxMapperBase(object): # trim_blocks=True, # lstrip_blocks=True, ) + self.jinja_env.filters['prepare_docstring'] = _wrapped_prepare self.url_root = url_root diff --git a/autoapi/templates/python/class.rst b/autoapi/templates/python/class.rst index 1f4fe4f..4e2e95d 100644 --- a/autoapi/templates/python/class.rst +++ b/autoapi/templates/python/class.rst @@ -2,8 +2,7 @@ {%- if obj.docstring %} - .. autoapi-nested-parse:: - {{ obj.docstring|indent(6) }} + {{ obj.docstring|prepare_docstring|indent(3) }} {% endif %} diff --git a/autoapi/templates/python/function.rst b/autoapi/templates/python/function.rst index e853603..8412892 100644 --- a/autoapi/templates/python/function.rst +++ b/autoapi/templates/python/function.rst @@ -3,7 +3,7 @@ .. function:: {{ obj.name }}({{ obj.args|join(',') }}) {% if obj.docstring %} - {{ obj.docstring.strip()|indent(3) }} + {{ obj.docstring|prepare_docstring|indent(3) }} {% endif %} {% endif %} \ No newline at end of file diff --git a/autoapi/templates/python/member.rst b/autoapi/templates/python/member.rst index 1f87d1f..2526660 100644 --- a/autoapi/templates/python/member.rst +++ b/autoapi/templates/python/member.rst @@ -1,4 +1,4 @@ .. {{ obj.type }}:: {{ obj.name }} - {{ obj.docstring|indent(3) }} + {{ obj.docstring|prepare_docstring|indent(3) }} diff --git a/autoapi/templates/python/method.rst b/autoapi/templates/python/method.rst index 93957e7..51e7535 100644 --- a/autoapi/templates/python/method.rst +++ b/autoapi/templates/python/method.rst @@ -3,7 +3,7 @@ .. method:: {{ obj.name }}({{ obj.args[1:]|join(',') }}) {% if obj.docstring %} - {{ obj.docstring|indent(3) }} + {{ obj.docstring|prepare_docstring|indent(3) }} {% endif %} {% endif %} diff --git a/autoapi/templates/python/module.rst b/autoapi/templates/python/module.rst index 1e3f1df..40778a8 100644 --- a/autoapi/templates/python/module.rst +++ b/autoapi/templates/python/module.rst @@ -6,7 +6,7 @@ {%- if obj.docstring %} .. autoapi-nested-parse:: - {{ obj.docstring|indent(3) }} + {{ obj.docstring|prepare_docstring|indent(3) }} {% endif %} From aa0699930070be4bb31659c8c7b9393da350256f Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 4 Nov 2016 15:19:56 -0700 Subject: [PATCH 2/6] Nest the wrapper --- autoapi/mappers/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autoapi/mappers/base.py b/autoapi/mappers/base.py index 3a86fa5..06f023c 100644 --- a/autoapi/mappers/base.py +++ b/autoapi/mappers/base.py @@ -154,10 +154,6 @@ class PythonMapperBase(object): return '.'.join(pieces) -def _wrapped_prepare(value): - return '\n'.join(prepare_docstring(value)) - - class SphinxMapperBase(object): '''Base class for mapping `PythonMapperBase` objects to Sphinx. @@ -182,6 +178,10 @@ class SphinxMapperBase(object): # trim_blocks=True, # lstrip_blocks=True, ) + + def _wrapped_prepare(value): + return '\n'.join(prepare_docstring(value)) + self.jinja_env.filters['prepare_docstring'] = _wrapped_prepare self.url_root = url_root From 491f8c906576ae2860c65a35bd4649bf07c9d54d Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 4 Nov 2016 15:34:48 -0700 Subject: [PATCH 3/6] Keep nested-parse --- autoapi/templates/python/class.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autoapi/templates/python/class.rst b/autoapi/templates/python/class.rst index 4e2e95d..29f1680 100644 --- a/autoapi/templates/python/class.rst +++ b/autoapi/templates/python/class.rst @@ -2,7 +2,9 @@ {%- if obj.docstring %} - {{ obj.docstring|prepare_docstring|indent(3) }} + .. autoapi-nested-parse:: + + {{ obj.docstring|prepare_docstring|indent(6) }} {% endif %} From 5901291d3f8b7fa13cc5c3aee4f034c8c3471226 Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 4 Nov 2016 15:37:46 -0700 Subject: [PATCH 4/6] Remove nested parse --- autoapi/templates/python/class.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/autoapi/templates/python/class.rst b/autoapi/templates/python/class.rst index 29f1680..4e2e95d 100644 --- a/autoapi/templates/python/class.rst +++ b/autoapi/templates/python/class.rst @@ -2,9 +2,7 @@ {%- if obj.docstring %} - .. autoapi-nested-parse:: - - {{ obj.docstring|prepare_docstring|indent(6) }} + {{ obj.docstring|prepare_docstring|indent(3) }} {% endif %} From e8514dfee29e7f7c5a079466697b1f9edcc499be Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 4 Nov 2016 15:44:47 -0700 Subject: [PATCH 5/6] Use shortname for methods --- autoapi/templates/python/method.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoapi/templates/python/method.rst b/autoapi/templates/python/method.rst index 51e7535..e49227e 100644 --- a/autoapi/templates/python/method.rst +++ b/autoapi/templates/python/method.rst @@ -1,6 +1,6 @@ {%- if obj.display %} -.. method:: {{ obj.name }}({{ obj.args[1:]|join(',') }}) +.. method:: {{ obj.short_name }}({{ obj.args[1:]|join(',') }}) {% if obj.docstring %} {{ obj.docstring|prepare_docstring|indent(3) }} From 875d6b6ffabf61ece17d464dac03e0972d4f2f70 Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 4 Nov 2016 15:47:57 -0700 Subject: [PATCH 6/6] Fix tests --- tests/test_integration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_integration.py b/tests/test_integration.py index 7cb4d7e..7415fb2 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -75,15 +75,15 @@ class PythonTests(LanguageIntegrationTests): example_file ) self.assertIn( - 'example.Foo.method_okay(foo=None, bar=None)', + 'method_okay(foo=None, bar=None)', example_file ) self.assertIn( - 'example.Foo.method_multiline(foo=None, bar=None, baz=None)', + 'method_multiline(foo=None, bar=None, baz=None)', example_file ) self.assertIn( - 'example.Foo.method_tricky(foo=None, bar=dict)', + 'method_tricky(foo=None, bar=dict)', example_file ) self.assertFalse(