beutify
This commit is contained in:
@@ -10,8 +10,9 @@ from urllib.parse import urlparse
|
|||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
|
|
||||||
|
|
||||||
STATIC_CACHE = {}
|
STATIC_CACHE = {}
|
||||||
|
|
||||||
|
|
||||||
def load_from_cache(path):
|
def load_from_cache(path):
|
||||||
global STATIC_CACHE
|
global STATIC_CACHE
|
||||||
if path not in STATIC_CACHE:
|
if path not in STATIC_CACHE:
|
||||||
@@ -19,6 +20,7 @@ def load_from_cache(path):
|
|||||||
STATIC_CACHE[path] = static_file.read()
|
STATIC_CACHE[path] = static_file.read()
|
||||||
return STATIC_CACHE[path]
|
return STATIC_CACHE[path]
|
||||||
|
|
||||||
|
|
||||||
def compile_html(mdfile=None, extensions=None, raw=None, **kwargs):
|
def compile_html(mdfile=None, extensions=None, raw=None, **kwargs):
|
||||||
html = None
|
html = None
|
||||||
with mdfile and open(mdfile, 'r') or sys.stdin as instream:
|
with mdfile and open(mdfile, 'r') or sys.stdin as instream:
|
||||||
@@ -33,6 +35,7 @@ def compile_html(mdfile=None, extensions=None, raw=None, **kwargs):
|
|||||||
doc = load_from_cache('/template.html').format(content=html, script='', css=css)
|
doc = load_from_cache('/template.html').format(content=html, script='', css=css)
|
||||||
return doc
|
return doc
|
||||||
|
|
||||||
|
|
||||||
class MarkdownHTTPServer(ThreadingHTTPServer):
|
class MarkdownHTTPServer(ThreadingHTTPServer):
|
||||||
|
|
||||||
def __init__(self, mdfile, extensions=(), handler=BaseHTTPRequestHandler, interface="127.0.0.1", port=8080):
|
def __init__(self, mdfile, extensions=(), handler=BaseHTTPRequestHandler, interface="127.0.0.1", port=8080):
|
||||||
@@ -41,6 +44,7 @@ class MarkdownHTTPServer(ThreadingHTTPServer):
|
|||||||
import signal
|
import signal
|
||||||
|
|
||||||
self.stop = False
|
self.stop = False
|
||||||
|
|
||||||
def sigint_handler(signum, frame):
|
def sigint_handler(signum, frame):
|
||||||
self.stop = True
|
self.stop = True
|
||||||
|
|
||||||
@@ -52,6 +56,7 @@ class MarkdownHTTPServer(ThreadingHTTPServer):
|
|||||||
self.condition_variable = threading.Condition()
|
self.condition_variable = threading.Condition()
|
||||||
self.hash = None
|
self.hash = None
|
||||||
self.etag = None
|
self.etag = None
|
||||||
|
|
||||||
def watch_file():
|
def watch_file():
|
||||||
watcher = inotify.adapters.Inotify()
|
watcher = inotify.adapters.Inotify()
|
||||||
watcher.add_watch(dirname(abspath(self.mdfile)))
|
watcher.add_watch(dirname(abspath(self.mdfile)))
|
||||||
@@ -88,8 +93,6 @@ class MarkdownHTTPServer(ThreadingHTTPServer):
|
|||||||
|
|
||||||
|
|
||||||
class MarkdownRequestHandler(BaseHTTPRequestHandler):
|
class MarkdownRequestHandler(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
|
|
||||||
status_map = {
|
status_map = {
|
||||||
200: "OK",
|
200: "OK",
|
||||||
204: "No Content",
|
204: "No Content",
|
||||||
@@ -132,7 +135,8 @@ class MarkdownRequestHandler(BaseHTTPRequestHandler):
|
|||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
path = urlparse(self.path)
|
path = urlparse(self.path)
|
||||||
if path.path == '/':
|
if path.path == '/':
|
||||||
self.answer(200, reply=load_from_cache('/template.html').format(content='',
|
self.answer(200, reply=load_from_cache('/template.html').format(
|
||||||
|
content='',
|
||||||
script='<script src="/hot-reload.js", type="text/javascript"></script>',
|
script='<script src="/hot-reload.js", type="text/javascript"></script>',
|
||||||
css='<link rel="stylesheet" href="github-markdown.css">'
|
css='<link rel="stylesheet" href="github-markdown.css">'
|
||||||
'<link rel="stylesheet" href="custom.css">'),
|
'<link rel="stylesheet" href="custom.css">'),
|
||||||
@@ -161,6 +165,7 @@ class MarkdownRequestHandler(BaseHTTPRequestHandler):
|
|||||||
else:
|
else:
|
||||||
self.answer(404)
|
self.answer(404)
|
||||||
|
|
||||||
|
|
||||||
def parse_args(args=None):
|
def parse_args(args=None):
|
||||||
parser = argparse.ArgumentParser(description='Make a complete, styled HTML document from a Markdown file.')
|
parser = argparse.ArgumentParser(description='Make a complete, styled HTML document from a Markdown file.')
|
||||||
parser.add_argument('mdfile', help='File to convert. Defaults to stdin.')
|
parser.add_argument('mdfile', help='File to convert. Defaults to stdin.')
|
||||||
@@ -184,11 +189,13 @@ def parse_args(args=None):
|
|||||||
pass
|
pass
|
||||||
return parser.parse_args(args)
|
return parser.parse_args(args)
|
||||||
|
|
||||||
|
|
||||||
def write_html(out=None, **kwargs):
|
def write_html(out=None, **kwargs):
|
||||||
doc = compile_html(**kwargs)
|
doc = compile_html(**kwargs)
|
||||||
with (out and open(out, 'w')) or sys.stdout as outstream:
|
with (out and open(out, 'w')) or sys.stdout as outstream:
|
||||||
outstream.write(doc)
|
outstream.write(doc)
|
||||||
|
|
||||||
|
|
||||||
def main(args=None):
|
def main(args=None):
|
||||||
args = parse_args(args)
|
args = parse_args(args)
|
||||||
if hasattr(args, 'watch') and args.watch:
|
if hasattr(args, 'watch') and args.watch:
|
||||||
@@ -201,5 +208,6 @@ def main(args=None):
|
|||||||
else:
|
else:
|
||||||
write_html(**vars(args))
|
write_html(**vars(args))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Reference in New Issue
Block a user