From f72c7102d6349af2633fa467e30ff8c6d6cff765 Mon Sep 17 00:00:00 2001 From: "daijian@air" Date: Fri, 11 May 2018 16:59:23 +0800 Subject: [PATCH 1/4] support cn --- simple_markdown/table.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/simple_markdown/table.py b/simple_markdown/table.py index e3e2c39..cd4e6a6 100644 --- a/simple_markdown/table.py +++ b/simple_markdown/table.py @@ -1,6 +1,9 @@ import re +def len2(txt): + return len(txt.encode('GBK')) + def enum(*sequential, **named): enums = dict(zip(sequential, range(len(sequential))), **named) from_string = dict((key, value) for key, value in enums.items()) @@ -41,15 +44,15 @@ def format(raw_table, margin=1, padding=0, default_justify=Justify.LEFT): matrix[:] = [row[:-1] for row in matrix] # ensure there's same column number for each row or add missings - col_cnt = max([len(row) for row in matrix]) + col_cnt = max([len2(row) for row in matrix]) matrix[:] = \ - [r if len(r) == col_cnt else r + [""]*(col_cnt-len(r)) for r in matrix] + [r if len2(r) == col_cnt else r + [""]*(col_cnt-len2(r)) for r in matrix] # merge the multiple "-" of the 2nd line 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 = [[len2(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)] @@ -74,7 +77,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] - len2(col), 2) text = " "*div + col + " "*(div+mod) line.append(text + "|") continue From b1801b98e4bcf64d66f2e00a771681cf71d64d1b Mon Sep 17 00:00:00 2001 From: "daijian@air" Date: Fri, 11 May 2018 17:56:40 +0800 Subject: [PATCH 2/4] fix chinese support problem --- .build | 12 ++++++++++++ simple_markdown/table.py | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 .build diff --git a/.build b/.build new file mode 100644 index 0000000..d48a02c --- /dev/null +++ b/.build @@ -0,0 +1,12 @@ +{ + "automatic_order": true, + "iterations": 1, + "mods_load_order": + [ + "tests/testpath.py", + "tests/test.py", + "simple_markdown/table.py", + "simple_markdown/__init__.py", + "markdown_table_formatter.py" + ] +} diff --git a/simple_markdown/table.py b/simple_markdown/table.py index cd4e6a6..b7a2dc8 100644 --- a/simple_markdown/table.py +++ b/simple_markdown/table.py @@ -2,7 +2,10 @@ def len2(txt): - return len(txt.encode('GBK')) + if isinstance(txt, str): + return len(txt.encode('GBK')) + else: + return len(txt) def enum(*sequential, **named): enums = dict(zip(sequential, range(len(sequential))), **named) @@ -15,6 +18,7 @@ def enum(*sequential, **named): Justify = enum("LEFT", "CENTER", "RIGHT") + def find_all(text): tables = [] offset = 0 @@ -44,17 +48,18 @@ def format(raw_table, margin=1, padding=0, default_justify=Justify.LEFT): matrix[:] = [row[:-1] for row in matrix] # ensure there's same column number for each row or add missings - col_cnt = max([len2(row) for row in matrix]) + col_cnt = max([len(row) for row in matrix]) matrix[:] = \ - [r if len2(r) == col_cnt else r + [""]*(col_cnt-len2(r)) for r in matrix] + [r if len(r) == col_cnt else r + [""]*(col_cnt-len(r)) for r in matrix] # merge the multiple "-" of the 2nd line matrix[1] = [re.sub("[-. ]+","-", col) for col in matrix[1]] # determine each cell text size text_width = [[len2(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)] + col_width = [max(size) + margin*2 + padding for size in zip(*text_width)] # get each column justification or apply default justify = [] @@ -77,14 +82,14 @@ 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] - len2(col), 2) + div, mod = divmod(col_width[col_idx] - len2(col), 2) text = " "*div + col + " "*(div+mod) line.append(text + "|") continue if justify[col_idx] == Justify.RIGHT: - text = col.rjust(col_width[col_idx] - margin*2) + text = col.rjust(col_width[col_idx] - margin*2 - (len2(col) - len(col))) elif justify[col_idx] == Justify.LEFT: - text = col.ljust(col_width[col_idx] - margin*2) + text = col.ljust(col_width[col_idx] - margin*2 - (len2(col) - len(col))) line.append(" "*margin + text + " "*margin + "|") table.append("".join(line)) From e74931dba25d684079ac9a606e1000fcddcb0efe Mon Sep 17 00:00:00 2001 From: "daijian@air" Date: Fri, 11 May 2018 18:01:37 +0800 Subject: [PATCH 3/4] enhance compatibility --- simple_markdown/table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simple_markdown/table.py b/simple_markdown/table.py index b7a2dc8..7d2065d 100644 --- a/simple_markdown/table.py +++ b/simple_markdown/table.py @@ -2,8 +2,8 @@ def len2(txt): - if isinstance(txt, str): - return len(txt.encode('GBK')) + if isinstance(txt, str): + return len(txt.encode('GBK', errors="replace")) else: return len(txt) From 8dc4044e2dc2f42468d0783f3ac9b8ee1c375a0d Mon Sep 17 00:00:00 2001 From: "daijian@air" Date: Fri, 11 May 2018 18:02:59 +0800 Subject: [PATCH 4/4] clean project --- .build | 12 ------------ .gitignore | 1 + 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 .build diff --git a/.build b/.build deleted file mode 100644 index d48a02c..0000000 --- a/.build +++ /dev/null @@ -1,12 +0,0 @@ -{ - "automatic_order": true, - "iterations": 1, - "mods_load_order": - [ - "tests/testpath.py", - "tests/test.py", - "simple_markdown/table.py", - "simple_markdown/__init__.py", - "markdown_table_formatter.py" - ] -} diff --git a/.gitignore b/.gitignore index bee8a64..dd50835 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +.build