Home Download Direct linkSettings
Aa
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
66
67
68
69
70
71
72
73
74
75
76
Copy
import re
from pathlib import Path

import flask
from werkzeug.exceptions import HTTPException

from src import cookie_parser, request_path, file_finders, markdown_parser
from src.code_highlight import create_missing_code_style_files

PROJECT_PATH = Path(__file__).parent.parent


def error404_handler(_):
    if rp := flask.g.get("rpath"):
        rp.is_not_found = True

    return flask.render_template("error/not_found.html"), 404


def error_handler(error):
    return flask.render_template("error/base.html", error=error), error.code


def load_license_text():
    with (PROJECT_PATH / "LICENSE.txt").open() as file:
        # regexp to replace all single line breaks with spaces
        return re.sub("(.)\n(.)", r"\1 \2", file.read())


def add_redirect_rule(app: flask.Flask, from_path: str, to_path: str):

    @app.route(from_path)
    def _redirect():
        return flask.redirect(to_path)


def init(app: flask.Flask):
    create_missing_code_style_files() 
    markdown_parser.init(app)
    cookie_parser.init(app)
    request_path.init(app)

    app.jinja_env.globals.update(
        FOLDER_EMOJI='\U0001f4c2',
        FILE_EMOJI='\U0001f4c4',
        HOSTNAME="sliva0.mk",
        LICENSE_TEXT=load_license_text(),
        get_articles=file_finders.get_articles,
    )

    app.jinja_env.trim_blocks = True
    app.jinja_env.lstrip_blocks = True

    add_redirect_rule(app, '/favicon.ico', '/static/media/favicon.png')

    for path in ("/", "/<path:subpath>"):
        app.add_url_rule(
            path,
            methods=["GET", "POST"],
            view_func=file_finders.content_file_finder,
        )

    for path in ("/source/", "/source/<path:subpath>"):
        app.add_url_rule(
            path,
            view_func=file_finders.source_file_finder,
        )

    app.add_url_rule(
        "/raw/<path:subpath>",
        view_func=file_finders.raw_file_finder,
    )

    app.register_error_handler(404, error404_handler)
    app.register_error_handler(HTTPException, error_handler)