added comment stripper preprocessor

This commit is contained in:
Amy G. Bowersox 2024-08-02 23:55:22 -06:00
parent abbebab00f
commit 5066ff2437

View File

@ -28,9 +28,14 @@ GENERICIMAGE_PATTERN = r'!\[(.*?)\]\((.*?)\)'
OBSLINK_PATTERN = r'\[\[(.*?)\]\]' OBSLINK_PATTERN = r'\[\[(.*?)\]\]'
GENERICLINK_PATTERN = r'\[(.*?)\]\((.*?)\)' GENERICLINK_PATTERN = r'\[(.*?)\]\((.*?)\)'
# Inline footnote reference pattern.
INLINE_FOOTNOTE_REF_PREFIX = STX + "erbosoft_fnref:" INLINE_FOOTNOTE_REF_PREFIX = STX + "erbosoft_fnref:"
INLINE_FOOTNOTE_REF_PATTERN = INLINE_FOOTNOTE_REF_PREFIX + "{}" + ETX INLINE_FOOTNOTE_REF_PATTERN = INLINE_FOOTNOTE_REF_PREFIX + "{}" + ETX
# Obsidian comment marker
COMMENT_MARKER = '%%'
def is_proper_url(s: str) -> bool: def is_proper_url(s: str) -> bool:
""" """
Checks to see if a string is a "proper" URL. Checks to see if a string is a "proper" URL.
@ -322,6 +327,53 @@ class ObsidianInlines(Extension):
md.inlinePatterns.register(SimpleTagInlineProcessor(r'()\=\=(.*?)\=\=', 'ins'), 'highlight', PRIO_BASE + 1) md.inlinePatterns.register(SimpleTagInlineProcessor(r'()\=\=(.*?)\=\=', 'ins'), 'highlight', PRIO_BASE + 1)
class ObsidianComments(Extension):
"""An extension that removes Obsidian-style comments."""
class CommentStripper(Preprocessor):
"""The actual preprocessor that removes comments."""
def run(self, lines: list[str]) -> list[str]:
"""
Removes the comments from the array of lines.
Args:
lines (list[str]): The Markdown liens read in.
Returns:
list[str]: The Markdown lines after the comments have been removed.
"""
newlines: list[str] = []
in_comment = False
defer_in_comment = False
for line in lines:
arr = line.split(COMMENT_MARKER)
if in_comment:
arr.pop(0)
in_comment = False
if len(arr) % 2 == 0:
defer_in_comment = True
if len(arr) > 0:
arr.pop()
if not in_comment:
i = len(arr) - 2
while i >= 0:
arr.pop(i)
i -= 2
newlines.append(''.join(arr))
if defer_in_comment:
in_comment = True
defer_in_comment = False
return newlines
def extendMarkdown(self, md: markdown.Markdown) -> None: # noqa: N802
"""
Registers the comment remover with the Markdown parser.
Args:
md (markdown.Markdown): The Markdown parser to register the comment remover with.
"""
md.preprocessors.register(ObsidianComments.CommentStripper(md), 'comments-multi', PRIO_BASE + 1)
class ObsidianStyleFootnotes(FootnoteExtension): class ObsidianStyleFootnotes(FootnoteExtension):
""" """
An extension of the standard Markdown processor footnote extension to support Obsidian-style inline footnotes An extension of the standard Markdown processor footnote extension to support Obsidian-style inline footnotes
@ -452,6 +504,7 @@ def create_markdown_parser(context: Context) -> markdown.Markdown:
markdown.Markdown: The new Markdown parser. markdown.Markdown: The new Markdown parser.
""" """
return markdown.Markdown(extensions=[MetaStripper(), return markdown.Markdown(extensions=[MetaStripper(),
ObsidianComments(),
ObsidianStyleFootnotes(SUPERSCRIPT_TEXT='[{}]', SEPARATOR='-'), ObsidianStyleFootnotes(SUPERSCRIPT_TEXT='[{}]', SEPARATOR='-'),
ObsidianImages(context), ObsidianImages(context),
ObsidianLinks(context), ObsidianLinks(context),