from flask import Blueprint, request, jsonify import difflib from util.logger import logger from auth.token import verify_token textdiff_blueprint = Blueprint('textdiff_tool', __name__) @textdiff_blueprint.route('/api/text/diff', methods=['POST']) def text_diff(): user = verify_token() if not user: return jsonify({"message": "Nicht autorisiert"}), 401 try: data = request.get_json() text1 = data.get("text1", "") text2 = data.get("text2", "") lines1 = text1.splitlines(keepends=True) lines2 = text2.splitlines(keepends=True) result = [] for line in difflib.unified_diff(lines1, lines2, lineterm=''): if line.startswith('+++') or line.startswith('---') or line.startswith('@@'): continue if line.startswith('+'): result.append({"type": "added", "text": line[1:]}) elif line.startswith('-'): result.append({"type": "removed", "text": line[1:]}) else: result.append({"type": "unchanged", "text": line[1:] if line.startswith(' ') else line}) logger.info(f"Text-Diff erstellt von {user['username']}") return jsonify({"diff": result}) except Exception as e: logger.error(f"Fehler Text-Diff: {e}") return jsonify({"message": "Fehler beim Vergleich"}), 500