-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from Alihtt/feature-markdown-add-id-extension
Feature markdown add id extension
- Loading branch information
Showing
6 changed files
with
71 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
__pycache__ | ||
build/ | ||
dist/ | ||
venv/ | ||
*.egg-info/ | ||
*.pypirc | ||
*.pyc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import markdown | ||
from xml.etree import ElementTree | ||
|
||
# Regex pattern to detect `{#id_name}` at the end of the line | ||
ADD_ID_RE = r"(.+?)\s\{#([a-zA-Z0-9_-]+)\}$" | ||
|
||
|
||
class AddIDPattern(markdown.inlinepatterns.Pattern): | ||
"""Pattern to match Markdown text ending with `{#id}` and set it as an ID.""" | ||
|
||
def handleMatch(self, m): | ||
text_content = m.group(2).strip() # Actual text content | ||
id_value = m.group(3) # The ID inside `{#id}` | ||
|
||
# Create a <span> element to hold the text and ID | ||
el = ElementTree.Element("span") | ||
el.text = markdown.util.AtomicString(text_content) | ||
el.set("id", id_value) | ||
return el | ||
|
||
|
||
class AddIDExtension(markdown.Extension): | ||
"""Add ID Extension for Python-Markdown.""" | ||
|
||
def extendMarkdown(self, md: markdown.core.Markdown, *args): | ||
"""Register AddIDPattern with the Markdown parser.""" | ||
md.inlinePatterns.register(AddIDPattern(ADD_ID_RE, md), "add_id", 9) | ||
|
||
|
||
def makeExtension(*args, **kwargs): | ||
return AddIDExtension(*args, **kwargs) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters