Skip to content

Commit

Permalink
Relax Snippets syntax to allow only one dash -8<- (#1917)
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser authored Jan 20, 2023
1 parent 6614be1 commit 3de3645
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
5 changes: 5 additions & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 9.9.2

- **FIX**: Snippets syntax can break in XML comments as XML comments do not allow `--`. Relax Snippets syntax such that
`-8<-` (single `-`) are allowed.

## 9.9.1

- **FIX**: Use a different CDN for Twemoji icons as MaxCDN is no longer available.
Expand Down
6 changes: 3 additions & 3 deletions docs/src/markdown/extensions/snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ As you can see, the notation is ASCII scissors cutting a line followed by the fi
variant, the file name follows directly after the scissors and is quoted. In the case of the block format, the file
names follow on separate lines and an additional scissor is added afterwards to signal the end of the block.

The dashes can be as few as 2 (`--8<--`) or longer if desired (`---8<---------`); whatever your preference is. The
The dashes can be as few as 1 (`-8<-`) or longer if desired (`---8<---------`); whatever your preference is. The
important thing is that the notation must reside on a line(s) by itself, and the path, must be quoted in the case of the
single line notation. If the file name is indented, the content will be indented to that level as well.

Expand Down Expand Up @@ -163,7 +163,7 @@ And then just include it in our document:
!!! new "New 9.6"

If it is necessary to demonstrate the snippet syntax, an escaping method is required. If you need to escape snippets,
just place a `;` right before `--8<--`. This will work for both single line and block format. An escaped snippet
just place a `;` right before `-8<-`. This will work for both single line and block format. An escaped snippet
notation will be passed through the Markdown parser with the first `;` removed.

=== "Markdown"
Expand All @@ -183,7 +183,7 @@ notation will be passed through the Markdown parser with the first `;` removed.
```

!!! warning "Legacy Escaping"
The legacy escape method required placing a space at the end of the line with `--8<--`, while this should still
The legacy escape method required placing a space at the end of the line with `-8<-`, while this should still
work, this behavior will be removed at sometime in the future and is discouraged.

## Specifying Snippet Locations
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(9, 9, 1, "final")
__version_info__ = Version(9, 9, 2, "final")
__version__ = __version_info__._get_canonical()
8 changes: 4 additions & 4 deletions pymdownx/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class SnippetPreprocessor(Preprocessor):
^(?P<space>[ \t]*)
(?P<escape>;*)
(?P<all>
(?P<inline_marker>-{2,}8<-{2,}[ \t]+)
(?P<inline_marker>-{1,}8<-{1,}[ \t]+)
(?P<snippet>(?:"(?:\\"|[^"\n\r])+?"|'(?:\\'|[^'\n\r])+?'))(?![ \t]) |
(?P<block_marker>-{2,}8<-{2,})(?![ \t])
(?P<block_marker>-{1,}8<-{1,})(?![ \t])
)\r?$
'''
)
Expand All @@ -66,7 +66,7 @@ class SnippetPreprocessor(Preprocessor):
RE_SNIPPET_SECTION = re.compile(
r'''(?xi)
^.*?
(?P<inline_marker>-{2,}8<-{2,}[ \t]+)
(?P<inline_marker>-{1,}8<-{1,}[ \t]+)
\[[ \t]*(?P<type>start|end)[ \t]*:[ \t]*(?P<name>[a-z][0-9a-z]*)[ \t]*\]
.*?$
'''
Expand Down Expand Up @@ -324,7 +324,7 @@ def run(self, lines):

self.seen = set()
if self.auto_append:
lines.extend("\n\n--8<--\n{}\n--8<--\n".format('\n\n'.join(self.auto_append)).split('\n'))
lines.extend("\n\n-8<-\n{}\n-8<-\n".format('\n\n'.join(self.auto_append)).split('\n'))

return self.parse_snippets(lines)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_extensions/_snippets/section.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ div {
}
/* --8<-- [end: cssSection] */

<!-- --8<-- [start: htmlSection] -->
<!-- -8<- [start: htmlSection] -->
<div><p>content</p></div>
<!-- --8<-- [end: htmlSection] -->
<!-- -8<- [end: htmlSection] -->

/* --8<-- [end: cssSection2] */
/* --8<-- [start: cssSection2] */
Expand Down
35 changes: 34 additions & 1 deletion tests/test_extensions/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,26 @@ def test_section_inline(self):
True
)

def test_section_inline_min(self):
"""Test section partial in inline snippet using minimum tokens."""

self.check_markdown(
R'''
```
-8<- "section.txt:cssSection"
```
''',
'''
<div class="highlight"><pre><span></span><code>div {
color: red;
}
</code></pre></div>
''',
True
)

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

self.check_markdown(
R'''
Expand All @@ -256,6 +274,21 @@ def test_section_block(self):
True
)

def test_section_block_min(self):
"""Test section partial in block snippet using minimum tokens."""

self.check_markdown(
R'''
-8<-
section.txt:htmlSection
-8<-
''',
'''
<div><p>content</p></div>
''',
True
)

def test_section_end_first(self):
"""Test section when the end is specified first."""

Expand Down

0 comments on commit 3de3645

Please sign in to comment.