Skip to content

Commit

Permalink
Strip sections from included file (#2541)
Browse files Browse the repository at this point in the history
Strip sections from included files when whole file or lines are included
  • Loading branch information
facelessuser authored Dec 6, 2024
1 parent 377e8ff commit 9df48db
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 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

## 10.12.1

- **FIX**: Snippets: Fix issue where when non sections of files are included, section labels are not stripped.

## 10.12

- **NEW**: Blocks: Blocks extensions no longer considered in beta.
Expand Down
2 changes: 1 addition & 1 deletion pymdownx/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ def parse_version(ver, pre=False):
return Version(major, minor, micro, release, pre, post, dev)


__version_info__ = Version(10, 12, 0, "final")
__version_info__ = Version(10, 12, 1, "final")
__version__ = __version_info__._get_canonical()
19 changes: 17 additions & 2 deletions pymdownx/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def download(self, url):
# Process lines
return [l.decode(self.encoding) for l in content.splitlines()]

def parse_snippets(self, lines, file_name=None, is_url=False):
def parse_snippets(self, lines, file_name=None, is_url=False, is_section=False):
"""Parse snippets snippet."""

if file_name:
Expand Down Expand Up @@ -257,6 +257,20 @@ def parse_snippets(self, lines, file_name=None, is_url=False):
continue

elif not block:
if not is_section:
# Check for section line, if present remove, if escaped, reformat it
m2 = self.RE_SNIPPET_SECTION.match(line)
if m2 and m2.group('escape'):
line = (
m2.group('pre') + m2.group('escape').replace(';', '', 1) + m2.group('inline_marker') +
m2.group('section') + m2.group('post')
)
m2 = None

# Found a section that must be removed
if m2 is not None:
continue

# Not in snippet, and we didn't find an inline,
# so just a normal line
new_lines.append(line)
Expand Down Expand Up @@ -350,7 +364,8 @@ def parse_snippets(self, lines, file_name=None, is_url=False):
space + l2 for l2 in self.parse_snippets(
s_lines,
snippet,
is_url=url
is_url=url,
is_section=section is not None
)
]
)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_extensions/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,41 @@ def test_section_inline_escaped_other_section(self):
True
)

def test_whole_file_with_sections(self):
"""Test whole file with sections."""

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

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

Expand Down

0 comments on commit 9df48db

Please sign in to comment.