From 85073b161e2e00782939306b2b517561d9bf71d0 Mon Sep 17 00:00:00 2001 From: thematdev Date: Fri, 27 Jan 2023 17:31:58 +0300 Subject: [PATCH] Stage1 of migration --- app/__init__.py | 28 +++++----------------------- app/hash_manager.py | 17 ----------------- app/templates/no_template.html | 1 + 3 files changed, 6 insertions(+), 40 deletions(-) delete mode 100644 app/hash_manager.py create mode 100644 app/templates/no_template.html diff --git a/app/__init__.py b/app/__init__.py index 29dfc16..d8c81a6 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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}/') 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() diff --git a/app/hash_manager.py b/app/hash_manager.py deleted file mode 100644 index 0f3acd7..0000000 --- a/app/hash_manager.py +++ /dev/null @@ -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() - - - diff --git a/app/templates/no_template.html b/app/templates/no_template.html new file mode 100644 index 0000000..eab8cf8 --- /dev/null +++ b/app/templates/no_template.html @@ -0,0 +1 @@ +{{markdown|safe}}