added prefix option

This commit is contained in:
2023-10-20 14:55:31 +08:00
parent 40bd2111bf
commit ef8da4e6cc
5 changed files with 26 additions and 14 deletions

View File

@@ -1,20 +1,25 @@
import sys
from os.path import dirname, join, relpath
from time import time
from typing import Optional
from typing import Optional, TYPE_CHECKING
import markdown
if TYPE_CHECKING:
from _typeshed import StrOrBytesPath
STATIC_RESOURCES: set[str] = {
'/github-markdown.css',
'/custom.css',
'/hot-reload.js',
'/pygment.css',
'/markdown.svg'
}
STATIC_CACHE: dict[str, tuple[str, float]] = {}
MARDOWN_EXTENSIONS = ['extra', 'smarty', 'tables', 'codehilite']
def load_from_cache(path) -> tuple[str, float]:
global STATIC_CACHE
if path not in STATIC_CACHE:
@@ -24,7 +29,8 @@ def load_from_cache(path) -> tuple[str, float]:
def compile_html(url_path,
mdfile=None,
mdfile: 'StrOrBytesPath',
prefix: Optional['StrOrBytesPath'] = None,
extensions: Optional[list[str]] = None,
raw: bool = False) -> str:
with mdfile and open(mdfile, 'r') or sys.stdin as instream:
@@ -33,9 +39,9 @@ def compile_html(url_path,
doc = html
else:
parent = dirname(url_path)
prefix = relpath('/', start=parent)
prefix = prefix or relpath('/', start=parent)
script = f'<script src="{prefix}/hot-reload.js", type="text/javascript" defer="true"></script>'
css = ''
css = f'<link rel="icon" type="image/x-icon" href="{prefix}/markdown.svg">'
for css_file in ('github-markdown.css', 'pygment.css', 'custom.css'):
css += f' <link rel="stylesheet" href="{prefix}/{css_file}">'
doc = load_from_cache('/template.html')[0].format(content=html, script=script, css=css)