Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for include directive #684

Merged
merged 23 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,16 @@ class GoldenRetriever(Dog):
"""
```

Since version 11, pdoc processes such reStructuredText elements by default.
You can also include only parts of a file with the
[`start-line`, `end-line`, `start-after`, and `end-after` options](https://docutils.sourceforge.io/docs/ref/rst/directives.html#including-an-external-document-fragment):

```python
"""
.. include:: ../README.md
:start-line: 1
:end-before: Changelog
"""
```


## ...add a title page?
Expand Down
37 changes: 37 additions & 0 deletions pdoc/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,38 @@ def replace_link(m: re.Match[str]) -> str:
return contents


def _rst_extract_options(contents: str) -> tuple[str, dict[str, str]]:
"""
Extract options from the beginning of reStructuredText directives.

Return the trimmed content and a dict of options.
"""
options = {}
while match := re.match(r"^\s*:(.+?):(.*)([\s\S]*)", contents):
key, value, contents = match.groups()
options[key] = value.strip()

return contents, options


def _rst_include_trim(contents: str, options: dict[str, str]) -> str:
"""
<https://docutils.sourceforge.io/docs/ref/rst/directives.html#include-options>
"""
if "end-line" in options or "start-line" in options:
lines = contents.splitlines()
if i := options.get("end-line"):
lines = lines[: int(i)]
if i := options.get("start-line"):
lines = lines[int(i) :]
contents = "\n".join(lines)
if x := options.get("end-before"):
contents = contents[: contents.index(x)]
if x := options.get("start-after"):
contents = contents[contents.index(x) + len(x) :]
return contents


def _rst_admonitions(contents: str, source_file: Path | None) -> str:
"""
Convert reStructuredText admonitions - a bit tricky because they may already be indented themselves.
Expand All @@ -371,6 +403,7 @@ def _rst_admonition(m: re.Match[str]) -> str:
type = m.group("type")
val = m.group("val").strip()
contents = dedent(m.group("contents")).strip()
contents, options = _rst_extract_options(contents)

if type == "include":
loc = source_file or Path(".")
Expand All @@ -379,6 +412,10 @@ def _rst_admonition(m: re.Match[str]) -> str:
except OSError as e:
warnings.warn(f"Cannot include {val!r}: {e}")
included = "\n"
try:
included = _rst_include_trim(included, options) + "\n"
except ValueError as e:
warnings.warn(f"Failed to process include options for {val!r}: {e}")
included = _rst_admonitions(included, loc.parent / val)
return indent(included, ind)
if type == "math":
Expand Down
54 changes: 54 additions & 0 deletions test/test_docstrings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from hypothesis import assume
from hypothesis import given
from hypothesis.strategies import text
import pytest
Expand Down Expand Up @@ -26,6 +27,59 @@ def test_rst(s):
assert not s or ret


@given(text())
def test_rst_extract_options_fuzz(s):
assume(not s.startswith(":"))
content, options = docstrings._rst_extract_options(s)
assert not options
assert content == s
mhils marked this conversation as resolved.
Show resolved Hide resolved


def test_rst_extract_options():
content = (
":alpha: beta\n"
":charlie:delta:foxtrot\n"
"rest of content\n"
":option ignored: as follows content\n"
)
content, options = docstrings._rst_extract_options(content)
assert options == {
"alpha": "beta",
"charlie": "delta:foxtrot",
}
assert content == ("\nrest of content\n" ":option ignored: as follows content\n")


@given(text())
def test_rst_include_trim_fuzz(s):
mhils marked this conversation as resolved.
Show resolved Hide resolved
content = docstrings._rst_include_trim(s, {})
assert content == s


def test_rst_include_trim_lines():
content = "alpha\nbeta\ncharlie\ndelta\necho"
trimmed = docstrings._rst_include_trim(
content, {"start-line": "2", "end-line": "4"}
)
assert trimmed == "charlie\ndelta"


def test_rst_include_trim_pattern():
content = "alpha\nbeta\ncharlie\ndelta\necho"
trimmed = docstrings._rst_include_trim(
content, {"start-after": "beta", "end-before": "echo"}
)
assert trimmed == "\ncharlie\ndelta\n"


def test_rst_include_trim_mixture():
content = "alpha\nbeta\ncharlie\ndelta\necho"
trimmed = docstrings._rst_include_trim(
content, {"start-after": "beta", "end-line": "4"}
)
assert trimmed == "\ncharlie\ndelta"


def test_rst_include_nonexistent():
with pytest.warns(UserWarning, match="Cannot include 'nonexistent.txt'"):
docstrings.rst(".. include:: nonexistent.txt", None)
Loading
Loading