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

paired md:myst format also works as MyST Markdown #1315

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all 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
40 changes: 31 additions & 9 deletions src/jupytext/myst.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
except ImportError:
MarkdownIt = None

MYST_FRONTMATER_FIELD = "jupyter"
MYST_FORMAT_NAME = "myst"
CODE_DIRECTIVE = "{code-cell}"
RAW_DIRECTIVE = "{raw-cell}"
Expand Down Expand Up @@ -99,15 +100,17 @@
pass
else:
try:
if (
metadata.get("jupytext", {})
format_name = (

Check warning on line 103 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L103

Added line #L103 was not covered by tests
metadata.get(MYST_FRONTMATER_FIELD, {})
.get("jupytext", {})
.get("text_representation", {})
.get("format_name", "")
== MYST_FORMAT_NAME
):
return True
)
except AttributeError:
pass
else:
if format_name == MYST_FORMAT_NAME:
return True

Check warning on line 113 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L112-L113

Added lines #L112 - L113 were not covered by tests

# is there at least on fenced code block with a code/raw directive language
for token in tokens:
Expand Down Expand Up @@ -155,7 +158,7 @@
tags: [hide-output, show-input]
---
"""
string = yaml.dump(data, Dumper=CompactDumper)
string = yaml.dump(data, Dumper=CompactDumper, sort_keys=False)

Check warning on line 161 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L161

Added line #L161 was not covered by tests
lines = string.splitlines()
if compact and all(line and line[0].isalpha() for line in lines):
return "\n".join([f":{line}" for line in lines]) + "\n\n"
Expand Down Expand Up @@ -289,12 +292,17 @@
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as error:
raise MystMetadataParsingError(f"Notebook metadata: {error}")

# create an empty notebook
# create an empty notebook, extracting ipynb metadata from metadata_nb
nbf_version = nbf.v4
kwargs = {"metadata": nbf.from_dict(metadata_nb)}
kwargs = {"metadata": nbf.from_dict(metadata_nb.pop(MYST_FRONTMATER_FIELD, {}))}

Check warning on line 297 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L297

Added line #L297 was not covered by tests
notebook = nbf_version.new_notebook(**kwargs)
source_map = [] # this is a list of the starting line number for each cell

# add remaining frontmatter as yaml block in first cell
if metadata_nb:
metadata_nb = dump_yaml_blocks(metadata_nb, compact=False)
notebook.cells.append(nbf_version.new_markdown_cell(source=metadata_nb.strip()))

Check warning on line 304 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L302-L304

Added lines #L302 - L304 were not covered by tests

def _flush_markdown(start_line, token, md_metadata):
"""When we find a cell we check if there is preceding text.o"""
endline = token.map[0] if token else len(lines)
Expand Down Expand Up @@ -375,8 +383,22 @@
if pygments_lexer is None:
pygments_lexer = default_lexer

# update nb_metadata with yaml block in first cell
# TODO(itcarroll): config to move kernelspec
frontmatter = {}
if nb.cells[0].source.startswith("---"):
try:
frontmatter = yaml.safe_load(nb.cells[0].source[4:-4])
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as error:
raise MystMetadataParsingError(f"MyST frontmatter: {error}")

Check warning on line 393 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L388-L393

Added lines #L388 - L393 were not covered by tests
else:
nb.cells.pop(0)

Check warning on line 395 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L395

Added line #L395 was not covered by tests

if nb_metadata:
string += dump_yaml_blocks(nb_metadata, compact=False)
frontmatter[MYST_FRONTMATER_FIELD] = nb_metadata

Check warning on line 398 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L398

Added line #L398 was not covered by tests

if frontmatter:
string += dump_yaml_blocks(frontmatter, compact=False)

Check warning on line 401 in src/jupytext/myst.py

View check run for this annotation

Codecov / codecov/patch

src/jupytext/myst.py#L400-L401

Added lines #L400 - L401 were not covered by tests

last_cell_md = False
for i, cell in enumerate(nb.cells):
Expand Down
Loading