Identify example code blocks in docstrings and wrap in backticks#36
Identify example code blocks in docstrings and wrap in backticks#36douglasdavis wants to merge 3 commits intopython-lsp:developfrom
Conversation
|
You might be interested in the test cases in https://github.com/krassowski/docstring-to-markdown (also see #22). |
|
Thanks for pointing me to docstring-to-markdown! Looks like the logic doesn't capture lines that don't start with
|
|
This is intended to accommodate non-code outputs of doctests. Consider the following:
You do not want the output text to be color-highlighted because it is not Python syntax. No strong opinion on dropping/non-dropping the ">>>" but I guess more Markdown parsers will highlight Python correctly if it is dropped (plus it saves space). |
Yea I see what you mean, I pretty much exclusively use the numpydoc style which is where I was getting inspiration.
Same thing as above, I was thinking in numpydoc style! Unfortunately there's the existence of both rst |
|
Literally the reason I created docstring-to-markdown ;) |
|
There is also https://github.com/Carreau/papyri being worked on and it might turn up to be a good intermediary to convert docstrings to markdown, but I have not had time to evaluate it recently. |
7b9e897 to
8fbee2f
Compare
101b2b6 to
02ead9a
Compare
| example_snippets = re.findall(EX_SNIPPET_RE, contents) | ||
| # wrap the snippets that were found in backticks | ||
| for snippet in example_snippets: | ||
| contents = contents.replace(snippet, f"```python\n{snippet}\n```") |
There was a problem hiding this comment.
This will lead to unexpected results with the following snippet:
print(format_docstring("""
Test docstring with two examples
>>> example
>>> example 2 should work too
"""))
Result:
Test docstring with two examples ```python >>> example ``` ```python >>> example ``` 2 should work too
9b8491a to
0d5a3f3
Compare
91088b9 to
7aaa31f
Compare
7aaa31f to
4f6fb90
Compare
4f6fb90 to
42462c7
Compare
42462c7 to
f98adbe
Compare
|
Closing in favor of PR #80. |
This PR adds a regex search for
>>>, capturing all text until the next double newline and wraps the matches in backticks such that example blocks use markdowns code block syntaxBefore:
After: