2
0

table.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 += '"'
  95. celstyle += '"'
  96. rowstyle += '"'
  97. table_class += '"'
  98. return([alltable, rowstyle, celstyle, row, cel, table_class, num])
  99. def table(data):
  100. data = re.sub("(?:\|\|\r\n)", "#table#<tablenobr>", data)
  101. while(1):
  102. y = re.search("(\|\|(?:(?:(?:(?:(?!\|\|).)*)(?:\n?))+))", data)
  103. if(y):
  104. a = y.groups()
  105. mid_data = re.sub("\|\|", "#table#", a[0])
  106. mid_data = re.sub("\r\n", "<br>", mid_data)
  107. data = re.sub("(\|\|((?:(?:(?:(?!\|\|).)*)(?:\n?))+))", mid_data, data, 1)
  108. else:
  109. break
  110. data = re.sub("#table#", "||", data)
  111. data = re.sub("<tablenobr>", "\r\n", data)
  112. while(1):
  113. m = re.search("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", data)
  114. if(m):
  115. results = m.groups()
  116. table = results[0]
  117. while(1):
  118. a = re.search("^(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\||<\/td>).)*)", table)
  119. if(a):
  120. row = ''
  121. cel = ''
  122. celstyle = ''
  123. rowstyle = ''
  124. alltable = ''
  125. table_d = ''
  126. num = 0
  127. result = a.groups()
  128. if(result[1]):
  129. table_d = table_p(result[1], result[0], result[2])
  130. alltable = table_d[0]
  131. rowstyle = table_d[1]
  132. celstyle = table_d[2]
  133. row = table_d[3]
  134. cel = table_d[4]
  135. table_class = table_d[5]
  136. num = table_d[6]
  137. table = re.sub("^(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "<table " + table_class + " " + alltable + "><tbody><tr " + rowstyle + "><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  138. else:
  139. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  140. if(re.search('^ (.*) $', result[2])):
  141. celstyle += 'text-align: center;'
  142. elif(re.search('^ (.*)$', result[2])):
  143. celstyle += 'text-align: right;'
  144. elif(re.search('^(.*) $', result[2])):
  145. celstyle += 'text-align: left;'
  146. table = re.sub("^(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "<table><tbody><tr><td " + cel + " style='" + celstyle + "'>", table, 1)
  147. else:
  148. break
  149. table = re.sub("\|\|$", "</td></tr></tbody></table>", table)
  150. while(1):
  151. b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\||<\/td>).)*)", table)
  152. if(b):
  153. row = ''
  154. cel = ''
  155. celstyle = ''
  156. rowstyle = ''
  157. table_d = ''
  158. result = b.groups()
  159. if(result[1]):
  160. table_d = table_p(result[1], result[0], result[2], num)
  161. rowstyle = table_d[1]
  162. celstyle = table_d[2]
  163. row = table_d[3]
  164. cel = table_d[4]
  165. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td></tr><tr " + rowstyle + "><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  166. else:
  167. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  168. if(re.search('^ (.*) $', result[2])):
  169. celstyle += 'text-align: center;'
  170. elif(re.search('^ (.*)$', result[2])):
  171. celstyle += 'text-align: right;'
  172. elif(re.search('^(.*) $', result[2])):
  173. celstyle += 'text-align: left;'
  174. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td></tr><tr><td " + cel + " style='" + celstyle + "'>", table, 1)
  175. else:
  176. break
  177. while(1):
  178. c = re.search("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\||<\/td>).)*)", table)
  179. if(c):
  180. row = ''
  181. cel = ''
  182. celstyle = ''
  183. table_d = ''
  184. result = c.groups()
  185. if(result[1]):
  186. table_d = table_p(result[1], result[0], result[2], num)
  187. celstyle = table_d[2]
  188. row = table_d[3]
  189. cel = table_d[4]
  190. table = re.sub("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  191. else:
  192. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  193. if(re.search('^ (.*) $', result[2])):
  194. celstyle += 'text-align: center;'
  195. elif(re.search('^ (.*)$', result[2])):
  196. celstyle += 'text-align: right;'
  197. elif(re.search('^(.*) $', result[2])):
  198. celstyle += 'text-align: left;'
  199. table = re.sub("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td><td " + cel + " style='" + celstyle + "'>", table, 1)
  200. else:
  201. break
  202. data = re.sub("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
  203. else:
  204. break
  205. return(data)