html_generator.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. '''
  2. This is a library for formatting gpt4chan outputs as nice HTML.
  3. '''
  4. import re
  5. def process_post(post, c):
  6. t = post.split('\n')
  7. number = t[0].split(' ')[1]
  8. if len(t) > 1:
  9. src = '\n'.join(t[1:])
  10. else:
  11. src = ''
  12. src = re.sub('>', '>', src)
  13. src = re.sub('(&gt;&gt;[0-9]*)', '<span class="quote">\\1</span>', src)
  14. src = re.sub('\n', '<br>\n', src)
  15. src = f'<blockquote class="message">{src}\n'
  16. src = f'<span class="name">Anonymous </span> <span class="number">No.{number}</span>\n{src}'
  17. return src
  18. def generate_4chan_html(f):
  19. css = """
  20. #container {
  21. background-color: #eef2ff;
  22. padding: 17px;
  23. }
  24. .reply {
  25. background-color: rgb(214, 218, 240);
  26. border-bottom-color: rgb(183, 197, 217);
  27. border-bottom-style: solid;
  28. border-bottom-width: 1px;
  29. border-image-outset: 0;
  30. border-image-repeat: stretch;
  31. border-image-slice: 100%;
  32. border-image-source: none;
  33. border-image-width: 1;
  34. border-left-color: rgb(0, 0, 0);
  35. border-left-style: none;
  36. border-left-width: 0px;
  37. border-right-color: rgb(183, 197, 217);
  38. border-right-style: solid;
  39. border-right-width: 1px;
  40. border-top-color: rgb(0, 0, 0);
  41. border-top-style: none;
  42. border-top-width: 0px;
  43. color: rgb(0, 0, 0);
  44. display: table;
  45. font-family: arial, helvetica, sans-serif;
  46. font-size: 13.3333px;
  47. margin-bottom: 4px;
  48. margin-left: 0px;
  49. margin-right: 0px;
  50. margin-top: 4px;
  51. overflow-x: hidden;
  52. overflow-y: hidden;
  53. padding-bottom: 2px;
  54. padding-left: 2px;
  55. padding-right: 2px;
  56. padding-top: 2px;
  57. }
  58. .number {
  59. color: rgb(0, 0, 0);
  60. font-family: arial, helvetica, sans-serif;
  61. font-size: 13.3333px;
  62. width: 342.65px;
  63. }
  64. .op {
  65. color: rgb(0, 0, 0);
  66. font-family: arial, helvetica, sans-serif;
  67. font-size: 13.3333px;
  68. margin-bottom: 8px;
  69. margin-left: 0px;
  70. margin-right: 0px;
  71. margin-top: 4px;
  72. overflow-x: hidden;
  73. overflow-y: hidden;
  74. }
  75. .op blockquote {
  76. margin-left:7px;
  77. }
  78. .name {
  79. color: rgb(17, 119, 67);
  80. font-family: arial, helvetica, sans-serif;
  81. font-size: 13.3333px;
  82. font-weight: 700;
  83. margin-left: 7px;
  84. }
  85. .quote {
  86. color: rgb(221, 0, 0);
  87. font-family: arial, helvetica, sans-serif;
  88. font-size: 13.3333px;
  89. text-decoration-color: rgb(221, 0, 0);
  90. text-decoration-line: underline;
  91. text-decoration-style: solid;
  92. text-decoration-thickness: auto;
  93. }
  94. .greentext {
  95. color: rgb(120, 153, 34);
  96. font-family: arial, helvetica, sans-serif;
  97. font-size: 13.3333px;
  98. }
  99. blockquote {
  100. margin-block-start: 1em;
  101. margin-block-end: 1em;
  102. margin-inline-start: 40px;
  103. margin-inline-end: 40px;
  104. }
  105. """
  106. posts = []
  107. post = ''
  108. c = -2
  109. for line in f.splitlines():
  110. line += "\n"
  111. if line == '-----\n':
  112. continue
  113. elif line.startswith('--- '):
  114. c += 1
  115. if post != '':
  116. src = process_post(post, c)
  117. posts.append(src)
  118. post = line
  119. else:
  120. post += line
  121. if post != '':
  122. src = process_post(post, c)
  123. posts.append(src)
  124. for i in range(len(posts)):
  125. if i == 0:
  126. posts[i] = f'<div class="op">{posts[i]}</div>\n'
  127. else:
  128. posts[i] = f'<div class="reply">{posts[i]}</div>\n'
  129. output = ''
  130. output += f'<style>{css}</style><div id="container">'
  131. for post in posts:
  132. output += post
  133. output += '</div>'
  134. output = output.split('\n')
  135. for i in range(len(output)):
  136. output[i] = re.sub(r'^(&gt;(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
  137. output[i] = re.sub(r'^<blockquote class="message">(&gt;(.*?)(<br>|</div>))', r'<blockquote class="message"><span class="greentext">\1</span>', output[i])
  138. output = '\n'.join(output)
  139. return output