Skip to content

Commit

Permalink
[FEATURE] Support newlines and tabs in scripting strings (#1105)
Browse files Browse the repository at this point in the history
Allows the usage of `\n` and `\t` in scripting strings
  • Loading branch information
jmbannon authored Oct 27, 2024
1 parent 1bdc65f commit 80054aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/ytdl_sub/script/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,16 @@ def _parse_string(self, str_open_token: str) -> String:
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

Expand Down
1 change: 1 addition & 0 deletions tests/unit/script/functions/test_string_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_contains_any(self, value, expected_output):
("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(
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/script/types/test_string.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,10 +46,13 @@ def test_string_not_arg(self, string: str):
("{%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("")})
Expand Down

0 comments on commit 80054aa

Please sign in to comment.