import os
from flask import Flask, request, render_template
from datetime import datetime

from werkzeug.exceptions import HTTPException

from app import app
app.config['UPLOAD_FOLDER'] = "uploads/"

@app.errorhandler(400)
def handle_bad_request(e):
    print(f"400 BAD REQUEST: {e}")
    return '400<br>BAD REQUEST'

@app.template_filter('timestamp')
def timestamp_filter(value):
    try:
        dt = datetime.fromtimestamp(value)
        return dt.strftime("%d.%m.%Y %H:%M")
    except:
        return value

import html, re

@app.template_filter('content')
def content_filter(value):
    value = value.strip()
    value = html.escape(value)
    # Regulärer Ausdruck, um URLs zu finden
    url_pattern = re.compile(
        r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)'
    )

    # Ersetze jede gefundene URL durch einen <a>-Tag
    def replace_url(match):
        url = match.group(0)
        if not url.startswith(('http://', 'https://')):
            url = 'http://' + url
        return f'<a href="{url}" target="_blank">{match.group(0)}</a>'

    value = url_pattern.sub(replace_url, value)
    value = value.replace('\n', '<br>')

    return value

import auth
import entry
import user

@app.route("/")
def template():
    return render_template("index.html", name="Hmmm")

if __name__ == "__main__":
    app.run(debug=True)
