forked from github.com/blag
WIP
This commit is contained in:
@@ -8,6 +8,7 @@ processing.
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
from xml.etree.ElementTree import Element
|
||||
|
||||
from markdown import Markdown
|
||||
from markdown.extensions import Extension
|
||||
@@ -17,7 +18,7 @@ from markdown.treeprocessors import Treeprocessor
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def markdown_factory():
|
||||
def markdown_factory() -> Markdown:
|
||||
"""Create a Markdown instance.
|
||||
|
||||
This method exists only to ensure we use the same Markdown instance
|
||||
@@ -33,12 +34,12 @@ def markdown_factory():
|
||||
'meta', 'fenced_code', 'codehilite', 'smarty',
|
||||
MarkdownLinkExtension()
|
||||
],
|
||||
output_format='html5',
|
||||
output_format='html',
|
||||
)
|
||||
return md
|
||||
|
||||
|
||||
def convert_markdown(md, markdown):
|
||||
def convert_markdown(md: Markdown, markdown: str) -> tuple[str, dict]:
|
||||
"""Convert markdown into html and extract meta data.
|
||||
|
||||
Some meta data is treated special:
|
||||
@@ -87,15 +88,19 @@ class MarkdownLinkTreeprocessor(Treeprocessor):
|
||||
|
||||
"""
|
||||
|
||||
def run(self, root):
|
||||
def run(self, root: Element):
|
||||
for element in root.iter():
|
||||
if element.tag == 'a':
|
||||
url = element.get('href')
|
||||
# element.get could also return None, we haven't seen this so
|
||||
# far, so lets wait if we raise this
|
||||
assert url is not None
|
||||
url = str(url)
|
||||
converted = self.convert(url)
|
||||
element.set('href', converted)
|
||||
return root
|
||||
|
||||
def convert(self, url):
|
||||
def convert(self, url: str):
|
||||
scheme, netloc, path, query, fragment = urlsplit(url)
|
||||
logger.debug(
|
||||
f'{url}: {scheme=} {netloc=} {path=} {query=} {fragment=}'
|
||||
@@ -113,7 +118,7 @@ class MarkdownLinkExtension(Extension):
|
||||
"""markdown.extension that converts relative .md- to .html-links.
|
||||
|
||||
"""
|
||||
def extendMarkdown(self, md):
|
||||
def extendMarkdown(self, md: Markdown):
|
||||
md.treeprocessors.register(
|
||||
MarkdownLinkTreeprocessor(md), 'mdlink', 0,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user