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:
45
sg/sg.py
45
sg/sg.py
@@ -13,8 +13,11 @@ import os
|
||||
import shutil
|
||||
import logging
|
||||
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
|
||||
import feedgenerator
|
||||
|
||||
@@ -96,8 +99,11 @@ def convert_to_html(convertibles):
|
||||
])
|
||||
)
|
||||
|
||||
md = markdown.Markdown(
|
||||
extensions=['meta', 'fenced_code', 'codehilite'],
|
||||
md = Markdown(
|
||||
extensions=[
|
||||
'meta', 'fenced_code', 'codehilite',
|
||||
MarkdownLinkExtension()
|
||||
],
|
||||
output_format='html5',
|
||||
)
|
||||
|
||||
@@ -172,5 +178,38 @@ def convert_to_html(convertibles):
|
||||
|
||||
# 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__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user