-
-
Save bennokr/13293234eaf57bac887a to your computer and use it in GitHub Desktop.
IPython to Jekyll Markdown
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
# modification of config created here: https://gist.github.com/cscorley/9144544 | |
### Usage: | |
## run `ipython profile create jekyll` | |
## put this file in `IPYTHONDIR/profile_jekyll/` | |
## put `jekyll.tpl` in `IPYTHONDIR/templates/` | |
## run ipython nbconvert --to markdown --profile=jekyll your-file.ipynb | |
try: | |
from urllib.parse import quote # Py 3 | |
except ImportError: | |
from urllib2 import quote # Py 2 | |
import os | |
import sys | |
f = None | |
for arg in sys.argv: | |
if arg.endswith('.ipynb'): | |
f = arg.split('.ipynb')[0] | |
break | |
c = get_config() | |
ipythondir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) | |
c.NbConvertApp.export_format = 'markdown' | |
c.MarkdownExporter.template_path = [os.path.join(ipythondir, 'templates')] | |
c.MarkdownExporter.template_file = 'jekyll' | |
#c.Application.verbose_crash=True | |
# modify this function to point your images to a custom path | |
# by default this saves all images to a directory 'images' in the root of the blog directory | |
def path2support(path): | |
"""Turn a file path into a URL""" | |
return '{{ BASE_PATH }}/images/' + os.path.basename(path) | |
c.MarkdownExporter.filters = {'path2support': path2support} | |
# Preprocess javascript | |
from IPython.nbconvert.preprocessors import * | |
class RemoveLanguageMagicsPreprocessor(Preprocessor): | |
def preprocess_cell(self, cell, resources, index): | |
if 'magics_language' in cell['metadata']: | |
magics = cell['metadata']['magics_language'] | |
cell['source'] = cell['source'][len(magics)+3:] | |
return cell, resources | |
c.Exporter.preprocessors = [HighlightMagicsPreprocessor, RemoveLanguageMagicsPreprocessor] | |
c.NbConvertBase.display_data_priority = [ | |
'svg', | |
'png', | |
'html', | |
'markdown', | |
'jpeg', | |
'javascript', | |
'text', | |
'latex', | |
] | |
if f: | |
c.NbConvertApp.output_base = f.lower().replace(' ', '-') |
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
{% extends 'markdown.tpl' %} | |
{%- block header -%} | |
--- | |
layout: post | |
title: "{{resources['metadata']['name']}}" | |
tags: | |
- python | |
- notebook | |
--- | |
{%- endblock header -%} | |
{% block codecell scoped %} | |
<a name="{{ cell.prompt_number }}" ></a> | |
{{ super() }} | |
{% endblock codecell %} | |
{% block input %} | |
{% if cell.source %} | |
```{{ cell['metadata']['magics_language'] or nb.metadata.language_info.name }} | |
{{ cell.source }} | |
``` | |
{% endif %} | |
{% endblock input %} | |
{% block data_javascript scoped %} | |
<p id="js-output-{{ cell.prompt_number }}"></p> | |
<script type="text/javascript" id="js-{{ cell.prompt_number }}"> | |
element = $("#js-output-{{ cell.prompt_number }}"); | |
{{ output.javascript }} | |
</script> | |
{% endblock data_javascript %} | |
{% block data_svg %} | |
![svg]({{ output.output.svg_filename | path2support }}) | |
{% endblock data_svg %} | |
{% block data_png %} | |
![png]({{ output.png_filename | path2support }}) | |
{% endblock data_png %} | |
{% block data_jpg %} | |
![jpeg]({{ output.jpeg_filename | path2support }}) | |
{% endblock data_jpg %} | |
{% block markdowncell scoped %} | |
{{ cell.source | wrap_text(80) }} | |
{% endblock markdowncell %} | |
{% block headingcell scoped %} | |
{{ '#' * cell.level }} {{ cell.source | replace('\n', ' ') }} | |
{% endblock headingcell %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment