From 06dd749b9f671d646145ca89db3015c5bddef330 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Thu, 30 Jul 2020 23:42:40 +0200 Subject: [PATCH] MyST Markdown files are recognized as such even if `myst` is missing --- CHANGELOG.md | 3 ++- jupytext/formats.py | 13 ++++++++++++ tests/test_ipynb_to_myst.py | 40 ++++++++++++++++++++++++++++++++----- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c286c1f70..16149847c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,9 +16,10 @@ 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) -- Support spaces in `--pipe` commands (#562) +- Spaces in `--pipe` commands are supported (#562) - Use `>=` and `<` rather than `~=` in the `extras_require` of `setup.py` (#589) - Bash commands starting with special characters are now correctly detected, thanks to Aaron Gokaslan (#587) +- MyST Markdown files are recognized as such even if `myst` is missing (#556) 1.5.2 (2020-07-21) diff --git a/jupytext/formats.py b/jupytext/formats.py index e349c057f..10731eaad 100644 --- a/jupytext/formats.py +++ b/jupytext/formats.py @@ -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 @@ -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 diff --git a/tests/test_ipynb_to_myst.py b/tests/test_ipynb_to_myst.py index 89de264a9..12a6bb974 100644 --- a/tests/test_ipynb_to_myst.py +++ b/tests/test_ipynb_to_myst.py @@ -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, @@ -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")