Skip to content

ENH: Add option to convert code to code-cell directives in md output #24

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions rst_to_myst/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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."""
Expand All @@ -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]))

Expand Down
12 changes: 10 additions & 2 deletions rst_to_myst/markdownit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisjsewell I can't find the documentation on token_types -- so I don't see how to specify a myst directive when adding this token.

directive_name = "code-cell"
argument, content = node.children
self.add_token(
"fence",
"code",
token_type,
directive_name,
0,
content=content.astext() + "\n",
markup="```",
Expand Down
2 changes: 2 additions & 0 deletions rst_to_myst/mdformat_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,28 @@ 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",
)
5 changes: 5 additions & 0 deletions tests/test_cli/test_convert_code_to_code_cell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# head

```
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be:

```{code-cell}
<code>
```

import numpy as np
```