Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mystnb format #456

Merged
merged 18 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ install:
pip install -r requirements.txt;
fi
# install is required for testing the pre-commit mode
- pip install . || true
- pip install .[myst] || true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the install to an extra, if thats ok

# install black if available (Python 3.6 and above), and autopep8 for testing the pipe mode
- pip install black || true
- pip install autopep8 || true
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dependencies:
- setuptools
- twine
- pandoc
- myst-parser
15 changes: 15 additions & 0 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
from .languages import _SCRIPT_EXTENSIONS, _COMMENT_CHARS, same_language
from .pandoc import pandoc_version, is_pandoc_available
from .magics import is_magic
from .myst import (
MYST_FORMAT_NAME,
is_myst_available,
myst_version,
myst_extensions,
)


class JupytextFormatError(ValueError):
Expand Down Expand Up @@ -162,6 +168,15 @@ def __init__(self,
cell_exporter_class=None,
current_version_number=pandoc_version()))

if is_myst_available():
JUPYTEXT_FORMATS.extend([NotebookFormatDescription(
format_name=MYST_FORMAT_NAME,
extension=ext,
header_prefix='',
cell_reader_class=None,
cell_exporter_class=None,
current_version_number=myst_version()) for ext in myst_extensions()])

NOTEBOOK_EXTENSIONS = list(dict.fromkeys(['.ipynb'] + [fmt.extension for fmt in JUPYTEXT_FORMATS]))
EXTENSION_PREFIXES = ['.lgt', '.spx', '.pct', '.hyd', '.nb']

Expand Down
24 changes: 24 additions & 0 deletions jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .languages import default_language_from_metadata_and_ext, set_main_and_cell_language
from .pep8 import pep8_lines_between_cells
from .pandoc import md_to_notebook, notebook_to_md
from .myst import myst_extensions, myst_to_notebook, notebook_to_myst


class TextNotebookConverter(NotebookReader, NotebookWriter):
Expand Down Expand Up @@ -54,6 +55,9 @@ def reads(self, s, **_):
if self.fmt.get('format_name') == 'pandoc':
return md_to_notebook(s)

if self.ext in myst_extensions():
return myst_to_notebook(s)

lines = s.splitlines()

cells = []
Expand Down Expand Up @@ -123,6 +127,26 @@ def writes(self, nb, metadata=None, **kwargs):
metadata=metadata,
cells=cells))

if self.ext in myst_extensions():
pygments_lexer = metadata.get("language_info", {}).get("pygments_lexer", None)
metadata = insert_jupytext_info_and_filter_metadata(metadata, self.ext, self.implementation)

cells = []
for cell in nb.cells:
cell_metadata = filter_metadata(cell.metadata,
self.fmt.get('cell_metadata_filter'),
_IGNORE_CELL_METADATA)
if cell.cell_type == 'code':
cells.append(new_code_cell(source=cell.source, metadata=cell_metadata))
else:
cells.append(NotebookNode(source=cell.source, metadata=cell_metadata, cell_type=cell.cell_type))
return notebook_to_myst(NotebookNode(
nbformat=nb.nbformat,
nbformat_minor=nb.nbformat_minor,
metadata=metadata,
cells=cells),
default_lexer=pygments_lexer)

# Copy the notebook, in order to be sure we do not modify the original notebook
nb = NotebookNode(
nbformat=nb.nbformat,
Expand Down
Loading