Skip to content

Commit

Permalink
MyST Markdown files are recognized as such even if myst is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jul 30, 2020
1 parent 38c85c0 commit 535766f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ error will occur if the required dependencies, resp. `myst-parser` and `pandoc`,
**Fixed**
- Configured coverage targets in `codecov.yml`
- Only scripts can have an encoding comment, not Markdown or R Markdown files (#576)
- MyST Markdown files are recognized as such even if `myst` is missing (#556)


1.5.2 (2020-07-21)
Expand Down
13 changes: 13 additions & 0 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import os
import re
import yaml
import warnings
import nbformat
from .header import header_to_metadata_and_cell, insert_or_test_version_number
Expand Down Expand Up @@ -261,6 +262,18 @@ def read_metadata(text, ext):
if ext in [".r", ".R"] and not metadata:
metadata, _, _, _ = header_to_metadata_and_cell(lines, "#'", ext)

# MyST has the metadata at the root level
if not metadata and ext in myst_extensions() and text.startswith("---"):
for header in yaml.safe_load_all(text):
if (
header.get("jupytext", {})
.get("text_representation", {})
.get("format_name")
== "myst"
):
return header
return metadata

return metadata


Expand Down
40 changes: 35 additions & 5 deletions tests/test_ipynb_to_myst.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from textwrap import dedent
import pytest
from nbformat.v4.nbbase import new_notebook

from tornado.web import HTTPError
from jupytext.formats import get_format_implementation, JupytextFormatError
from jupytext.myst import (
myst_to_notebook,
Expand Down Expand Up @@ -149,12 +149,42 @@ def test_add_source_map():
assert notebook.metadata.source_map == [3, 5, 7, 12]


PLEASE_INSTALL_MYST = "The MyST Markdown format requires 'myst_parser>=0.8'."


@requires_no_myst
def test_meaningfull_error_when_myst_is_missing(tmpdir):
def test_meaningfull_error_write_myst_missing(tmpdir):
nb_file = tmpdir.join("notebook.ipynb")
jupytext.write(new_notebook(), str(nb_file))

with pytest.raises(
ImportError, match="The MyST Markdown format requires 'myst_parser>=0.8'."
):
with pytest.raises(ImportError, match=PLEASE_INSTALL_MYST):
jupytext_cli([str(nb_file), "--to", "md:myst"])


@requires_no_myst
def test_meaningfull_error_open_myst_missing(tmpdir):
md_file = tmpdir.join("notebook.md")
md_file.write(
"""---
jupytext:
text_representation:
extension: '.md'
format_name: myst
kernelspec:
display_name: Python 3
language: python
name: python3
---
1 + 1
"""
)

with pytest.raises(ImportError, match=PLEASE_INSTALL_MYST):
jupytext_cli([str(md_file), "--to", "ipynb"])

cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)

with pytest.raises(HTTPError, match=PLEASE_INSTALL_MYST):
cm.get("notebook.md")

0 comments on commit 535766f

Please sign in to comment.