Skip to content

Commit

Permalink
UI: make text between quotes colored in chat mode
Browse files Browse the repository at this point in the history
  • Loading branch information
oobabooga committed Jul 24, 2024
1 parent 98ed6d3 commit e637b70
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
26 changes: 26 additions & 0 deletions modules/html_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,39 @@ def fix_newlines(string):
return string


def replace_quotes(text):

# Define a list of quote pairs (opening and closing), using HTML entities
quote_pairs = [
('&quot;', '&quot;'), # Double quotes
('&ldquo;', '&rdquo;'), # Unicode left and right double quotation marks
('&lsquo;', '&rsquo;'), # Unicode left and right single quotation marks
('&laquo;', '&raquo;'), # French quotes
('&bdquo;', '&ldquo;'), # German quotes
('&lsquo;', '&rsquo;'), # Alternative single quotes
('&#8220;', '&#8221;'), # Unicode quotes (numeric entities)
('&#x201C;', '&#x201D;'), # 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 <q> tags, keeping original quotes
replaced_text = re.sub(pattern, lambda m: f'<q>{m.group(1)}{m.group(2)}{m.group(3)}</q>', text, flags=re.DOTALL)

return replaced_text


def replace_blockquote(m):
return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '')


@functools.lru_cache(maxsize=4096)
def convert_to_markdown(string):

# Quote to <q></q>
string = replace_quotes(string)

# Blockquote
string = re.sub(r'(^|[\n])&gt;', r'\1>', string)
pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL)
Expand Down

0 comments on commit e637b70

Please sign in to comment.