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

Identify example code blocks in docstrings and wrap in backticks #36

Closed

Conversation

douglasdavis
Copy link
Contributor

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 syntax

Before:

>>> a = '5'
>>> a
5

>>> b = 5
>>> c = 10
>>> b + c
15

After:

```python
>>> a = '5'
>>> a
5
````

````python
>>> b = 5
>>> c = 10
>>> b + c
15
```

@krassowski
Copy link
Contributor

You might be interested in the test cases in https://github.com/krassowski/docstring-to-markdown (also see #22).

@douglasdavis
Copy link
Contributor Author

douglasdavis commented May 27, 2021

Thanks for pointing me to docstring-to-markdown! Looks like the logic doesn't capture lines that don't start with >>> but immediately follow a line that does (and all >>> prefixes are dropped)

teststr = """
Examples
--------

>>> a = '5'
>>> b = 3
>>> b
3

>>> x = 'abc'
>>> x
abc

"""

convert(teststr) yields:

#### Examples


```python
a = '5'
b = 3
b
```

```
3
```


```python
x = 'abc'
x
```

```
abc
```

@krassowski
Copy link
Contributor

This is intended to accommodate non-code outputs of doctests. Consider the following:

>>> print 'Python-specific usage examples begun with ">>>"'
Python-specific usage examples begun with ">>>"
>>> print '(cut and pasted from interactive sessions)'
(cut and pasted from interactive sessions)"""

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).

@douglasdavis
Copy link
Contributor Author

douglasdavis commented May 27, 2021

You do not want the output text to be color-highlighted because it is not Python syntax.

Yea I see what you mean, I pretty much exclusively use the numpydoc style which is where I was getting inspiration.

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).

Same thing as above, I was thinking in numpydoc style!

Unfortunately there's the existence of both rst code-block:: and the Example block >>> style. I wonder how non trivial it would be to support both. Too bad there's not a single python docstring style 🤷‍♂️. the sphinx code to handle all of this is definitely not short!

@krassowski
Copy link
Contributor

Literally the reason I created docstring-to-markdown ;)

@krassowski
Copy link
Contributor

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.

@douglasdavis douglasdavis force-pushed the dev-example-blocks branch 2 times, most recently from 101b2b6 to 02ead9a Compare June 28, 2021 13:33
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```")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@douglasdavis douglasdavis force-pushed the dev-example-blocks branch 2 times, most recently from 9b8491a to 0d5a3f3 Compare August 2, 2021 13:34
@douglasdavis douglasdavis force-pushed the dev-example-blocks branch 3 times, most recently from 91088b9 to 7aaa31f Compare August 9, 2021 18:28
@ccordoba12
Copy link
Member

Closing in favor of PR #80.

@ccordoba12 ccordoba12 closed this Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants