epub converter: prettify tables processing

This commit is contained in:
shirshasa
2021-08-02 11:28:03 +03:00
parent b1738fb739
commit 25320816b5

View File

@@ -72,25 +72,11 @@ def preprocess_table(body_tag: BeautifulSoup):
tables = body_tag.find_all("table") tables = body_tag.find_all("table")
for table in tables: for table in tables:
tds = table.find_all(re.compile("td|th|tr")) tds = table.find_all(re.compile("td|th|tr"))
border_sizes = []
for td in tds: for td in tds:
style = td.get('style') style = td.get('style')
width = '' width = ''
if style: if style:
border_match = re.search(r"border: ?(\d+\.?\d*)(p[tx])", style) or\
re.search(r"border-top-width: ?(\d+\.?\d*)(p[tx])", style) or\
re.search(r"border-left-width: ?(\d+\.?\d*)(p[tx])", style) or \
re.search(r"border-right-width: ?(\d+\.?\d*)(p[tx])", style) or \
re.search(r"border-bottom-width: ?(\d+\.?\d*)(p[tx])", style)
if border_match:
size = border_match.group(1)
units = border_match.group(2)
border_sizes.append(float(size))
width_match = re.search(r"[^-]width: ?(\d+\.?\d*)(p[tx])", style) width_match = re.search(r"[^-]width: ?(\d+\.?\d*)(p[tx])", style)
if width_match: if width_match:
size = width_match.group(1) size = width_match.group(1)
units = width_match.group(2) units = width_match.group(2)
@@ -107,10 +93,7 @@ def preprocess_table(body_tag: BeautifulSoup):
if td.attrs.get('style') == '': if td.attrs.get('style') == '':
del td.attrs['style'] del td.attrs['style']
if border_sizes: if not table.attrs.get('border') or table.attrs.get('border') in ['0', '0px']:
border_size = sum(border_sizes) / len(border_sizes)
table.attrs['border'] = f'{border_size:.2}'
else:
table.attrs['border'] = '1' table.attrs['border'] = '1'