Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions simple_markdown/table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import re


def len_utf8(text):
lentext = len(text)
lentext_utf8 = len(text.encode('utf-8'))
length = int((lentext_utf8 - lentext)/2 + lentext)
return length

def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
from_string = dict((key, value) for key, value in enums.items())
Expand Down Expand Up @@ -49,7 +55,7 @@ def format(raw_table, margin=1, padding=0, default_justify=Justify.LEFT):
matrix[1] = [re.sub("[-. ]+","-", col) for col in matrix[1]]

# determine each cell text size
text_width = [[len(col) for col in row] for row in matrix]
text_width = [[len_utf8(col) for col in row] for row in matrix]
# determine column width (including space padding/margin)
col_width = [max(size) + margin*2 + padding for size in zip(*text_width)]

Expand All @@ -74,7 +80,7 @@ def format(raw_table, margin=1, padding=0, default_justify=Justify.LEFT):
continue
for col_idx, col in enumerate(row):
if justify[col_idx] == Justify.CENTER:
div, mod = divmod(col_width[col_idx] - len(col), 2)
div, mod = divmod(col_width[col_idx] - len_utf8(col), 2)
text = " "*div + col + " "*(div+mod)
line.append(text + "|")
continue
Expand Down