This repository has been archived on 2023-01-27. You can view files and clone it, but cannot push or open issues or pull requests.
flask-wikipages/app/__init__.py

41 lines
1.1 KiB
Python

from flask import Flask, url_for, render_template
import os
import json
from app.config import PANDOC_LINK, PANDOC_PATH
import subprocess
app = Flask(__name__)
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
#TODO: make instead of datetime hash of commit
def dated_url_for(endpoint, **values):
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path, endpoint, filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
@app.route(f'/{PANDOC_LINK}/<page>')
def get_pandoc_page(page):
with open(f'{PANDOC_PATH}/{page}/config.json') as f:
data = json.loads(f.read())
filename = f'{PANDOC_PATH}/{page}/main.md'
inserted = subprocess.Popen(f'pandoc {filename} -t html --mathjax', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
template = data['template']
return render_template(template, markdown=inserted)
@app.route('/')
def index():
return render_template('index.html')