table.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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;)*)((?:(?!\|\|).)*)", 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. table = re.sub("^(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "<table><tbody><tr><td " + cel + ">", table, 1)
  141. else:
  142. break
  143. table = re.sub("\|\|$", "</td></tr></tbody></table>", table)
  144. while(1):
  145. b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\|).)*)", table)
  146. if(b):
  147. row = ''
  148. cel = ''
  149. celstyle = ''
  150. rowstyle = ''
  151. table_d = ''
  152. result = b.groups()
  153. if(result[1]):
  154. table_d = table_p(result[1], result[0], result[2], num)
  155. rowstyle = table_d[1]
  156. celstyle = table_d[2]
  157. row = table_d[3]
  158. cel = table_d[4]
  159. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td></tr><tr " + rowstyle + "><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  160. else:
  161. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  162. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td></tr><tr><td " + cel + ">", table, 1)
  163. else:
  164. break
  165. while(1):
  166. c = re.search("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)((?:(?!\|\|).)*)", table)
  167. if(c):
  168. row = ''
  169. cel = ''
  170. celstyle = ''
  171. table_d = ''
  172. result = c.groups()
  173. if(result[1]):
  174. table_d = table_p(result[1], result[0], result[2], num)
  175. celstyle = table_d[2]
  176. row = table_d[3]
  177. cel = table_d[4]
  178. table = re.sub("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td><td " + cel + " " + row + " " + celstyle + ">", table, 1)
  179. else:
  180. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  181. table = re.sub("(\|\|(?:(?:\|\|)*))((?:&lt;(?:(?:(?!&gt;).)*)&gt;)*)", "</td><td " + cel + ">", table, 1)
  182. else:
  183. break
  184. data = re.sub("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
  185. else:
  186. break
  187. return(data)