| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import re
- def table_p(d, d2, d3, num = 0):
- table_class = 'class="'
- alltable = 'style="'
- celstyle = 'style="'
- rowstyle = 'style="'
- row = ''
- cel = ''
- table_w = re.search("<table\s?width=((?:(?!>).)*)>", d)
- table_h = re.search("<table\s?height=((?:(?!>).)*)>", d)
- table_a = re.search("<table\s?align=((?:(?!>).)*)>", d)
- if table_w:
- alltable += 'width: ' + table_w.groups()[0] + ';'
- if table_h:
- alltable += 'height: ' + table_h.groups()[0] + ';'
- if table_a:
- if table_a.groups()[0] == 'right':
- alltable += 'float: right;'
- elif table_a.groups()[0] == 'center':
- alltable += 'margin: auto;'
-
- table_t_a = re.search("<table\s?textalign=((?:(?!>).)*)>", d)
- if table_t_a:
- num = 1
- if table_t_a.groups()[0] == 'right':
- alltable += 'text-align: right;'
- elif table_t_a.groups()[0] == 'center':
- alltable += 'text-align: center;'
- row_t_a = re.search("<row\s?textalign=((?:(?!>).)*)>", d)
- if row_t_a:
- if row_t_a.groups()[0] == 'right':
- rowstyle += 'text-align: right;'
- elif row_t_a.groups()[0] == 'center':
- rowstyle += 'text-align: center;'
- else:
- rowstyle += 'text-align: left;'
-
- table_cel = re.search("<-((?:(?!>).)*)>", d)
- if table_cel:
- cel = 'colspan="' + table_cel.groups()[0] + '"'
- else:
- cel = 'colspan="' + str(round(len(d2) / 2)) + '"'
- table_row = re.search("<\|((?:(?!>).)*)>", d)
- if table_row:
- row = 'rowspan="' + table_row.groups()[0] + '"'
- row_bgcolor_2 = re.search("<rowbgcolor=(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
- row_bgcolor_3 = re.search("<rowbgcolor=(\w+)>", d)
- if row_bgcolor_2:
- rowstyle += 'background: ' + row_bgcolor_2.groups()[0] + ';'
- elif row_bgcolor_3:
- rowstyle += 'background: ' + row_bgcolor_3.groups()[0] + ';'
-
- table_border_2 = re.search("<table\s?bordercolor=(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
- table_border_3 = re.search("<table\s?bordercolor=(\w+)>", d)
- if table_border_2:
- alltable += 'border: ' + table_border_2.groups()[0] + ' 2px solid;'
- elif table_border_3:
- alltable += 'border: ' + table_border_3.groups()[0] + ' 2px solid;'
-
- table_bgcolor_2 = re.search("<table\s?bgcolor=(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
- table_bgcolor_3 = re.search("<table\s?bgcolor=(\w+)>", d)
- if table_bgcolor_2:
- alltable += 'background: ' + table_bgcolor_2.groups()[0] + ';'
- elif table_bgcolor_3:
- alltable += 'background: ' + table_bgcolor_3.groups()[0] + ';'
-
- bgcolor_2 = re.search("<(?:bgcolor=)?(#(?:[0-9a-f-A-F]{3}){1,2})>", d)
- bgcolor_3 = re.search("<(?:bgcolor=)?(\w+)>", d)
- if bgcolor_2:
- celstyle += 'background: ' + bgcolor_2.groups()[0] + ';'
- elif bgcolor_3:
- celstyle += 'background: ' + bgcolor_3.groups()[0] + ';'
-
- n_width = re.search("<width=((?:(?!>).)*)>", d)
- n_height = re.search("<height=((?:(?!>).)*)>", d)
- if n_width:
- celstyle += 'width: ' + n_width.groups()[0] + ';'
- if n_height:
- celstyle += 'height: ' + n_height.groups()[0] + ';'
-
- text_right = re.search("<\)>", d)
- text_center = re.search("<:>", d)
- text_left = re.search("<\(>", d)
- if text_right:
- celstyle += 'text-align: right;'
- elif text_center:
- celstyle += 'text-align: center;'
- elif text_left:
- celstyle += 'text-align: left;'
- elif num == 0:
- if re.search('^ (.*) $', d3):
- celstyle += 'text-align: center;'
- elif re.search('^ (.*)$', d3):
- celstyle += 'text-align: right;'
- elif re.search('^(.*) $', d3):
- celstyle += 'text-align: left;'
- text_class = re.search("<table\s?class=((?:(?!>).)+)>", d)
- if text_class:
- d = text_class.groups()
- table_class += d[0]
-
- alltable += 'margin-top: 10px;"'
- celstyle += '"'
- rowstyle += '"'
- table_class += '"'
- return [alltable, rowstyle, celstyle, row, cel, table_class, num]
- def table(data):
- data += '\r\n'
- data = re.sub('(\r+)', '\r', data)
- data = re.sub("(?:\|\|\r\n)", "#table-start##table-no-br#", data)
-
- while 1:
- y = re.search("(\|\|(?:(?:(?!\|\|).)+(?:\n*))+)", data)
- if y:
- a = y.groups()
-
- mid_data = re.sub("\|\|", "#table-start#", a[0])
- mid_data = re.sub("\r\n", "<br>", mid_data)
-
- data = re.sub("(\|\|(?:(?:(?!\|\|).)+(?:\n*))+)", mid_data, data, 1)
- else:
- break
-
- data = re.sub("#table-start#", "||", data)
- data = re.sub("#table-no-br#", "\r\n", data)
-
- while 1:
- m = re.search("(?:\n|<br>|<span>)(\|\|(?:(?:(?:.*)\n?)\|\|)+)", data)
- if m:
- results = m.groups()
- table = results[0]
- while 1:
- a = re.search("^(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)((?:(?!\|\||<\/td>).)*)", table)
- if a:
- row = ''
- cel = ''
- celstyle = ''
- rowstyle = ''
- alltable = ''
- table_d = ''
- num = 0
- result = a.groups()
- if result[1]:
- table_d = table_p(result[1], result[0], result[2])
- alltable = table_d[0]
- rowstyle = table_d[1]
- celstyle = table_d[2]
- row = table_d[3]
- cel = table_d[4]
- table_class = table_d[5]
- num = table_d[6]
-
- table = re.sub("^(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)", "<table " + table_class + " " + alltable + "><tbody><tr " + rowstyle + "><td " + cel + " " + row + " " + celstyle + ">", table, 1)
- else:
- cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
- if re.search('^ (.*) $', result[2]):
- celstyle += 'text-align: center;'
- elif re.search('^ (.*)$', result[2]):
- celstyle += 'text-align: right;'
- elif re.search('^(.*) $', result[2]):
- celstyle += 'text-align: left;'
- table = re.sub("^(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)", "<table style='margin-top: 10px;'><tbody><tr><td " + cel + " style='" + celstyle + "'>", table, 1)
- else:
- break
-
- table = re.sub("\|\|$", "</td></tr></tbody></table>", table)
-
- while 1:
- b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)((?:(?!\|\||<\/td>).)*)", table)
- if b:
- row = ''
- cel = ''
- celstyle = ''
- rowstyle = ''
- table_d = ''
- result = b.groups()
- if result[1]:
- table_d = table_p(result[1], result[0], result[2], num)
- rowstyle = table_d[1]
- celstyle = table_d[2]
- row = table_d[3]
- cel = table_d[4]
-
- table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)", "</td></tr><tr " + rowstyle + "><td " + cel + " " + row + " " + celstyle + ">", table, 1)
- else:
- cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
- if re.search('^ (.*) $', result[2]):
- celstyle += 'text-align: center;'
- elif re.search('^ (.*)$', result[2]):
- celstyle += 'text-align: right;'
- elif re.search('^(.*) $', result[2]):
- celstyle += 'text-align: left;'
- table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)", "</td></tr><tr><td " + cel + " style='" + celstyle + "'>", table, 1)
- else:
- break
- while 1:
- c = re.search("(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)((?:(?!\|\||<\/td>).)*)", table)
- if c:
- row = ''
- cel = ''
- celstyle = ''
- table_d = ''
- result = c.groups()
- if result[1]:
- table_d = table_p(result[1], result[0], result[2], num)
- celstyle = table_d[2]
- row = table_d[3]
- cel = table_d[4]
- table = re.sub("(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)", "</td><td " + cel + " " + row + " " + celstyle + ">", table, 1)
- else:
- cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
- if re.search('^ (.*) $', result[2]):
- celstyle += 'text-align: center;'
- elif re.search('^ (.*)$', result[2]):
- celstyle += 'text-align: right;'
- elif re.search('^(.*) $', result[2]):
- celstyle += 'text-align: left;'
- table = re.sub("(\|\|(?:(?:\|\|)*))((?:<(?:(?:(?!>).)*)>)*)", "</td><td " + cel + " style='" + celstyle + "'>", table, 1)
- else:
- break
-
- data = re.sub("(?:\n|<br>|<span>)(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
- else:
- break
-
- return data
|