2023-11-12 21:09:21 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from . import get_article_metadata, find_article
|
2023-11-11 02:09:31 +00:00
|
|
|
from flask import Blueprint
|
2023-11-11 21:41:25 +00:00
|
|
|
from flask import make_response, render_template, send_from_directory
|
2023-11-11 02:09:31 +00:00
|
|
|
from html_sanitizer import Sanitizer
|
|
|
|
from markdown import markdown
|
|
|
|
from vreader.config import Config
|
|
|
|
|
2023-11-11 21:41:25 +00:00
|
|
|
|
2023-11-11 02:09:31 +00:00
|
|
|
bp = Blueprint("common", __name__)
|
|
|
|
sanitizer = Sanitizer()
|
|
|
|
|
2023-11-11 21:41:25 +00:00
|
|
|
|
|
|
|
@bp.route("/static/<path:path>")
|
|
|
|
def static(path):
|
|
|
|
return send_from_directory("static", path)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/manifest.json")
|
|
|
|
def manifest():
|
|
|
|
return send_from_directory("static", "manifest.json")
|
|
|
|
|
|
|
|
|
2023-11-11 02:09:31 +00:00
|
|
|
@bp.route("/", methods=["GET"])
|
|
|
|
def main_entry():
|
2023-11-11 02:59:44 +00:00
|
|
|
# Get Files
|
2023-11-11 02:09:31 +00:00
|
|
|
directory = str(Config.DATA_PATH)
|
|
|
|
all_files = os.listdir(directory)
|
2023-11-11 02:59:44 +00:00
|
|
|
|
2023-11-12 21:04:31 +00:00
|
|
|
# Sort Files
|
|
|
|
markdown_files = [file for file in all_files if file.endswith(".md")]
|
|
|
|
markdown_files = sorted(markdown_files, reverse=True)
|
2023-11-11 02:59:44 +00:00
|
|
|
|
2023-11-12 21:04:31 +00:00
|
|
|
# Get Article Metadata
|
|
|
|
articles = [get_article_metadata(filename, directory) for filename in markdown_files]
|
2023-11-11 02:09:31 +00:00
|
|
|
|
|
|
|
return make_response(render_template("index.html", articles=articles))
|
|
|
|
|
2023-11-11 21:41:25 +00:00
|
|
|
|
2023-11-11 02:09:31 +00:00
|
|
|
@bp.route("/articles/<id>", methods=["GET"])
|
|
|
|
def article_item(id):
|
|
|
|
|
|
|
|
if len(id) != 11:
|
2023-11-12 03:14:21 +00:00
|
|
|
return make_response(
|
|
|
|
render_template("error.html", status=404, message="Invalid Article")
|
|
|
|
), 404
|
2023-11-11 02:09:31 +00:00
|
|
|
|
2023-11-12 21:04:31 +00:00
|
|
|
metadata = find_article(id)
|
2023-11-11 02:09:31 +00:00
|
|
|
if not metadata:
|
2023-11-12 03:14:21 +00:00
|
|
|
return make_response(
|
|
|
|
render_template("error.html", status=404, message="Invalid Article")
|
|
|
|
), 404
|
2023-11-11 02:09:31 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
with open(metadata["filepath"], 'r', encoding='utf-8') as file:
|
|
|
|
article_contents = file.read()
|
|
|
|
|
|
|
|
markdown_html = sanitizer.sanitize(markdown(article_contents))
|
|
|
|
|
|
|
|
return make_response(
|
|
|
|
render_template("article.html", metadata=metadata, markdown_html=markdown_html)
|
|
|
|
)
|
2023-11-12 03:14:21 +00:00
|
|
|
except Exception as e:
|
|
|
|
return make_response(
|
|
|
|
render_template("error.html", status=404, message=e)
|
|
|
|
), 404
|