You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.2 KiB
Python

# IMPORT
import os
from flask import Flask, render_template, request, url_for, redirect, jsonify, abort, send_from_directory
import markdown
import frontmatter
from datetime import datetime
import json
# FUNCTIONS
def list_files(folder, remove_ext=False):
''' Read all the functions in a folder '''
names = []
for entry in os.scandir(folder):
# add to the list only proper files
if entry.is_file(follow_symlinks=False):
# remove the extension from the filename
n = os.path.splitext(entry.name)[0]
if remove_ext:
n = entry.name
names.append(n)
return names
def list_folders(folder):
''' Return all the folders in a folder '''
names = []
for entry in os.scandir(folder):
# add to the list only proper files
if not entry.name.startswith('.') and entry.is_dir():
# remove the extension from the filename
names.append(entry.name)
return names
def get_md_contents(filename, directory='./contents'):
''' Return contents from a filename as frontmatter handler '''
with open(f"{directory}/{filename}", "r") as f:
metadata, content = frontmatter.parse(f.read())
html_content = markdown.markdown(content, extensions=['markdown.extensions.attr_list','markdown.extensions.codehilite','markdown.extensions.fenced_code'])
return metadata, html_content
# FLASK APP
base_url = "~kamo"
projects = 'projects'
# create flask application
app = Flask(__name__,
static_url_path=f'/soupboat/{base_url}/static',
static_folder=f'/soupboat/{base_url}/static')
# Markdown(app, extensions=['extra'])
# app.jinja_env.extend(jinja2_highlight_cssclass = 'codehilite')
# add the base_url variable to all the flask templates
@app.context_processor
def set_base_url():
return dict(base_url=base_url)
# Homepage
@app.route(f"/{base_url}/")
def home_page():
# get the basic info of the website from the /contents/home.md file
meta, content = get_md_contents("home.md")
projects_list = []
for project in list_folders("./projects"):
project_info = get_md_contents("documentation.md",
f"./{projects}/{project}")[0]
project_date = datetime.strptime(project_info['date'], '%d/%m/%Y')
project_info['date'] = datetime.strftime(project_date, '%d %b, %y')
project_info['categories'].sort()
project_info['slug'] = project
projects_list.append(project_info)
projects_list.sort(reverse=True, key=lambda project: datetime.strptime(
project['date'], '%d %b, %y'))
# get the list of the projects, the functions, and the corpora
home = {
**meta,
"content": content,
"projects": projects_list
}
return render_template("home.html", **home)
# For generic pages we can include a common template and change only the contents
@app.route(f"/{base_url}/<slug>/")
def dynamic_page(slug=None):
# meta is a dictionary that contains all the attributes in the markdown file (ex: title, description, soup, etc)
# content is the body of the md file aka the text content
# in this way we can access those frontmatter attributes in jinja simply using the variables title, description, soup, etc
meta, content = get_md_contents(f"{slug}.md")
return render_template("page.html", **meta, content=content)
# Single project
@app.route(f"/{base_url}/projects/<project>/")
def p_info(project=None):
meta, content = get_md_contents("documentation.md",
f"./{projects}/{project}")
template = 'project.html'
if 'template' in meta:
template = meta['template']
return render_template(template, **meta, content=content)
@app.route(f'/{base_url}/projects/<project>/<path:filename>')
def sendStaticFiles(project, filename):
return send_from_directory(app.root_path + f'/projects/{project}/', filename, conditional=True)
@app.route(f'/{base_url}/hook/', methods=['GET', 'POST'])
def pull():
if request.method == 'POST':
post_data = json.loads(request.form['payload'])
stream = os.popen('git pull')
output = stream.read()
return output
return 'GET method not supported'
# RUN
app.run(port="3132")