table.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import re
  2. def table_p(d, d2, d3, num = 0):
  3. table_class = 'class="'
  4. alltable = 'style="'
  5. celstyle = 'style="'
  6. rowstyle = 'style="'
  7. row = ''
  8. cel = ''
  9. table_w = re.search("<table\s?width=((?:(?!>).)*)>", d)
  10. table_h = re.search("<table\s?height=((?:(?!>).)*)>", d)
  11. table_a = re.search("<table\s?align=((?:(?!>).)*)>", d)
  12. if table_w:
  13. alltable += 'width: ' + table_w.groups()[0] + ';'
  14. if table_h:
  15. alltable += 'height: ' + table_h.groups()[0] + ';'
  16. if table_a:
  17. if table_a.groups()[0] == 'right':
  18. alltable += 'float: right;'
  19. elif table_a.groups()[0] == 'center':
  20. alltable += 'margin: auto;'
  21. table_t_a = re.search("<table\s?textalign=((?:(?!>).)*)>", d)
  22. if table_t_a:
  23. num = 1
  24. if table_t_a.groups()[0] == 'right':
  25. alltable += 'text-align: right;'
  26. elif table_t_a.groups()[0] == 'center':
  27. alltable += 'text-align: center;'
  28. row_t_a = re.search("<row\s?textalign=((?:(?!>).)*)>", d)
  29. if row_t_a:
  30. if row_t_a.groups()[0] == 'right':
  31. rowstyle += 'text-align: right;'
  32. elif row_t_a.groups()[0] == 'center':
  33. rowstyle += 'text-align: center;'
  34. else:
  35. rowstyle += 'text-align: left;'
  36. table_cel = re.search("<-((?:(?!>).)*)>", d)
  37. if table_cel:
  38. cel = 'colspan="' + table_cel.groups()[0] + '"'
  39. else:
  40. cel = 'colspan="' + str(round(len(d2) / 2)) + '"'
  41. table_row = re.search("<\|((?:(?!>).)*)>", d)
  42. if table_row:
  43. row = 'rowspan="' + table_row.groups()[0] + '"'
  44. row_bgcolor_2 = re.search("<rowbgcolor=(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
  45. row_bgcolor_3 = re.search("<rowbgcolor=(\w+)>", d)
  46. if row_bgcolor_2:
  47. rowstyle += 'background: ' + row_bgcolor_2.groups()[0] + ';'
  48. elif row_bgcolor_3:
  49. rowstyle += 'background: ' + row_bgcolor_3.groups()[0] + ';'
  50. table_border_2 = re.search("<table\s?bordercolor=(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
  51. table_border_3 = re.search("<table\s?bordercolor=(\w+)>", d)
  52. if table_border_2:
  53. alltable += 'border: ' + table_border_2.groups()[0] + ' 2px solid;'
  54. elif table_border_3:
  55. alltable += 'border: ' + table_border_3.groups()[0] + ' 2px solid;'
  56. table_bgcolor_2 = re.search("<table\s?bgcolor=(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
  57. table_bgcolor_3 = re.search("<table\s?bgcolor=(\w+)>", d)
  58. if table_bgcolor_2:
  59. alltable += 'background: ' + table_bgcolor_2.groups()[0] + ';'
  60. elif table_bgcolor_3:
  61. alltable += 'background: ' + table_bgcolor_3.groups()[0] + ';'
  62. bgcolor_2 = re.search("<(?:bgcolor=)?(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
  63. bgcolor_3 = re.search("<(?:bgcolor=)?(\w+)>", d)
  64. if bgcolor_2:
  65. celstyle += 'background: ' + bgcolor_2.groups()[0] + ';'
  66. elif bgcolor_3:
  67. celstyle += 'background: ' + bgcolor_3.groups()[0] + ';'
  68. n_width = re.search("<width=((?:(?!>).)*)>", d)
  69. n_height = re.search("<height=((?:(?!>).)*)>", d)
  70. if n_width:
  71. celstyle += 'width: ' + n_width.groups()[0] + ';'
  72. if n_height:
  73. celstyle += 'height: ' + n_height.groups()[0] + ';'
  74. text_right = re.search("<\)>", d)
  75. text_center = re.search("<:>", d)
  76. text_left = re.search("<\(>", d)
  77. if text_right:
  78. celstyle += 'text-align: right;'
  79. elif text_center:
  80. celstyle += 'text-align: center;'
  81. elif text_left:
  82. celstyle += 'text-align: left;'
  83. elif num == 0:
  84. if re.search('^ (.*) $', d3):
  85. celstyle += 'text-align: center;'
  86. elif re.search('^ (.*)$', d3):
  87. celstyle += 'text-align: right;'
  88. elif re.search('^(.*) $', d3):
  89. celstyle += 'text-align: left;'
  90. text_class = re.search("<table\s?class=((?:(?!>).)+)>", d)
  91. if text_class:
  92. d = text_class.groups()
  93. table_class += d[0]
  94. alltable += 'margin-top: 10px;"'
  95. celstyle += '"'
  96. rowstyle += '"'
  97. table_class += '"'
  98. return [alltable, rowstyle, celstyle, row, cel, table_class, num]
  99. def table(data):
  100. data += '\r\n'
  101. data = re.sub('(\r+)', '\r', data)
  102. data = re.sub("(?:\|\|\r\n)", "#table-start##table-no-br#", data)
  103. while 1:
  104. y = re.search("(\|\|(?:(?:(?!\|\|).)+(?:\n*))+)", data)
  105. if y:
  106. a = y.groups()
  107. mid_data = re.sub("\|\|", "#table-start#", a[0])
  108. mid_data = re.sub("\r\n", "<br>", mid_data)
  109. data = re.sub("(\|\|(?:(?:(?!\|\|).)+(?:\n*))+)", mid_data, data, 1)
  110. else:
  111. break
  112. data = re.sub("#table-start#", "||", data)
  113. data = re.sub("#table-no-br#", "\r\n", data)
  114. while 1:
  115. m = re.search("(?:\n|<br>)(\|\|(?:(?:(?:.*)\n?)\|\|)+)", data)
  116. if m:
  117. results = m.groups()
  118. table = results[0]
  119. while 1:
  120. a = re.search("^(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\||<\/td>).)*)", table)
  121. if a:
  122. row = ''
  123. cel = ''
  124. celstyle = ''
  125. rowstyle = ''
  126. alltable = ''
  127. table_d = ''
  128. num = 0
  129. result = a.groups()
  130. if result[1]:
  131. table_d = table_p(result[1], result[0], result[2])
  132. alltable = table_d[0]
  133. rowstyle = table_d[1]
  134. celstyle = table_d[2]
  135. row = table_d[3]
  136. cel = table_d[4]
  137. table_class = table_d[5]
  138. num = table_d[6]
  139. table = re.sub("^(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "<table " + table_class + " " + alltable + "><tbody><tr " + rowstyle + "><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  140. else:
  141. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  142. if re.search('^ (.*) $', result[2]):
  143. celstyle += 'text-align: center;'
  144. elif re.search('^ (.*)$', result[2]):
  145. celstyle += 'text-align: right;'
  146. elif re.search('^(.*) $', result[2]):
  147. celstyle += 'text-align: left;'
  148. table = re.sub("^(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "<table style='margin-top: 10px;'><tbody><tr><td " + cel + " style='" + celstyle + "'>", table, 1)
  149. else:
  150. break
  151. table = re.sub("\|\|$", "</td></tr></tbody></table>", table)
  152. while 1:
  153. b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\||<\/td>).)*)", table)
  154. if b:
  155. row = ''
  156. cel = ''
  157. celstyle = ''
  158. rowstyle = ''
  159. table_d = ''
  160. result = b.groups()
  161. if result[1]:
  162. table_d = table_p(result[1], result[0], result[2], num)
  163. rowstyle = table_d[1]
  164. celstyle = table_d[2]
  165. row = table_d[3]
  166. cel = table_d[4]
  167. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td></tr><tr " + rowstyle + "><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  168. else:
  169. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  170. if re.search('^ (.*) $', result[2]):
  171. celstyle += 'text-align: center;'
  172. elif re.search('^ (.*)$', result[2]):
  173. celstyle += 'text-align: right;'
  174. elif re.search('^(.*) $', result[2]):
  175. celstyle += 'text-align: left;'
  176. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td></tr><tr><td " + cel + " style='" + celstyle + "'>", table, 1)
  177. else:
  178. break
  179. while 1:
  180. c = re.search("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\||<\/td>).)*)", table)
  181. if c:
  182. row = ''
  183. cel = ''
  184. celstyle = ''
  185. table_d = ''
  186. result = c.groups()
  187. if result[1]:
  188. table_d = table_p(result[1], result[0], result[2], num)
  189. celstyle = table_d[2]
  190. row = table_d[3]
  191. cel = table_d[4]
  192. table = re.sub("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  193. else:
  194. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  195. if re.search('^ (.*) $', result[2]):
  196. celstyle += 'text-align: center;'
  197. elif re.search('^ (.*)$', result[2]):
  198. celstyle += 'text-align: right;'
  199. elif re.search('^(.*) $', result[2]):
  200. celstyle += 'text-align: left;'
  201. table = re.sub("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td><td " + cel + " style='" + celstyle + "'>", table, 1)
  202. else:
  203. break
  204. data = re.sub("(?:\n|<br>)(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
  205. else:
  206. break
  207. return data