Skip to content

Commit 61b4571

Browse files
authored
Ignore nested snippet sections (#2003)
1 parent 07e4b90 commit 61b4571

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed

Diff for: docs/src/markdown/about/changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 9.11
4+
5+
- **NEW**: Snippets: Ignore nested snippet section syntax when including a section.
6+
37
## 9.10
48

59
- **NEW**: Blocks: Add new experimental general purpose blocks that provide a framework for creating fenced block

Diff for: pymdownx/snippets.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ class SnippetPreprocessor(Preprocessor):
6666

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

@@ -103,14 +104,27 @@ def extract_section(self, section, lines):
103104

104105
# Found a snippet section marker with our specified name
105106
m = self.RE_SNIPPET_SECTION.match(l)
106-
if m is not None and m.group('name') == section:
107+
108+
# Handle escaped line
109+
if m and start and m.group('escape'):
110+
l = (
111+
m.group('pre') + m.group('escape').replace(';', '', 1) + m.group('inline_marker') +
112+
m.group('section') + m.group('post')
113+
)
114+
115+
# Found a section we are looking for.
116+
elif m is not None and m.group('name') == section:
107117

108118
# We found the start
109119
if not start and m.group('type') == 'start':
110120
start = True
111121
found = True
112122
continue
113123

124+
# Ignore duplicate start
125+
elif start and m.group('type') == 'start':
126+
continue
127+
114128
# We found the end
115129
elif start and m.group('type') == 'end':
116130
start = False
@@ -120,6 +134,10 @@ def extract_section(self, section, lines):
120134
else:
121135
break
122136

137+
# Found a section we don't care about, so ignore it.
138+
elif m and start:
139+
continue
140+
123141
# We are currently in a section, so append the line
124142
if start:
125143
new_lines.append(l)

Diff for: tests/test_extensions/_snippets/section_nested.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* --8<-- [start: css-section] */
2+
div {
3+
color: red;
4+
/* --8<-- [start: css-section2] */
5+
background-color: white;
6+
padding: 16px
7+
/* --8<-- [end: css-section2] */
8+
}
9+
/* --8<-- [end: css-section] */
10+
11+
12+
/* --8<-- [start: css-section3] */
13+
div {
14+
color: red;
15+
/* ;--8<-- [start: css-section4] */
16+
background-color: white;
17+
padding: 16px
18+
/* ;--8<-- [end: css-section4] */
19+
}
20+
/* --8<-- [end: css-section3] */
21+
22+
/* --8<-- [start: css-section5] */
23+
div {
24+
color: red;
25+
/* --8<-- [start: css-section5] */
26+
background-color: white;
27+
padding: 16px
28+
}
29+
/* --8<-- [end: css-section5] */

Diff for: tests/test_extensions/test_snippets.py

+62
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,68 @@ def test_section_inline_min(self):
332332
True
333333
)
334334

335+
def test_section_inline_ignore_other_section(self):
336+
"""Test nested sections."""
337+
338+
self.check_markdown(
339+
R'''
340+
```
341+
-8<- "section_nested.txt:css-section"
342+
```
343+
''',
344+
'''
345+
<div class="highlight"><pre><span></span><code>div {
346+
color: red;
347+
background-color: white;
348+
padding: 16px
349+
}
350+
</code></pre></div>
351+
''',
352+
True
353+
)
354+
355+
def test_section_inline_escaped_other_section(self):
356+
"""Test nested escaped sections."""
357+
358+
self.check_markdown(
359+
R'''
360+
```
361+
-8<- "section_nested.txt:css-section3"
362+
```
363+
''',
364+
'''
365+
<div class="highlight"><pre><span></span><code>div {
366+
color: red;
367+
/* --8&lt;-- [start: css-section4] */
368+
background-color: white;
369+
padding: 16px
370+
/* --8&lt;-- [end: css-section4] */
371+
}
372+
</code></pre></div>
373+
''',
374+
True
375+
)
376+
377+
def test_section_ignore_double_start_section(self):
378+
"""Test nested sections."""
379+
380+
self.check_markdown(
381+
R'''
382+
```
383+
-8<- "section_nested.txt:css-section5"
384+
```
385+
''',
386+
'''
387+
<div class="highlight"><pre><span></span><code>div {
388+
color: red;
389+
background-color: white;
390+
padding: 16px
391+
}
392+
</code></pre></div>
393+
''',
394+
True
395+
)
396+
335397
def test_section_block(self):
336398
"""Test section partial in block snippet."""
337399

0 commit comments

Comments
 (0)