support deployment in relative url
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
from os.path import dirname, join
|
from os.path import dirname, join, relpath
|
||||||
from time import time
|
from time import time
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@@ -23,7 +23,8 @@ def load_from_cache(path) -> tuple[str, float]:
|
|||||||
return STATIC_CACHE[path]
|
return STATIC_CACHE[path]
|
||||||
|
|
||||||
|
|
||||||
def compile_html(mdfile=None,
|
def compile_html(url_path,
|
||||||
|
mdfile=None,
|
||||||
extensions: Optional[list[str]] = None,
|
extensions: Optional[list[str]] = None,
|
||||||
raw: bool = False) -> str:
|
raw: bool = False) -> str:
|
||||||
with mdfile and open(mdfile, 'r') or sys.stdin as instream:
|
with mdfile and open(mdfile, 'r') or sys.stdin as instream:
|
||||||
@@ -31,11 +32,11 @@ def compile_html(mdfile=None,
|
|||||||
if raw:
|
if raw:
|
||||||
doc = html
|
doc = html
|
||||||
else:
|
else:
|
||||||
css = ' <style>% s\n%s\n%s\n </style>' % (
|
parent = dirname(url_path)
|
||||||
load_from_cache('/github-markdown.css')[0],
|
prefix = relpath('/', start=parent)
|
||||||
load_from_cache('/pygment.css')[0],
|
script = f'<script src="{prefix}/hot-reload.js", type="text/javascript" defer="true"></script>'
|
||||||
load_from_cache('/custom.css')[0],
|
css = ''
|
||||||
)
|
for css_file in ('github-markdown.css', 'pygment.css', 'custom.css'):
|
||||||
script = '<script src="/hot-reload.js", type="text/javascript" defer="true"></script>'
|
css += f' <link rel="stylesheet" href="{prefix}/{css_file}">'
|
||||||
doc = load_from_cache('/template.html')[0].format(content=html, script=script, css=css)
|
doc = load_from_cache('/template.html')[0].format(content=html, script=script, css=css)
|
||||||
return doc
|
return doc
|
||||||
|
@@ -84,25 +84,16 @@ class Server:
|
|||||||
lambda: getmtime(path)
|
lambda: getmtime(path)
|
||||||
)
|
)
|
||||||
if etag != digest:
|
if etag != digest:
|
||||||
body = compile_html(path,
|
if exists(path) and isfile(path):
|
||||||
MARDOWN_EXTENSIONS,
|
return self.render_markdown(url_path, path, True, digest, start_response)
|
||||||
raw=True).encode()
|
else:
|
||||||
start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'),
|
return self.not_found(start_response)
|
||||||
('Etag', 'W/"%s"' % digest),
|
|
||||||
('Cache-Control', 'no-cache'),
|
|
||||||
])
|
|
||||||
return [body]
|
|
||||||
finally:
|
finally:
|
||||||
subscription.unsubscribe()
|
subscription.unsubscribe()
|
||||||
return self.not_modified(start_response, digest)
|
return self.not_modified(start_response, digest)
|
||||||
elif is_markdown(path):
|
elif is_markdown(path):
|
||||||
raw = query_string == 'reload'
|
raw = query_string == 'reload'
|
||||||
body = compile_html(path, MARDOWN_EXTENSIONS, raw=raw).encode()
|
return self.render_markdown(url_path, path, raw, digest, start_response)
|
||||||
start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8'),
|
|
||||||
('Etag', 'W/"%s"' % digest),
|
|
||||||
('Cache-Control', 'no-cache'),
|
|
||||||
])
|
|
||||||
return [body]
|
|
||||||
elif is_dotfile(path) and which("dot"):
|
elif is_dotfile(path) and which("dot"):
|
||||||
body = check_output(['dot', '-Tsvg', basename(path)], cwd=dirname(path))
|
body = check_output(['dot', '-Tsvg', basename(path)], cwd=dirname(path))
|
||||||
start_response('200 OK', [('Content-Type', 'image/svg+xml; charset=UTF-8'),
|
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'),
|
('Content-Type', 'text/html; charset=UTF-8'),
|
||||||
])
|
])
|
||||||
return [body]
|
return [body]
|
||||||
start_response('404 NOT_FOUND', [])
|
return self.not_found(start_response)
|
||||||
return []
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def stream_hash(source: BinaryIO, bufsize=0x1000) -> bytes:
|
def stream_hash(source: BinaryIO, bufsize=0x1000) -> bytes:
|
||||||
@@ -197,6 +187,21 @@ class Server:
|
|||||||
return etag, digest
|
return etag, digest
|
||||||
|
|
||||||
@staticmethod
|
@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')) -> []:
|
def not_modified(start_response, digest: str, cache_control=('Cache-Control', 'no-cache')) -> []:
|
||||||
start_response('304 Not Modified', [
|
start_response('304 Not Modified', [
|
||||||
('Etag', f'W/"{digest}"'),
|
('Etag', f'W/"{digest}"'),
|
||||||
@@ -204,6 +209,11 @@ class Server:
|
|||||||
])
|
])
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def not_found(start_response) -> list[bytes]:
|
||||||
|
start_response('404 NOT_FOUND', [])
|
||||||
|
return []
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def directory_listing(path_info, path) -> str:
|
def directory_listing(path_info, path) -> str:
|
||||||
title = "Directory listing for %s" % path_info
|
title = "Directory listing for %s" % path_info
|
||||||
|
Reference in New Issue
Block a user