html_generator.py 6.7 KB

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