website/start_site.py

207 lines
5.4 KiB
Python
Raw Permalink Normal View History

2021-06-01 23:15:46 +02:00
#!/usr/bin/env python
import os
2023-07-08 17:00:40 +02:00
import toml
2021-06-01 23:15:46 +02:00
from datetime import datetime
import pytz
from flask import Flask, url_for, render_template, send_from_directory
2023-07-08 17:00:40 +02:00
from flask import make_response
2021-06-01 23:15:46 +02:00
from feedgen.feed import FeedGenerator
2023-07-08 17:00:40 +02:00
from flaskext.markdown import Markdown
import markdown
import markdown.extensions.fenced_code
import markdown.extensions.codehilite
import markdown.extensions.toc
from markdown.extensions.toc import TocExtension
from pygments.formatters import HtmlFormatter
2021-06-01 23:15:46 +02:00
app = Flask(__name__)
2023-07-08 17:00:40 +02:00
Markdown(app)
meta_data = {
2023-07-12 00:08:49 +02:00
root[len("./blog/"):]: datetime.strptime(
open(os.path.join(root, "index.md"), encoding='UTF-8')
.readline().rstrip(),
"%Y-%m-%d"
2023-07-08 17:00:40 +02:00
)
for root, dirs, files in os.walk("./blog")
2023-07-12 00:08:49 +02:00
if "index.md" in files
2023-07-08 17:00:40 +02:00
}
app.config["blog"] = toml.load("settings.toml")
content_path = app.config["blog"]["content"]["path"]
highlight_style = app.config["blog"]["content"]["style"]
STYLESHEET = "stylesheet.css"
STYLESHEET_AUTO_COMPLETE = "auto-complete.css"
project_name = app.config["blog"]["project"]["name"]
project_title = app.config["blog"]["project"]["title"]
app.config["blog"]["style"] = toml.load("style.toml")
colors = app.config["blog"]["style"][highlight_style]
2021-06-01 23:15:46 +02:00
@app.route('/')
def index(_paths=meta_data):
2023-06-20 21:46:38 +02:00
''' Main Site.
'''
2023-07-08 17:00:40 +02:00
sorted_meta_data = dict(
sorted(
meta_data.items(),
reverse=True,
key=lambda item: item[1]
)
)
return render_template("index.html", colors=colors, _paths=sorted_meta_data)
2021-06-01 23:15:46 +02:00
@app.route('/blog/<blog_item>/index.html')
def blog(blog_item, _date=meta_data):
2023-06-20 21:46:38 +02:00
''' Blog Pages.
'''
2023-07-08 17:00:40 +02:00
md_file_path = os.path.join(f"blog/{blog_item}/index.md")
with open(md_file_path, "r") as _f:
md_file = _f.read()
md_extensions = [
'toc',
TocExtension(toc_class="", title=""),
"fenced_code",
"codehilite",
"tables",
"mdx_math"
]
md_configs = {"mdx_math": {"enable_dollar_delimiter": True}}
md = markdown.Markdown(
extensions=md_extensions,
extension_configs=md_configs
)
html = md.convert(md_file)
formatter = HtmlFormatter(
style=highlight_style,
full=True,
cssclass="codehilite"
)
css_string = formatter.get_style_defs()
md_css_string = "<style>" + css_string + "</style>"
md_template = md_css_string + html
res = render_template(
"blog.html",
#toc=md.toc,
md_doc=md_template,
colors=colors,
stylesheet=STYLESHEET,
#stylesheet_auto_complete=STYLESHEET_AUTO_COMPLETE,
project_name=project_name,
project_title=project_title,
# tree=cut_path_tree(
# #rem_readme(content_path, make_tree(content_path)),
# make_tree(content_path),
# content_path,
# ".md" # )
_date=meta_data[blog_item]
)
response = make_response(res)
response.headers["Content-Type"] = "text/html; charset=utf-8"
return response
# return render_template(
# f"blog/{blog_item}/index.html",
# _date=meta_data[blog_item]
# )
2021-06-01 23:15:46 +02:00
@app.route("/about.html")
def about():
2023-06-20 21:46:38 +02:00
''' About Page.
'''
2023-07-08 17:00:40 +02:00
return render_template("about.html", colors=colors)
2021-06-01 23:15:46 +02:00
@app.route("/contact.html")
def contact():
2023-06-20 21:46:38 +02:00
''' Contact Page.
'''
2023-07-08 17:00:40 +02:00
return render_template("contact.html", colors=colors)
2021-06-01 23:15:46 +02:00
@app.route("/rss.xml")
def rss(_items=meta_data):
2023-06-20 21:46:38 +02:00
''' RSS Feed.
Generates RSS feed as XML
'''
2023-07-08 17:00:40 +02:00
# rss_feed = []
2021-06-01 23:15:46 +02:00
_tz = pytz.timezone('Europe/Berlin')
_fg = FeedGenerator()
_fg.title("Website of Stefan Friese")
_fg.description("test")
_fg.language("en-us")
2023-07-08 17:00:40 +02:00
# _fg.author({'name': "Stefan Friese", 'email': 'stefan@stefan.works'})
_fg.link(href="https://stefan.works", rel="self")
2021-06-01 23:15:46 +02:00
for key in meta_data.keys():
_fe = _fg.add_entry()
_fe.id(f"https://stefan.works/blog/{key}/index.html")
_fe.title(key)
2023-07-08 17:00:40 +02:00
# _fe.description("test")
# _fe.author({'name': "Stefan Friese", 'email': 'stefan@stefan.works'})
2021-06-01 23:15:46 +02:00
_fe.link(href=f"https://stefan.works/blog/{key}/index.html")
_fe.pubDate(pubDate=_tz.localize(meta_data[key]))
_fg.rss_str(pretty=True)
_fg.rss_file('./static/rss.xml')
2023-07-08 17:00:40 +02:00
return send_from_directory(
os.path.join(
app.root_path,
'static'
),
'rss.xml'
)
2021-06-01 23:15:46 +02:00
@app.route('/favicon.ico')
def favicon():
2023-06-20 21:46:38 +02:00
''' Provides favicon.
'''
2023-07-08 17:00:40 +02:00
return send_from_directory(
os.path.join(
app.root_path,
'static'
),
'favicon.ico'
)
2021-06-01 23:15:46 +02:00
@app.errorhandler(404)
def page_not_found(_error):
2023-06-20 21:46:38 +02:00
''' Error Handling.
Error 404
'''
2023-07-08 17:00:40 +02:00
return render_template("/status_code/404.html", colors=colors), 404
2021-06-01 23:15:46 +02:00
@app.errorhandler(400)
def bad_request(_error):
2023-06-20 21:46:38 +02:00
''' Error Handling.
Error 400
'''
2023-07-08 17:00:40 +02:00
return render_template("/status_code/400.html", colors=colors), 400
2021-06-01 23:15:46 +02:00
@app.errorhandler(500)
def internal_server_error(_error):
2023-06-20 21:46:38 +02:00
''' Error Handling.
Error 500
'''
2023-07-08 17:00:40 +02:00
return render_template("/status_code/500.html", colors=colors), 500
2021-06-01 23:15:46 +02:00
with app.test_request_context():
print(url_for("index"))
2023-07-08 17:00:40 +02:00
# print(url_for("about"))
# print(url_for("contact"))
# print(url_for("rss"))
# print(url_for("static", filename="stylesheet.css"))
# print(meta_data)
# print(url_for("blog", blog_item="first_blog"))