mirror of
https://github.com/venthur/blag.git
synced 2025-11-25 20:52:43 +00:00
convert relative links to .md files to .html
this uses a markdown.treeprocessor as extension to get all html links from the resulting html document
This commit is contained in:
47
sg/sg.py
47
sg/sg.py
@@ -13,8 +13,11 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from urllib.parse import urlsplit, urlunsplit
|
||||||
|
|
||||||
import markdown
|
from markdown import Markdown
|
||||||
|
from markdown.extensions import Extension
|
||||||
|
from markdown.treeprocessors import Treeprocessor
|
||||||
from jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader
|
from jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader
|
||||||
import feedgenerator
|
import feedgenerator
|
||||||
|
|
||||||
@@ -96,9 +99,12 @@ def convert_to_html(convertibles):
|
|||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|
||||||
md = markdown.Markdown(
|
md = Markdown(
|
||||||
extensions=['meta', 'fenced_code', 'codehilite'],
|
extensions=[
|
||||||
output_format='html5',
|
'meta', 'fenced_code', 'codehilite',
|
||||||
|
MarkdownLinkExtension()
|
||||||
|
],
|
||||||
|
output_format='html5',
|
||||||
)
|
)
|
||||||
|
|
||||||
pages = []
|
pages = []
|
||||||
@@ -172,5 +178,38 @@ def convert_to_html(convertibles):
|
|||||||
|
|
||||||
# generate tags
|
# generate tags
|
||||||
|
|
||||||
|
|
||||||
|
class MarkdownLinkTreeprocessor(Treeprocessor):
|
||||||
|
"""Converts relative links to .md files to .html
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def run(self, root):
|
||||||
|
for element in root.iter():
|
||||||
|
if element.tag == 'a':
|
||||||
|
url = element.get('href')
|
||||||
|
converted = self.convert(url)
|
||||||
|
element.set('href', converted)
|
||||||
|
return root
|
||||||
|
|
||||||
|
def convert(self, url):
|
||||||
|
scheme, netloc, path, query, fragment = urlsplit(url)
|
||||||
|
logger.debug(f'{url} -> scheme: {scheme} netloc: {netloc} path: {path} query: {query} fragment: {fragment}')
|
||||||
|
if (scheme or netloc or not path):
|
||||||
|
return url
|
||||||
|
if path.endswith('.md'):
|
||||||
|
path = path[:-3] + '.html'
|
||||||
|
|
||||||
|
url = urlunsplit((scheme, netloc, path, query, fragment))
|
||||||
|
return url
|
||||||
|
|
||||||
|
|
||||||
|
class MarkdownLinkExtension(Extension):
|
||||||
|
def extendMarkdown(self, md):
|
||||||
|
md.treeprocessors.register(
|
||||||
|
MarkdownLinkTreeprocessor(md), 'mdlink', 0,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user