Skip to content

Commit

Permalink
upgrade myst-parser dep to 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aleivag committed Mar 18, 2023
1 parent 385c4a7 commit 83f264b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ repos:
args: [--config-file=pyproject.toml]
additional_dependencies:
- importlib_metadata
- myst-parser~=0.18.0
- myst-parser~=1.0.0
- "sphinx~=5.0"
- nbclient
- types-PyYAML
Expand Down
14 changes: 7 additions & 7 deletions myst_nb/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __post_init__(self):
default_factory=dict,
metadata={
"help": "Custom formats for reading notebook; suffix -> reader",
"docutils_exclude": True,
"omit": ["docutils"],
"sections": (Section.global_lvl, Section.read),
},
)
Expand Down Expand Up @@ -184,7 +184,7 @@ def __post_init__(self):
"validator": deep_mapping(instance_of(str), instance_of(str)),
"help": "Mapping of kernel name regex to replacement kernel name"
"(applied before execution)",
"docutils_exclude": True,
"omit": ["docutils"],
"sections": (Section.global_lvl, Section.execute),
},
)
Expand Down Expand Up @@ -220,7 +220,7 @@ def __post_init__(self):
"validator": deep_iterable(instance_of(str)),
"help": "Exclude (POSIX) glob patterns for notebooks",
"legacy_name": "execution_excludepatterns",
"docutils_exclude": True,
"omit": ["docutils"],
"sections": (Section.global_lvl, Section.execute),
},
)
Expand Down Expand Up @@ -387,7 +387,7 @@ def __post_init__(self):
"help": "Overrides for the base render priority of mime types: "
"list of (builder name, mime type, priority)",
# TODO how to allow this in docutils?
"docutils_exclude": True,
"omit": ["docutils"],
"sections": (Section.global_lvl, Section.file_lvl, Section.render),
},
repr=False,
Expand Down Expand Up @@ -454,7 +454,7 @@ def __post_init__(self):
metadata={
"validator": deep_mapping(instance_of(str), instance_of((str, int))),
"help": "Options for image outputs (class|alt|height|width|scale|align)",
"docutils_exclude": True,
"omit": ["docutils"],
# TODO backward-compatible change to "image_options"?
"cell_key": "image",
"sections": (
Expand All @@ -471,7 +471,7 @@ def __post_init__(self):
metadata={
"validator": deep_mapping(instance_of(str), instance_of((str, int))),
"help": "Options for figure outputs (classes|name|caption|caption_before)",
"docutils_exclude": True,
"omit": ["docutils"],
"cell_key": "figure",
"sections": (
Section.global_lvl,
Expand Down Expand Up @@ -505,7 +505,7 @@ def __post_init__(self):
instance_of(str), deep_mapping(instance_of(str), instance_of(str))
),
"help": "Javascript to be loaded on pages containing ipywidgets",
"docutils_exclude": True,
"omit": ["docutils"],
"sections": (Section.global_lvl, Section.render),
},
repr=False,
Expand Down
26 changes: 11 additions & 15 deletions myst_nb/core/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ def _flush_markdown(start_line, token, md_metadata):
md_metadata: dict = {}

for token in tokens:

nesting_level += token.nesting

if nesting_level != 0:
Expand Down Expand Up @@ -313,25 +312,22 @@ class _MockDirective:


def _read_fenced_cell(token, cell_index, cell_type):
from myst_parser.parsers.directives import (
DirectiveParsingError,
parse_directive_text,
)
from myst_parser.parsers.directives import parse_directive_text

try:
_, options, body_lines, _ = parse_directive_text(
directive_class=_MockDirective,
first_line="",
content=token.content,
validate_options=False,
)
except DirectiveParsingError as err:
result = parse_directive_text(
directive_class=_MockDirective,
first_line="",
content=token.content,
validate_options=False,
)
if result.warnings:
raise MystMetadataParsingError(
"{} cell {} at line {} could not be read: {}".format(
cell_type, cell_index, token.map[0] + 1, err
cell_type, cell_index, token.map[0] + 1, result.warnings[0]
)
)
return options, body_lines

return result.options, result.body


def _read_cell_metadata(token, cell_index):
Expand Down
13 changes: 2 additions & 11 deletions myst_nb/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ def render_nb_cell_code(self: SelfType, token: SyntaxTreeNode) -> None:

self.add_line_and_source_path(cell_container, token)
with self.current_node_context(cell_container, append=True):

# render the code source code
if not remove_input:
cell_input = nodes.container(
Expand Down Expand Up @@ -311,11 +310,6 @@ class MimeData:
"""Index of the output in the cell"""
line: int | None = None
"""Source line of the cell"""
md_headings: bool = False
"""Whether to render headings in text/markdown blocks."""
# we can only do this if know the content will be rendered into the main body
# of the document, e.g. not inside a container node
# (otherwise it will break the structure of the AST)

@property
def string(self) -> str:
Expand Down Expand Up @@ -599,9 +593,7 @@ def render_markdown(self, data: MimeData) -> list[nodes.Element]:
fmt = self.renderer.get_cell_level_config(
"render_markdown_format", data.cell_metadata, line=data.line
)
return self._render_markdown_base(
data, fmt=fmt, inline=False, allow_headings=data.md_headings
)
return self._render_markdown_base(data, fmt=fmt, inline=False)

def render_text_plain(self, data: MimeData) -> list[nodes.Element]:
"""Render a notebook text/plain mime data output."""
Expand Down Expand Up @@ -797,7 +789,7 @@ def render_widget_view_inline(self, data: MimeData) -> list[nodes.Element]:
return self.render_widget_view(data)

def _render_markdown_base(
self, data: MimeData, *, fmt: str, inline: bool, allow_headings: bool
self, data: MimeData, *, fmt: str, inline: bool
) -> list[nodes.Element]:
"""Base render for a notebook markdown mime output (block or inline)."""
psuedo_element = nodes.Element() # element to hold the parsed markdown
Expand Down Expand Up @@ -833,7 +825,6 @@ def _render_markdown_base(
data.string,
data.line or 0,
inline=inline,
allow_headings=allow_headings,
)
finally:
# restore the parser
Expand Down
14 changes: 3 additions & 11 deletions myst_nb/docutils_.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
create_warning,
token_line,
)
from myst_parser.parsers.docutils_ import (
DOCUTILS_EXCLUDED_ARGS as DOCUTILS_EXCLUDED_ARGS_MYST,
)
from myst_parser.parsers.docutils_ import Parser as MystParser
from myst_parser.parsers.docutils_ import create_myst_config, create_myst_settings_spec
from myst_parser.parsers.mdit import create_md_parser
Expand Down Expand Up @@ -81,7 +78,7 @@ class Parser(MystParser):
settings_spec = (
"MyST-NB options",
None,
create_myst_settings_spec(DOCUTILS_EXCLUDED_ARGS, NbParserConfig, "nb_"),
create_myst_settings_spec(NbParserConfig, "nb_"),
*MystParser.settings_spec,
)
"""Runtime settings specification."""
Expand Down Expand Up @@ -116,18 +113,14 @@ def _parse(self, inputstring: str, document: nodes.document) -> None:

# get markdown parsing configuration
try:
md_config = create_myst_config(
document.settings, DOCUTILS_EXCLUDED_ARGS_MYST
)
md_config = create_myst_config(document.settings)
except (TypeError, ValueError) as error:
logger.error(f"myst configuration invalid: {error.args[0]}")
md_config = MdParserConfig()

# get notebook rendering configuration
try:
nb_config = create_myst_config(
document.settings, DOCUTILS_EXCLUDED_ARGS, NbParserConfig, "nb_"
)
nb_config = create_myst_config(document.settings, NbParserConfig, "nb_")
except (TypeError, ValueError) as error:
logger.error(f"myst-nb configuration invalid: {error.args[0]}")
nb_config = NbParserConfig()
Expand Down Expand Up @@ -301,7 +294,6 @@ def _render_nb_cell_code_outputs(
self.add_line_and_source_path_r(_nodes, token)
self.current_node.extend(_nodes)
elif output.output_type in ("display_data", "execute_result"):

# Note, this is different to the sphinx implementation,
# here we directly select a single output, based on the mime_priority,
# as opposed to output all mime types, and select in a post-transform
Expand Down
6 changes: 2 additions & 4 deletions myst_nb/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from markdown_it.tree import SyntaxTreeNode
from myst_parser.config.main import MdParserConfig, merge_file_level
from myst_parser.mdit_to_docutils.base import token_line
from myst_parser.mdit_to_docutils.sphinx_ import SphinxRenderer, create_warning
from myst_parser.mdit_to_docutils.sphinx_ import SphinxRenderer
from myst_parser.warnings_ import create_warning
from myst_parser.parsers.mdit import create_md_parser
from myst_parser.parsers.sphinx_ import MystParser
import nbformat
Expand Down Expand Up @@ -262,7 +263,6 @@ def _render_nb_cell_code_outputs(
self.add_line_and_source_path_r(_nodes, token)
self.current_node.extend(_nodes)
elif output.output_type in ("display_data", "execute_result"):

# Note, this is different to the docutils implementation,
# where we directly select a single output, based on the mime_priority.
# Here, we do not know the mime priority until we know the output format
Expand Down Expand Up @@ -504,9 +504,7 @@ class HideInputCells(SphinxPostTransform):
formats = ("html",)

def run(self, **kwargs):

for node in findall(self.document)(nodes.container):

if (
node.get("nb_element") == "cell_code"
and node.get("hide_mode")
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies = [
"ipython",
"jupyter-cache~=0.5.0",
"nbclient", # nbclient version pinned by jupyter-client
"myst-parser~=0.18.0",
"myst-parser~=1.0.0",
"nbformat~=5.0",
"pyyaml",
"sphinx>=4,<6",
Expand Down Expand Up @@ -80,9 +80,9 @@ rtd = [
"numpy",
"pandas",
"plotly",
"sphinx-book-theme~=0.3.0",
"sphinx-book-theme~=1.0.0",
"sphinx-copybutton",
"sphinx-design~=0.1.0",
"sphinx-design~=0.3.0",
"sphinxcontrib-bibtex",
"sympy",
]
Expand Down
9 changes: 4 additions & 5 deletions tests/nb_fixtures/reporter_warnings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ cells:
source: |
{unknown}`a`
.
<string>:20002: (ERROR/3) Unknown interpreted text role "unknown".
<string>:20002: (WARNING/2) Unknown interpreted text role "unknown". [myst.role_unknown]
.


Unknown directive:
.
cells:
Expand All @@ -24,7 +23,7 @@ cells:
```{xyz}
```
.
<string>:10003: (ERROR/3) Unknown directive type "xyz".
<string>:10003: (WARNING/2) Unknown directive type: 'xyz' [myst.directive_unknown]
.

Directive parsing error:
Expand Down Expand Up @@ -66,5 +65,5 @@ cells:

[a]: c
.
<string>:20004: (WARNING/2) Duplicate reference definition: A [myst.ref]
.
<string>:20004: (WARNING/2) Duplicate reference definition: A [myst.duplicate_def]
.

0 comments on commit 83f264b

Please sign in to comment.