support deployment in relative url

This commit is contained in:
2023-10-20 07:37:52 +08:00
parent 335c2ddd7f
commit 40bd2111bf
2 changed files with 35 additions and 24 deletions

View File

@@ -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 = ' <style>% s\n%s\n%s\n </style>' % (
load_from_cache('/github-markdown.css')[0],
load_from_cache('/pygment.css')[0],
load_from_cache('/custom.css')[0],
)
script = '<script src="/hot-reload.js", type="text/javascript" defer="true"></script>'
parent = dirname(url_path)
prefix = relpath('/', start=parent)
script = f'<script src="{prefix}/hot-reload.js", type="text/javascript" defer="true"></script>'
css = ''
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)
return doc

View File

@@ -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