Skip to content

Commit

Permalink
boxes/helper: Add support for rendering tables.
Browse files Browse the repository at this point in the history
Added a helper function, render_table, to process a table and
return its content as a list of strings which can then be rendered
in the MessageBox.

Tests amended.
  • Loading branch information
preetmishra committed Mar 18, 2020
1 parent 45c7192 commit 10ab02e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
12 changes: 11 additions & 1 deletion tests/ui/test_ui_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,17 @@ def test_private_message_to_self(self, mocker):
('<hr/>', ['[RULER NOT RENDERED]']),
('<img>', ['[IMAGE NOT RENDERED]']),
('<img/>', ['[IMAGE NOT RENDERED]']),
('<table>stuff</table>', ['[TABLE NOT RENDERED]']),
('<table><thead><tr><th>Firstname</th><th>Lastname</th></tr></thead>'
'<tbody><tr><td>John</td><td>Doe</td></tr><tr><td>Mary</td><td>Moe'
'</td></tr></tbody></table>', [
('bold', '------------------------\n'),
('bold', '| Firstname | Lastname |\n'),
('bold', '------------------------\n'),
'| John | Doe |\n',
'------------------------\n',
'| Mary | Moe |\n',
'------------------------',
]),
('<span class="katex-display">some-math</span>', ['some-math']),
('<span class="katex">some-math</span>', ['some-math']),
('<ul><li>text</li></ul>', ['', ' * ', '', 'text']),
Expand Down
40 changes: 40 additions & 0 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,43 @@ def notify(title: str, html_text: str) -> None:
if command:
res = subprocess.run(shlex.split(command), stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)


def render_table(element: Any) -> List[Union[str, Tuple[str, str]]]:
"""
Renders table to be displayed in the MessageBox.
"""
# Header.
headers = element.find('thead').findChild().findChildren()
# Cells store data column-wise.
cells = [[] for _ in headers] # type: List[List[str]]
for index, header in enumerate(headers):
cells[index].append(' ' + header.text + ' ')

# Body.
rows = element.find('tbody').find_all('tr')
for row in rows:
for index, data in enumerate(row.findChildren()):
cells[index].append(' ' + data.text + ' ')

# Rendering.
col_width = [len(max(column, key=lambda s: len(s))) for column in cells]
dashes = ('-' * (sum(col_width) + len(headers) + 1)) + '\n'
contents = [] # type: List[Union[str, Tuple[str, str]]]
contents.append(('bold', dashes))
rows_count = len(rows) + 1 # +1 to include header.
for index in range(rows_count):
row = '|' + '|'.join(col[index].ljust(col_width[col_index])
for col_index, col in enumerate(cells)) + '|\n'
if index == 0:
# Make the headers bold.
contents.append(('bold', row))
contents.append(('bold', dashes))
else:
contents.append(row)
if index == rows_count - 1:
# Remove '\n' for the last row.
contents.append(dashes[:-1])
else:
contents.append(dashes)
return contents
5 changes: 3 additions & 2 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from zulipterminal.config.keys import is_command_key, keys_for_command
from zulipterminal.helper import (
Message, match_groups, match_stream, match_user,
Message, match_groups, match_stream, match_user, render_table,
)
from zulipterminal.urwid_types import urwid_Size

Expand Down Expand Up @@ -386,7 +386,6 @@ def soup2markup(self, soup: Any) -> List[Any]:
'br': '', # No indicator of absence
'hr': 'RULER',
'img': 'IMAGE',
'table': 'TABLE'
}
unrendered_div_classes = { # In pairs of 'div_class': 'text'
# TODO: Support embedded content & twitter preview?
Expand Down Expand Up @@ -482,6 +481,8 @@ def soup2markup(self, soup: Any) -> List[Any]:
# TODO: Support nested lists
markup.append(' * ')
markup.extend(self.soup2markup(element))
elif element.name == 'table':
markup.extend(render_table(element))
else:
markup.extend(self.soup2markup(element))
return markup
Expand Down

0 comments on commit 10ab02e

Please sign in to comment.