-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (52 loc) · 1.83 KB
/
main.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from gpt import generate_html_through_markdown
import asyncio
from aiohttp import web
from os import listdir
import json
import subprocess
subprocess.run(['pygmentize', '-S', 'default', '-f', 'html',
'-a', '.codehilite', '>', 'syntax.css'], shell=True)
if 'pages.json' not in listdir():
with open('pages.json', 'w') as f:
json.dump({}, f)
with open('pages.json') as f:
pages = json.load(f)
async def style(request):
with open('style.css', 'r') as f:
return web.Response(text=f.read(), content_type='text/css')
async def syntax(request):
with open('syntax.css', 'r') as f:
return web.Response(text=f.read(), content_type='text/css')
generating = []
async def in_generating(path):
while path in generating:
await asyncio.sleep(0.1)
async def handle(request):
path = '/'+(request.match_info.get('tail', '').strip('/'))
url_params = request.rel_url.query
g = '&'.join([f'{k}={v}' for k, v in url_params.items()])
if len(g) > 0:
path += '?'+g
if path.endswith('.js'):
return web.Response(text='', status=404)
if path.endswith('favicon.ico'):
return web.Response(text='', status=404)
if path.endswith('style.css'):
return await style(request)
if path.endswith('syntax.css'):
return await syntax(request)
if path not in pages:
if path not in generating:
generating.append(path)
pages[path] = await generate_html_through_markdown(path)
generating.remove(path)
else:
await in_generating(path)
with open('pages.json', 'w') as f:
json.dump(pages, f)
return web.Response(text=pages[path], content_type='text/html')
app = web.Application()
app.add_routes([
web.get('/{tail:.*}', handle)
])
web.run_app(app, host='127.0.0.1', port=8080)