-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for the pandoc format for Jupyter notebook
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Jupyter notebook to Markdown and back, using Pandoc""" | ||
|
||
import subprocess | ||
import packaging.version | ||
import nbformat | ||
|
||
|
||
class PandocError(ChildProcessError): | ||
"""An error related to Pandoc""" | ||
pass | ||
|
||
|
||
def pandoc(args, text=''): | ||
"""Execute pandoc with the given arguments""" | ||
cmd = [u'pandoc'] + args.split() | ||
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) | ||
out, err = proc.communicate(text.encode('utf-8')) | ||
if proc.returncode: | ||
raise PandocError('pandoc exited with return code {}\n{}'.format(proc.returncode, str(err))) | ||
return out.decode('utf-8') | ||
|
||
|
||
def pandoc_version(): | ||
"""Pandoc's version number""" | ||
version = pandoc(u'--version').splitlines()[0].split()[1] | ||
|
||
if packaging.version.parse(version) < packaging.version.parse('2.7.1'): | ||
raise PandocError('Please install pandoc>=2.7.1 (found version {})'.format(version)) | ||
|
||
return version | ||
|
||
|
||
def md_to_notebook(text): | ||
"""Convert a Markdown text to a Jupyter notebook, using Pandoc""" | ||
json = pandoc(u'--from markdown --to ipynb', text) | ||
return nbformat.reads(json, as_version=4) | ||
|
||
|
||
def notebook_to_md(notebook): | ||
"""Convert a notebook to its Markdown representation, using Pandoc""" | ||
text = nbformat.writes(notebook) | ||
return pandoc(u'--from ipynb --to markdown', text) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from testfixtures import compare | ||
import jupytext | ||
|
||
|
||
def test_pandoc_implicit(markdown='''# Lorem ipsum | ||
**Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Nunc luctus | ||
bibendum felis dictum sodales. | ||
``` code | ||
print("hello") | ||
``` | ||
'''): | ||
nb = jupytext.reads(markdown, 'md:pandoc') | ||
markdown2 = jupytext.writes(nb, 'md') | ||
|
||
nb2 = jupytext.reads(markdown2, 'md') | ||
compare(nb, nb2) | ||
|
||
markdown3 = jupytext.writes(nb2, 'md') | ||
compare(markdown2, markdown3) | ||
|
||
|
||
def test_pandoc_explicit(markdown='''::: {.cell .markdown} | ||
Lorem | ||
===== | ||
**Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Nunc luctus | ||
bibendum felis dictum sodales. | ||
::: | ||
'''): | ||
nb = jupytext.reads(markdown, 'md') | ||
markdown2 = jupytext.writes(nb, 'md').replace('\r\n', '\n') | ||
compare(markdown, markdown2) |