center tables <th>s #12
This commit is contained in:
@ -7,7 +7,6 @@ import os.path
|
|||||||
class MLPDevListener(sublime_plugin.EventListener):
|
class MLPDevListener(sublime_plugin.EventListener):
|
||||||
|
|
||||||
def on_post_save(self, view):
|
def on_post_save(self, view):
|
||||||
# return
|
|
||||||
if not (os.path.dirname(__file__) in view.file_name() and
|
if not (os.path.dirname(__file__) in view.file_name() and
|
||||||
view.file_name().endswith('.py')):
|
view.file_name().endswith('.py')):
|
||||||
return
|
return
|
||||||
|
|||||||
@ -29,8 +29,18 @@ def pre_table(s_table):
|
|||||||
text += '|\n'
|
text += '|\n'
|
||||||
|
|
||||||
for j, cell in enumerate(row):
|
for j, cell in enumerate(row):
|
||||||
text += '| ' + ''.join(str(node) for node in cell.contents) \
|
text += '| '
|
||||||
+ ' ' * (cols_width[j] - len(cell.text) + 1)
|
if cell.name == 'th':
|
||||||
|
title = ' ' * ((cols_width[j] - len(cell.text)) // 2) \
|
||||||
|
+ ''.join(str(node) for node in cell.contents) \
|
||||||
|
+ ' ' * int(round((cols_width[j] - len(cell.text)) / 2 ) + 1)
|
||||||
|
# + 1 because of the added space before the closing | of each cell
|
||||||
|
if cols_width[j] + 1 != len(title):
|
||||||
|
title += ' '
|
||||||
|
text += title
|
||||||
|
else:
|
||||||
|
text += ''.join(str(node) for node in cell.contents) \
|
||||||
|
+ ' ' * (cols_width[j] - len(cell.text) + 1)
|
||||||
text += '|\n'
|
text += '|\n'
|
||||||
text += '</pre></code>'
|
text += '</pre></code>'
|
||||||
return text
|
return text
|
||||||
@ -40,7 +50,3 @@ def pre_tables(html):
|
|||||||
for table in soup.find_all('table'):
|
for table in soup.find_all('table'):
|
||||||
table.replace_with(BeautifulSoup(pre_table(table), 'html.parser'))
|
table.replace_with(BeautifulSoup(pre_table(table), 'html.parser'))
|
||||||
return str(soup)
|
return str(soup)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# CSW: ignore
|
|
||||||
print(pre_tables(html))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user