diff --git a/src/md2html/md2html.py b/src/md2html/md2html.py index 41b50fc..9cf95a1 100644 --- a/src/md2html/md2html.py +++ b/src/md2html/md2html.py @@ -1,5 +1,5 @@ import sys -from os.path import dirname, join +from os.path import dirname, join, relpath from time import time from typing import Optional @@ -23,7 +23,8 @@ def load_from_cache(path) -> tuple[str, float]: return STATIC_CACHE[path] -def compile_html(mdfile=None, +def compile_html(url_path, + mdfile=None, extensions: Optional[list[str]] = None, raw: bool = False) -> str: with mdfile and open(mdfile, 'r') or sys.stdin as instream: @@ -31,11 +32,11 @@ def compile_html(mdfile=None, if raw: doc = html else: - css = ' ' % ( - load_from_cache('/github-markdown.css')[0], - load_from_cache('/pygment.css')[0], - load_from_cache('/custom.css')[0], - ) - script = '' + parent = dirname(url_path) + prefix = relpath('/', start=parent) + script = f'' + css = '' + for css_file in ('github-markdown.css', 'pygment.css', 'custom.css'): + css += f' ' doc = load_from_cache('/template.html')[0].format(content=html, script=script, css=css) return doc diff --git a/src/md2html/server.py b/src/md2html/server.py index 56ffdf7..c711a7b 100644 --- a/src/md2html/server.py +++ b/src/md2html/server.py @@ -84,25 +84,16 @@ class Server: lambda: getmtime(path) ) if etag != digest: - body = compile_html(path, - MARDOWN_EXTENSIONS, - raw=True).encode() - start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'), - ('Etag', 'W/"%s"' % digest), - ('Cache-Control', 'no-cache'), - ]) - return [body] + if exists(path) and isfile(path): + return self.render_markdown(url_path, path, True, digest, start_response) + else: + return self.not_found(start_response) finally: subscription.unsubscribe() return self.not_modified(start_response, digest) elif is_markdown(path): raw = query_string == 'reload' - body = compile_html(path, MARDOWN_EXTENSIONS, raw=raw).encode() - start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'), - ('Etag', 'W/"%s"' % digest), - ('Cache-Control', 'no-cache'), - ]) - return [body] + return self.render_markdown(url_path, path, raw, digest, start_response) elif is_dotfile(path) and which("dot"): body = check_output(['dot', '-Tsvg', basename(path)], cwd=dirname(path)) start_response('200 OK', [('Content-Type', 'image/svg+xml; charset=UTF-8'), @@ -132,8 +123,7 @@ class Server: ('Content-Type', 'text/html; charset=UTF-8'), ]) return [body] - start_response('404 NOT_FOUND', []) - return [] + return self.not_found(start_response) @staticmethod def stream_hash(source: BinaryIO, bufsize=0x1000) -> bytes: @@ -197,6 +187,21 @@ class Server: return etag, digest @staticmethod + def render_markdown(url_path: 'StrOrBytesPath', + path: str, + raw: bool, + digest: str, + start_response) -> list[bytes]: + body = compile_html(url_path, + path, + MARDOWN_EXTENSIONS, + raw=raw).encode() + start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'), + ('Etag', 'W/"%s"' % digest), + ('Cache-Control', 'no-cache'), + ]) + return [body] + @staticmethod def not_modified(start_response, digest: str, cache_control=('Cache-Control', 'no-cache')) -> []: start_response('304 Not Modified', [ ('Etag', f'W/"{digest}"'), @@ -204,6 +209,11 @@ class Server: ]) return [] + @staticmethod + def not_found(start_response) -> list[bytes]: + start_response('404 NOT_FOUND', []) + return [] + @staticmethod def directory_listing(path_info, path) -> str: title = "Directory listing for %s" % path_info