Repository: executablebooks/MyST-Parser
Pull request #488 (2022-01-02 14:32:11)

👌 IMPROVE: `DocutilsRenderer.create_highlighted_code_block`
Minor improvement, to handle null lexer_name,
and allow for the use of an alternative node class.

codecov[bot]: # [Codecov](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks) Report
> Merging [#488](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks) (d380224) into [master](https://codecov.io/gh/executablebooks/MyST-Parser/commit/885651fc4e25dbac414c20cfe8fe3232ba4e833b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks) (885651f) will **decrease** coverage by `0.29%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488/graphs/tree.svg?width=650&height=150&src=pr&token=ggpNnuB7ll&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks)](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks)

```diff
@@            Coverage Diff             @@
##           master     #488      +/-   ##
==========================================
- Coverage   90.03%   89.74%   -0.30%     
==========================================
  Files          16       16              
  Lines        2058     2057       -1     
==========================================
- Hits         1853     1846       -7     
- Misses        205      211       +6     
```

| Flag | Coverage Δ | |
|---|---|---|
| pytests | `89.74% <66.66%> (-0.30%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks) | Coverage Δ | |
|---|---|---|
| [myst\_parser/docutils\_renderer.py](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks#diff-bXlzdF9wYXJzZXIvZG9jdXRpbHNfcmVuZGVyZXIucHk=) | `92.15% <66.66%> (-0.02%)` | :arrow_down: |
| [myst\_parser/mathjax.py](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks#diff-bXlzdF9wYXJzZXIvbWF0aGpheC5weQ==) | `82.60% <0.00%> (-13.05%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks). Last update [885651f...d380224](https://codecov.io/gh/executablebooks/MyST-Parser/pull/488?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=executablebooks).


Patch:
diff --git a/myst_parser/docutils_renderer.py b/myst_parser/docutils_renderer.py
index d247844..642fc3a 100644
--- a/myst_parser/docutils_renderer.py
+++ b/myst_parser/docutils_renderer.py
@@ -16,6 +16,7 @@ from typing import (
     Optional,
     Sequence,
     Tuple,
+    Type,
     Union,
     cast,
 )
@@ -432,12 +433,13 @@ class DocutilsRenderer(RendererProtocol):
     def create_highlighted_code_block(
         self,
         text: str,
-        lexer_name: str,
+        lexer_name: Optional[str],
         number_lines: bool = False,
         lineno_start: int = 1,
         source: Optional[str] = None,
         line: Optional[int] = None,
-    ) -> nodes.literal_block:
+        node_cls: Type[nodes.Element] = nodes.literal_block,
+    ) -> nodes.Element:
         """Create a literal block with syntax highlighting.
 
         This mimics the behaviour of the `code-block` directive.
@@ -450,19 +452,19 @@ class DocutilsRenderer(RendererProtocol):
         Note, this function does not add the literal block to the document.
         """
         if self.sphinx_env is not None:
-            node = nodes.literal_block(text, text, language=lexer_name)
+            node = node_cls(text, text, language=lexer_name or "none")
             if number_lines:
                 node["linenos"] = True
                 if lineno_start != 1:
                     node["highlight_args"] = {"linenostart": lineno_start}
         else:
-            node = nodes.literal_block(
+            node = node_cls(
                 text, classes=["code"] + ([lexer_name] if lexer_name else [])
             )
             try:
                 lex_tokens = Lexer(
                     text,
-                    lexer_name,
+                    lexer_name or "",
                     "short"
                     if self.config.get("myst_highlight_code_blocks", True)
                     else "none",
@@ -476,7 +478,7 @@ class DocutilsRenderer(RendererProtocol):
                         if value is not None
                     },
                 )
-                lex_tokens = Lexer(text, lexer_name, "none")
+                lex_tokens = Lexer(text, lexer_name or "", "none")
 
             if number_lines:
                 lex_tokens = NumberLines(
@@ -497,10 +499,7 @@ class DocutilsRenderer(RendererProtocol):
         return node
 
     def render_code_block(self, token: SyntaxTreeNode) -> None:
-        # this should never have a language, since it is just indented text, however,
-        # creating a literal_block with no language will raise a warning in sphinx
         lexer = token.info.split()[0] if token.info else None
-        lexer = lexer or "none"
         node = self.create_highlighted_code_block(
             token.content,
             lexer,


Test patch:
diff --git a/tests/test_renderers/fixtures/docutil_syntax_elements.md b/tests/test_renderers/fixtures/docutil_syntax_elements.md
index 8fd4648..91add67 100644
--- a/tests/test_renderers/fixtures/docutil_syntax_elements.md
+++ b/tests/test_renderers/fixtures/docutil_syntax_elements.md
@@ -127,7 +127,7 @@ Block Code:
     foo
 .
 <document source="notset">
-    <literal_block classes="code none" xml:space="preserve">
+    <literal_block classes="code" xml:space="preserve">
         foo
 .