|
1 | 1 | '''
|
2 | 2 |
|
3 |
| -This is a library for formatting GPT-4chan and chat outputs as nice HTML. |
| 3 | +This is a library for formatting text outputs as nice HTML. |
4 | 4 |
|
5 | 5 | '''
|
6 | 6 |
|
|
21 | 21 | with open(Path(__file__).resolve().parent / '../css/html_cai_style.css', 'r') as f:
|
22 | 22 | cai_css = f.read()
|
23 | 23 |
|
24 |
| -def generate_basic_html(s): |
25 |
| - s = '\n'.join([f'<p>{line}</p>' for line in s.split('\n')]) |
26 |
| - s = f'<style>{readable_css}</style><div class="container">{s}</div>' |
27 |
| - return s |
| 24 | +def fix_newlines(string): |
| 25 | + string = string.replace('\n', '\n\n') |
| 26 | + string = re.sub(r"\n{3,}", "\n\n", string) |
| 27 | + string = string.strip() |
| 28 | + return string |
| 29 | + |
| 30 | +# This could probably be generalized and improved |
| 31 | +def convert_to_markdown(string): |
| 32 | + string = string.replace('\\begin{code}', '```') |
| 33 | + string = string.replace('\\end{code}', '```') |
| 34 | + string = string.replace('\\begin{blockquote}', '> ') |
| 35 | + string = string.replace('\\end{blockquote}', '') |
| 36 | + string = re.sub(r"(.)```", r"\1\n```", string) |
| 37 | +# string = fix_newlines(string) |
| 38 | + return markdown.markdown(string, extensions=['fenced_code']) |
| 39 | + |
| 40 | +def generate_basic_html(string): |
| 41 | + string = convert_to_markdown(string) |
| 42 | + string = f'<style>{readable_css}</style><div class="container">{string}</div>' |
| 43 | + return string |
28 | 44 |
|
29 | 45 | def process_post(post, c):
|
30 | 46 | t = post.split('\n')
|
@@ -108,7 +124,7 @@ def generate_chat_html(history, name1, name2, character):
|
108 | 124 | img_me = load_html_image(["img_me.png", "img_me.jpg", "img_me.jpeg"])
|
109 | 125 |
|
110 | 126 | for i,_row in enumerate(history[::-1]):
|
111 |
| - row = [markdown.markdown(re.sub(r"(.)```", r"\1\n```", entry), extensions=['fenced_code']) for entry in _row] |
| 127 | + row = [convert_to_markdown(entry) for entry in _row] |
112 | 128 |
|
113 | 129 | output += f"""
|
114 | 130 | <div class="message">
|
|
0 commit comments