From 604db47228689c512fd5f7eb75669b1a0f30e867 Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Tue, 7 Nov 2017 23:10:11 +0000 Subject: [PATCH] #3998: Toctree section numbering in plain text output --- sphinx/writers/text.py | 5 +++- tests/test_build_text.py | 51 +++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py index ce60164b2..b54bde38b 100644 --- a/sphinx/writers/text.py +++ b/sphinx/writers/text.py @@ -1002,7 +1002,10 @@ class TextTranslator(nodes.NodeVisitor): def visit_reference(self, node): # type: (nodes.Node) -> None - pass + if self.add_secnumbers: + numbers = node.get("secnumber") + if numbers is not None: + self.add_text('.'.join(map(str, numbers)) + self.secnumber_suffix) def depart_reference(self, node): # type: (nodes.Node) -> None diff --git a/tests/test_build_text.py b/tests/test_build_text.py index 6e9aec4b6..2651f35ce 100644 --- a/tests/test_build_text.py +++ b/tests/test_build_text.py @@ -115,7 +115,20 @@ def test_list_items_in_admonition(app, status, warning): @with_text_app() def test_secnums(app, status, warning): app.builder.build_all() - result = (app.outdir / 'doc2.txt').text(encoding='utf8') + contents = (app.outdir / 'contents.txt').text(encoding='utf8') + expect = ( + "* Section A\n" + "\n" + "* Section B\n" + "\n" + " * Sub Ba\n" + "\n" + " * Sub Bb\n" + "\n" + "* 日本語\n" + ) + assert contents == expect + doc2 = (app.outdir / 'doc2.txt').text(encoding='utf8') expect = ( "Section B\n" "*********\n" @@ -128,11 +141,24 @@ def test_secnums(app, status, warning): "Sub Bb\n" "======\n" ) - assert result == expect + assert doc2 == expect app.config.text_add_secnumbers = True app.builder.build_all() - result = (app.outdir / 'doc2.txt').text(encoding='utf8') + contents = (app.outdir / 'contents.txt').text(encoding='utf8') + expect = ( + "* 1. Section A\n" + "\n" + "* 2. Section B\n" + "\n" + " * 2.1. Sub Ba\n" + "\n" + " * 2.2. Sub Bb\n" + "\n" + "* 3. 日本語\n" + ) + assert contents == expect + doc2 = (app.outdir / 'doc2.txt').text(encoding='utf8') expect = ( "2. Section B\n" "************\n" @@ -145,11 +171,24 @@ def test_secnums(app, status, warning): "2.2. Sub Bb\n" "===========\n" ) - assert result == expect + assert doc2 == expect app.config.text_secnumber_suffix = " " app.builder.build_all() - result = (app.outdir / 'doc2.txt').text(encoding='utf8') + contents = (app.outdir / 'contents.txt').text(encoding='utf8') + expect = ( + "* 1 Section A\n" + "\n" + "* 2 Section B\n" + "\n" + " * 2.1 Sub Ba\n" + "\n" + " * 2.2 Sub Bb\n" + "\n" + "* 3 日本語\n" + ) + assert contents == expect + doc2 = (app.outdir / 'doc2.txt').text(encoding='utf8') expect = ( "2 Section B\n" "***********\n" @@ -162,5 +201,5 @@ def test_secnums(app, status, warning): "2.2 Sub Bb\n" "==========\n" ) - assert result == expect + assert doc2 == expect