''' This is a library for formatting text outputs as nice HTML. ''' import os import re import time from pathlib import Path import markdown from PIL import Image, ImageOps # 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_cai_style.css', 'r') as f: cai_css = f.read() with open(Path(__file__).resolve().parent / '../css/html_instruct_style.css', 'r') as f: instruct_css = f.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 # This could probably be generalized and improved def convert_to_markdown(string): string = string.replace('\\begin{code}', '```') string = string.replace('\\end{code}', '```') string = string.replace('\\begin{blockquote}', '> ') string = string.replace('\\end{blockquote}', '') string = re.sub(r"(.)```", r"\1\n```", string) string = fix_newlines(string) return markdown.markdown(string, extensions=['fenced_code']) def generate_basic_html(string): string = convert_to_markdown(string) string = f'
{string}
' return string def process_post(post, c): t = post.split('\n') number = t[0].split(' ')[1] if len(t) > 1: src = '\n'.join(t[1:]) else: src = '' src = re.sub('>', '>', src) src = re.sub('(>>[0-9]*)', '\\1', src) src = re.sub('\n', '
\n', src) src = f'
{src}\n' src = f'Anonymous No.{number}\n{src}' return src def generate_4chan_html(f): posts = [] post = '' c = -2 for line in f.splitlines(): line += "\n" if line == '-----\n': continue elif line.startswith('--- '): c += 1 if post != '': src = process_post(post, c) posts.append(src) post = line else: post += line if post != '': src = process_post(post, c) posts.append(src) for i in range(len(posts)): if i == 0: posts[i] = f'
{posts[i]}
\n' else: posts[i] = f'
{posts[i]}
\n' output = '' output += f'
' for post in posts: output += post output += '
' output = output.split('\n') for i in range(len(output)): output[i] = re.sub(r'^(>(.*?)(
|))', r'\1', output[i]) output[i] = re.sub(r'^
(>(.*?)(
|))', r'
\1', output[i]) output = '\n'.join(output) return output def make_thumbnail(image): image = image.resize((350, round(image.size[1] / image.size[0] * 350)), Image.Resampling.LANCZOS) if image.size[1] > 470: image = ImageOps.fit(image, (350, 470), Image.ANTIALIAS) return image def get_image_cache(path): cache_folder = Path("cache") if not cache_folder.exists(): cache_folder.mkdir() mtime = os.stat(path).st_mtime if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache): img = make_thumbnail(Image.open(path)) output_file = Path(f'cache/{path.name}_cache.png') img.convert('RGB').save(output_file, format='PNG') image_cache[path] = [mtime, output_file.as_posix()] return image_cache[path][1] def generate_instruct_html(history): output = f'
' for i, _row in enumerate(history[::-1]): row = [convert_to_markdown(entry) for entry in _row] output += f"""
{row[1]}
""" if len(row[0]) == 0: # don't display empty user messages continue output += f"""
{row[0]}
""" output += "
" return output def generate_cai_chat_html(history, name1, name2, reset_cache=False): output = f'
' # We use ?name2 and ?time.time() to force the browser to reset caches img_bot = f'' if Path("cache/pfp_character.png").exists() else '' img_me = f'' if Path("cache/pfp_me.png").exists() else '' for i, _row in enumerate(history[::-1]): row = [convert_to_markdown(entry) for entry in _row] output += f"""
{img_bot}
{name2}
{row[1]}
""" if len(row[0]) == 0: # don't display empty user messages continue output += f"""
{img_me}
{name1}
{row[0]}
""" output += "
" return output def generate_chat_html(history, name1, name2): return generate_cai_chat_html(history, name1, name2) def chat_html_wrapper(history, name1, name2, mode, reset_cache=False): if mode == "cai-chat": return generate_cai_chat_html(history, name1, name2, reset_cache) elif mode == "chat": return generate_chat_html(history, name1, name2) elif mode == "instruct": return generate_instruct_html(history) else: return ''