2023-01-06 21:14:08 -05:00
|
|
|
'''
|
|
|
|
|
2023-03-17 15:06:11 -04:00
|
|
|
This is a library for formatting text outputs as nice HTML.
|
2023-01-06 21:14:08 -05:00
|
|
|
|
|
|
|
'''
|
2023-02-23 12:41:42 -05:00
|
|
|
|
2023-02-17 12:30:39 -05:00
|
|
|
import os
|
2023-01-06 21:14:08 -05:00
|
|
|
import re
|
2023-01-15 10:20:04 -05:00
|
|
|
from pathlib import Path
|
2023-01-06 21:14:08 -05:00
|
|
|
|
2023-03-15 11:33:26 -04:00
|
|
|
import markdown
|
2023-02-17 08:58:54 -05:00
|
|
|
from PIL import Image
|
|
|
|
|
2023-02-27 11:41:00 -05:00
|
|
|
# This is to store the paths to the thumbnails of the profile pictures
|
2023-02-17 12:30:39 -05:00
|
|
|
image_cache = {}
|
|
|
|
|
2023-03-15 13:19:28 -04:00
|
|
|
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()
|
2023-03-15 11:33:26 -04:00
|
|
|
|
2023-03-17 15:06:11 -04:00
|
|
|
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'<style>{readable_css}</style><div class="container">{string}</div>'
|
|
|
|
return string
|
2023-01-15 14:43:31 -05:00
|
|
|
|
2023-01-06 21:14:08 -05:00
|
|
|
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]*)', '<span class="quote">\\1</span>', src)
|
|
|
|
src = re.sub('\n', '<br>\n', src)
|
|
|
|
src = f'<blockquote class="message">{src}\n'
|
|
|
|
src = f'<span class="name">Anonymous </span> <span class="number">No.{number}</span>\n{src}'
|
|
|
|
return src
|
|
|
|
|
2023-01-10 23:10:11 -05:00
|
|
|
def generate_4chan_html(f):
|
2023-01-06 21:14:08 -05:00
|
|
|
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'<div class="op">{posts[i]}</div>\n'
|
|
|
|
else:
|
|
|
|
posts[i] = f'<div class="reply">{posts[i]}</div>\n'
|
|
|
|
|
|
|
|
output = ''
|
2023-03-15 13:19:28 -04:00
|
|
|
output += f'<style>{_4chan_css}</style><div id="parent"><div id="container">'
|
2023-01-06 21:14:08 -05:00
|
|
|
for post in posts:
|
|
|
|
output += post
|
2023-02-17 20:47:41 -05:00
|
|
|
output += '</div></div>'
|
2023-01-06 21:14:08 -05:00
|
|
|
output = output.split('\n')
|
|
|
|
for i in range(len(output)):
|
2023-01-06 23:20:10 -05:00
|
|
|
output[i] = re.sub(r'^(>(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
|
|
|
|
output[i] = re.sub(r'^<blockquote class="message">(>(.*?)(<br>|</div>))', r'<blockquote class="message"><span class="greentext">\1</span>', output[i])
|
2023-01-06 21:14:08 -05:00
|
|
|
output = '\n'.join(output)
|
|
|
|
|
|
|
|
return output
|
2023-01-15 10:20:04 -05:00
|
|
|
|
2023-02-27 11:41:00 -05:00
|
|
|
def get_image_cache(path):
|
|
|
|
cache_folder = Path("cache")
|
|
|
|
if not cache_folder.exists():
|
|
|
|
cache_folder.mkdir()
|
2023-02-17 12:30:39 -05:00
|
|
|
|
2023-02-27 11:41:00 -05:00
|
|
|
mtime = os.stat(path).st_mtime
|
2023-02-17 12:30:39 -05:00
|
|
|
if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache):
|
|
|
|
img = Image.open(path)
|
2023-02-26 10:37:26 -05:00
|
|
|
img.thumbnail((200, 200))
|
2023-02-27 11:41:00 -05:00
|
|
|
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()]
|
2023-02-17 12:30:39 -05:00
|
|
|
|
|
|
|
return image_cache[path][1]
|
2023-02-17 08:58:54 -05:00
|
|
|
|
2023-03-12 15:34:09 -04:00
|
|
|
def load_html_image(paths):
|
|
|
|
for str_path in paths:
|
|
|
|
path = Path(str_path)
|
|
|
|
if path.exists():
|
|
|
|
return f'<img src="file/{get_image_cache(path)}">'
|
|
|
|
return ''
|
2023-01-15 10:20:04 -05:00
|
|
|
|
2023-03-12 15:34:09 -04:00
|
|
|
def generate_chat_html(history, name1, name2, character):
|
2023-03-15 13:19:28 -04:00
|
|
|
output = f'<style>{cai_css}</style><div class="chat" id="chat">'
|
2023-03-24 16:18:27 -04:00
|
|
|
|
2023-03-12 15:34:09 -04:00
|
|
|
img_bot = load_html_image([f"characters/{character}.{ext}" for ext in ['png', 'jpg', 'jpeg']] + ["img_bot.png","img_bot.jpg","img_bot.jpeg"])
|
|
|
|
img_me = load_html_image(["img_me.png", "img_me.jpg", "img_me.jpeg"])
|
2023-01-25 17:37:44 -05:00
|
|
|
|
2023-01-19 23:59:51 -05:00
|
|
|
for i,_row in enumerate(history[::-1]):
|
2023-03-17 15:06:11 -04:00
|
|
|
row = [convert_to_markdown(entry) for entry in _row]
|
2023-03-24 16:18:27 -04:00
|
|
|
|
2023-01-15 10:20:04 -05:00
|
|
|
output += f"""
|
|
|
|
<div class="message">
|
|
|
|
<div class="circle-bot">
|
2023-03-12 15:34:09 -04:00
|
|
|
{img_bot}
|
2023-01-15 10:20:04 -05:00
|
|
|
</div>
|
|
|
|
<div class="text">
|
|
|
|
<div class="username">
|
|
|
|
{name2}
|
|
|
|
</div>
|
2023-02-15 18:20:56 -05:00
|
|
|
<div class="message-body">
|
2023-03-12 15:34:09 -04:00
|
|
|
{row[1]}
|
2023-01-15 10:20:04 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
2023-03-24 16:18:27 -04:00
|
|
|
if len(row[0]) == 0: # don't display empty user messages
|
2023-03-20 12:55:57 -04:00
|
|
|
continue
|
2023-03-24 16:18:27 -04:00
|
|
|
|
2023-03-20 12:55:57 -04:00
|
|
|
output += f"""
|
|
|
|
<div class="message">
|
|
|
|
<div class="circle-you">
|
|
|
|
{img_me}
|
|
|
|
</div>
|
|
|
|
<div class="text">
|
|
|
|
<div class="username">
|
|
|
|
{name1}
|
2023-01-15 10:20:04 -05:00
|
|
|
</div>
|
2023-03-20 12:55:57 -04:00
|
|
|
<div class="message-body">
|
|
|
|
{row[0]}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
2023-01-15 10:20:04 -05:00
|
|
|
|
|
|
|
output += "</div>"
|
|
|
|
return output
|