From 438f9fe3de7aeacb2ad396224c07520bece526a4 Mon Sep 17 00:00:00 2001 From: Takayuki Shimizukawa Date: Tue, 26 Aug 2014 20:52:02 +0900 Subject: [PATCH] `make text` generate wrong table when it has empty table cells. Closes #1544 --- CHANGES | 1 + sphinx/writers/text.py | 4 ++-- tests/test_build_text.py | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 2003236a3..86ecb86fa 100644 --- a/CHANGES +++ b/CHANGES @@ -39,6 +39,7 @@ Bugs fixed * PR#281, PR#282, #1509: TODO extension not compatible with websupport. Thanks to Takeshi Komiya. * #1477: gettext does not extract nodes.line in a table or list. +* #1544: `make text` generate wrong table when it has empty table cells. Release 1.2.2 (released Mar 2, 2014) ==================================== diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py index 42ba05077..82b0f452b 100644 --- a/sphinx/writers/text.py +++ b/sphinx/writers/text.py @@ -11,7 +11,7 @@ import os import re import textwrap -from itertools import groupby +from itertools import groupby, izip_longest from docutils import nodes, writers from docutils.utils import column_width @@ -503,7 +503,7 @@ class TextTranslator(nodes.NodeVisitor): self.add_text(''.join(out) + self.nl) def writerow(row): - lines = zip(*row) + lines = izip_longest(*row) for line in lines: out = ['|'] for i, cell in enumerate(line): diff --git a/tests/test_build_text.py b/tests/test_build_text.py index d65135047..e6e4d5be2 100644 --- a/tests/test_build_text.py +++ b/tests/test_build_text.py @@ -139,3 +139,29 @@ def test_nonascii_maxwidth(app): lines = [line.strip() for line in result.splitlines() if line.strip()] line_widths = [column_width(line) for line in lines] assert max(line_widths) < MAXWIDTH + + +@with_text_app() +def test_table_with_empty_cell(app): + contents = (u""" + +-----+-----+ + | XXX | XXX | + +-----+-----+ + | | XXX | + +-----+-----+ + | XXX | | + +-----+-----+ + """) + + (app.srcdir / 'contents.rst').write_text(contents, encoding='utf-8') + app.builder.build_all() + result = (app.outdir / 'contents.txt').text(encoding='utf-8') + + lines = [line.strip() for line in result.splitlines() if line.strip()] + assert lines[0] == "+-------+-------+" + assert lines[1] == "| XXX | XXX |" + assert lines[2] == "+-------+-------+" + assert lines[3] == "| | XXX |" + assert lines[4] == "+-------+-------+" + assert lines[5] == "| XXX | |" + assert lines[6] == "+-------+-------+"