table.py 8.2 KB

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