Skip to content

Commit

Permalink
Leave trailing line break in reST directives (#385)
Browse files Browse the repository at this point in the history
* Add to `pdoc.test.Docformats.test_reST_include` to catch issue.

* Add a single line-break at the end of `.. include::`ed files - except for when in a code block.

* Minor change comment

* Revert last 2 commits.

* Don't consume trailing newline from reST directives.

* [] itself a disjunctive list of characters
  • Loading branch information
howamith authored Feb 3, 2022
1 parent 4aa70de commit 2cce30a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pdoc/html_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ def _admonition(match, module=None, limit_types=None):
if limit_types and type not in limit_types:
return match.group(0)

if text is None:
text = ""

if type == 'include' and module:
try:
return _ToMarkdown._include_file(indent, value,
Expand Down Expand Up @@ -323,7 +326,8 @@ def admonitions(text, module, limit_types=None):
See: https://python-markdown.github.io/extensions/admonition/
"""
substitute = partial(re.compile(r'^(?P<indent> *)\.\. ?(\w+)::(?: *(.*))?'
r'((?:\n(?:(?P=indent) +.*| *$))*)', re.MULTILINE).sub,
r'((?:\n(?:(?P=indent) +.*| *$))*[^\r\n])*',
re.MULTILINE).sub,
partial(_ToMarkdown._admonition, module=module,
limit_types=limit_types))
# Apply twice for nested (e.g. image inside warning)
Expand Down
17 changes: 16 additions & 1 deletion pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,22 @@ def test_reST_include(self):
<p>1
x = 2
x = 3
x =</p>'''
x =</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</tbody>
</table>
<p>Remaining.</p>'''
mod = pdoc.Module(pdoc.import_module(
os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE, '_reST_include', 'test.py')))
html = to_html(mod.docstring, module=mod)
Expand Down
3 changes: 3 additions & 0 deletions pdoc/test/example_pkg/_reST_include/table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| Name | Value |
| ----- | ----- |
| Hello | World |
4 changes: 4 additions & 0 deletions pdoc/test/example_pkg/_reST_include/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
.. include:: foo/../_include_me.py
:start-after: =
:end-before: 4
.. include:: table.md
Remaining.
"""

0 comments on commit 2cce30a

Please sign in to comment.