html_generator.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. '''
  2. This is a library for formatting GPT-4chan and chat outputs as nice HTML.
  3. '''
  4. import re
  5. from pathlib import Path
  6. def generate_basic_html(s):
  7. s = '\n'.join([f'<p style="margin-bottom: 20px">{line}</p>' for line in s.split('\n')])
  8. s = f'<div style="max-width: 600px; margin-left: auto; margin-right: auto; background-color:#efefef; color:#0b0f19; padding:3em; font-size:1.1em; font-family: helvetica">{s}</div>'
  9. return s
  10. def process_post(post, c):
  11. t = post.split('\n')
  12. number = t[0].split(' ')[1]
  13. if len(t) > 1:
  14. src = '\n'.join(t[1:])
  15. else:
  16. src = ''
  17. src = re.sub('>', '&gt;', src)
  18. src = re.sub('(&gt;&gt;[0-9]*)', '<span class="quote">\\1</span>', src)
  19. src = re.sub('\n', '<br>\n', src)
  20. src = f'<blockquote class="message">{src}\n'
  21. src = f'<span class="name">Anonymous </span> <span class="number">No.{number}</span>\n{src}'
  22. return src
  23. def generate_4chan_html(f):
  24. css = """
  25. #container {
  26. background-color: #eef2ff;
  27. padding: 17px;
  28. }
  29. .reply {
  30. background-color: rgb(214, 218, 240);
  31. border-bottom-color: rgb(183, 197, 217);
  32. border-bottom-style: solid;
  33. border-bottom-width: 1px;
  34. border-image-outset: 0;
  35. border-image-repeat: stretch;
  36. border-image-slice: 100%;
  37. border-image-source: none;
  38. border-image-width: 1;
  39. border-left-color: rgb(0, 0, 0);
  40. border-left-style: none;
  41. border-left-width: 0px;
  42. border-right-color: rgb(183, 197, 217);
  43. border-right-style: solid;
  44. border-right-width: 1px;
  45. border-top-color: rgb(0, 0, 0);
  46. border-top-style: none;
  47. border-top-width: 0px;
  48. color: rgb(0, 0, 0);
  49. display: table;
  50. font-family: arial, helvetica, sans-serif;
  51. font-size: 13.3333px;
  52. margin-bottom: 4px;
  53. margin-left: 0px;
  54. margin-right: 0px;
  55. margin-top: 4px;
  56. overflow-x: hidden;
  57. overflow-y: hidden;
  58. padding-bottom: 2px;
  59. padding-left: 2px;
  60. padding-right: 2px;
  61. padding-top: 2px;
  62. }
  63. .number {
  64. color: rgb(0, 0, 0);
  65. font-family: arial, helvetica, sans-serif;
  66. font-size: 13.3333px;
  67. width: 342.65px;
  68. }
  69. .op {
  70. color: rgb(0, 0, 0);
  71. font-family: arial, helvetica, sans-serif;
  72. font-size: 13.3333px;
  73. margin-bottom: 8px;
  74. margin-left: 0px;
  75. margin-right: 0px;
  76. margin-top: 4px;
  77. overflow-x: hidden;
  78. overflow-y: hidden;
  79. }
  80. .op blockquote {
  81. margin-left:7px;
  82. }
  83. .name {
  84. color: rgb(17, 119, 67);
  85. font-family: arial, helvetica, sans-serif;
  86. font-size: 13.3333px;
  87. font-weight: 700;
  88. margin-left: 7px;
  89. }
  90. .quote {
  91. color: rgb(221, 0, 0);
  92. font-family: arial, helvetica, sans-serif;
  93. font-size: 13.3333px;
  94. text-decoration-color: rgb(221, 0, 0);
  95. text-decoration-line: underline;
  96. text-decoration-style: solid;
  97. text-decoration-thickness: auto;
  98. }
  99. .greentext {
  100. color: rgb(120, 153, 34);
  101. font-family: arial, helvetica, sans-serif;
  102. font-size: 13.3333px;
  103. }
  104. blockquote {
  105. margin-block-start: 1em;
  106. margin-block-end: 1em;
  107. margin-inline-start: 40px;
  108. margin-inline-end: 40px;
  109. }
  110. """
  111. posts = []
  112. post = ''
  113. c = -2
  114. for line in f.splitlines():
  115. line += "\n"
  116. if line == '-----\n':
  117. continue
  118. elif line.startswith('--- '):
  119. c += 1
  120. if post != '':
  121. src = process_post(post, c)
  122. posts.append(src)
  123. post = line
  124. else:
  125. post += line
  126. if post != '':
  127. src = process_post(post, c)
  128. posts.append(src)
  129. for i in range(len(posts)):
  130. if i == 0:
  131. posts[i] = f'<div class="op">{posts[i]}</div>\n'
  132. else:
  133. posts[i] = f'<div class="reply">{posts[i]}</div>\n'
  134. output = ''
  135. output += f'<style>{css}</style><div id="container">'
  136. for post in posts:
  137. output += post
  138. output += '</div>'
  139. output = output.split('\n')
  140. for i in range(len(output)):
  141. output[i] = re.sub(r'^(&gt;(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
  142. output[i] = re.sub(r'^<blockquote class="message">(&gt;(.*?)(<br>|</div>))', r'<blockquote class="message"><span class="greentext">\1</span>', output[i])
  143. output = '\n'.join(output)
  144. return output
  145. def generate_chat_html(history, name1, name2, character):
  146. css = """
  147. .chat {
  148. margin-left: auto;
  149. margin-right: auto;
  150. max-width: 800px;
  151. height: 66.67vh;
  152. overflow-y: auto;
  153. padding-right: 20px;
  154. display: flex;
  155. flex-direction: column-reverse;
  156. }
  157. .message {
  158. display: grid;
  159. grid-template-columns: 50px 1fr;
  160. padding-bottom: 20px;
  161. font-size: 15px;
  162. font-family: helvetica;
  163. }
  164. .circle-you {
  165. width: 45px;
  166. height: 45px;
  167. background-color: rgb(244, 78, 59);
  168. border-radius: 50%;
  169. }
  170. .circle-bot {
  171. width: 45px;
  172. height: 45px;
  173. background-color: rgb(59, 78, 244);
  174. border-radius: 50%;
  175. }
  176. .circle-bot img {
  177. border-radius: 50%;
  178. width: 100%;
  179. height: 100%;
  180. object-fit: cover;
  181. }
  182. .text {
  183. }
  184. .text p {
  185. margin-top: 5px;
  186. }
  187. .username {
  188. font-weight: bold;
  189. }
  190. .body {
  191. }
  192. """
  193. output = ''
  194. output += f'<style>{css}</style><div class="chat" id="chat">'
  195. img = ''
  196. for i in [
  197. f"characters/{character}.png",
  198. f"characters/{character}.jpg",
  199. f"characters/{character}.jpeg",
  200. "profile.png",
  201. "profile.jpg",
  202. "profile.jpeg",
  203. ]:
  204. if Path(i).exists():
  205. img = f'<img src="file/{i}">'
  206. break
  207. for i,_row in enumerate(history[::-1]):
  208. row = _row.copy()
  209. row[0] = re.sub(r"[\\]*\*", r"*", row[0])
  210. row[1] = re.sub(r"[\\]*\*", r"*", row[1])
  211. row[0] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[0])
  212. row[1] = re.sub(r"(\*)([^\*\n]*)(\*)", r"<em>\2</em>", row[1])
  213. p = '\n'.join([f"<p>{x}</p>" for x in row[1].split('\n')])
  214. output += f"""
  215. <div class="message">
  216. <div class="circle-bot">
  217. {img}
  218. </div>
  219. <div class="text">
  220. <div class="username">
  221. {name2}
  222. </div>
  223. <div class="body">
  224. {p}
  225. </div>
  226. </div>
  227. </div>
  228. """
  229. if not (i == len(history)-1 and len(row[0]) == 0):
  230. p = '\n'.join([f"<p>{x}</p>" for x in row[0].split('\n')])
  231. output += f"""
  232. <div class="message">
  233. <div class="circle-you">
  234. </div>
  235. <div class="text">
  236. <div class="username">
  237. {name1}
  238. </div>
  239. <div class="body">
  240. {p}
  241. </div>
  242. </div>
  243. </div>
  244. """
  245. output += "</div>"
  246. return output