From 83f264b2018c030cc6b0e6eb6ece4c49b1ab99ce Mon Sep 17 00:00:00 2001 From: Alvaro Leiva Geisse Date: Fri, 17 Mar 2023 08:12:03 -0700 Subject: [PATCH] upgrade myst-parser dep to 1.0 --- .pre-commit-config.yaml | 2 +- myst_nb/core/config.py | 14 ++++++------- myst_nb/core/read.py | 26 +++++++++++-------------- myst_nb/core/render.py | 13 ++----------- myst_nb/docutils_.py | 14 +++---------- myst_nb/sphinx_.py | 6 ++---- pyproject.toml | 6 +++--- tests/nb_fixtures/reporter_warnings.txt | 9 ++++----- 8 files changed, 33 insertions(+), 57 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index abd65307..5530654b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/myst_nb/core/config.py b/myst_nb/core/config.py index d90e85ab..7e21d014 100644 --- a/myst_nb/core/config.py +++ b/myst_nb/core/config.py @@ -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), }, ) @@ -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), }, ) @@ -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), }, ) @@ -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, @@ -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": ( @@ -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, @@ -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, diff --git a/myst_nb/core/read.py b/myst_nb/core/read.py index a9d6c27b..2b3dae4b 100644 --- a/myst_nb/core/read.py +++ b/myst_nb/core/read.py @@ -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: @@ -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): diff --git a/myst_nb/core/render.py b/myst_nb/core/render.py index 5a2c04f0..7b7368cb 100644 --- a/myst_nb/core/render.py +++ b/myst_nb/core/render.py @@ -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( @@ -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: @@ -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.""" @@ -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 @@ -833,7 +825,6 @@ def _render_markdown_base( data.string, data.line or 0, inline=inline, - allow_headings=allow_headings, ) finally: # restore the parser diff --git a/myst_nb/docutils_.py b/myst_nb/docutils_.py index eeac7a32..01d395b7 100644 --- a/myst_nb/docutils_.py +++ b/myst_nb/docutils_.py @@ -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 @@ -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.""" @@ -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() @@ -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 diff --git a/myst_nb/sphinx_.py b/myst_nb/sphinx_.py index 588741c0..8b93410d 100644 --- a/myst_nb/sphinx_.py +++ b/myst_nb/sphinx_.py @@ -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 @@ -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 @@ -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") diff --git a/pyproject.toml b/pyproject.toml index 18261905..4cbb4503 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", @@ -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", ] diff --git a/tests/nb_fixtures/reporter_warnings.txt b/tests/nb_fixtures/reporter_warnings.txt index 813d082c..1e226162 100644 --- a/tests/nb_fixtures/reporter_warnings.txt +++ b/tests/nb_fixtures/reporter_warnings.txt @@ -10,10 +10,9 @@ cells: source: | {unknown}`a` . -:20002: (ERROR/3) Unknown interpreted text role "unknown". +:20002: (WARNING/2) Unknown interpreted text role "unknown". [myst.role_unknown] . - Unknown directive: . cells: @@ -24,7 +23,7 @@ cells: ```{xyz} ``` . -:10003: (ERROR/3) Unknown directive type "xyz". +:10003: (WARNING/2) Unknown directive type: 'xyz' [myst.directive_unknown] . Directive parsing error: @@ -66,5 +65,5 @@ cells: [a]: c . -:20004: (WARNING/2) Duplicate reference definition: A [myst.ref] -. \ No newline at end of file +:20004: (WARNING/2) Duplicate reference definition: A [myst.duplicate_def] +.