Skip to content

Commit

Permalink
Ignore nested snippet sections (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser authored Apr 4, 2023
1 parent 07e4b90 commit 61b4571
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 22 additions & 4 deletions pymdownx/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ class SnippetPreprocessor(Preprocessor):

RE_SNIPPET_SECTION = re.compile(
r'''(?xi)
^.*?
^(?P<pre>.*?)
(?P<escape>;*)
(?P<inline_marker>-{1,}8<-{1,}[ \t]+)
\[[ \t]*(?P<type>start|end)[ \t]*:[ \t]*(?P<name>[a-z][-_0-9a-z]*)[ \t]*\]
.*?$
(?P<section>\[[ \t]*(?P<type>start|end)[ \t]*:[ \t]*(?P<name>[a-z][-_0-9a-z]*)[ \t]*\])
(?P<post>.*?)$
'''
)

Expand Down Expand Up @@ -103,14 +104,27 @@ 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':
start = True
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
Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_extensions/_snippets/section_nested.txt
Original file line number Diff line number Diff line change
@@ -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] */
62 changes: 62 additions & 0 deletions tests/test_extensions/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 class="highlight"><pre><span></span><code>div {
color: red;
background-color: white;
padding: 16px
}
</code></pre></div>
''',
True
)

def test_section_inline_escaped_other_section(self):
"""Test nested escaped sections."""

self.check_markdown(
R'''
```
-8<- "section_nested.txt:css-section3"
```
''',
'''
<div class="highlight"><pre><span></span><code>div {
color: red;
/* --8&lt;-- [start: css-section4] */
background-color: white;
padding: 16px
/* --8&lt;-- [end: css-section4] */
}
</code></pre></div>
''',
True
)

def test_section_ignore_double_start_section(self):
"""Test nested sections."""

self.check_markdown(
R'''
```
-8<- "section_nested.txt:css-section5"
```
''',
'''
<div class="highlight"><pre><span></span><code>div {
color: red;
background-color: white;
padding: 16px
}
</code></pre></div>
''',
True
)

def test_section_block(self):
"""Test section partial in block snippet."""

Expand Down

0 comments on commit 61b4571

Please sign in to comment.