From dd54f5fe33272c7707ccb28c55e92b30413a51aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Mon, 13 Apr 2026 23:26:11 +0300 Subject: [PATCH] new babycode format for new style --- app/__init__.py | 43 ++++++++++++++++++++++--------------------- app/lib/babycode.py | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 244f06a..778b53c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -196,6 +196,19 @@ def create_app(): cache.init_app(app) + 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 + from app.routes.users import bp as users_bp + from app.routes.guides import bp as guides_bp + from app.routes.mod import bp as mod_bp + app.register_blueprint(app_bp) + app.register_blueprint(topics_bp) + app.register_blueprint(threads_bp) + app.register_blueprint(users_bp) + app.register_blueprint(guides_bp) + app.register_blueprint(mod_bp) + with app.app_context(): from .schema import create as create_tables from .migrations import run_migrations @@ -222,19 +235,6 @@ def create_app(): with open('.git/refs/heads/main') as f: commit = f.read().strip() - 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 - from app.routes.users import bp as users_bp - from app.routes.guides import bp as guides_bp - from app.routes.mod import bp as mod_bp - app.register_blueprint(app_bp) - app.register_blueprint(topics_bp) - app.register_blueprint(threads_bp) - app.register_blueprint(users_bp) - app.register_blueprint(guides_bp) - app.register_blueprint(mod_bp) - @app.context_processor def inject_constants(): return { @@ -254,6 +254,7 @@ def create_app(): 'get_motds': MOTD.get_all, 'get_time_now': lambda: int(time.time()), 'is_logged_in': is_logged_in, + 'is_mod': lambda: is_logged_in() and get_active_user().is_mod(), 'get_active_user': get_active_user, 'get_post_url': get_post_url, } @@ -289,14 +290,14 @@ def create_app(): def basename_noext(subj): return os.path.splitext(os.path.basename(subj))[0] - # @app.errorhandler(404) - # def _handle_404(e): - # if request.path.startswith('/hyperapi/'): - # return '

not found

', e.code - # elif request.path.startswith('/api/'): - # return {'error': 'not found'}, e.code - # else: - # return render_template('common/404.html'), e.code + @app.errorhandler(404) + def _handle_404(e): + if request.path.startswith('/hyperapi/'): + return '

not found

', e.code + elif request.path.startswith('/api/'): + return {'error': 'not found'}, e.code + else: + return render_template('common/404.html'), e.code # # @app.errorhandler(413) # def _handle_413(e): diff --git a/app/lib/babycode.py b/app/lib/babycode.py index e760aac..85640c8 100644 --- a/app/lib/babycode.py +++ b/app/lib/babycode.py @@ -6,7 +6,7 @@ from pygments.lexers import get_lexer_by_name from pygments.util import ClassNotFound as PygmentsClassNotFound import re -BABYCODE_VERSION = 8 +BABYCODE_VERSION = 9 class BabycodeError(Exception): @@ -183,7 +183,7 @@ class HTMLRenderer(BabycodeRenderer): if mention_data not in self.mentions: self.mentions.append(mention_data) - return f"{'@' if not target_user.has_display_name() else ''}{target_user.get_readable_name()}" + return f"{'@' if not target_user.has_display_name() else ''}{target_user.get_readable_name()}" def render(self, ast): out = super().render(ast) @@ -201,7 +201,7 @@ class RSSXMLRenderer(BabycodeRenderer): if not target_user: return f"@{e['name']}" - return f'{'@' if not target_user.has_display_name() else ''}{target_user.get_readable_name()}' + return f'{'@' if not target_user.has_display_name() else ''}{target_user.get_readable_name()}' NAMED_COLORS = [ @@ -345,16 +345,21 @@ def tag_code(children, attr): return f"{children}" else: input_code = children.strip() - button = f"" - unhighlighted = f"
code block{button}{input_code}
" - if not attr: - return unhighlighted - try: - lexer = get_lexer_by_name(attr.strip()) - formatter = HtmlFormatter(nowrap=True) - return f"
{lexer.name}{button}{highlight(Markup(input_code).unescape(), lexer, formatter)}
" - except PygmentsClassNotFound: - return unhighlighted + language = 'code block' + if attr: + try: + lexer = get_lexer_by_name(attr.strip()) + formatter = HtmlFormatter(nowrap=True) + language = lexer.name + code = highlight(Markup(input_code).unescape(), lexer, formatter) + except PygmentsClassNotFound: + code = input_code + else: + code = input_code + + button = f'' + block = f'
{language}{button}
{code}
' + return block def tag_list(children): @@ -383,8 +388,8 @@ def tag_color(children, attr): def tag_spoiler(children, attr): spoiler_name = attr if attr else "Spoiler" - content = f"" - container = f"""""" + content = f"" + container = f"""
{spoiler_name}{content}
""" return container @@ -393,6 +398,14 @@ def tag_image(children, attr): return f"
{img}
" +def tag_quote(children, attr): + if attr: + quotee = attr.strip() + else: + quotee = 'Quote' + + return f'
{quotee}
{children}
' + TAGS = { "b": lambda children, attr: f"{children}", "i": lambda children, attr: f"{children}", @@ -401,7 +414,7 @@ TAGS = { "img": tag_image, "url": lambda children, attr: f"{children}", - "quote": lambda children, attr: f"
{children}
", + "quote": tag_quote, "code": tag_code, "ul": lambda children, attr: f"", "ol": lambda children, attr: f"
    {tag_list(children)}
",