[FEATURE] Support newlines and tabs in scripting strings (#1105)

Allows the usage of `\n` and `\t` in scripting strings
This commit is contained in:
Jesse Bannon 2024-10-27 08:09:05 -07:00 committed by GitHub
parent 1bdc65f2e1
commit 80054aa77b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 3 deletions

View file

@ -301,8 +301,16 @@ class _Parser:
self._pos += len(str_open_token)
return String(value=string_value)
self._pos += 1
string_value += ch
# Read literal "\n" as newlines
if self._read(increment_pos=False, length=2) == "\\n":
string_value += "\n"
self._pos += 2
elif self._read(increment_pos=False, length=2) == "\\t":
string_value += "\t"
self._pos += 2
else:
self._pos += 1
string_value += ch
raise STRINGS_NOT_CLOSED

View file

@ -132,6 +132,7 @@ class TestNumericFunctions:
("no splits", " | ", None, ["no splits"]),
("one | split", " | ", None, ["one", "split"]),
("max | split | one", " | ", 1, ["max", "split | one"]),
("multiline\ndescription", "\\n", None, ["multiline", "description"]),
],
)
def test_split(

View file

@ -1,6 +1,7 @@
import re
import pytest
from unit.script.conftest import single_variable_output
from ytdl_sub.script.parser import STRINGS_NOT_CLOSED
from ytdl_sub.script.parser import STRINGS_ONLY_ARGS
@ -45,10 +46,13 @@ class TestString:
("{%string('backslash \\\\')}", "backslash \\\\"),
("{%string('''triple quote with \" ' \\''')}", "triple quote with \" ' \\"),
('{%string("""triple quote with " \' \\""")}', "triple quote with \" ' \\"),
("{%string('literal \\n newlines')}", "literal \n newlines"),
("{%string('supports \t tabs')}", "supports \t tabs"),
("{%string('literal \\t tabs')}", "literal \t tabs"),
],
)
def test_string(self, string: str, expected_string: str):
assert Script({"out": string}).resolve() == ScriptOutput({"out": String(expected_string)})
assert single_variable_output(string) == expected_string
def test_null_is_empty_string(self):
assert Script({"out": "{%string(null)}"}).resolve() == ScriptOutput({"out": String("")})