from IPython.nbconvert.utils.pandoc import pandoc
from IPython.display import HTML, Javascript, display
from IPython.nbconvert.filters import citation2latex, strip_files_prefix, \
markdown2html, markdown2latex
def pandoc_render(markdown):
"""Render Pandoc Markdown->LaTeX content."""
## Convert the markdown directly to latex. This is what nbconvert does.
#latex = pandoc(markdown, "markdown", "latex")
#html = pandoc(markdown, "markdown", "html", ["--mathjax"])
# nbconvert template conversions
html = strip_files_prefix(markdown2html(markdown))
latex = markdown2latex(citation2latex(markdown))
display(HTML(data="
" \
"
NBConvert Latex Output
" \
"
" + latex + "
"\
"
" \
"" \
"" \
"
NBViewer Output
" \
"
" + html + "
" \
"
"))
javascript = """
$.getScript("https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js");
"""
display(Javascript(data=javascript))
def notebook_render(markdown):
javascript = """
var mdcell = new IPython.MarkdownCell();
mdcell.create_element();
mdcell.set_text('""" + markdown.replace("\\", "\\\\").replace("'", "\'").replace("\n", "\\n") + """');
mdcell.render();
$(element).append(mdcell.element)
.removeClass()
.css('left', '66%')
.css('position', 'absolute')
.css('width', '30%')
mdcell.element.prepend(
$('')
.removeClass()
.css('background', '#AAAAFF')
.css('width', '100 %')
.html('Notebook Output')
);
container.show()
"""
display(Javascript(data=javascript))
def pandoc_html_render(markdown):
"""Render Pandoc Markdown->LaTeX content."""
# Convert the markdown directly to latex. This is what nbconvert does.
latex = pandoc(markdown, "markdown", "latex")
# Convert the pandoc generated latex to HTML so it can be rendered in
# the web browser.
html = pandoc(latex, "latex", "html", ["--mathjax"])
display(HTML(data="HTML Pandoc Output
" \
"" + html + "
"))
return html
def compare_render(markdown):
notebook_render(markdown)
pandoc_render(markdown)
try:
import lxml
print 'LXML found!'
except:
print 'Warning! No LXML found - the old citation2latex filter will not work'
compare_render(r"""
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6""")
compare_render(r"""
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6 """)
print("\n"*10)
compare_render(r"""
[Link2Heading](http://127.0.0.1:8888/0a2d8086-ee24-4e5b-a32b-f66b525836cb#General-markdown)
""")
compare_render(r"""
This is Markdown **bold** and *italic* text.
""")
compare_render(r"""
- li 1
- li 2
1. li 3
1. li 4
- li 5
""")
compare_render(ur"""
überschuß +***^°³³ α β θ
""")
compare_render(r"""
# Heading 1 ~~strikeout~~
""")
compare_render(r"""
above
--------
below
""")
compare_render(r"""
This is Markdown ~subscript~ and ^superscript^ text.
""")
compare_render(r"""
This is Markdown not_italic_.
""")
compare_render(r"""
\newcommand{\tuple}[1]{\langle #1 \rangle}
$\tuple{a, b, c}$
""")
compare_render(r"""
$\newcommand{\foo}[1]{...:: #1 ::...}$
$\foo{bar}$
""")
compare_render(r"""
This is HTML bold and italic text.
""")
compare_render(r"""
Center aligned
""")
compare_render(r"""
This is \LaTeX \bf{bold} and \emph{italic} text.
""")
compare_render(r"""
**foo** $\left( \sum_{k=1}^n a_k b_k \right)^2 \leq$ b\$ar $$test$$
\cite{}
""")
compare_render(r"""
""")
compare_render(r"""
+---+---+
| a | b |
+---+---+
| c | d |
+---+---+
""")
compare_render(r"""
|Left |Center |Right|
|:----|:-----:|----:|
|Text1|Text2 |Text3|
""")
compare_render(r"""
Right Aligned Center Aligned Left Aligned
------------- -------------- ------------
Why does this
actually work? Who
knows ...
""")
print("\n"*5)
compare_render(r"""
![Alternate Text](http://ipython.org/_static/IPy_header.png)
""")
compare_render(r"""
""")
compare_render(r"""
My equation:
$$ 5/x=2y $$
It is inline $ 5/x=2y $ here.
""")
compare_render(r"""
$5 \cdot x=2$
$
5 \cdot x=2$
$
5 \cdot x=2
$
""")
compare_render(r"""
\begin{align}
a & b\\
d & c
\end{align}
\begin{eqnarray}
a & b \\
c & d
\end{eqnarray}
""")
compare_render(r"""
1<2 is true, but 3>4 is false.
$1<2$ is true, but $3>4$ is false.
1<2 it is even worse if it is alone in a line.
""")
compare_render(r"""
some source code
```
a = "test"
print(a)
```
""")
compare_render(r"""
some source code
```python
a = "test"
print(a)
```
""")