From 7e445b3a685b2ded2f2b06c89455c27eb773bcc8 Mon Sep 17 00:00:00 2001 From: Nicolas Le Cam Date: Sun, 12 Oct 2025 03:26:13 +0200 Subject: [PATCH] enh(python) Support t-strings Fixes #4325 --- CHANGES.md | 1 + src/languages/python.js | 8 ++++---- test/markup/python/template-strings.expect.txt | 10 ++++++++++ test/markup/python/template-strings.txt | 10 ++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 test/markup/python/template-strings.expect.txt create mode 100644 test/markup/python/template-strings.txt diff --git a/CHANGES.md b/CHANGES.md index b1413c29a5..cb84f39877 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,7 @@ Core Grammars: - enh(json) add json5 support [Kerry Shetline][] - fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][] - fix(csharp) Support digit separators [te-ing][] +- enh(python) Support t-strings [Nicolas Le Cam][] Documentation: diff --git a/src/languages/python.js b/src/languages/python.js index 2604889790..ab7930c030 100644 --- a/src/languages/python.js +++ b/src/languages/python.js @@ -194,7 +194,7 @@ export default function(hljs) { relevance: 10 }, { - begin: /([fF][rR]|[rR][fF]|[fF])'''/, + begin: /([fFtT][rR]|[rR][fFtT]|[fFtT])'''/, end: /'''/, contains: [ hljs.BACKSLASH_ESCAPE, @@ -204,7 +204,7 @@ export default function(hljs) { ] }, { - begin: /([fF][rR]|[rR][fF]|[fF])"""/, + begin: /([fFtT][rR]|[rR][fFtT]|[fFtT])"""/, end: /"""/, contains: [ hljs.BACKSLASH_ESCAPE, @@ -232,7 +232,7 @@ export default function(hljs) { end: /"/ }, { - begin: /([fF][rR]|[rR][fF]|[fF])'/, + begin: /([fFtT][rR]|[rR][fFtT]|[fFtT])'/, end: /'/, contains: [ hljs.BACKSLASH_ESCAPE, @@ -241,7 +241,7 @@ export default function(hljs) { ] }, { - begin: /([fF][rR]|[rR][fF]|[fF])"/, + begin: /([fFtT][rR]|[rR][fFtT]|[fFtT])"/, end: /"/, contains: [ hljs.BACKSLASH_ESCAPE, diff --git a/test/markup/python/template-strings.expect.txt b/test/markup/python/template-strings.expect.txt new file mode 100644 index 0000000000..ea18264ea5 --- /dev/null +++ b/test/markup/python/template-strings.expect.txt @@ -0,0 +1,10 @@ +t"Hello {name}" +T"Hello {name}" +t"Hello {name!r}" +t"Value: {value:.2f}" +t"Value: {value:.{precision}f}" +t"Hello {name=}" +rt'Did you say "{trade}"?\n' +tr'Did you say "{trade}"?\n' +t"Hello {(lambda: name)}" +t"Hello {await get_name()}" diff --git a/test/markup/python/template-strings.txt b/test/markup/python/template-strings.txt new file mode 100644 index 0000000000..1519e8d80b --- /dev/null +++ b/test/markup/python/template-strings.txt @@ -0,0 +1,10 @@ +t"Hello {name}" +T"Hello {name}" +t"Hello {name!r}" +t"Value: {value:.2f}" +t"Value: {value:.{precision}f}" +t"Hello {name=}" +rt'Did you say "{trade}"?\n' +tr'Did you say "{trade}"?\n' +t"Hello {(lambda: name)}" +t"Hello {await get_name()}"