From cf4bf3caa3185bf7e7f6463b702dac6accf67f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sat, 16 Aug 2025 05:28:44 +0300 Subject: [PATCH] add versioning to markup languages and reparse old format version posts --- app/__init__.py | 23 +++++++++++++++++++++-- app/db.py | 4 ++-- app/lib/babycode.py | 4 +++- app/migrations.py | 1 + app/routes/posts.py | 10 +++++++--- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 7b76095..2412e34 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,6 +1,6 @@ from flask import Flask, session from dotenv import load_dotenv -from .models import Avatars, Users +from .models import Avatars, Users, PostHistory, Posts from .auth import digest from .routes.users import is_logged_in, get_active_user from .routes.threads import get_post_url @@ -9,7 +9,7 @@ from .constants import ( InfoboxKind, InfoboxIcons, InfoboxHTMLClass, REACTION_EMOJI, ) -from .lib.babycode import babycode_to_html, EMOJI +from .lib.babycode import babycode_to_html, EMOJI, BABYCODE_VERSION from datetime import datetime import os import time @@ -48,6 +48,23 @@ def create_deleted_user(): "permission": PermissionLevel.SYSTEM.value, }) +def reparse_posts(): + from .db import db + post_histories = PostHistory.findall([ + ('markup_language', '=', 'babycode'), + ('format_version', 'IS NOT', BABYCODE_VERSION) + ]) + if len(post_histories) == 0: + return + print('Re-parsing babycode, this may take a while...') + with db.transaction(): + for ph in post_histories: + ph.update({ + 'content': babycode_to_html(ph['original_markup']), + 'format_version': BABYCODE_VERSION, + }) + print('Re-parsing done.') + def create_app(): app = Flask(__name__) app.config.from_file('../config/pyrom_config.toml', load=tomllib.load, text=False) @@ -76,6 +93,8 @@ def create_app(): create_admin() create_deleted_user() + reparse_posts() + from app.routes.app import bp as app_bp from app.routes.topics import bp as topics_bp from app.routes.threads import bp as threads_bp diff --git a/app/db.py b/app/db.py index b026310..eb6b980 100644 --- a/app/db.py +++ b/app/db.py @@ -202,9 +202,9 @@ class Model: @classmethod - def findall(cls, condition): + def findall(cls, condition, operator='='): rows = db.QueryBuilder(cls.table)\ - .where(condition)\ + .where(condition, operator)\ .all() res = [] for row in rows: diff --git a/app/lib/babycode.py b/app/lib/babycode.py index e9aca96..abfc23b 100644 --- a/app/lib/babycode.py +++ b/app/lib/babycode.py @@ -2,6 +2,8 @@ from .babycode_parser import Parser from markupsafe import escape import re +BABYCODE_VERSION = 1 + NAMED_COLORS = [ 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green', 'lime', 'olive', 'yellow', @@ -146,7 +148,7 @@ def babycode_to_html(s): parser.valid_emotes = EMOJI.keys() elements = parser.parse() - print(elements) + # print(elements) out = "" def fold(element, nobr): if isinstance(element, str): diff --git a/app/migrations.py b/app/migrations.py index 5c3ebae..d733e32 100644 --- a/app/migrations.py +++ b/app/migrations.py @@ -10,6 +10,7 @@ MIGRATIONS = [ migrate_old_avatars, 'DELETE FROM sessions', # delete old lua porom sessions 'ALTER TABLE "users" ADD COLUMN "invited_by" INTEGER REFERENCES users(id)', # invitation system + 'ALTER TABLE "post_history" ADD COLUMN "format_version" INTEGER DEFAULT NULL', ] def run_migrations(): diff --git a/app/routes/posts.py b/app/routes/posts.py index caaa1d3..1d4b637 100644 --- a/app/routes/posts.py +++ b/app/routes/posts.py @@ -2,7 +2,7 @@ from flask import ( Blueprint, redirect, url_for, flash, render_template, request ) from .users import login_required, get_active_user -from ..lib.babycode import babycode_to_html +from ..lib.babycode import babycode_to_html, BABYCODE_VERSION from ..constants import InfoboxKind from ..db import db from ..models import Posts, PostHistory, Threads, Topics @@ -11,6 +11,7 @@ bp = Blueprint("posts", __name__, url_prefix = "/post") def create_post(thread_id, user_id, content, markup_language="babycode"): + parsed_content = babycode_to_html(content) with db.transaction(): post = Posts.create({ "thread_id": thread_id, @@ -20,10 +21,11 @@ def create_post(thread_id, user_id, content, markup_language="babycode"): revision = PostHistory.create({ "post_id": post.id, - "content": babycode_to_html(content), + "content": parsed_content, "is_initial_revision": True, "original_markup": content, "markup_language": markup_language, + "format_version": BABYCODE_VERSION, }) post.update({"current_revision_id": revision.id}) @@ -31,14 +33,16 @@ def create_post(thread_id, user_id, content, markup_language="babycode"): def update_post(post_id, new_content, markup_language='babycode'): + parsed_content = babycode_to_html(content) with db.transaction(): post = Posts.find({'id': post_id}) new_revision = PostHistory.create({ 'post_id': post.id, - 'content': babycode_to_html(new_content), + 'content': parsed_content, 'is_initial_revision': False, 'original_markup': new_content, 'markup_language': markup_language, + 'format_version': BABYCODE_VERSION, }) post.update({'current_revision_id': new_revision.id})