markdown support
All checks were successful
Build and push container image / build-and-push-image (push) Successful in 5m35s
All checks were successful
Build and push container image / build-and-push-image (push) Successful in 5m35s
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
# Imports
|
# Imports
|
||||||
from flask import Blueprint, render_template, abort
|
from flask import Blueprint, render_template, abort
|
||||||
from os import getenv as env
|
from os import getenv as env
|
||||||
import logging, os, re, markdown
|
import logging, os, markdown
|
||||||
|
|
||||||
# Create blueprint
|
# Create blueprint
|
||||||
bp = Blueprint(
|
bp = Blueprint(
|
||||||
'dynamic_routes',
|
'dynamic_routes',
|
||||||
__name__,
|
__name__,
|
||||||
template_folder=env('TEMPLATE_FOLDER', default='../templates'),
|
template_folder=env('TEMPLATE_FOLDER', default='templates'),
|
||||||
static_folder=env('STATIC_FOLDER', default='../static')
|
static_folder=env('STATIC_FOLDER', default='../static')
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
# Get all files in folder
|
# Get all files in folder
|
||||||
def ListFiles(path):
|
def ListFiles(path):
|
||||||
path = os.path.join(bp.template_folder, 'pages', path)[3:]
|
path = os.path.join(bp.template_folder, 'pages', path)
|
||||||
files = []
|
files = []
|
||||||
for root, dirs, files_in_dir in os.walk(path):
|
for root, dirs, files_in_dir in os.walk(path):
|
||||||
for file in files_in_dir:
|
for file in files_in_dir:
|
||||||
@@ -28,26 +28,25 @@ def ListFiles(path):
|
|||||||
# Catch-all route for generic pages
|
# Catch-all route for generic pages
|
||||||
@bp.route('/<path:filename>')
|
@bp.route('/<path:filename>')
|
||||||
def catch_all(filename):
|
def catch_all(filename):
|
||||||
if os.path.exists(os.path.join(bp.template_folder, 'pages', filename)[3:]):
|
if os.path.exists(os.path.join(bp.template_folder, 'pages', filename)):
|
||||||
|
if os.path.isdir(os.path.join(bp.template_folder, 'pages', filename)):
|
||||||
|
return render_template(
|
||||||
|
'bases/directory.html',
|
||||||
|
directory=filename + "/" if not filename.endswith('/') else filename,
|
||||||
|
pages=ListFiles(filename)
|
||||||
|
)
|
||||||
|
|
||||||
return render_template(f'pages/{filename}')
|
return render_template(f'pages/{filename}')
|
||||||
|
|
||||||
elif os.path.exists(os.path.join(bp.template_folder, 'pages', filename + '.html')[3:]):
|
elif os.path.exists(os.path.join(bp.template_folder, 'pages', filename + '.html')):
|
||||||
return render_template(f'pages/{filename}.html')
|
return render_template(f'pages/{filename}.html')
|
||||||
|
|
||||||
elif os.path.exists(os.path.join(bp.template_folder, 'pages', filename + '.md')[3:]):
|
elif os.path.exists(os.path.join(bp.template_folder, 'pages', filename + '.md')):
|
||||||
print("yay")
|
output = markdown.markdown(open(os.path.join(bp.template_folder, 'pages', filename + '.md'), "r").read())
|
||||||
print(markdown.markdownFromFile("../templates/pages/test.md"))
|
|
||||||
return render_template(
|
return render_template(
|
||||||
f'bases/md.html',
|
f'bases/md.html',
|
||||||
title = filename.split("/")[-1],
|
title = filename.split("/")[-1],
|
||||||
markdown = markdown.markdownFromFile(os.path.join(bp.template_folder, 'pages', filename + '.md'))
|
markdown = output
|
||||||
)
|
|
||||||
|
|
||||||
elif os.path.isdir(os.path.join(bp.template_folder, 'pages', filename)[3:]):
|
|
||||||
return render_template(
|
|
||||||
'bases/directory.html',
|
|
||||||
directory=filename + "/" if not filename.endswith('/') else filename,
|
|
||||||
pages=ListFiles(filename)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ def bad_request(error:HTTPException=None):
|
|||||||
return render_template('errors/400.html', error=error), 400
|
return render_template('errors/400.html', error=error), 400
|
||||||
|
|
||||||
# Route for all other errors
|
# Route for all other errors
|
||||||
@bp.route('/error')
|
@bp.route('/idk')
|
||||||
@bp.app_errorhandler(Exception)
|
@bp.app_errorhandler(Exception)
|
||||||
def unauthorized(error:HTTPException=None):
|
def nah(error:HTTPException=None):
|
||||||
if isinstance(error, HTTPException):
|
if isinstance(error, HTTPException):
|
||||||
return render_template(
|
return render_template(
|
||||||
'errors/error.html',
|
'errors/error.html',
|
||||||
@@ -47,6 +47,6 @@ def unauthorized(error:HTTPException=None):
|
|||||||
return render_template(
|
return render_template(
|
||||||
'errors/error.html',
|
'errors/error.html',
|
||||||
code=418,
|
code=418,
|
||||||
description="meow :3",
|
description="I honestly dont know",
|
||||||
name="I'm a teapot"
|
name="I'm a teapot"
|
||||||
), 418
|
), 418
|
||||||
@@ -5,6 +5,6 @@
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<section>
|
<section>
|
||||||
{{ markdown }}
|
{{ markdown|safe }}
|
||||||
</section>
|
</section>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
# hello
|
# hello
|
||||||
this is a test
|
## this is a test
|
||||||
|
|
||||||
|
this is actually a md file, try putting .md at the end of this path
|
||||||
Reference in New Issue
Block a user