Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions html4docx/h4d.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def set_initial_attrs(self, document=None):
self.list_restart_counter = 0
self.current_ol_num_id = None
self._list_num_ids = {}
self._ol_id_stack = [] # Stack to track parent ol IDs for nested lists

# NEW: Set style map & tag overrides according to options

Expand Down Expand Up @@ -1448,7 +1449,9 @@ def handle_starttag(self, tag, attrs):
return
elif tag in ['ol', 'ul']:
if tag == 'ol':
# Assign new ID if it's a fresh top-level list
# Push current ID to stack before creating new one (for nested lists)
self._ol_id_stack.append(self.current_ol_num_id)
# Assign new ID for this <ol>
self.list_restart_counter += 1
self.current_ol_num_id = self.list_restart_counter
else:
Expand Down Expand Up @@ -1609,7 +1612,11 @@ def handle_endtag(self, tag):
utils.remove_last_occurence(self.tags['list'], tag)
if tag == 'ol':
self._list_num_ids.pop(self.current_ol_num_id, None)
self.current_ol_num_id = None
# Restore parent's ol ID from stack (for nested lists)
if self._ol_id_stack:
self.current_ol_num_id = self._ol_id_stack.pop()
else:
self.current_ol_num_id = None
return
elif tag == 'table':
if self.include_tables:
Expand Down