From 61b45712d8f01c79fc28207292d492b15884d8b5 Mon Sep 17 00:00:00 2001 From: Isaac Muse Date: Tue, 4 Apr 2023 17:02:10 -0600 Subject: [PATCH] Ignore nested snippet sections (#2003) --- docs/src/markdown/about/changelog.md | 4 ++ pymdownx/snippets.py | 26 ++++++-- .../_snippets/section_nested.txt | 29 +++++++++ tests/test_extensions/test_snippets.py | 62 +++++++++++++++++++ 4 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 tests/test_extensions/_snippets/section_nested.txt diff --git a/docs/src/markdown/about/changelog.md b/docs/src/markdown/about/changelog.md index 9ec0da4ef..350c39402 100644 --- a/docs/src/markdown/about/changelog.md +++ b/docs/src/markdown/about/changelog.md @@ -1,5 +1,9 @@ # Changelog +## 9.11 + +- **NEW**: Snippets: Ignore nested snippet section syntax when including a section. + ## 9.10 - **NEW**: Blocks: Add new experimental general purpose blocks that provide a framework for creating fenced block diff --git a/pymdownx/snippets.py b/pymdownx/snippets.py index 44318160f..e7ae2e986 100644 --- a/pymdownx/snippets.py +++ b/pymdownx/snippets.py @@ -66,10 +66,11 @@ class SnippetPreprocessor(Preprocessor): RE_SNIPPET_SECTION = re.compile( r'''(?xi) - ^.*? + ^(?P
.*?)
+        (?P;*)
         (?P-{1,}8<-{1,}[ \t]+)
-        \[[ \t]*(?Pstart|end)[ \t]*:[ \t]*(?P[a-z][-_0-9a-z]*)[ \t]*\]
-        .*?$
+        (?P
\[[ \t]*(?Pstart|end)[ \t]*:[ \t]*(?P[a-z][-_0-9a-z]*)[ \t]*\]) + (?P.*?)$ ''' ) @@ -103,7 +104,16 @@ def extract_section(self, section, lines): # Found a snippet section marker with our specified name m = self.RE_SNIPPET_SECTION.match(l) - if m is not None and m.group('name') == section: + + # Handle escaped line + if m and start and m.group('escape'): + l = ( + m.group('pre') + m.group('escape').replace(';', '', 1) + m.group('inline_marker') + + m.group('section') + m.group('post') + ) + + # Found a section we are looking for. + elif m is not None and m.group('name') == section: # We found the start if not start and m.group('type') == 'start': @@ -111,6 +121,10 @@ def extract_section(self, section, lines): found = True continue + # Ignore duplicate start + elif start and m.group('type') == 'start': + continue + # We found the end elif start and m.group('type') == 'end': start = False @@ -120,6 +134,10 @@ def extract_section(self, section, lines): else: break + # Found a section we don't care about, so ignore it. + elif m and start: + continue + # We are currently in a section, so append the line if start: new_lines.append(l) diff --git a/tests/test_extensions/_snippets/section_nested.txt b/tests/test_extensions/_snippets/section_nested.txt new file mode 100644 index 000000000..cdc7cc54c --- /dev/null +++ b/tests/test_extensions/_snippets/section_nested.txt @@ -0,0 +1,29 @@ +/* --8<-- [start: css-section] */ +div { + color: red; + /* --8<-- [start: css-section2] */ + background-color: white; + padding: 16px + /* --8<-- [end: css-section2] */ +} +/* --8<-- [end: css-section] */ + + +/* --8<-- [start: css-section3] */ +div { + color: red; + /* ;--8<-- [start: css-section4] */ + background-color: white; + padding: 16px + /* ;--8<-- [end: css-section4] */ +} +/* --8<-- [end: css-section3] */ + +/* --8<-- [start: css-section5] */ +div { + color: red; + /* --8<-- [start: css-section5] */ + background-color: white; + padding: 16px +} +/* --8<-- [end: css-section5] */ diff --git a/tests/test_extensions/test_snippets.py b/tests/test_extensions/test_snippets.py index 310060206..e91a8c4de 100644 --- a/tests/test_extensions/test_snippets.py +++ b/tests/test_extensions/test_snippets.py @@ -332,6 +332,68 @@ def test_section_inline_min(self): True ) + def test_section_inline_ignore_other_section(self): + """Test nested sections.""" + + self.check_markdown( + R''' + ``` + -8<- "section_nested.txt:css-section" + ``` + ''', + ''' +
div {
+                color: red;
+                background-color: white;
+                padding: 16px
+            }
+            
+ ''', + True + ) + + def test_section_inline_escaped_other_section(self): + """Test nested escaped sections.""" + + self.check_markdown( + R''' + ``` + -8<- "section_nested.txt:css-section3" + ``` + ''', + ''' +
div {
+                color: red;
+                /* --8<-- [start: css-section4] */
+                background-color: white;
+                padding: 16px
+                /* --8<-- [end: css-section4] */
+            }
+            
+ ''', + True + ) + + def test_section_ignore_double_start_section(self): + """Test nested sections.""" + + self.check_markdown( + R''' + ``` + -8<- "section_nested.txt:css-section5" + ``` + ''', + ''' +
div {
+                color: red;
+                background-color: white;
+                padding: 16px
+            }
+            
+ ''', + True + ) + def test_section_block(self): """Test section partial in block snippet."""