Skip to content
This repository has been archived by the owner on Jan 3, 2018. It is now read-only.

Commit

Permalink
Merge pull request #252 from r-gaia-cs/ipynb-template
Browse files Browse the repository at this point in the history
Template for IPython Notebook
  • Loading branch information
Greg Wilson committed Feb 10, 2014
2 parents 1ba3abd + 5659c1b commit c241573
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 30 deletions.
49 changes: 20 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OUT = _site
TMP = tmp
LINK_OUT = /tmp/bc-links

# Source and destination Markdown/HTML pages.
# Source Markdown pages.
MARKDOWN_SRC = \
LICENSE.md \
NEW_MATERIAL.md \
Expand All @@ -26,23 +26,20 @@ MARKDOWN_SRC = \
$(wildcard git/novice/*.md) \
$(wildcard python/novice/*.md) \
$(wildcard sql/novice/*.md)
MARKDOWN_DST = \
$(patsubst %.md,$(OUT)/%.html,$(MARKDOWN_SRC))

# Source, cached, and destination Notebook files/HTML pages.
NOTEBOOK_SRC = \
$(wildcard shell/novice/*.ipynb) \
$(wildcard git/novice/*.ipynb) \
$(wildcard python/novice/*.ipynb) \
$(wildcard sql/novice/*.ipynb)
NOTEBOOK_TMP = \
$(patsubst %.ipynb,$(TMP)/%.html,$(NOTEBOOK_SRC))
NOTEBOOK_DST = \
$(patsubst %.ipynb,$(OUT)/%.html,$(NOTEBOOK_SRC))

# Mark cached versions of compiled notebooks as SECONDARY so that GNU
# Make won't delete them after rebuilding.
.SECONDARY : $(NOTEBOOK_TMP)
NOTEBOOK_MD = \
$(patsubst %.ipynb,%.md,$(NOTEBOOK_SRC))

HTML_DST = \
$(patsubst %.md,$(OUT)/%.html,$(MARKDOWN_SRC)) \
$(patsubst %.md,$(OUT)/%.html,$(NOTEBOOK_MD))

#-----------------------------------------------------------

Expand All @@ -56,26 +53,16 @@ commands :
## check : build site.
# We know we're done when the compiled IPython Notebook files are
# in the output directory.
check : $(NOTEBOOK_DST)

# Cannot create final versions of compiled notebook files until Jekyll
# has re-created the output directory.
$(NOTEBOOK_DST) : $(OUT)

# Copy cached versions of compiled notebook files into output directory.
$(OUT)/%.html : $(TMP)/%.html
cp $< $@
check : $(OUT)/index.html

# Build HTML versions of Markdown source files using Jekyll. This always
# erases and re-creates the output directory.
$(OUT) : $(MARKDOWN_SRC)
# Build HTML versions of Markdown source files using Jekyll.
$(OUT)/index.html : $(MARKDOWN_SRC) $(NOTEBOOK_MD)
jekyll -t build -d $(OUT)
mv $(OUT)/NEW_MATERIAL.html $(OUT)/index.html

# Build HTML versions of IPython Notebooks. This is slow, so we cache
# the results in a temporary directory.
$(TMP)/%.html : %.ipynb
@mkdir -p $$(dirname $@)
ipython nbconvert --output="$(subst .html,,$@)" "$<"
# Build Markdown versions of IPython Notebooks.
%.md : %.ipynb
ipython nbconvert --template=./swc.tpl --to=markdown --output="$(subst .md,,$@)" "$<"

## fixme : find places where fixes are needed.
fixme :
Expand All @@ -100,10 +87,14 @@ links :
## clean : clean up
clean :
rm -rf $(OUT) $(TMP) $$(find . -name '*~' -print) $$(find . -name '*.pyc' -print)
rm -f $(HTML_DST)

## show : show variables
show :
@echo "OUT" $(OUT)
@echo "TMP" $(TMP)
@echo "LINK_OUT" $(LINK_OUT)
@echo "MARKDOWN_SRC" $(MARKDOWN_SRC)
@echo "MARKDOWN_DST" $(MARKDOWN_DST)
@echo "NOTEBOOK_SRC" $(NOTEBOOK_SRC)
@echo "NOTEBOOK_DST" $(NOTEBOOK_DST)
@echo "NOTEBOOK_MD" $(NOTEBOOK_MD)
@echo "HTML_DST" $(HTML_DST)
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ rss_url : "http://software-carpentry.org/feed.xml"
swc_prefix : "http://software-carpentry.org"
twitter_name : "@swcarpentry"
twitter_url : "https://twitter.com/swcarpentry"
exclude : [branding, data, tmp]
exclude : [branding, data, tmp, swc.tpl]
55 changes: 55 additions & 0 deletions swc.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
layout: lesson
root: ../..
---
{% extends 'markdown.tpl' %}

{% block input %}
<div class="input">
<span>In [{{ cell.prompt_number }}]:</span>
<pre>{{ cell.input | escape }}</pre>
</div>
{% endblock input %}

{% block output_group %}
<div class="output">
<span>Out [{{ cell.prompt_number }}]:</span>
<pre>{{ super() }}</pre>
</div>
{% endblock output_group %}

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

{% block pyout %}
{{ output.text | escape }}
{% endblock pyout %}

{% block pyerr %}
{{ output.traceback | join('\n') | strip_ansi | escape }}
{% endblock pyerr %}

{% block data_svg %}
<img src="../../{{ output.svg_filename | path2url }}">
{% endblock data_svg %}

{% block data_png %}
<img src="../../{{ output.png_filename | path2url }}">
{% endblock data_png %}

{% block data_jpg %}
<img src="../../{{ output.jpeg_filename | path2url }}">
{% endblock data_jpg %}

{% block markdowncell %}
{% if 'cell_tags' in cell.metadata %}
<div class="{{ cell.metadata['cell_tags'][0] }}">
{{ cell.source | markdown2html | strip_files_prefix }}
</div>
{% else %}
<div>
{{ cell.source | markdown2html | strip_files_prefix }}
</div>
{% endif %}
{%- endblock markdowncell %}

0 comments on commit c241573

Please sign in to comment.