Create a gist now

Instantly share code, notes, and snippets.

@bennokr /jekyll.py forked from cscorley/jekyll.py
Last active Oct 4, 2015

What would you like to do?
IPython to Jekyll Markdown
# 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(' ', '-')
{% 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