diff --git a/diff/main.py b/diff/main.py new file mode 100644 index 0000000..4abfac9 --- /dev/null +++ b/diff/main.py @@ -0,0 +1,289 @@ +from flask import Flask, request, render_template_string +import difflib +import argparse + +app = Flask(__name__) + +HTML_TEMPLATE = ''' + + + + + Enhanced Diff Tool + + + + +
+
+

Diff Tool

+
🌓 Toggle Theme
+
+ +
+
+
+

Original Content

+ + +
+ +
+

Modified Content

+ + +
+
+ +
+ + {% if diff_html %} + + {% endif %} +
+
+ + {% if message %} +
{{ message }}
+ {% endif %} + + {% if diff_html %} +
+ {{ diff_html|safe }} +
+ {% endif %} +
+ + + + +''' + +@app.route('/', methods=['GET']) +def index(): + return render_template_string(HTML_TEMPLATE, + text1=request.args.get('text1', ''), + text2=request.args.get('text2', '')) + +@app.route('/diff', methods=['POST']) +def diff_files(): + # Handle file uploads and text inputs + file1 = request.files.get('file1') + file2 = request.files.get('file2') + text1 = request.form.get('text1', '').strip() + text2 = request.form.get('text2', '').strip() + + # Validate input + if (not file1 and not text1) or (not file2 and not text2): + return render_template_string(HTML_TEMPLATE, + message="Please provide content for both sides to compare.", + text1=text1, + text2=text2) + + try: + # Process first content + if file1 and file1.filename: + content1 = file1.read().decode('utf-8').splitlines() + filename1 = file1.filename + else: + content1 = text1.splitlines() + filename1 = "Original Content" + + # Process second content + if file2 and file2.filename: + content2 = file2.read().decode('utf-8').splitlines() + filename2 = file2.filename + else: + content2 = text2.splitlines() + filename2 = "Modified Content" + + except UnicodeDecodeError: + return render_template_string(HTML_TEMPLATE, + message="Error: Only UTF-8 encoded text files are supported.", + text1=text1, + text2=text2) + + # Generate diff + differ = difflib.HtmlDiff( + tabsize=4, + wrapcolumn=80, + linejunk=None, + charjunk=difflib.IS_CHARACTER_JUNK + ) + + diff_html = differ.make_file( + content1, + content2, + fromdesc=filename1, + todesc=filename2, + context=True, + numlines=3 + ) + + return render_template_string(HTML_TEMPLATE, + text1=text1, + text2=text2, + diff_html=diff_html) + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description="Run SSL Certificate Decoder") + parser.add_argument('--port', type=int, default=5000, help='Port to run the service on') + parser.add_argument('--host', type=str, default='127.0.0.1', help='Host to run the service on') + args = parser.parse_args() + app.run(debug=True, port=args.port, host=args.host) diff --git a/diff/requirements.txt b/diff/requirements.txt new file mode 100644 index 0000000..54cbde4 --- /dev/null +++ b/diff/requirements.txt @@ -0,0 +1,14 @@ +astroid==3.3.10 +blinker==1.9.0 +click==8.2.0 +dill==0.4.0 +Flask==3.1.1 +isort==6.0.1 +itsdangerous==2.2.0 +Jinja2==3.1.6 +MarkupSafe==3.0.2 +mccabe==0.7.0 +platformdirs==4.3.8 +pylint==3.3.7 +tomlkit==0.13.2 +Werkzeug==3.1.3