Skip to content
This repository has been archived by the owner on Mar 26, 2022. It is now read-only.

Commit

Permalink
Support rendering multi-line list items to rst
Browse files Browse the repository at this point in the history
For list items going across multiple lines, the renderer previously did
not indent the latter lines, which leads to invalid rst (see
https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#bullet-lists;
subsequent list items must line up with the first item).

This commit fixes that by introducing an indent when entering a list
item.

Note that there are still some issues (for example, paragraphs within
list items are still not handled correctly), but this at least seems to
be a step in the right direction.
  • Loading branch information
charmasaur committed Aug 18, 2020
1 parent 1b4ede8 commit 1953d21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions commonmark/render/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ def list(self, node, entering):
self.cr()

def item(self, node, entering):
tagname = '*' if node.list_data['type'] == 'bullet' else '#.'
tagname = '* ' if node.list_data['type'] == 'bullet' else '#. '

if entering:
self.out(tagname + ' ')
self.out(tagname)
self.indent_length += len(tagname)
else:
self.indent_length -= len(tagname)
self.cr()

def block_quote(self, node, entering):
Expand Down
18 changes: 18 additions & 0 deletions commonmark/tests/rst_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ def test_ordered_list(self):
#. One
#. Two
#. Three
"""
self.assertEqualRender(src_markdown, expected_rst)

def test_ordered_list_with_multi_line_items(self):
src_markdown = """
This is an ordered list with multi-line items:
1. First item,
with lazy indentation.
2. Second item,
with normal indentation.
"""
expected_rst = """
This is an ordered list with multi-line items:
#. First item,
with lazy indentation.
#. Second item,
with normal indentation.
"""
self.assertEqualRender(src_markdown, expected_rst)

Expand Down

0 comments on commit 1953d21

Please sign in to comment.