diff --git a/pydocx/DocxParser.py b/pydocx/DocxParser.py index 14309803..e13604f1 100644 --- a/pydocx/DocxParser.py +++ b/pydocx/DocxParser.py @@ -461,10 +461,32 @@ def parse_list(self, el, text): return self.parse_table_cell_contents(el, parsed) return parsed + def _build_list(self, el, text): + # Get the list style for the pending list. + lst_style = self.get_list_style( + el.num_id.num_id, + el.ilvl, + ) + + parsed = text + # Create the actual list and return it. + if lst_style == 'bullet': + return self.unordered_list(parsed) + else: + return self.ordered_list( + parsed, + lst_style, + ) + def _parse_list(self, el, text): parsed = self.parse_list_item(el, text) num_id = el.num_id ilvl = el.ilvl + # Everything after this point assumes the first element is not also the + # last. If the first element is also the last then early return by + # building and returning the completed list. + if el.is_last_list_item_in_root: + return self._build_list(el, parsed) next_el = el.next def is_same_list(next_el, num_id, ilvl): @@ -523,20 +545,7 @@ def should_parse_last_el(last_el, first_el): if parsed == '': return parsed - # Get the list style for the pending list. - lst_style = self.get_list_style( - el.num_id.num_id, - el.ilvl, - ) - - # Create the actual list and return it. - if lst_style == 'bullet': - return self.unordered_list(parsed) - else: - return self.ordered_list( - parsed, - lst_style, - ) + return self._build_list(el, parsed) def parse_p(self, el, text): if text == '': diff --git a/pydocx/parsers/Docx2Html.py b/pydocx/parsers/Docx2Html.py index 87b8ed49..f97e39be 100644 --- a/pydocx/parsers/Docx2Html.py +++ b/pydocx/parsers/Docx2Html.py @@ -147,16 +147,17 @@ def page_break(self): def indent(self, text, just='', firstLine='', left='', right=''): slug = '

Center Justified
-

Right justified

-

-Right justified and pushed in from right

-

-Center justified and pushed in from left and it is -great and it is the coolest thing of all time and I like it and -I think it is cool

- -Left justified and pushed in from left

-''') + +

+

Center Justified
+

+

+

Right justified
+

+

+

+ Right justified and pushed in from right +
+

+

+

+ Center justified and pushed in from left and it is + great and it is the coolest thing of all time and I like it and + I think it is cool +
+

+

+

+ Left justified and pushed in from left +
+

+ + ''') def _converter(*args, **kwargs): diff --git a/pydocx/tests/test_xml.py b/pydocx/tests/test_xml.py index d5c6e6d3..c1a5bf8a 100644 --- a/pydocx/tests/test_xml.py +++ b/pydocx/tests/test_xml.py @@ -718,6 +718,34 @@ def get_xml(self): return xml +class SingleListItem(_TranslationTestCase): + expected_output = ''' + +
    +
  1. AAA
  2. +
+

BBB

+ + ''' + + numbering_dict = { + '1': { + '0': 'lowerLetter', + } + } + + def get_xml(self): + li = DXB.li(text='AAA', ilvl=0, numId=1) + p_tags = [ + DXB.p_tag('BBB'), + ] + body = li + for p_tag in p_tags: + body += p_tag + xml = DXB.xml(body) + return xml + + class SimpleTableTest(_TranslationTestCase): expected_output = '''