import html import os import re import time from pathlib import Path import markdown from PIL import Image, ImageOps from modules.logging_colors import logger from modules.utils import get_available_chat_styles # This is to store the paths to the thumbnails of the profile pictures image_cache = {} with open(Path(__file__).resolve().parent / '../css/html_readable_style.css', 'r') as f: readable_css = f.read() with open(Path(__file__).resolve().parent / '../css/html_4chan_style.css', 'r') as css_f: _4chan_css = css_f.read() with open(Path(__file__).resolve().parent / '../css/html_instruct_style.css', 'r') as f: instruct_css = f.read() # Custom chat styles chat_styles = {} for k in get_available_chat_styles(): chat_styles[k] = open(Path(f'css/chat_style-{k}.css'), 'r').read() def fix_newlines(string): string = string.replace('\n', '\n\n') string = re.sub(r"\n{3,}", "\n\n", string) string = string.strip() return string def replace_blockquote(m): return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '') def convert_to_markdown(string): # Blockquote pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL) string = pattern.sub(replace_blockquote, string) # Code string = string.replace('\\begin{code}', '```') string = string.replace('\\end{code}', '```') string = re.sub(r"(.)```", r"\1\n```", string) result = '' is_code = False for line in string.split('\n'): if line.lstrip(' ').startswith('```'): is_code = not is_code result += line if is_code or line.startswith('|'): # Don't add an extra \n for tables or code result += '\n' else: result += '\n\n' if is_code: result = result + '```' # Unfinished code block result = result.strip() # Unfinished list, like "\n1.". A |delete| string is added and then # removed to force a
.
if re.search(r'(\d+\.?)$', result):
delete_str = '|delete|'
if not result.endswith('.'):
result += '.'
result = re.sub(r'(\d+\.)$', r'\g<1> ' + delete_str, result)
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])
pos = html_output.rfind(delete_str)
if pos > -1:
html_output = html_output[:pos] + html_output[pos + len(delete_str):]
else:
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])
# Unescape code blocks
pattern = re.compile(r']*>(.*?)
', re.DOTALL)
html_output = pattern.sub(lambda x: html.unescape(x.group()), html_output)
return html_output
def generate_basic_html(string):
string = convert_to_markdown(string)
string = f'