From 5a05cabd6acd7de3929b355a67ca76298f3baa27 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 28 Nov 2019 02:00:26 +0900 Subject: [PATCH] Fix #6867: text: extra spaces are inserted to hyphenated words on folding lines --- CHANGES | 2 ++ sphinx/writers/text.py | 33 ++++++++++++++++++++------------- tests/test_build_text.py | 22 ++++++++++++---------- tests/test_intl.py | 7 +++++-- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/CHANGES b/CHANGES index 0887943e5..bed017d55 100644 --- a/CHANGES +++ b/CHANGES @@ -11,6 +11,7 @@ Incompatible changes match the first line of the code block. * #1331: Change default User-Agent header to ``"Sphinx/X.Y.Z requests/X.Y.Z python/X.Y.Z"``. It can be changed via :confval:`user_agent`. +* #6867: text: content of admonitions starts after a blank line Deprecated ---------- @@ -78,6 +79,7 @@ Bugs fixed fully matched in a text paragraph on the same page, the search does not include this match. * #6848: config.py shouldn't pop extensions from overrides +* #6867: text: extra spaces are inserted to hyphenated words on folding lines Testing -------- diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py index 879ce5ada..a71aefa80 100644 --- a/sphinx/writers/text.py +++ b/sphinx/writers/text.py @@ -440,15 +440,14 @@ class TextTranslator(SphinxTranslator): toformat = [] do_format() if first is not None and result: - itemindent, item = result[0] - result_rest, result = result[1:], [] - if item: - 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) + # insert prefix into first line (ex. *, [1], See also, etc.) + newindent = result[0][0] - indent + if result[0][1] == ['']: + result.insert(0, (newindent, [first])) + else: + text = first + result[0][1].pop(0) + result.insert(0, (newindent, [text])) + self.states[-1].extend(result) def visit_document(self, node: Element) -> None: @@ -916,12 +915,20 @@ class TextTranslator(SphinxTranslator): def _visit_admonition(self, node: Element) -> None: self.new_state(2) - if isinstance(node.children[0], nodes.Sequential): - self.add_text(self.nl) - def _depart_admonition(self, node: Element) -> None: label = admonitionlabels[node.tagname] - self.end_state(first=label + ': ') + indent = sum(self.stateindent) + len(label) + print(self.states[-1]) + if (len(self.states[-1]) == 1 and + self.states[-1][0][0] == 0 and + MAXWIDTH - indent >= sum(len(s) for s in self.states[-1][0][1])): + # short text: append text after admonition label + self.stateindent[-1] += len(label) + self.end_state(first=label + ': ') + else: + # long text: append label before the block + self.states[-1].insert(0, (0, [self.nl])) + self.end_state(first=label + ':') visit_attention = _visit_admonition depart_attention = _depart_admonition diff --git a/tests/test_build_text.py b/tests/test_build_text.py index 5b19f43ef..efdc0f5eb 100644 --- a/tests/test_build_text.py +++ b/tests/test_build_text.py @@ -31,16 +31,18 @@ def test_maxwitdh_with_prefix(app, status, warning): 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') + assert lines[0].startswith('See also:') + assert lines[1].startswith('') + assert lines[2].startswith(' ham') + assert lines[3].startswith(' ham') + assert lines[4] == '' + assert lines[5].startswith('* ham') + assert lines[6].startswith(' ham') + assert lines[7] == '' + assert lines[8].startswith('* ham') + assert lines[9].startswith(' ham') + assert lines[10] == '' + assert lines[11].startswith('spam egg') @with_text_app() diff --git a/tests/test_intl.py b/tests/test_intl.py index a6bd17512..080ee9221 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -315,7 +315,8 @@ def test_text_seealso(app): "\n*********************\n" "\nSee also: SHORT TEXT 1\n" "\nSee also: LONG TEXT 1\n" - "\nSee also: SHORT TEXT 2\n" + "\nSee also:\n" + "\n SHORT TEXT 2\n" "\n LONG TEXT 2\n") assert result == expect @@ -356,7 +357,9 @@ def test_text_figure_captions(app): "14.4. IMAGE UNDER NOTE\n" "======================\n" "\n" - "Note: [image: i18n under note][image]\n" + "Note:\n" + "\n" + " [image: i18n under note][image]\n" "\n" " [image: img under note][image]\n") assert result == expect