Skip to content

Commit

Permalink
Add custom nbconvert template to add captions to images
Browse files Browse the repository at this point in the history
Solves #6
Based on jupyter/nbconvert#1112 but applied
to markdown rather than HTML
  • Loading branch information
THargreaves committed Jun 3, 2020
1 parent 4f69e50 commit 97d8e78
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
5 changes: 3 additions & 2 deletions build/build_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
process = subprocess.Popen([
'jupyter', 'nbconvert',
notebook_path,
'--to', 'markdown',
'--to', 'markdown', '--template=build/markdown_with_captions.tpl',
'--TagRemovePreprocessor.enabled=True',
"--TagRemovePreprocessor.remove_cell_tags=['remove_cell']",
"--TagRemovePreprocessor.remove_input_tags=['remove_input']",
Expand All @@ -33,6 +33,7 @@
'jupyter nbconvert',
notebook_path,
'--to markdown',
'--template=build/markdown_with_captions.tpl',
'--TagRemovePreprocessor.enabled=True',
"--TagRemovePreprocessor.remove_cell_tags=\"['remove_cell']\"",
"--TagRemovePreprocessor.remove_input_tags=\"['remove_input']\"",
Expand All @@ -53,7 +54,7 @@
file_data = re.sub(f'{post}_files/', f'/images/{post}/', file_data)

# remove captions
file_data = re.sub(r'!\[\w+\]', '![]', file_data)
# file_data = re.sub(r'!\[\w+\]', '![]', file_data)

# remove additional table formatting
file_data = re.sub('table border="1" class="dataframe"', 'table', file_data)
Expand Down
74 changes: 74 additions & 0 deletions build/markdown_with_captions.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{% extends 'display_priority.tpl' %}


{% block in_prompt %}
{% endblock in_prompt %}

{% block output_prompt %}
{%- endblock output_prompt %}

{% block input %}
```
{%- if 'magics_language' in cell.metadata -%}
{{ cell.metadata.magics_language}}
{%- elif 'name' in nb.metadata.get('language_info', {}) -%}
{{ nb.metadata.language_info.name }}
{%- endif %}
{{ cell.source}}
```
{% endblock input %}

{% block error %}
{{ super() }}
{% endblock error %}

{% block traceback_line %}
{{ line | indent | strip_ansi }}
{% endblock traceback_line %}

{% block execute_result %}

{% block data_priority scoped %}
{{ super() }}
{% endblock %}
{% endblock execute_result %}

{% block stream %}
{{ output.text | indent }}
{% endblock stream %}

{% block data_svg %}
![{{(output | get_metadata('alt', 'image/svg+xml')) or (cell | get_metadata('alt')) or ""}}]({{ output.metadata.filenames['image/svg+xml'] | path2url }})
{% endblock data_svg %}

{% block data_png %}
![{{(output | get_metadata('alt', 'image/png')) or (cell | get_metadata('alt')) or ""}}]({{ output.metadata.filenames['image/png'] | path2url }})
{% endblock data_png %}

{% block data_jpg %}
![{{(output | get_metadata('alt', 'image/jpeg')) or (cell | get_metadata('alt')) or ""}}]({{ output.metadata.filenames['image/jpeg'] | path2url }})
{% endblock data_jpg %}

{% block data_latex %}
{{ output.data['text/latex'] }}
{% endblock data_latex %}

{% block data_html scoped %}
{{ output.data['text/html'] }}
{% endblock data_html %}

{% block data_markdown scoped %}
{{ output.data['text/markdown'] }}
{% endblock data_markdown %}

{% block data_text scoped %}
{{ output.data['text/plain'] | indent }}
{% endblock data_text %}

{% block markdowncell scoped %}
{{ cell.source }}
{% endblock markdowncell %}

{% block unknowncell scoped %}
unknown type {{ cell.type }}
{% endblock unknowncell %}

0 comments on commit 97d8e78

Please sign in to comment.