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

Support for cell attachments #114

Merged
merged 2 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions doc/markdown-cells.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@
"SVG file (remote): ![Jupyter logo (remote)](http://jupyter.org/assets/nav_logo.svg)"
]
},
{
"attachments": {
"stickfigure.png": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAHv0lEQVR42u2ae2xbZxnGf+f4OLbjOHGcNq2bpqF12tJmZGtJttK1m7Z1XMomVQyp4zYESEMwMNLEKEgIbQJNFKkIVUNogJj2B9u6cYkETBUtl5UNWDt10NGMbiU0G+klpE6aq9PYMX+c5xRjEl8SJ3WcvdKR7ePvXN7ney/P+34fvCWLW4x5eIYHCAGV+v2PcgcgBFTpewB4J/AxYDmQAKLAaeBcOQLQKgW3AymgETgL1AJHgAlgI9ADPAQcB8bLxZ1WAZ3AYaALuBdo0flrgTogLID6gDc0xlMOynuAm4DXgU9plj1Zxt4B/BM4JYAWvPL3AX8VCPnO6A5gRFayoKUFiAOvFTibzcDfgTuvphuYRbjHkPz518BAAVbTCkwCr17NQGgW6T5J+fV1BQCwRFnhcjnEgE/KnDs1s6vEB6aSauDzwN+ALeWSAquV0rqApwXG9wXGBoERBtqkfB8wCqwtJyLkEevbI8Vf1Kcf+JNA2CzljwPfBl4qRyocltIjwBdEfA4ClwA38Czwu1KhwnMpzcAJ4CsCpU5HSTE/aw4D463KMs8shtnOVP5dmv0DQH0pv6yriEonFQRvBR5QK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also be embedded in the notebook itself. Just drag an image file into the Markdown cell you are just editing or copy and paste some image data from an image editor/viewer.\n",
"\n",
"The generated Markdown code will look just like a \"normal\" image link, except that it will have an `attachment:` prefix:\n",
"\n",
" ![a stick figure](attachment:stickfigure.png)\n",
"\n",
"This is a cell attachment: ![a stick figure](attachment:stickfigure.png)\n",
"\n",
"In the Jupyter Notebook, there is a speciall \"Attachments\" cell toolbar which you can use to see all attachments of a cell and delete them, if needed."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
29 changes: 28 additions & 1 deletion src/nbsphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
{%- if 'nbsphinx-toctree' in cell.metadata %}
{{ cell | extract_toctree }}
{%- else %}
{{ super() }}
{{ cell | save_attachments or super() | replace_attachments }}
{% endif %}
{% endblock markdowncell %}

Expand Down Expand Up @@ -524,6 +524,31 @@ class Exporter(nbconvert.RSTExporter):
def __init__(self, execute='auto', kernel_name='', execute_arguments=[],
allow_errors=False, timeout=30, codecell_lexer='none'):
"""Initialize the Exporter."""

# NB: The following stateful Jinja filters are a hack until
# template-based processing is dropped
# (https://github.com/spatialaudio/nbsphinx/issues/36) or someone
# comes up with a better idea.

# NB: This instance-local state makes the methods non-reentrant!
attachment_storage = []

def save_attachments(cell):
for filename, bundle in cell.get('attachments', {}).items():
attachment_storage.append((filename, bundle))

def replace_attachments(text):
for filename, bundle in attachment_storage:
# For now, this works only if there is a single MIME bundle
(mime_type, data), = bundle.items()
text = re.sub(
r'^(\s*\.\. (\|[^|]*\| image|figure)::) attachment:{0}$'
.format(filename),
r'\1 data:{0};base64,{1}'.format(mime_type, data),
text, flags=re.MULTILINE)
attachment_storage.clear()
return text

self._execute = execute
self._kernel_name = kernel_name
self._execute_arguments = execute_arguments
Expand All @@ -543,6 +568,8 @@ def __init__(self, execute='auto', kernel_name='', execute_arguments=[],
'markdown2rst': markdown2rst,
'get_empty_lines': _get_empty_lines,
'extract_toctree': _extract_toctree,
'save_attachments': save_attachments,
'replace_attachments': replace_attachments,
'get_output_type': _get_output_type,
'json_dumps': json.dumps,
})
Expand Down