From e637b702ff9d6955e830fe96b94bf5313e9f2703 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 23 Jul 2024 21:29:30 -0700 Subject: [PATCH] UI: make text between quotes colored in chat mode --- css/main.css | 12 ++++++++++++ modules/chat.py | 2 +- modules/html_generator.py | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/css/main.css b/css/main.css index d8e12e59..6f2a9fb7 100644 --- a/css/main.css +++ b/css/main.css @@ -406,6 +406,18 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* { color: var(--body-text-color); } +.message q { + color: #707070; +} + +.dark .message q { + color: orange; +} + +.message q::before, .message q::after { + content: ""; +} + .message-body li { list-style-position: outside; } diff --git a/modules/chat.py b/modules/chat.py index c95673ce..9919cb76 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -488,7 +488,7 @@ def start_new_chat(state): greeting = replace_character_names(state['greeting'], state['name1'], state['name2']) if greeting != '': history['internal'] += [['<|BEGIN-VISIBLE-CHAT|>', greeting]] - history['visible'] += [['', apply_extensions('output', greeting, state, is_chat=True)]] + history['visible'] += [['', apply_extensions('output', html.escape(greeting), state, is_chat=True)]] unique_id = datetime.now().strftime('%Y%m%d-%H-%M-%S') save_history(history, unique_id, state['character_menu'], state['mode']) diff --git a/modules/html_generator.py b/modules/html_generator.py index 657133bd..61e61b0f 100644 --- a/modules/html_generator.py +++ b/modules/html_generator.py @@ -42,6 +42,29 @@ def fix_newlines(string): return string +def replace_quotes(text): + + # Define a list of quote pairs (opening and closing), using HTML entities + quote_pairs = [ + ('"', '"'), # Double quotes + ('“', '”'), # Unicode left and right double quotation marks + ('‘', '’'), # Unicode left and right single quotation marks + ('«', '»'), # French quotes + ('„', '“'), # German quotes + ('‘', '’'), # Alternative single quotes + ('“', '”'), # Unicode quotes (numeric entities) + ('“', '”'), # Unicode quotes (hex entities) + ] + + # Create a regex pattern that matches any of the quote pairs, including newlines + pattern = '|'.join(f'({re.escape(open_q)})(.*?)({re.escape(close_q)})' for open_q, close_q in quote_pairs) + + # Replace matched patterns with tags, keeping original quotes + replaced_text = re.sub(pattern, lambda m: f'{m.group(1)}{m.group(2)}{m.group(3)}', text, flags=re.DOTALL) + + return replaced_text + + def replace_blockquote(m): return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '') @@ -49,6 +72,9 @@ def replace_blockquote(m): @functools.lru_cache(maxsize=4096) def convert_to_markdown(string): + # Quote to + string = replace_quotes(string) + # Blockquote string = re.sub(r'(^|[\n])>', r'\1>', string) pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL)