From 9745f6130da99732d57f0a75dbce6078dfc96075 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sat, 3 Jan 2026 11:55:35 -0800 Subject: [PATCH] [BACKEND] Simplify script quote generation --- src/ytdl_sub/utils/script.py | 15 ++++++++++++++- tests/unit/utils/test_script_utils.py | 26 +++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/ytdl_sub/utils/script.py b/src/ytdl_sub/utils/script.py index 027a0bd8..753916f4 100644 --- a/src/ytdl_sub/utils/script.py +++ b/src/ytdl_sub/utils/script.py @@ -113,7 +113,20 @@ class ScriptUtils: if isinstance(arg, String): if arg.native == "": return "" if top_level else "''" - return arg.native if top_level else f"'''{arg.native}'''" + + contains_single_quote = "'" in arg.native + contains_double_quote = '"' in arg.native + + if not contains_single_quote and not contains_double_quote: + quote = '"' + elif not contains_single_quote and contains_double_quote: + quote = "'" + elif contains_single_quote and not contains_double_quote: + quote = '"' + else: + quote = "'''" + + return arg.native if top_level else f"{quote}{arg.native}{quote}" if isinstance(arg, Integer): out = f"%int({arg.native})" diff --git a/tests/unit/utils/test_script_utils.py b/tests/unit/utils/test_script_utils.py index f9d9b395..608066b5 100644 --- a/tests/unit/utils/test_script_utils.py +++ b/tests/unit/utils/test_script_utils.py @@ -18,6 +18,9 @@ class TestScriptUtils: "string": "value", "quotes": "has '' and \"\"", "triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''", + "has-double-quotes": 'i got "some double quotes" in here', + "has-single-quotes": "i got 'some single quotes' in here", + "has-both-quotes": "i got 'both quotes\" in here", "int": 1, "bool": True, "list": [1, 2, 3], @@ -60,7 +63,15 @@ class TestScriptUtils: def test_to_syntax_tree(self): out = ScriptUtils.to_native_script( - {"{var_a}": "{var_b}", "static_a": "string with {var_c} in it"} + { + "{var_a}": "{var_b}", + "static_a": "string with {var_c} in it", + "quotes": "has '' and \"\"", + "triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''", + "has-double-quotes": 'i got "some double quotes" in here', + "has-single-quotes": "i got 'some single quotes' in here", + "has-both-quotes": "i got 'both quotes\" in here", + } ) assert parse(out) == SyntaxTree( ast=[ @@ -75,6 +86,19 @@ class TestScriptUtils: BuiltInFunction(name="string", args=[String(value=" in it")]), ], ), + String(value="quotes"): String(value="has '' and \"\""), + String(value="triple-single-quote"): String( + value="right here! '''''''''''''''''''''''''''''' ack '''''''" + ), + String(value="has-double-quotes"): String( + value='i got "some double quotes" in here' + ), + String(value="has-single-quotes"): String( + value="i got 'some single quotes' in here" + ), + String(value="has-both-quotes"): String( + value="i got 'both quotes\" in here" + ), } ) ]