From 3de364507935cffcda4cf451bf2ed31e1a23aeff Mon Sep 17 00:00:00 2001 From: Isaac Muse Date: Fri, 20 Jan 2023 08:29:26 -0700 Subject: [PATCH] Relax Snippets syntax to allow only one dash `-8<-` (#1917) --- docs/src/markdown/about/changelog.md | 5 +++ docs/src/markdown/extensions/snippets.md | 6 ++-- pymdownx/__meta__.py | 2 +- pymdownx/snippets.py | 8 ++--- tests/test_extensions/_snippets/section.txt | 4 +-- tests/test_extensions/test_snippets.py | 35 ++++++++++++++++++++- 6 files changed, 49 insertions(+), 11 deletions(-) diff --git a/docs/src/markdown/about/changelog.md b/docs/src/markdown/about/changelog.md index 0d3efae84..533f32147 100644 --- a/docs/src/markdown/about/changelog.md +++ b/docs/src/markdown/about/changelog.md @@ -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. diff --git a/docs/src/markdown/extensions/snippets.md b/docs/src/markdown/extensions/snippets.md index 1682b7f22..c50a43055 100644 --- a/docs/src/markdown/extensions/snippets.md +++ b/docs/src/markdown/extensions/snippets.md @@ -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. @@ -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" @@ -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 diff --git a/pymdownx/__meta__.py b/pymdownx/__meta__.py index 0656d3a27..31071ac56 100644 --- a/pymdownx/__meta__.py +++ b/pymdownx/__meta__.py @@ -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() diff --git a/pymdownx/snippets.py b/pymdownx/snippets.py index 24dd6f0da..cf5f903bb 100644 --- a/pymdownx/snippets.py +++ b/pymdownx/snippets.py @@ -49,9 +49,9 @@ class SnippetPreprocessor(Preprocessor): ^(?P[ \t]*) (?P;*) (?P - (?P-{2,}8<-{2,}[ \t]+) + (?P-{1,}8<-{1,}[ \t]+) (?P(?:"(?:\\"|[^"\n\r])+?"|'(?:\\'|[^'\n\r])+?'))(?![ \t]) | - (?P-{2,}8<-{2,})(?![ \t]) + (?P-{1,}8<-{1,})(?![ \t]) )\r?$ ''' ) @@ -66,7 +66,7 @@ class SnippetPreprocessor(Preprocessor): RE_SNIPPET_SECTION = re.compile( r'''(?xi) ^.*? - (?P-{2,}8<-{2,}[ \t]+) + (?P-{1,}8<-{1,}[ \t]+) \[[ \t]*(?Pstart|end)[ \t]*:[ \t]*(?P[a-z][0-9a-z]*)[ \t]*\] .*?$ ''' @@ -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) diff --git a/tests/test_extensions/_snippets/section.txt b/tests/test_extensions/_snippets/section.txt index 8a05e940f..ea43f9f4c 100644 --- a/tests/test_extensions/_snippets/section.txt +++ b/tests/test_extensions/_snippets/section.txt @@ -4,9 +4,9 @@ div { } /* --8<-- [end: cssSection] */ - +

content

- + /* --8<-- [end: cssSection2] */ /* --8<-- [start: cssSection2] */ diff --git a/tests/test_extensions/test_snippets.py b/tests/test_extensions/test_snippets.py index 5261b2d36..4bb03d90d 100644 --- a/tests/test_extensions/test_snippets.py +++ b/tests/test_extensions/test_snippets.py @@ -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 {
+                color: red;
+            }
+            
+ ''', + True + ) + def test_section_block(self): - """Test section partial in inline snippet.""" + """Test section partial in block snippet.""" self.check_markdown( R''' @@ -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<- + ''', + ''' +

content

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