Skip to content

Commit

Permalink
Merge pull request ipython#4174 from minrk/markup-templates
Browse files Browse the repository at this point in the history
various issues in markdown and rst templates

- remove prompts from default output
- add missing HTML block
- remove double-wrapping of latex
- use plain-markdown indentation for code blocks (could use GFM ``` ` ` `python```)
- images didn't work in either one at all
- markdown extracts output

closes ipython#4024
  • Loading branch information
minrk committed Sep 9, 2013
2 parents b743e16 + 6148c2f commit 8c88aec
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
3 changes: 2 additions & 1 deletion IPython/nbconvert/exporters/exporter.py
Expand Up @@ -68,7 +68,8 @@
'strip_math_space': filters.strip_math_space,
'wrap_text': filters.wrap_text,
'escape_latex': filters.escape_latex,
'citation2latex': filters.citation2latex
'citation2latex': filters.citation2latex,
'path2url': filters.path2url,
}

#-----------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions IPython/nbconvert/exporters/markdown.py
Expand Up @@ -13,6 +13,7 @@
# Imports
#-----------------------------------------------------------------------------

from IPython.config import Config
from IPython.utils.traitlets import Unicode

from .exporter import Exporter
Expand All @@ -29,3 +30,9 @@ class MarkdownExporter(Exporter):
file_extension = Unicode(
'md', config=True,
help="Extension of the file that should be written to disk")

@property
def default_config(self):
c = Config({'ExtractOutputPreprocessor':{'enabled':True}})
c.merge(super(MarkdownExporter,self).default_config)
return c
7 changes: 7 additions & 0 deletions IPython/nbconvert/filters/strings.py
Expand Up @@ -19,6 +19,7 @@
import os
import re
import textwrap
from urllib2 import quote
from xml.etree import ElementTree

from IPython.core.interactiveshell import InteractiveShell
Expand All @@ -38,6 +39,7 @@
'get_lines',
'ipython2python',
'posix_path',
'path2url',
]


Expand Down Expand Up @@ -181,3 +183,8 @@ def posix_path(path):
if os.path.sep != '/':
return path.replace(os.path.sep, '/')
return path

def path2url(path):
"""Turn a file path into a URL"""
parts = path.split(os.path.sep)
return '/'.join(quote(part) for part in parts)
2 changes: 1 addition & 1 deletion IPython/nbconvert/templates/latex/sphinx.tplx
Expand Up @@ -362,7 +362,7 @@ Note: For best display, use latex syntax highlighting. =))
((*- endblock -*))

((*- block data_jpg -*))
((( conditionally_center_output(insert_graphics(output.jpg_filename | posix_path)) )))
((( conditionally_center_output(insert_graphics(output.jpeg_filename | posix_path)) )))
((*- endblock -*))

((*- block data_svg -*))
Expand Down
22 changes: 10 additions & 12 deletions IPython/nbconvert/templates/markdown.tpl
Expand Up @@ -2,19 +2,13 @@


{% block in_prompt %}
In[{{ cell.prompt_number if cell.prompt_number else ' ' }}]:
{% endblock in_prompt %}

{% block output_prompt %}
{% if cell.haspyout %}
Out[{{ cell.prompt_number }}]:
{%- endif %}
{%- endblock output_prompt %}

{% block input %}
```
{{ cell.input }}
```
{{ cell.input | indent(4)}}
{% endblock input %}

{% block pyerr %}
Expand All @@ -26,6 +20,7 @@ Out[{{ cell.prompt_number }}]:
{% endblock traceback_line %}

{% block pyout %}

{% block data_priority scoped %}
{{ super() }}
{% endblock %}
Expand All @@ -36,23 +31,25 @@ Out[{{ cell.prompt_number }}]:
{% endblock stream %}

{% block data_svg %}
[!image]({{ output.svg_filename }})
![svg]({{ output.svg_filename | path2url }})
{% endblock data_svg %}

{% block data_png %}
[!image]({{ output.png_filename }})
![png]({{ output.png_filename | path2url }})
{% endblock data_png %}

{% block data_jpg %}
[!image]({{ output.jpg_filename }})
![jpeg]({{ output.jpeg_filename | path2url }})
{% endblock data_jpg %}

{% block data_latex %}
$$
{{ output.latex }}
$$
{% endblock data_latex %}

{% block data_html scoped %}
{{ output.html }}
{% endblock data_html %}

{% block data_text scoped %}
{{ output.text | indent }}
{% endblock data_text %}
Expand All @@ -61,6 +58,7 @@ $$
{{ cell.source | wrap_text(80) }}
{% endblock markdowncell %}


{% block headingcell scoped %}
{{ '#' * cell.level }} {{ cell.source | replace('\n', ' ') }}
{% endblock headingcell %}
Expand Down
25 changes: 15 additions & 10 deletions IPython/nbconvert/templates/rst.tpl
Expand Up @@ -2,25 +2,22 @@


{% block in_prompt %}

In[{{ cell.prompt_number if cell.prompt_number else ' ' }}]:

.. code:: python

{% endblock in_prompt %}

{% block output_prompt %}
{% if cell.haspyout -%}
Out[{{ cell.prompt_number }}]:
{% endif %}
{% endblock output_prompt %}

{% block input %}
{%- if not cell.input.isspace() -%}
.. code:: python

{{ cell.input | indent}}
{%- endif -%}
{% endblock input %}

{% block pyerr %}
::

{{ super() }}
{% endblock pyerr %}

Expand Down Expand Up @@ -49,19 +46,27 @@ In[{{ cell.prompt_number if cell.prompt_number else ' ' }}]:
{% endblock data_png %}

{% block data_jpg %}
..jpg image:: {{ output.jpg_filename }}
.. image:: {{ output.jpeg_filename }}
{% endblock data_jpg %}

{% block data_latex %}
.. math::
{{ output.latex | indent }}

{{ output.latex | strip_dollars | indent }}
{% endblock data_latex %}

{% block data_text scoped %}
.. parsed-literal::

{{ output.text | indent }}
{% endblock data_text %}

{% block data_html scoped %}
.. raw:: html

{{ output.html | indent }}
{% endblock data_html %}

{% block markdowncell scoped %}
{{ cell.source | markdown2rst }}
{% endblock markdowncell %}
Expand Down

0 comments on commit 8c88aec

Please sign in to comment.