Skip to content

Commit 77f95de

Browse files
author
Jason Ward
committed
refs #24: Early return if the first list element is also the last.
1 parent 096842a commit 77f95de

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

pydocx/DocxParser.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,29 @@ def parse_list(self, el, text):
426426
return self.parse_table_cell_contents(el, parsed)
427427
return parsed
428428

429+
def _build_list(self, el, text):
430+
# Get the list style for the pending list.
431+
lst_style = self.get_list_style(
432+
el.num_id,
433+
el.ilvl,
434+
)
435+
436+
parsed = text
437+
# Create the actual list and return it.
438+
if lst_style == 'bullet':
439+
return self.unordered_list(parsed)
440+
else:
441+
return self.ordered_list(
442+
parsed,
443+
lst_style,
444+
)
445+
429446
def _parse_list(self, el, text):
430447
parsed = self.parse_list_item(el, text)
431448
num_id = el.num_id
432449
ilvl = el.ilvl
450+
if el.is_last_list_item_in_root:
451+
return self._build_list(el, parsed)
433452
next_el = el.next
434453

435454
def is_same_list(next_el, num_id, ilvl):
@@ -488,20 +507,7 @@ def should_parse_last_el(last_el, first_el):
488507
if parsed == '':
489508
return parsed
490509

491-
# Get the list style for the pending list.
492-
lst_style = self.get_list_style(
493-
el.num_id,
494-
el.ilvl,
495-
)
496-
497-
# Create the actual list and return it.
498-
if lst_style == 'bullet':
499-
return self.unordered_list(parsed)
500-
else:
501-
return self.ordered_list(
502-
parsed,
503-
lst_style,
504-
)
510+
return self._build_list(el, parsed)
505511

506512
def parse_p(self, el, text):
507513
if text == '':

0 commit comments

Comments
 (0)