2021-12-29 02:18:06 +01:00
|
|
|
import datetime
|
2021-12-28 23:29:33 +01:00
|
|
|
import re
|
2021-12-28 21:54:12 +01:00
|
|
|
import os
|
|
|
|
|
2021-12-29 02:18:06 +01:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from flask import Flask, send_from_directory, render_template, make_response, request
|
|
|
|
from livereload import Server
|
|
|
|
|
|
|
|
import xml_lexer
|
|
|
|
|
2021-12-28 23:29:33 +01:00
|
|
|
|
2021-12-28 21:54:12 +01:00
|
|
|
app = Flask(__name__)
|
2021-12-28 23:29:33 +01:00
|
|
|
app.jinja_options.setdefault('extensions', []).append('jinja2_highlight.HighlightExtension')
|
|
|
|
|
|
|
|
|
|
|
|
TEMPLATES = "templates"
|
|
|
|
PAGES_BASE = "pages"
|
|
|
|
STATIC = ["images"]
|
|
|
|
|
|
|
|
ROOT = os.environ.get("EA_ROOT", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_xrpc_list():
|
|
|
|
output = "<ul>"
|
|
|
|
proto = TEMPLATES + "/" + PAGES_BASE + "/proto"
|
|
|
|
for base, _, files in os.walk(proto):
|
|
|
|
prefix = base[len(proto):].replace("\\", "/").strip("/")
|
|
|
|
if prefix:
|
|
|
|
prefix = prefix.replace("/", ".") + "."
|
|
|
|
for i in files:
|
|
|
|
delim = "_" if prefix else "."
|
|
|
|
href = f"{ROOT}/proto{base[len(proto):]}/{i}"
|
|
|
|
output += f"<li><code><a href=\"{href}\">"
|
|
|
|
output += prefix + i.replace(".html", delim + "%s")
|
|
|
|
output += "</code></a></li>"
|
|
|
|
with open(os.path.join(base, i)) as f:
|
|
|
|
headers = re.findall('<h2 id="([^"]*?)">', f.read())
|
|
|
|
output += "<ul>"
|
|
|
|
for j in headers:
|
|
|
|
output += f"<li><code><a href=\"{href}#{j}\">"
|
|
|
|
output += prefix + i.replace(".html", delim + j)
|
|
|
|
output += "</code></a></li>"
|
|
|
|
output += "</ul>"
|
|
|
|
return output + "</ul>"
|
|
|
|
|
2021-12-28 21:54:12 +01:00
|
|
|
|
|
|
|
@app.route("/styles.css")
|
|
|
|
def styles():
|
|
|
|
return send_from_directory(".", "styles.css")
|
2021-12-28 23:29:33 +01:00
|
|
|
@app.route("/tango.css")
|
|
|
|
def tango():
|
|
|
|
return send_from_directory(".", "tango.css")
|
2021-12-28 21:54:12 +01:00
|
|
|
|
|
|
|
|
2021-12-28 23:29:33 +01:00
|
|
|
for i in STATIC:
|
|
|
|
for base, _, files in os.walk(i):
|
|
|
|
for name in files:
|
|
|
|
def handler(base, name):
|
|
|
|
def handler():
|
|
|
|
return send_from_directory(base, name)
|
|
|
|
return handler
|
|
|
|
local_base = base.replace("\\", "/").strip(".").strip("/")
|
|
|
|
route = local_base + "/" + name
|
|
|
|
if not route.startswith("/"):
|
|
|
|
route = "/" + route
|
2021-12-28 21:54:12 +01:00
|
|
|
|
2021-12-28 23:29:33 +01:00
|
|
|
handler = handler(base, name)
|
|
|
|
handler.__name__ == route
|
|
|
|
app.add_url_rule(route, route, handler)
|
|
|
|
|
|
|
|
|
|
|
|
for base, _, files in os.walk(TEMPLATES + "/" + PAGES_BASE):
|
2021-12-28 21:54:12 +01:00
|
|
|
if ".git" in base:
|
|
|
|
continue
|
|
|
|
if base.startswith(TEMPLATES):
|
|
|
|
base = base[len(TEMPLATES):]
|
|
|
|
|
|
|
|
for name in files:
|
|
|
|
if name.endswith(".html"):
|
|
|
|
def handler(base, name):
|
|
|
|
def handler():
|
2021-12-28 23:29:33 +01:00
|
|
|
return render_template(
|
|
|
|
os.path.join(base, name).strip("/").replace("\\", "/"),
|
|
|
|
ROOT=ROOT,
|
|
|
|
generate_xrpc_list=generate_xrpc_list
|
|
|
|
)
|
2021-12-28 21:54:12 +01:00
|
|
|
return handler
|
|
|
|
|
|
|
|
local_base = base.replace("\\", "/").strip(".").strip("/")
|
|
|
|
if local_base.startswith(PAGES_BASE):
|
|
|
|
local_base = local_base[len(PAGES_BASE):]
|
|
|
|
|
|
|
|
route = local_base + "/" + name
|
|
|
|
if route.endswith("/index.html"):
|
|
|
|
route = route[:-10]
|
|
|
|
if not route.startswith("/"):
|
|
|
|
route = "/" + route
|
|
|
|
|
|
|
|
handler = handler(base, name)
|
|
|
|
handler.__name__ == route
|
|
|
|
app.add_url_rule(route, route, handler)
|
|
|
|
|
|
|
|
|
2021-12-29 02:18:06 +01:00
|
|
|
from flask import url_for
|
|
|
|
|
|
|
|
@app.route("/sitemap.xml")
|
|
|
|
def sitemap():
|
|
|
|
host_components = urlparse(request.host_url)
|
2021-12-29 02:18:51 +01:00
|
|
|
host_base = "https://bsnk.me" + ROOT
|
2021-12-29 02:18:06 +01:00
|
|
|
|
|
|
|
links = []
|
|
|
|
for rule in app.url_map.iter_rules():
|
|
|
|
if "GET" in rule.methods and len(rule.arguments) == 0:
|
|
|
|
url = url_for(rule.endpoint, **(rule.defaults or {}))
|
|
|
|
if not url.endswith(("/", ".html", ".png")):
|
|
|
|
continue
|
|
|
|
path = rule.endpoint
|
|
|
|
if path.endswith("/"):
|
|
|
|
path += "index.html"
|
|
|
|
path = os.path.join(TEMPLATES, PAGES_BASE, path.lstrip("/"))
|
|
|
|
if os.path.exists(path):
|
|
|
|
mod_time = os.path.getmtime(path)
|
|
|
|
mod_time = datetime.datetime.fromtimestamp(mod_time).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
else:
|
|
|
|
mod_time = None
|
|
|
|
links.append((host_base + url, mod_time))
|
|
|
|
response = make_response(render_template("sitemap.xml", urls=links))
|
|
|
|
response.headers["Content-Type"] = "application/xml"
|
|
|
|
return response
|
2021-12-28 21:54:12 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
app.config['DEBUG'] = True
|
|
|
|
|
2021-12-28 23:29:33 +01:00
|
|
|
app.run(debug=True, port=3000, host="0.0.0.0")
|
|
|
|
|
|
|
|
# server = Server(app.wsgi_app)
|
|
|
|
# server.watch(".")
|
|
|
|
# server.serve(port=3000)
|