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 processing of literal blocks in try examples directive #134

Merged
merged 3 commits into from Feb 13, 2024

Conversation

steppi
Copy link
Collaborator

@steppi steppi commented Feb 2, 2024

This PR adds support for processing literal blocks when converting RST to notebooks in the TryExamplesDirective. I noticed these weren't being handled correctly while going over locally built SciPy docs carefully in preparation for submitting a PR to SciPy. Screenshot below of what can go wrong when literal blocks aren't handled correctly. I'll post a link to this example after this fix is in once it finishes building and is deployed.

image

@steppi steppi added the bug Something isn't working label Feb 2, 2024
@steppi
Copy link
Collaborator Author

steppi commented Feb 2, 2024

This isn't quite working yet. Still some edge cases to work out. Marking as draft.

@steppi steppi marked this pull request as draft February 2, 2024 21:17
Case where the next line after a literal block opens
another literal block.
@steppi
Copy link
Collaborator Author

steppi commented Feb 2, 2024

It's working now. Here's a link https://steppi.github.io/scipy/reference/generated/scipy.integrate.quad.html#scipy.integrate.quad. I didn't correctly handle the case where a literal block directly follows another literal block.

Copy link
Collaborator

@Carreau Carreau left a comment

Choose a reason for hiding this comment

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

Just one question, but I think we can move forward without having to wait for the anwser.

@@ -216,6 +216,50 @@ def _process_latex(md_text):
return "\n".join(wrapped_lines)


def _process_literal_blocks(md_text):
md_lines = md_text.split("\n")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Dumb questions, but how will this behave on windows where the line separator may be \r\n ? Or is it always \n in sphinx ? maybe:

Suggested change
md_lines = md_text.split("\n")
md_lines = md_text.splitlines()

I know there are other differences like if there is a terminal \n, or empty string.

Copy link
Collaborator Author

@steppi steppi Feb 13, 2024

Choose a reason for hiding this comment

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

It's a good question. Basically yes, I think we can always assume it's '\n' in Sphinx. Changing to splitlines won't affect the outcome, but it seems like a good change to make just because it better demonstrates intent.

The details: Python's open function has a newline kwarg whose default behavior is to normalize all line breaks to '\n', and this is what is used in Sphinx. So in Sphinx it's always '\n', regardless of the convention used by the RST files themselves. From the docs for open:

newline determines how to parse newline characters from the stream. It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.*

When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

I think the only difference we'd see using splitlines() vs split('\n') is that, like you said, the former ignores terminal newlines, and I don't think this actually would make a difference. I think Jupyter just ignores trailing newlines in markdown cells.

@Carreau Carreau merged commit 96f789e into jupyterlite:main Feb 13, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants