mirror of
https://gitea.tendokyu.moe/eamuse/docs.git
synced 2024-11-24 06:50:11 +01:00
79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
from flask import Flask, send_from_directory, render_template
|
|
from livereload import Server
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/styles.css")
|
|
def styles():
|
|
return send_from_directory(".", "styles.css")
|
|
|
|
|
|
for base, folders, files in os.walk("images"):
|
|
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
|
|
|
|
handler = handler(base, name)
|
|
handler.__name__ == route
|
|
app.add_url_rule(route, route, handler)
|
|
|
|
TEMPLATES = "templates"
|
|
PAGES_BASE = "pages"
|
|
for base, folders, files in os.walk(TEMPLATES + "/" + PAGES_BASE):
|
|
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():
|
|
return render_template(os.path.join(base, name).strip("/").replace("\\", "/"), ROOT=os.environ.get("EA_ROOT"))
|
|
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)
|
|
|
|
|
|
from flask import url_for
|
|
def has_no_empty_params(rule):
|
|
defaults = rule.defaults if rule.defaults is not None else ()
|
|
arguments = rule.arguments if rule.arguments is not None else ()
|
|
return len(defaults) >= len(arguments)
|
|
@app.route("/site-map")
|
|
def site_map():
|
|
links = []
|
|
for rule in app.url_map.iter_rules():
|
|
# Filter out rules we can't navigate to in a browser
|
|
# and rules that require parameters
|
|
if "GET" in rule.methods and has_no_empty_params(rule):
|
|
url = url_for(rule.endpoint, **(rule.defaults or {}))
|
|
links.append((url, rule.endpoint))
|
|
return str(links)
|
|
|
|
if __name__ == '__main__':
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
app.config['DEBUG'] = True
|
|
|
|
server = Server(app.wsgi_app)
|
|
server.watch("templates")
|
|
server.serve(port=3000) |