2022-01-11 20:16:27 +01:00
|
|
|
import os
|
2024-04-11 02:18:56 +02:00
|
|
|
import re
|
2022-01-11 20:16:27 +01:00
|
|
|
from typing import Optional
|
2024-04-11 02:18:56 +02:00
|
|
|
from urllib.parse import urljoin
|
2022-01-11 20:16:27 +01:00
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
from flask import (
|
|
|
|
jsonify,
|
|
|
|
request,
|
|
|
|
Response,
|
|
|
|
send_from_directory as send_from_directory_,
|
|
|
|
render_template,
|
|
|
|
)
|
2022-01-11 20:16:27 +01:00
|
|
|
|
|
|
|
from .app import app
|
|
|
|
from .config import config
|
2022-06-14 00:32:35 +02:00
|
|
|
from ._sorters import PagesSortByTimeGroupedByFolder
|
2022-01-11 20:16:27 +01:00
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
def send_from_directory(
|
|
|
|
path: str, file: str, alternative_path: Optional[str] = None, *args, **kwargs
|
|
|
|
):
|
2022-01-11 20:16:27 +01:00
|
|
|
if not os.path.exists(os.path.join(path, file)) and alternative_path:
|
|
|
|
path = alternative_path
|
|
|
|
return send_from_directory_(path, file, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/", methods=["GET"])
|
2022-01-11 20:16:27 +01:00
|
|
|
def home_route():
|
2022-06-14 00:32:35 +02:00
|
|
|
return render_template(
|
2023-04-16 01:44:44 +02:00
|
|
|
"index.html",
|
2022-06-14 00:32:35 +02:00
|
|
|
pages=app.get_pages(sorter=PagesSortByTimeGroupedByFolder),
|
2023-04-16 01:44:44 +02:00
|
|
|
config=config,
|
2022-06-14 00:32:35 +02:00
|
|
|
)
|
2022-01-11 20:16:27 +01:00
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/img/<img>", methods=["GET"])
|
2022-01-11 20:16:27 +01:00
|
|
|
def img_route(img: str):
|
|
|
|
return send_from_directory(app.img_dir, img, config.default_img_dir)
|
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/favicon.ico", methods=["GET"])
|
2022-01-11 20:16:27 +01:00
|
|
|
def favicon_route():
|
2023-04-16 01:44:44 +02:00
|
|
|
return img_route("favicon.ico")
|
2022-01-11 20:16:27 +01:00
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/js/<file>", methods=["GET"])
|
2022-06-14 10:25:57 +02:00
|
|
|
def js_route(file: str):
|
|
|
|
return send_from_directory(app.js_dir, file, config.default_js_dir)
|
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/pwabuilder-sw.js", methods=["GET"])
|
2022-06-14 10:25:57 +02:00
|
|
|
def pwa_builder_route():
|
2023-04-16 01:44:44 +02:00
|
|
|
return send_from_directory(app.js_dir, "pwabuilder-sw.js", config.default_js_dir)
|
2022-06-14 10:25:57 +02:00
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/pwabuilder-sw-register.js", methods=["GET"])
|
2022-06-14 10:25:57 +02:00
|
|
|
def pwa_builder_register_route():
|
2023-04-16 01:44:44 +02:00
|
|
|
return send_from_directory(
|
|
|
|
app.js_dir, "pwabuilder-sw-register.js", config.default_js_dir
|
|
|
|
)
|
2022-06-14 10:25:57 +02:00
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/css/<style>", methods=["GET"])
|
2022-01-11 20:16:27 +01:00
|
|
|
def css_route(style: str):
|
|
|
|
return send_from_directory(app.css_dir, style, config.default_css_dir)
|
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/fonts/<file>", methods=["GET"])
|
2022-01-11 20:16:27 +01:00
|
|
|
def fonts_route(file: str):
|
|
|
|
return send_from_directory(app.fonts_dir, file, config.default_fonts_dir)
|
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/manifest.json", methods=["GET"])
|
2022-06-14 02:24:36 +02:00
|
|
|
def manifest_route():
|
2023-04-16 01:44:44 +02:00
|
|
|
# If there is a manifest.json in the content directory, use it
|
|
|
|
manifest_file = os.path.join(config.content_dir, "manifest.json")
|
|
|
|
if os.path.isfile(manifest_file):
|
|
|
|
return send_from_directory(config.content_dir, "manifest.json")
|
|
|
|
|
|
|
|
# Otherwise, generate a default manifest.json
|
|
|
|
return jsonify(
|
|
|
|
{
|
|
|
|
"name": config.title,
|
|
|
|
"short_name": config.title,
|
|
|
|
"icons": [
|
|
|
|
{"src": "/img/icon-48.png", "sizes": "48x48", "type": "image/png"},
|
|
|
|
{"src": "/img/icon-72.png", "sizes": "72x72", "type": "image/png"},
|
|
|
|
{"src": "/img/icon-96.png", "sizes": "96x96", "type": "image/png"},
|
|
|
|
{"src": "/img/icon-144.png", "sizes": "144x144", "type": "image/png"},
|
|
|
|
{"src": "/img/icon-168.png", "sizes": "168x168", "type": "image/png"},
|
|
|
|
{"src": "/img/icon-192.png", "sizes": "192x192", "type": "image/png"},
|
|
|
|
{"src": "/img/icon-256.png", "sizes": "256x256", "type": "image/png"},
|
|
|
|
{"src": "/img/icon-512.png", "sizes": "512x512", "type": "image/png"},
|
|
|
|
],
|
|
|
|
"gcm_sender_id": "",
|
|
|
|
"gcm_user_visible_only": True,
|
|
|
|
"start_url": "/",
|
|
|
|
"permissions": ["gcm"],
|
|
|
|
"scope": "",
|
|
|
|
"orientation": "portrait",
|
|
|
|
"display": "standalone",
|
2023-04-16 01:51:09 +02:00
|
|
|
"theme_color": "#000000",
|
|
|
|
"background_color": "#ffffff",
|
2023-04-16 01:44:44 +02:00
|
|
|
}
|
|
|
|
)
|
2022-06-14 02:24:36 +02:00
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/article/<path:path>/<article>", methods=["GET"])
|
2022-06-14 00:32:35 +02:00
|
|
|
def article_with_path_route(path: str, article: str):
|
|
|
|
return app.get_page(os.path.join(path, article))
|
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/article/<article>", methods=["GET"])
|
2022-01-11 20:16:27 +01:00
|
|
|
def article_route(article: str):
|
2023-04-16 01:44:44 +02:00
|
|
|
return article_with_path_route("", article)
|
2022-01-11 20:16:27 +01:00
|
|
|
|
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
@app.route("/rss", methods=["GET"])
|
2022-01-11 20:16:27 +01:00
|
|
|
def rss_route():
|
2023-05-08 12:10:15 +02:00
|
|
|
short_description = "short" in request.args or config.short_feed
|
|
|
|
pages = app.get_pages(
|
|
|
|
with_content=not short_description,
|
|
|
|
skip_header=True,
|
|
|
|
skip_html_head=True,
|
|
|
|
)
|
2022-01-11 20:16:27 +01:00
|
|
|
|
2023-04-16 01:44:44 +02:00
|
|
|
return Response(
|
|
|
|
"""<?xml version="1.0" encoding="UTF-8" ?>
|
2022-01-11 20:16:27 +01:00
|
|
|
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
|
|
|
|
<channel>
|
|
|
|
<title>{title}</title>
|
|
|
|
<link>{link}</link>
|
|
|
|
<description>{description}</description>
|
2022-01-11 23:38:28 +01:00
|
|
|
<category>{categories}</category>
|
2022-01-11 20:16:27 +01:00
|
|
|
<image>
|
|
|
|
<url>{link}/img/icon.png</url>
|
|
|
|
<title>{title}</title>
|
|
|
|
<link>{link}</link>
|
|
|
|
</image>
|
|
|
|
<pubDate>{last_pub_date}</pubDate>
|
|
|
|
<language>{language}</language>
|
|
|
|
|
|
|
|
{items}
|
|
|
|
</channel>
|
2023-04-16 01:44:44 +02:00
|
|
|
</rss>""".format(
|
|
|
|
title=config.title,
|
|
|
|
description=config.description,
|
|
|
|
link=config.link,
|
|
|
|
categories=",".join(config.categories),
|
|
|
|
language=config.language,
|
|
|
|
last_pub_date=(
|
|
|
|
pages[0][1]["published"].strftime("%a, %d %b %Y %H:%M:%S GMT")
|
|
|
|
if pages
|
|
|
|
else ""
|
|
|
|
),
|
|
|
|
items="\n\n".join(
|
|
|
|
[
|
2023-05-08 12:10:15 +02:00
|
|
|
(
|
|
|
|
"""
|
2022-01-11 20:16:27 +01:00
|
|
|
<item>
|
|
|
|
<title>{title}</title>
|
|
|
|
<link>{base_link}{link}</link>
|
2023-05-08 12:10:15 +02:00
|
|
|
<pubDate>{published}</pubDate>
|
|
|
|
<description><![CDATA[{content}]]></description>
|
2024-04-11 02:21:09 +02:00
|
|
|
<media:content medium="image" url="{image}" width="200" height="150" />
|
2022-01-11 20:16:27 +01:00
|
|
|
</item>
|
2023-05-08 12:10:15 +02:00
|
|
|
"""
|
|
|
|
).format(
|
2023-04-16 01:44:44 +02:00
|
|
|
base_link=config.link,
|
|
|
|
title=page.get("title", "[No Title]"),
|
|
|
|
link=page.get("uri", ""),
|
2024-04-11 02:18:56 +02:00
|
|
|
published=(
|
|
|
|
page["published"].strftime("%a, %d %b %Y %H:%M:%S GMT")
|
|
|
|
if "published" in page
|
|
|
|
else ""
|
|
|
|
),
|
|
|
|
content=(
|
|
|
|
page.get("description", "")
|
|
|
|
if short_description
|
|
|
|
else page.get("content", "")
|
|
|
|
),
|
|
|
|
image=(
|
|
|
|
urljoin(config.link, page["image"])
|
|
|
|
if page.get("image")
|
|
|
|
and not re.search(r"^https?://", page["image"])
|
|
|
|
else page.get("image", "")
|
|
|
|
),
|
2023-04-16 01:44:44 +02:00
|
|
|
)
|
|
|
|
for _, page in pages
|
|
|
|
]
|
|
|
|
),
|
|
|
|
),
|
|
|
|
mimetype="application/xml",
|
|
|
|
)
|
2022-01-11 20:16:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|