From 9b61af29de7dd8af6b945daf89e7074d2df107a3 Mon Sep 17 00:00:00 2001 From: mmcky Date: Fri, 2 Jul 2021 13:34:35 +1000 Subject: [PATCH 1/2] Add option to convert code to code-cell directives in md output --- rst_to_myst/cli.py | 9 ++++++++ rst_to_myst/markdownit.py | 12 ++++++++-- rst_to_myst/mdformat_render.py | 2 ++ tests/test_cli.py | 22 +++++++++++++++++++ .../test_convert_code_to_code_cell.md | 5 +++++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/test_cli/test_convert_code_to_code_cell.md diff --git a/rst_to_myst/cli.py b/rst_to_myst/cli.py index 71059bb..e289ca6 100644 --- a/rst_to_myst/cli.py +++ b/rst_to_myst/cli.py @@ -177,6 +177,12 @@ def split_extension(ctx, param, value): show_default=True, help="Convert math (where possible) to dollar-delimited math", ) +OPT_CODE_TO_CODE_CELL = click.option( + "--code-to-code-cell", + default=False, + show_default=True, + help="Convert code directives to code-cell directives", +) @main.command("ast") @@ -211,6 +217,7 @@ def ast(stream: TextIOWrapper, language: str, sphinx: bool, extensions, conversi @OPT_CITE_PREFIX @OPT_COLON_FENCES @OPT_DOLLAR_MATH +@OPT_CODE_TO_CODE_CELL @OPT_CONVERSIONS @OPT_CONFIG def tokens( @@ -223,6 +230,7 @@ def tokens( cite_prefix: str, colon_fences: bool, dollar_math: bool, + code_to_code_cell: bool, conversions, ): """Parse file / stdin (-) and print Markdown-It tokens.""" @@ -239,6 +247,7 @@ def tokens( cite_prefix=cite_prefix + "_", colon_fences=colon_fences, dollar_math=dollar_math, + code_to_code_cell=code_to_code_cell, ) click.echo(yaml_dump([token.as_dict() for token in output.tokens])) diff --git a/rst_to_myst/markdownit.py b/rst_to_myst/markdownit.py index bd9a6de..e9cee79 100644 --- a/rst_to_myst/markdownit.py +++ b/rst_to_myst/markdownit.py @@ -25,6 +25,7 @@ def __init__( default_role: Optional[str] = None, colon_fences: bool = True, dollar_math: bool = True, + code_to_code_cell: bool = False, ): self._document = document self._warning_stream = warning_stream or StringIO() @@ -35,6 +36,7 @@ def __init__( self.default_role = default_role self.colon_fences = colon_fences self.dollar_math = dollar_math + self.code_to_code_cell = code_to_code_cell self.reset_state() @@ -86,6 +88,7 @@ def nested_parse(self, nodes: List[nodes.Element]) -> List[Token]: default_role=self.default_role, colon_fences=self.colon_fences, dollar_math=self.dollar_math, + code_to_code_cell=self.code_to_code_cell, ) for node in nodes: node.walkabout(new_inst) @@ -619,10 +622,15 @@ def visit_DirectiveNode(self, node): and len(node.children) == 2 ): # special case, where we can use standard Markdown fences + token_type = "fence" + directive_name = "code" + if self.code_to_code_cell: + token_type = "directive" + directive_name = "code-cell" argument, content = node.children self.add_token( - "fence", - "code", + token_type, + directive_name, 0, content=content.astext() + "\n", markup="```", diff --git a/rst_to_myst/mdformat_render.py b/rst_to_myst/mdformat_render.py index 3422d2e..3bd2e9a 100644 --- a/rst_to_myst/mdformat_render.py +++ b/rst_to_myst/mdformat_render.py @@ -197,6 +197,7 @@ def rst_to_myst( consecutive_numbering: bool = True, colon_fences: bool = True, dollar_math: bool = True, + code_to_code_cell: bool = False, ) -> ConvertedOutput: """Convert RST text to MyST Markdown text. @@ -236,6 +237,7 @@ def rst_to_myst( default_role=default_role, colon_fences=colon_fences, dollar_math=dollar_math, + code_to_code_cell = code_to_code_cell, ) output = token_renderer.to_tokens() myst_extension = get_myst_extensions(output.tokens) diff --git a/tests/test_cli.py b/tests/test_cli.py index ffe505f..888cd7e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -82,3 +82,25 @@ def test_convert(tmp_path: Path, file_regression): encoding="utf8", extension=".md", ) + +def test_convert_code_to_code_cell(tmp_path: Path, file_regression): + tmp_path.joinpath("test-code-cell.rst").write_text( + "head\n====\n\n.. code::\n\n\timport numpy as np", encoding="utf8" + ) + tmp_path.joinpath("config-code-cell.yaml").write_text("code_to_code_cell: true\n", encoding="utf8") + runner = CliRunner() + result = runner.invoke( + cli.convert, + [ + "--config", + str(tmp_path.joinpath("config-code-cell.yaml")), + str(tmp_path.joinpath("test-code-cell.rst")), + ], + ) + assert result.exit_code == 0, result.output + assert tmp_path.joinpath("test-code-cell.md").exists() + file_regression.check( + tmp_path.joinpath("test-code-cell.md").read_text(encoding="utf8"), + encoding="utf8", + extension=".md", + ) \ No newline at end of file diff --git a/tests/test_cli/test_convert_code_to_code_cell.md b/tests/test_cli/test_convert_code_to_code_cell.md new file mode 100644 index 0000000..533834f --- /dev/null +++ b/tests/test_cli/test_convert_code_to_code_cell.md @@ -0,0 +1,5 @@ +# head + +``` +import numpy as np +``` From 06c8a757ab6da2b1351cd9807b49374caeabfa6b Mon Sep 17 00:00:00 2001 From: mmcky Date: Fri, 2 Jul 2021 13:39:21 +1000 Subject: [PATCH 2/2] fix pre-commit --- rst_to_myst/mdformat_render.py | 2 +- tests/test_cli.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/rst_to_myst/mdformat_render.py b/rst_to_myst/mdformat_render.py index 3bd2e9a..cd8804e 100644 --- a/rst_to_myst/mdformat_render.py +++ b/rst_to_myst/mdformat_render.py @@ -237,7 +237,7 @@ def rst_to_myst( default_role=default_role, colon_fences=colon_fences, dollar_math=dollar_math, - code_to_code_cell = code_to_code_cell, + code_to_code_cell=code_to_code_cell, ) output = token_renderer.to_tokens() myst_extension = get_myst_extensions(output.tokens) diff --git a/tests/test_cli.py b/tests/test_cli.py index 888cd7e..3d32ae5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -83,11 +83,14 @@ def test_convert(tmp_path: Path, file_regression): extension=".md", ) + def test_convert_code_to_code_cell(tmp_path: Path, file_regression): tmp_path.joinpath("test-code-cell.rst").write_text( "head\n====\n\n.. code::\n\n\timport numpy as np", encoding="utf8" ) - tmp_path.joinpath("config-code-cell.yaml").write_text("code_to_code_cell: true\n", encoding="utf8") + tmp_path.joinpath("config-code-cell.yaml").write_text( + "code_to_code_cell: true\n", encoding="utf8" + ) runner = CliRunner() result = runner.invoke( cli.convert, @@ -103,4 +106,4 @@ def test_convert_code_to_code_cell(tmp_path: Path, file_regression): tmp_path.joinpath("test-code-cell.md").read_text(encoding="utf8"), encoding="utf8", extension=".md", - ) \ No newline at end of file + )