Stage1 of migration
This commit is contained in:
parent
f46fd4bf72
commit
85073b161e
@ -1,7 +1,10 @@
|
||||
from flask import Flask, url_for, render_template, abort
|
||||
# Project is now migrating to another toolchain
|
||||
# Stage1: Part of rendering is done via Makefiles
|
||||
# but flask is still responsible for template substitution
|
||||
|
||||
from flask import Flask, url_for, render_template
|
||||
import os
|
||||
from app.config import PANDOC_LINK, PANDOC_PATH, WHITELIST_PATH
|
||||
from app.hash_manager import hash_file_sha512
|
||||
from app.page_model import WikiPage
|
||||
from typing import List
|
||||
|
||||
@ -42,30 +45,9 @@ def dated_url_for(endpoint, **values):
|
||||
return url_for(endpoint, **values)
|
||||
|
||||
|
||||
def render_page(page: WikiPage):
|
||||
if page.name not in get_all_names():
|
||||
raise Exception("Page doesn't exist!")
|
||||
in_filename = page.get_file('main.md')
|
||||
out_filename = page.get_file('render.html')
|
||||
os.system(f'pandoc {in_filename} --to html --mathjax --output {out_filename}')
|
||||
print('Creating lock file')
|
||||
os.system(f'echo {hash_file_sha512(in_filename)} > {out_filename}.lock')
|
||||
|
||||
|
||||
@app.route(f'/{PANDOC_LINK}/<name>')
|
||||
def get_pandoc_page(name: str):
|
||||
page = get_page(name)
|
||||
if not os.path.exists(page.get_file('render.html')) or not os.path.exists(page.get_file('render.html.lock')):
|
||||
print(f'Rendered page or lockfile for {name} does not exist! Rendering {name}')
|
||||
render_page(page)
|
||||
else:
|
||||
with open(page.get_file('render.html.lock'), 'r') as f:
|
||||
rendered_hash = f.read().strip()
|
||||
current_hash = hash_file_sha512(page.get_file('main.md'))
|
||||
if rendered_hash != current_hash:
|
||||
print(f'CURRENT: {current_hash}, RENDERED: {rendered_hash}')
|
||||
print(f'Integrity test failed, rendering {name}!')
|
||||
render_page(page)
|
||||
template = page.template
|
||||
with open(page.get_file('render.html'), 'r') as f:
|
||||
inserted = f.read()
|
||||
|
@ -1,17 +0,0 @@
|
||||
import hashlib
|
||||
|
||||
|
||||
BUF_SZ = 4096
|
||||
|
||||
|
||||
def hash_file_sha512(filename):
|
||||
res = hashlib.sha512()
|
||||
with open(filename, 'rb') as f:
|
||||
data = f.read(BUF_SZ)
|
||||
while data:
|
||||
res.update(data)
|
||||
data = f.read(BUF_SZ)
|
||||
return res.hexdigest()
|
||||
|
||||
|
||||
|
1
app/templates/no_template.html
Normal file
1
app/templates/no_template.html
Normal file
@ -0,0 +1 @@
|
||||
{{markdown|safe}}
|
Reference in New Issue
Block a user