Skip to content

Commit 21accd1

Browse files
committed
Rewrite of lists formatting.
Correctly supports basic list formatting, but not yet non-standard ordered lists (like other types of bullets, other starting number).
1 parent bf680c6 commit 21accd1

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

sphinxcontrib/writers/rst.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self, document, builder):
5757
self.states = [[]]
5858
self.stateindent = [0]
5959
self.list_counter = []
60+
self.list_formatter = []
6061
self.sectionlevel = 0
6162
self.table = None
6263
if self.builder.config.rst_indent:
@@ -477,33 +478,35 @@ def visit_transition(self, node):
477478
raise nodes.SkipNode
478479

479480
def visit_bullet_list(self, node):
480-
self.list_counter.append(-1)
481+
def bullet_list_format(counter):
482+
return '*'
483+
self.list_counter.append(-1) # TODO: just 0 is fine.
484+
self.list_formatter.append(bullet_list_format)
481485
def depart_bullet_list(self, node):
482486
self.list_counter.pop()
487+
self.list_formatter.pop()
483488

484489
def visit_enumerated_list(self, node):
490+
def enumerated_list_format(counter):
491+
return str(counter) + '.'
485492
self.list_counter.append(0)
493+
self.list_formatter.append(enumerated_list_format)
486494
def depart_enumerated_list(self, node):
487495
self.list_counter.pop()
496+
self.list_formatter.pop()
488497

489498
def visit_list_item(self, node):
490-
if self.list_counter[-1] == -1:
491-
# bullet list
492-
self.new_state(self.indent)
493-
elif self.list_counter[-1] == -2:
494-
# definition list
495-
pass
496-
else:
497-
# enumerated list
498-
self.list_counter[-1] += 1
499-
self.new_state(len(str(self.list_counter[-1])) + self.indent)
499+
self.list_counter[-1] += 1
500+
bullet_formatter = self.list_formatter[-1]
501+
bullet = bullet_formatter(self.list_counter[-1])
502+
indent = max(self.indent, len(bullet) + 1)
503+
self.new_state(indent)
500504
def depart_list_item(self, node):
501-
if self.list_counter[-1] == -1:
502-
self.end_state(first='* ', end=None)
503-
elif self.list_counter[-1] == -2:
504-
pass
505-
else:
506-
self.end_state(first='%s. ' % self.list_counter[-1], end=None)
505+
# formatting to make the string `self.stateindent[-1]` chars long.
506+
format = '%%-%ds' % (self.stateindent[-1])
507+
bullet_formatter = self.list_formatter[-1]
508+
bullet = format % bullet_formatter(self.list_counter[-1])
509+
self.end_state(first=bullet, end=None)
507510

508511
def visit_definition_list(self, node):
509512
pass

tests/test_rst_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from tests.utils import run_parse_test
22

3+
import pytest
4+
35

46
def test_bullet_list(src_dir, expected_dir, output_dir):
57
run_parse_test(src_dir, expected_dir, output_dir, 'common', ['bullet-list'])
@@ -17,10 +19,12 @@ def test_multiline_list(src_dir, expected_dir, output_dir):
1719
run_parse_test(src_dir, expected_dir, output_dir, 'common', ['multiline-list'])
1820

1921

22+
@pytest.mark.skip(reason="work in progress")
2023
def test_ordered_list_properties(src_dir, expected_dir, output_dir):
2124
run_parse_test(src_dir, expected_dir, output_dir, 'common', ['ordered-list-properties'])
2225

2326

27+
@pytest.mark.skip(reason="work in progress")
2428
def test_bullet_list_consecutive(src_dir, expected_dir, output_dir):
2529
run_parse_test(src_dir, expected_dir, output_dir, 'common', ['bullet-list-consecutive'])
2630

tox.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ deps =
2424
sphinx1.7: Sphinx >= 1.7, < 1.8
2525
sphinx1.8: Sphinx >= 1.8, < 2.0
2626
sphinx2.0: Sphinx >= 2.0, < 2.1
27+
sphinx2.1: Sphinx >= 2.1, < 2.2
28+
sphinx2.2: Sphinx >= 2.2, < 2.3
29+
sphinx2.3: Sphinx >= 2.3, < 2.4
2730
sphinx2.4: Sphinx >= 2.4, < 3.0
2831
sphinx3.0: Sphinx >= 3.0, < 3.1
2932
sphinx3.4: Sphinx >= 3.4, < 4.0
@@ -33,5 +36,5 @@ commands =
3336

3437
[pytest]
3538
filterwarnings =
36-
; suppress FutureWarning in Sphinx 1.0...2.0 about Node.traverse()
39+
; suppress FutureWarning in Sphinx 1.0...2.2 about Node.traverse()
3740
ignore::FutureWarning:sphinx.util.nodes

0 commit comments

Comments
 (0)