diff --git a/CHANGES b/CHANGES index e0ec9b412..60d5fdfb2 100644 --- a/CHANGES +++ b/CHANGES @@ -107,6 +107,7 @@ Bugs fixed by `autodoc_mock_imports`. * #1953: ``Sphinx.add_node`` does not add handlers the translator installed by `html_translator_class` +* #1797: text builder inserts blank line on top Documentation ------------- diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py index 2463920ef..0c8488efc 100644 --- a/sphinx/writers/text.py +++ b/sphinx/writers/text.py @@ -281,8 +281,11 @@ class TextTranslator(nodes.NodeVisitor): char = '^' text = ''.join(x[1] for x in self.states.pop() if x[0] == -1) self.stateindent.pop() - self.states[-1].append( - (0, ['', text, '%s' % (char * column_width(text)), ''])) + title = ['', text, '%s' % (char * column_width(text)), ''] + if len(self.states) == 2 and len(self.states[-1]) == 0: + # remove an empty line before title if it is first section title in the document + title.pop(0) + self.states[-1].append((0, title)) def visit_subtitle(self, node): pass diff --git a/tests/test_build_text.py b/tests/test_build_text.py index 613d95d1f..f95e4d2ab 100644 --- a/tests/test_build_text.py +++ b/tests/test_build_text.py @@ -65,7 +65,7 @@ def test_nonascii_title_line(app, status, warning): app.builder.build_update() result = (app.outdir / 'nonascii_title.txt').text(encoding='utf-8') expect_underline = '******' - result_underline = result.splitlines()[2].strip() + result_underline = result.splitlines()[1].strip() assert expect_underline == result_underline diff --git a/tests/test_intl.py b/tests/test_intl.py index b31f1678b..42dfa56ef 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -126,7 +126,7 @@ def test_text_builder(app, status, warning): yield assert_re_search, warning_expr, warnings result = (app.outdir / 'warnings.txt').text(encoding='utf-8') - expect = (u"\nI18N WITH REST WARNINGS" + expect = (u"I18N WITH REST WARNINGS" u"\n***********************\n" u"\nLINE OF >>``<