diff --git a/src/dragonglass/mparse.py b/src/dragonglass/mparse.py index 184588d..698c3cb 100644 --- a/src/dragonglass/mparse.py +++ b/src/dragonglass/mparse.py @@ -1,7 +1,7 @@ #/usr/bin/env python3 import re -import xml.etree.ElementTree as etree +import xml.etree.ElementTree as etree # noqa: N813 from typing import Any from urllib.parse import unquote as urlunquote from urllib.parse import urlparse @@ -15,6 +15,14 @@ from .dragonglass import Context PRIO_BASE = 10000 # priority base for our extensions +# Patterns for ObsidianImages +OBSIMAGE_PATTERN = r'!\[\[(.*?)\]\]' +GENERICIMAGE_PATTERN = r'!\[(.*?)\]\((.*?)\)' + +# Patterns for ObsidianLinks +OBSLINK_PATTERN = r'\[\[(.*?)\]\]' +GENERICLINK_PATTERN = r'\[(.*?)\]\((.*?)\)' + def is_proper_url(s: str) -> bool: """ @@ -61,7 +69,7 @@ class MetaStripper(Extension): lines.pop(0) return lines - def extendMarkdown(self, md: markdown.Markdown) -> None: + def extendMarkdown(self, md: markdown.Markdown) -> None: # noqa: N802 md.preprocessors.register(MetaStripper.MetaStripperProc(md), 'metastripper', PRIO_BASE) @@ -98,7 +106,7 @@ class ObsidianImages(Extension): super(ObsidianImages.ObsidianImageProc, self).__init__(pattern, md) self._extref = extref - def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: + def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: # noqa: N802 name, width, height = self._extref._parse_dimensions(m.group(1)) link = self._extref._lookup_image_reference(name) if link is None: @@ -120,7 +128,7 @@ class ObsidianImages(Extension): super(ObsidianImages.GenericImageProc, self).__init__(pattern, md) self._extref = extref - def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: + def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: # noqa: N802 name, width, height = self._extref._parse_dimensions(m.group(1)) link = m.group(2) if is_proper_url(link): @@ -149,9 +157,7 @@ class ObsidianImages(Extension): el.text = link return el, m.start(0), m.end(0) - def extendMarkdown(self, md: markdown.Markdown) -> None: - OBSIMAGE_PATTERN = r'!\[\[(.*?)\]\]' - GENERICIMAGE_PATTERN = r'!\[(.*?)\]\((.*?)\)' + def extendMarkdown(self, md: markdown.Markdown) -> None: # noqa: N802 md.inlinePatterns.register(ObsidianImages.ObsidianImageProc(OBSIMAGE_PATTERN, md, self), 'obsidian_images', PRIO_BASE + 1010) md.inlinePatterns.register(ObsidianImages.GenericImageProc(GENERICIMAGE_PATTERN, md, self), @@ -195,7 +201,7 @@ class ObsidianLinks(Extension): super(ObsidianLinks.ObsidianLinksProc, self).__init__(pattern, md) self._extref = extref - def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: + def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: # noqa: N802 link, text = self._extref._parse_reference(m.group(1)) if link is None: el = etree.Element('span') @@ -213,7 +219,7 @@ class ObsidianLinks(Extension): super(ObsidianLinks.GenericLinksProc, self).__init__(pattern, md) self._extref = extref - def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: + def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element, int, int]: # noqa: N802 text = m.group(1) link = m.group(2) if is_proper_url(link): @@ -233,9 +239,7 @@ class ObsidianLinks(Extension): el.text = text return el, m.start(0), m.end(0) - def extendMarkdown(self, md: markdown.Markdown) -> None: - OBSLINK_PATTERN = r'\[\[(.*?)\]\]' - GENERICLINK_PATTERN = r'\[(.*?)\]\((.*?)\)' + def extendMarkdown(self, md: markdown.Markdown) -> None: # noqa: N802 md.inlinePatterns.register(ObsidianLinks.ObsidianLinksProc(OBSLINK_PATTERN, md, self), 'obsidian_links', PRIO_BASE + 110) md.inlinePatterns.register(ObsidianLinks.GenericLinksProc(GENERICLINK_PATTERN, md, self), @@ -244,10 +248,13 @@ class ObsidianLinks(Extension): class ObsidianInlines(Extension): """An extension that handles the special Obsidian markdown format sequences.""" - def extendMarkdown(self, md: markdown.Markdown) -> None: + def extendMarkdown(self, md: markdown.Markdown) -> None: # noqa: N802 md.inlinePatterns.register(SimpleTagInlineProcessor(r'()~~(.*?)~~', 'del'), 'strikeout', PRIO_BASE) md.inlinePatterns.register(SimpleTagInlineProcessor(r'()\=\=(.*?)\=\=', 'ins'), 'highlight', PRIO_BASE + 1) def create_markdown_parser(context: Context) -> markdown.Markdown: - return markdown.Markdown(extensions=[MetaStripper(), ObsidianImages(context), ObsidianLinks(context), ObsidianInlines()]) + return markdown.Markdown(extensions=[MetaStripper(), + ObsidianImages(context), + ObsidianLinks(context), + ObsidianInlines()])