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

[FEATURE] Add filter for admonitions #212

Merged
merged 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions docs/pandoc_filters/admonitionfilter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python

"""
Pandoc filter to convert divs with class="admonition" to ipynb
"""

import sys
from pandocfilters import toJSONFilter, RawBlock, Div, stringify

# from: https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions
# admonition is a special case with arbitrary header
admonition_types = ["attention", "caution", "danger", "error", "hint", "important", "note", "tip", "warning", "admonition"]
# keywords in arbitrary admonition header
admonition_subtypes = ["goal"]
# colors and icons for admonition_types (without "admonition") and subtypes
admonition_colors = {"attention": "#FFA07A", "caution": "#FFA07A", "danger": "#CD5C5C", "error": "#CD5C5C", "hint": "#F0F8FF", "important": "#FFA500", "note": "#BDE5F8", "tip": "#F0E68C", "warning": "#FFA07A", "goal": "#98FB98"}
admonition_icons = {"attention": "fas fa-exclamation", "caution": "fas fa-exclamation-triangle", "danger": "fas fa-exclamation-triangle", "error": "fas fa-bomb", "hint": "far fa-lightbulb", "important": "fas fa-exclamation", "note": "far fa-sticky-note", "tip": "far fa-lightbulb", "warning": "fas fa-exclamation-triangle", "goal": "far fa-check-square"}


def html(x):
return RawBlock('html', x)


def admonitions(key, value, format, meta):
if key == 'Div':
[[ident, classes, kvs], contents] = value
if any(item in classes for item in admonition_types) and format == "ipynb":
header = stringify(contents[0])
if "admonition" not in classes:
admonition_subtype = header.lower()
else:
for subtype in admonition_subtypes:
if subtype in header.lower():
admonition_subtype = subtype
break
newcontents = [html('<div style="background-color: ' + admonition_colors[admonition_subtype] + '; margin: 10px 0px; padding:12px;"><p style="font-size: x-large"><i class="' + admonition_icons[admonition_subtype] + '"></i> <b>' + header + '</b></p>')] + contents[1:] + [html('</div>')]
return Div([ident, classes, kvs], newcontents)


if __name__ == "__main__":
toJSONFilter(admonitions)
2 changes: 1 addition & 1 deletion docs/source/generate_ipynb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

echo "Process Started"

for FILE in *.rst; do a=(${FILE/./ }); pandoc ${FILE} -o ${a[0]}.ipynb --filter ../pandoc_filters/code_pandocfilter.py --filter ../pandoc_filters/ignore_pandocfilter.py --filter ../pandoc_filters/transformlinks_pandocfilter.py; echo "${a[0]}.ipynb Generated"; done
for FILE in *.rst; do a=(${FILE/./ }); pandoc ${FILE} -o ${a[0]}.ipynb --filter ../pandoc_filters/admonitionfilter.py --filter ../pandoc_filters/code_pandocfilter.py --filter ../pandoc_filters/ignore_pandocfilter.py --filter ../pandoc_filters/transformlinks_pandocfilter.py; echo "${a[0]}.ipynb Generated"; done

echo "Process Completed Successfully"