Multiline string are displayed correctly

pull/279/head
Joseph Yu 3 years ago committed by Ashley Whetter
parent b9a6fcd350
commit 5435536988

@ -3,8 +3,14 @@ Changelog
Versions follow `Semantic Versioning <https://semver.org/>`_ (``<major>.<minor>.<patch>``).
UNRELEASED (2021-MM-DD)
-----------------------
UNRELEASED (TBC)
----------------
Features
^^^^^^^^
* `#267 <https://github.com/readthedocs/sphinx-autoapi/issues/267>`
Expandable value for multi-line string attributes.
Trivial/Internal Changes
^^^^^^^^^^^^^^^^^^^^^^^^

@ -1,6 +1,31 @@
{% if obj.display %}
.. {{ obj.type }}:: {{ obj.name }}
{%+ if obj.value is not none or obj.annotation is not none %}:annotation:{% if obj.annotation %} :{{ obj.annotation }}{% endif %}{% if obj.value is not none %} = {{ obj.value }}{% endif %}{% endif %}
{%+ if obj.value is not none or obj.annotation is not none -%}
:annotation:
{%- if obj.annotation %} :{{ obj.annotation }}
{%- endif %}
{%- if obj.value is not none %} = {%
if obj.value is string and obj.value.splitlines()|count > 1 -%}
Multiline-String
.. raw:: html
<details><summary>Show Value</summary>
.. code-block:: text
:linenos:
{{ obj.value|indent(width=8) }}
.. raw:: html
</details>
{%- else -%}
{{ obj.value|string|truncate(100) }}
{%- endif %}
{%- endif %}
{% endif %}
{{ obj.docstring|prepare_docstring|indent(3) }}

@ -10,6 +10,17 @@ from typing import ClassVar, Dict, Iterable, List, Union, overload
from example2 import B
software = "sphinx"
code_snippet = """
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
# from future.builtins.disabled import *
# from builtins import *
print("chunky o'block")
"""
max_rating: int = 10
is_valid: bool

@ -176,6 +176,9 @@ class TestPy3Module(object):
with io.open(example_path, encoding="utf8") as example_handle:
example_file = example_handle.read()
assert "software = sphinx" in example_file
assert "code_snippet = Multiline-String" in example_file
assert "max_rating :int = 10" in example_file
assert "is_valid" in example_file
@ -792,3 +795,42 @@ def test_custom_jinja_filters(builder):
example_file = example_handle.read()
assert "This is using custom filters." in example_file
def test_string_module_attributes(builder):
"""Test toggle for multi-line string attribute values (GitHub #267)."""
keep_rst = {
"autoapi_keep_files": True,
"autoapi_root": "_build/autoapi", # Preserve RST files under _build for cleanup
}
builder("py3example", confoverrides=keep_rst)
example_path = os.path.join(keep_rst["autoapi_root"], "example", "index.rst")
with io.open(example_path, encoding="utf8") as example_handle:
example_file = example_handle.read()
code_snippet_contents = [
".. data:: code_snippet",
" :annotation: = Multiline-String",
"",
" .. raw:: html",
"",
" <details><summary>Show Value</summary>",
"",
" .. code-block:: text",
" :linenos:",
"",
" ", # <--- Line array monstrosity to preserve these leading spaces
" # -*- coding: utf-8 -*-",
" from __future__ import absolute_import, division, print_function, unicode_literals",
" # from future.builtins.disabled import *",
" # from builtins import *",
"",
""" print("chunky o'block")""",
"",
"",
" .. raw:: html",
"",
" </details>",
]
assert "\n".join(code_snippet_contents) in example_file

Loading…
Cancel
Save