From f6fca7a8ac75e3fa2d2456646fe7c6c310547cba Mon Sep 17 00:00:00 2001 From: Takayuki Shimizukawa Date: Sat, 9 Mar 2013 22:58:47 +0900 Subject: [PATCH] Fix: text builder breach max-witdh specificatoin if paragraph have prefixed text. ex: 'See also:' --- sphinx/writers/text.py | 13 +++++++++---- tests/test_build_text.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py index 9df974079..965769117 100644 --- a/sphinx/writers/text.py +++ b/sphinx/writers/text.py @@ -199,9 +199,14 @@ class TextTranslator(nodes.NodeVisitor): do_format() if first is not None and result: itemindent, item = result[0] + result_rest, result = result[1:], [] if item: - result.insert(0, (itemindent - indent, [first + item[0]])) - result[1] = (itemindent, item[1:]) + toformat = [first + ' '.join(item)] + do_format() #re-create `result` from `toformat` + _dummy, new_item = result[0] + result.insert(0, (itemindent - indent, [new_item[0]])) + result[1] = (itemindent, new_item[1:]) + result.extend(result_rest) self.states[-1].extend(result) def visit_document(self, node): @@ -572,11 +577,11 @@ class TextTranslator(nodes.NodeVisitor): self.new_state(len(str(self.list_counter[-1])) + 2) def depart_list_item(self, node): if self.list_counter[-1] == -1: - self.end_state(first='* ', end=None) + self.end_state(first='* ') elif self.list_counter[-1] == -2: pass else: - self.end_state(first='%s. ' % self.list_counter[-1], end=None) + self.end_state(first='%s. ' % self.list_counter[-1]) def visit_definition_list_item(self, node): self._li_has_classifier = len(node) >= 2 and \ diff --git a/tests/test_build_text.py b/tests/test_build_text.py index 79edc6230..e36076a58 100644 --- a/tests/test_build_text.py +++ b/tests/test_build_text.py @@ -30,6 +30,35 @@ def with_text_app(*args, **kw): return with_app(*args, **default_kw) +@with_text_app() +def test_maxwitdh_with_prefix(app): + long_string = u' '.join([u"ham"] * 30) + contents = ( + u".. seealso:: %(long_string)s\n\n" + u"* %(long_string)s\n" + u"* %(long_string)s\n" + u"\nspam egg\n" + ) % locals() + + (app.srcdir / 'contents.rst').write_text(contents, encoding='utf-8') + app.builder.build_all() + result = (app.outdir / 'contents.txt').text(encoding='utf-8') + + lines = result.splitlines() + line_widths = [column_width(line) for line in lines] + assert max(line_widths) < MAXWIDTH + assert lines[0].startswith('See also: ham') + assert lines[1].startswith(' ham') + assert lines[2] == '' + assert lines[3].startswith('* ham') + assert lines[4].startswith(' ham') + assert lines[5] == '' + assert lines[6].startswith('* ham') + assert lines[7].startswith(' ham') + assert lines[8] == '' + assert lines[9].startswith('spam egg') + + @with_text_app() def test_multibyte_title_line(app): title = u'\u65e5\u672c\u8a9e'