-
Notifications
You must be signed in to change notification settings - Fork 517
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
Generate notebook with HTML for admonitions #152
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import sys | ||
|
||
from docutils.core import publish_from_doctree | ||
|
||
from bs4 import BeautifulSoup | ||
|
||
from myst_parser.main import MdParserConfig, default_parser | ||
|
||
import jupytext | ||
|
||
# https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#directives | ||
# Docutils supports the following directives: | ||
# Admonitions: attention, caution, danger, error, hint, important, note, tip, | ||
# warning and the generic admonition | ||
all_admonitions = [ | ||
"attention", | ||
"caution", | ||
"danger", | ||
"error", | ||
"hint", | ||
"important", | ||
"note", | ||
"tip", | ||
"warning", | ||
"admonition", | ||
] | ||
|
||
# follows colors defined by JupterBook CSS | ||
sphinx_name_to_bootstrap = { | ||
"attention": "warning", | ||
"caution": "warning", | ||
"danger": "danger", | ||
"error": "danger", | ||
"hint": "warning", | ||
"important": "info", | ||
"note": "info", | ||
"tip": "warning", | ||
"warning": "danger", | ||
"admonition": "info", | ||
} | ||
|
||
all_directive_names = ["{" + adm + "}" for adm in all_admonitions] | ||
|
||
|
||
def convert_to_html(doc, css_selector=None): | ||
"""Converts docutils document to HTML and select part of it with CSS | ||
selector. | ||
""" | ||
html_str = publish_from_doctree(doc, writer_name="html").decode("utf-8") | ||
html_node = BeautifulSoup(html_str, features="html.parser") | ||
|
||
if css_selector is not None: | ||
html_node = html_node.select_one(css_selector) | ||
|
||
return html_node | ||
|
||
|
||
def admonition_html(doc): | ||
"""Returns admonition HTML from docutils document. | ||
|
||
Assumes that the docutils document has a single child which is an | ||
admonition. | ||
""" | ||
assert len(doc.children) == 1 | ||
adm_node = doc.children[0] | ||
assert adm_node.tagname in all_admonitions | ||
html_node = convert_to_html(doc, "div.admonition") | ||
bootstrap_class = sphinx_name_to_bootstrap[adm_node.tagname] | ||
html_node.attrs["class"] += [f"alert alert-{bootstrap_class}"] | ||
html_node.select_one( | ||
".admonition-title").attrs["style"] = "font-weight: bold;" | ||
|
||
return str(html_node) | ||
|
||
|
||
def replace_admonition_in_cell_source(cell_str): | ||
"""Returns cell source with admonition replaced by its generated HTML. | ||
""" | ||
config = MdParserConfig(renderer="docutils") | ||
parser = default_parser(config) | ||
tokens = parser.parse(cell_str) | ||
|
||
admonition_tokens = [ | ||
t for t in tokens | ||
if t.type == "fence" and t.info in all_directive_names | ||
] | ||
|
||
cell_lines = cell_str.splitlines() | ||
new_cell_str = cell_str | ||
|
||
for t in admonition_tokens: | ||
adm_begin, adm_end = t.map | ||
adm_src = "\n".join(cell_lines[adm_begin:adm_end]) | ||
adm_doc = parser.render(adm_src) | ||
adm_html = admonition_html(adm_doc) | ||
new_cell_str = new_cell_str.replace(adm_src, adm_html) | ||
|
||
return new_cell_str | ||
|
||
|
||
def replace_admonition_in_nb(nb): | ||
"""Replaces all admonitions by its generated HTML in a notebook object. | ||
""" | ||
# FIXME this would not work with advanced syntax for admonition with ::: | ||
# but we are not using it for now. We could parse all the markdowns cell, a | ||
# bit wasteful, but probably good enough | ||
cells_with_admonition = [ | ||
(i, c) | ||
for i, c in enumerate(nb["cells"]) | ||
if c["cell_type"] == "markdown" | ||
and any(directive in c["source"] for directive in all_directive_names) | ||
] | ||
|
||
for i, c in cells_with_admonition: | ||
cell_src = c["source"] | ||
output_src = replace_admonition_in_cell_source(cell_src) | ||
nb.cells[i]["source"] = output_src | ||
|
||
|
||
def replace_admonition_in_filename(input_filename, output_filename): | ||
"""Converts .py file to .ipynb file where admonitions have been replaced by | ||
their generated HTML. | ||
|
||
Context: MyST syntax is not supported inside a Jupyter notebook. This is a | ||
hacky way to keep using MyST admonitions for our JupyterBook and have | ||
acceptable admonition HTML in the Jupyter notebook interface. | ||
""" | ||
nb = jupytext.read(input_filename) | ||
|
||
replace_admonition_in_nb(nb) | ||
|
||
jupytext.write(nb, output_filename) | ||
|
||
|
||
if __name__ == "__main__": | ||
input_filename = sys.argv[1] | ||
output_filename = sys.argv[2] | ||
replace_admonition_in_filename(input_filename, output_filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a small docstring to explain what is going on in this file / what is its purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good point, I added small docstrings (be it only to remember it myself) to the functions but not to the module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in d61c5c1