delinted, again (with new rules enabled)
This commit is contained in:
parent
7cadb9d834
commit
1660d91a84
|
@ -1,7 +1,7 @@
|
||||||
#/usr/bin/env python3
|
#/usr/bin/env python3
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import xml.etree.ElementTree as etree
|
import xml.etree.ElementTree as etree # noqa: N813
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib.parse import unquote as urlunquote
|
from urllib.parse import unquote as urlunquote
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
@ -15,6 +15,14 @@ from .dragonglass import Context
|
||||||
|
|
||||||
PRIO_BASE = 10000 # priority base for our extensions
|
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:
|
def is_proper_url(s: str) -> bool:
|
||||||
"""
|
"""
|
||||||
|
@ -61,7 +69,7 @@ class MetaStripper(Extension):
|
||||||
lines.pop(0)
|
lines.pop(0)
|
||||||
return lines
|
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)
|
md.preprocessors.register(MetaStripper.MetaStripperProc(md), 'metastripper', PRIO_BASE)
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,7 +106,7 @@ class ObsidianImages(Extension):
|
||||||
super(ObsidianImages.ObsidianImageProc, self).__init__(pattern, md)
|
super(ObsidianImages.ObsidianImageProc, self).__init__(pattern, md)
|
||||||
self._extref = extref
|
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))
|
name, width, height = self._extref._parse_dimensions(m.group(1))
|
||||||
link = self._extref._lookup_image_reference(name)
|
link = self._extref._lookup_image_reference(name)
|
||||||
if link is None:
|
if link is None:
|
||||||
|
@ -120,7 +128,7 @@ class ObsidianImages(Extension):
|
||||||
super(ObsidianImages.GenericImageProc, self).__init__(pattern, md)
|
super(ObsidianImages.GenericImageProc, self).__init__(pattern, md)
|
||||||
self._extref = extref
|
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))
|
name, width, height = self._extref._parse_dimensions(m.group(1))
|
||||||
link = m.group(2)
|
link = m.group(2)
|
||||||
if is_proper_url(link):
|
if is_proper_url(link):
|
||||||
|
@ -149,9 +157,7 @@ class ObsidianImages(Extension):
|
||||||
el.text = link
|
el.text = link
|
||||||
return el, m.start(0), m.end(0)
|
return el, m.start(0), m.end(0)
|
||||||
|
|
||||||
def extendMarkdown(self, md: markdown.Markdown) -> None:
|
def extendMarkdown(self, md: markdown.Markdown) -> None: # noqa: N802
|
||||||
OBSIMAGE_PATTERN = r'!\[\[(.*?)\]\]'
|
|
||||||
GENERICIMAGE_PATTERN = r'!\[(.*?)\]\((.*?)\)'
|
|
||||||
md.inlinePatterns.register(ObsidianImages.ObsidianImageProc(OBSIMAGE_PATTERN, md, self),
|
md.inlinePatterns.register(ObsidianImages.ObsidianImageProc(OBSIMAGE_PATTERN, md, self),
|
||||||
'obsidian_images', PRIO_BASE + 1010)
|
'obsidian_images', PRIO_BASE + 1010)
|
||||||
md.inlinePatterns.register(ObsidianImages.GenericImageProc(GENERICIMAGE_PATTERN, md, self),
|
md.inlinePatterns.register(ObsidianImages.GenericImageProc(GENERICIMAGE_PATTERN, md, self),
|
||||||
|
@ -195,7 +201,7 @@ class ObsidianLinks(Extension):
|
||||||
super(ObsidianLinks.ObsidianLinksProc, self).__init__(pattern, md)
|
super(ObsidianLinks.ObsidianLinksProc, self).__init__(pattern, md)
|
||||||
self._extref = extref
|
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))
|
link, text = self._extref._parse_reference(m.group(1))
|
||||||
if link is None:
|
if link is None:
|
||||||
el = etree.Element('span')
|
el = etree.Element('span')
|
||||||
|
@ -213,7 +219,7 @@ class ObsidianLinks(Extension):
|
||||||
super(ObsidianLinks.GenericLinksProc, self).__init__(pattern, md)
|
super(ObsidianLinks.GenericLinksProc, self).__init__(pattern, md)
|
||||||
self._extref = extref
|
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)
|
text = m.group(1)
|
||||||
link = m.group(2)
|
link = m.group(2)
|
||||||
if is_proper_url(link):
|
if is_proper_url(link):
|
||||||
|
@ -233,9 +239,7 @@ class ObsidianLinks(Extension):
|
||||||
el.text = text
|
el.text = text
|
||||||
return el, m.start(0), m.end(0)
|
return el, m.start(0), m.end(0)
|
||||||
|
|
||||||
def extendMarkdown(self, md: markdown.Markdown) -> None:
|
def extendMarkdown(self, md: markdown.Markdown) -> None: # noqa: N802
|
||||||
OBSLINK_PATTERN = r'\[\[(.*?)\]\]'
|
|
||||||
GENERICLINK_PATTERN = r'\[(.*?)\]\((.*?)\)'
|
|
||||||
md.inlinePatterns.register(ObsidianLinks.ObsidianLinksProc(OBSLINK_PATTERN, md, self),
|
md.inlinePatterns.register(ObsidianLinks.ObsidianLinksProc(OBSLINK_PATTERN, md, self),
|
||||||
'obsidian_links', PRIO_BASE + 110)
|
'obsidian_links', PRIO_BASE + 110)
|
||||||
md.inlinePatterns.register(ObsidianLinks.GenericLinksProc(GENERICLINK_PATTERN, md, self),
|
md.inlinePatterns.register(ObsidianLinks.GenericLinksProc(GENERICLINK_PATTERN, md, self),
|
||||||
|
@ -244,10 +248,13 @@ class ObsidianLinks(Extension):
|
||||||
|
|
||||||
class ObsidianInlines(Extension):
|
class ObsidianInlines(Extension):
|
||||||
"""An extension that handles the special Obsidian markdown format sequences."""
|
"""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'()~~(.*?)~~', 'del'), 'strikeout', PRIO_BASE)
|
||||||
md.inlinePatterns.register(SimpleTagInlineProcessor(r'()\=\=(.*?)\=\=', 'ins'), 'highlight', PRIO_BASE + 1)
|
md.inlinePatterns.register(SimpleTagInlineProcessor(r'()\=\=(.*?)\=\=', 'ins'), 'highlight', PRIO_BASE + 1)
|
||||||
|
|
||||||
|
|
||||||
def create_markdown_parser(context: Context) -> markdown.Markdown:
|
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()])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user