Skip to content

Commit

Permalink
Add tentative new org:pandoc format
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed May 13, 2020
1 parent ade2936 commit 086178b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
10 changes: 10 additions & 0 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ def __init__(
current_version_number=pandoc_version(),
)
)
JUPYTEXT_FORMATS.append(
NotebookFormatDescription(
format_name="pandoc",
extension=".org",
header_prefix="",
cell_reader_class=None,
cell_exporter_class=None,
current_version_number=pandoc_version(),
)
)

if is_myst_available():
JUPYTEXT_FORMATS.extend(
Expand Down
32 changes: 21 additions & 11 deletions jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
set_main_and_cell_language,
)
from .pep8 import pep8_lines_between_cells
from .pandoc import md_to_notebook, notebook_to_md
from .pandoc import text_to_notebook, notebook_to_text
from .myst import myst_extensions, myst_to_notebook, notebook_to_myst, MYST_FORMAT_NAME


Expand Down Expand Up @@ -72,8 +72,13 @@ def update_fmt_with_notebook_options(self, metadata):

def reads(self, s, **_):
"""Read a notebook represented as text"""
if self.fmt.get("format_name") == "pandoc":
return md_to_notebook(s)
if (
self.fmt.get("format_name") == "pandoc"
or self.fmt.get("extension") == ".org"
):
return text_to_notebook(
s, "org" if self.fmt.get("extension") == ".org" else "markdown"
)

if self.fmt.get("format_name") == MYST_FORMAT_NAME:
return myst_to_notebook(s)
Expand Down Expand Up @@ -143,7 +148,10 @@ def reads(self, s, **_):

def writes(self, nb, metadata=None, **kwargs):
"""Return the text representation of the notebook"""
if self.fmt.get("format_name") == "pandoc":
if (
self.fmt.get("format_name") == "pandoc"
or self.fmt.get("extension") == ".org"
):
metadata = insert_jupytext_info_and_filter_metadata(
metadata, self.ext, self.implementation
)
Expand All @@ -168,13 +176,15 @@ def writes(self, nb, metadata=None, **kwargs):
)
)

return notebook_to_md(
NotebookNode(
nbformat=nb.nbformat,
nbformat_minor=nb.nbformat_minor,
metadata=metadata,
cells=cells,
)
filtered_notebook = NotebookNode(
nbformat=nb.nbformat,
nbformat_minor=nb.nbformat_minor,
metadata=metadata,
cells=cells,
)
return notebook_to_text(
filtered_notebook,
"org" if self.fmt.get("extension") == ".org" else "markdown",
)

if self.fmt.get(
Expand Down
16 changes: 10 additions & 6 deletions jupytext/pandoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ def pandoc_version():
return version


def md_to_notebook(text):
"""Convert a Markdown text to a Jupyter notebook, using Pandoc"""
def text_to_notebook(text, pandoc_format_name):
"""Convert a Markdown/org-mode text to a Jupyter notebook, using Pandoc"""
tmp_file = tempfile.NamedTemporaryFile(delete=False)
tmp_file.write(text.encode("utf-8"))
tmp_file.close()

pandoc(
u"--from markdown --to ipynb -s --atx-headers --wrap=preserve --preserve-tabs",
u"--from {} --to ipynb -s --atx-headers --wrap=preserve --preserve-tabs".format(
pandoc_format_name
),
tmp_file.name,
tmp_file.name,
)
Expand All @@ -79,14 +81,16 @@ def md_to_notebook(text):
return notebook


def notebook_to_md(notebook):
"""Convert a notebook to its Markdown representation, using Pandoc"""
def notebook_to_text(notebook, pandoc_format_name):
"""Convert a notebook to its Markdown/org-mode representation, using Pandoc"""
tmp_file = tempfile.NamedTemporaryFile(delete=False)
tmp_file.write(ipynb_writes(notebook).encode("utf-8"))
tmp_file.close()

pandoc(
u"--from ipynb --to markdown -s --atx-headers --wrap=preserve --preserve-tabs",
u"--from ipynb --to {} -s --atx-headers --wrap=preserve --preserve-tabs".format(
pandoc_format_name
),
tmp_file.name,
tmp_file.name,
)
Expand Down

0 comments on commit 086178b

Please sign in to comment.