-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdserver
executable file
·28 lines (23 loc) · 986 Bytes
/
mdserver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2023 Benjamin Holt -- MIT License
# Small http server that renders markdown files automatically
import http.server
import mistletoe # install with `pip3 install mistletoe`
class MarkdownHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path.endswith(".md"):
# FIXME: This mostly works, but mangles unicode characters
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
with open(self.path[1:], "r") as f:
self.wfile.write(mistletoe.markdown(f.read()).encode())
else:
super().do_GET()
if __name__ == "__main__":
http.server.test(
HandlerClass=MarkdownHandler,
port=8910, # Use a non-default port, TODO: add command line option
bind="127.0.0.1", # Only accept connections from localhost, http.server is not secure
)