diff --git a/src/markdown_include_variants/_main.py b/src/markdown_include_variants/_main.py index 8ac2594..b0c831a 100644 --- a/src/markdown_include_variants/_main.py +++ b/src/markdown_include_variants/_main.py @@ -279,15 +279,18 @@ def run(self, lines: list[str]) -> list[str]: configs_str = line_match.group(2).strip() include_lines: list[tuple[int, int]] = [] highlight_lines: list[tuple[int, int]] = [] + title: Union[str, None] = None for config_match in re.finditer(re_config, configs_str): if config_match: name = config_match.group(1) - ranges_str = config_match.group(2) - ranges = parse_lines_index(ranges_str) + value_str = config_match.group(2) if name == "ln": - include_lines.extend(ranges) + include_lines.extend(parse_lines_index(value_str)) elif name == "hl": - highlight_lines.extend(ranges) + highlight_lines.extend(parse_lines_index(value_str)) + elif name == "title": + assert value_str.startswith('"') and value_str.endswith('"') + title = value_str[1:-1] use_hl_lines: list[tuple[int, int]] = [] if highlight_lines: if include_lines: @@ -356,6 +359,8 @@ def run(self, lines: list[str]) -> list[str]: first_line = "```python" if use_hl_lines: first_line += f' hl_lines="{use_hl_lines_str}"' + if title: + first_line += f' title="{title}"' block_lines.extend( [ @@ -373,6 +378,8 @@ def run(self, lines: list[str]) -> list[str]: if highlight_lines: hl_lines_full_str = generate_hl_string(highlight_lines) first_line_full += f' hl_lines="{hl_lines_full_str}"' + if title: + first_line_full += f' title="{title}"' block_lines.extend( [ "///// details | 👀 Full file preview", @@ -390,6 +397,9 @@ def run(self, lines: list[str]) -> list[str]: ] ) if len(sorted_variants): + first_line_variant = "```python" + if title: + first_line_variant += f' title="{title}"' block_lines.extend( [ "///// details | 🤓 Other versions and variants", @@ -413,7 +423,7 @@ def run(self, lines: list[str]) -> list[str]: block_lines.extend( [ - "```python", + first_line_variant, f"{{!{variant.path}!}}", "```", "////", diff --git a/tests/test_all_variants.py b/tests/test_all_variants.py index 42cf736..ec8f735 100644 --- a/tests/test_all_variants.py +++ b/tests/test_all_variants.py @@ -142,6 +142,77 @@ def test_all_variants_ln_hl(): ) +def test_all_variants_ln_hl_title(): + input_md = inspect.cleandoc( + """ + {* docs_src/all_variants/tutorial001_an_py311.py ln[3:5] hl[4:5] title["main.py"] *} + """ + ) + + result = markdown.markdown(input_md, extensions=[IncludeVariantsExtension()]) + assert result == snapshot( + inspect.cleandoc( + """ +
//// tab | Python 3.11+ + ```python hl_lines="4-5" title="main.py"
+{!docs_src/all_variants/tutorial001_an_py311.py[ln:3-5]!}
+```
+////
+///// details | 👀 Full file preview
+//// tab | Python 3.11+
+python hl_lines="4-5" title="main.py"
+ {!docs_src/all_variants/tutorial001_an_py311.py!}
////
+/////
+///// details | 🤓 Other versions and variants
+//// tab | Python 3.10+
+python title="main.py"
+ {!docs_src/all_variants/tutorial001_an_py310.py!}
+ ////
+ //// tab | Python 3.9+
python title="main.py"
+ {!docs_src/all_variants/tutorial001_an_py39.py!}
+ ////
+ //// tab | Python 3.8+
python title="main.py"
+ {!docs_src/all_variants/tutorial001_an.py!}
+ ////
+ //// tab | Python 3.11+ - non-Annotated
/// tip
+Prefer to use the Annotated version if possible.
///
+python title="main.py"
+ {!docs_src/all_variants/tutorial001_py311.py!}
+ ////
+ //// tab | Python 3.10+ - non-Annotated
/// tip
+Prefer to use the Annotated version if possible.
///
+python title="main.py"
+ {!docs_src/all_variants/tutorial001_py310.py!}
+ ////
+ //// tab | Python 3.9+ - non-Annotated
/// tip
+Prefer to use the Annotated version if possible.
///
+python title="main.py"
+ {!docs_src/all_variants/tutorial001_py39.py!}
+ ////
+ //// tab | Python 3.8+ - non-Annotated
/// tip
+Prefer to use the Annotated version if possible.
///
+python title="main.py"
+ {!docs_src/all_variants/tutorial001.py!}
+ ////
/////
+ """ + ) + ) + + def test_all_variants_ln_hl_include(): input_md = inspect.cleandoc( """ @@ -294,3 +365,157 @@ def test_all_variants_ln_hl_include(): """ ) ) + + +def test_all_variants_ln_hl_title_include(): + input_md = inspect.cleandoc( + """ + {* docs_src/all_variants/tutorial001_an_py311.py ln[3:5] hl[4:5] title["main.py"] *} + """ + ) + + result = markdown.markdown( + input_md, + extensions=[ + IncludeVariantsExtension(), + IncludeExtension(), + SuperFencesCodeExtension(), + HighlightExtension(), + ], + ) + assert result == snapshot( + inspect.cleandoc( + """ +//// tab | Python 3.11+ +
# Code above omitted 👆
+
+ print("an py311 line 3")
+ print("an py311 line 4")
+ print("an py311 line 5")
+
+ # Code below omitted 👇
+ ////
+///// details | 👀 Full file preview
+//// tab | Python 3.11+
+print("an py311 line 1")
+ print("an py311 line 2")
+ print("an py311 line 3")
+ print("an py311 line 4")
+ print("an py311 line 5")
+ print("an py311 line 6")
+ print("an py311 line 7")
+ print("an py311 line 8")
+ print("an py311 line 9")
+ print("an py311 line 10")
+ ////
+/////
+///// details | 🤓 Other versions and variants
+//// tab | Python 3.10+
+print("an py310 line 1")
+ print("an py310 line 2")
+ print("an py310 line 3")
+ print("an py310 line 4")
+ print("an py310 line 5")
+ print("an py310 line 6")
+ print("an py310 line 7")
+ print("an py310 line 8")
+ print("an py310 line 9")
+ print("an py310 line 10")
+ print("an py39 line 1")
+ print("an py39 line 2")
+ print("an py39 line 3")
+ print("an py39 line 4")
+ print("an py39 line 5")
+ print("an py39 line 6")
+ print("an py39 line 7")
+ print("an py39 line 8")
+ print("an py39 line 9")
+ print("an py39 line 10")
+ print("an line 1")
+ print("an line 2")
+ print("an line 3")
+ print("an line 4")
+ print("an line 5")
+ print("an line 6")
+ print("an line 7")
+ print("an line 8")
+ print("an line 9")
+ print("an line 10")
+ /// tip
+Prefer to use the Annotated version if possible.
///
+print("py311 line 1")
+ print("py311 line 2")
+ print("py311 line 3")
+ print("py311 line 4")
+ print("py311 line 5")
+ print("py311 line 6")
+ print("py311 line 7")
+ print("py311 line 8")
+ print("py311 line 9")
+ print("py311 line 10")
+ /// tip
+Prefer to use the Annotated version if possible.
///
+print("py310 line 1")
+ print("py310 line 2")
+ print("py310 line 3")
+ print("py310 line 4")
+ print("py310 line 5")
+ print("py310 line 6")
+ print("py310 line 7")
+ print("py310 line 8")
+ print("py310 line 9")
+ print("py310 line 10")
+ /// tip
+Prefer to use the Annotated version if possible.
///
+print("py39 line 1")
+ print("py39 line 2")
+ print("py39 line 3")
+ print("py39 line 4")
+ print("py39 line 5")
+ print("py39 line 6")
+ print("py39 line 7")
+ print("py39 line 8")
+ print("py39 line 9")
+ print("py39 line 10")
+ /// tip
+Prefer to use the Annotated version if possible.
///
+print("simple line 1")
+ print("simple line 2")
+ print("simple line 3")
+ print("simple line 4")
+ print("simple line 5")
+ print("simple line 6")
+ print("simple line 7")
+ print("simple line 8")
+ print("simple line 9")
+ print("simple line 10")
+ /////
+ """ + ) + ) diff --git a/tests/test_simple_title.py b/tests/test_simple_title.py new file mode 100644 index 0000000..1a2d342 --- /dev/null +++ b/tests/test_simple_title.py @@ -0,0 +1,59 @@ +import inspect + +import markdown +from inline_snapshot import snapshot +from mdx_include.mdx_include import IncludeExtension + +from markdown_include_variants import IncludeVariantsExtension + + +def test_simple_title(): + input_md = inspect.cleandoc( + """ + {*docs_src/simple/tutorial001.py title["Example 1"] *} + """ + ) + + result = markdown.markdown(input_md, extensions=[IncludeVariantsExtension()]) + assert result == snapshot( + inspect.cleandoc( + """ +//// tab | Python 3.8+
+ python title="Example 1"
+ {!docs_src/simple/tutorial001.py!}
////
+ """ + ) + ) + + +def test_simple_include(): + input_md = inspect.cleandoc( + """ + {* docs_src/simple/tutorial001.py title["Example 1"] *} + """ + ) + + result = markdown.markdown( + input_md, extensions=[IncludeVariantsExtension(), IncludeExtension()] + ) + assert result == snapshot( + inspect.cleandoc( + """ +//// tab | Python 3.8+ + ```python title="Example 1" + print("simple line 1") + print("simple line 2") + print("simple line 3") + print("simple line 4") + print("simple line 5") + print("simple line 6") + print("simple line 7") + print("simple line 8") + print("simple line 9") + print("simple line 10")
+```
+////
+ """ + ) + )