handle cases where the first item isn't numbered

This commit is contained in:
xenofem 2024-02-06 13:00:44 -05:00
parent 61a7eb07f9
commit 85ac6bc35c

View file

@ -245,26 +245,31 @@ def complete_prefix_number_ordering(entries):
return result return result
def unique_hierarchical_prefix_numbering(entries, start_point=0): def unique_hierarchical_prefix_numbering(entries, start_point=0):
matches = reversed(list(NUMBER_REGEX.finditer(entries[0].name))) longest_entry = max(entries, key=lambda e: len(e.name))
matches = reversed(list(NUMBER_REGEX.finditer(longest_entry.name)))
for m in matches: for m in matches:
pos = m.start() pos = m.start()
if pos < start_point: if pos < start_point:
return None return None
prefix = entries[0].name[:pos] prefix = longest_entry.name[:pos]
if all(e.name.startswith(prefix) for e in entries): if all(e.name.startswith(prefix) or prefix.startswith(e.stem) for e in entries):
numbering = {} numbering = {}
for e in entries: for e in entries:
n = NUMBER_REGEX.match(e.name[pos:]) if pos >= len(e.stem):
if n is None: i = 0
return None else:
i = int(n.group()) n = NUMBER_REGEX.match(e.name[pos:])
if n is None:
return None
i = int(n.group())
numbering.setdefault((i,), []).append(e) numbering.setdefault((i,), []).append(e)
indices = list(numbering.keys()) indices = list(numbering.keys())
for idx in indices: for idx in indices:
if len(numbering[idx]) > 1: if len(numbering[idx]) > 1:
ents_idx = numbering.pop(idx) ents_idx = numbering.pop(idx)
next_layer_start = pos + NUMBER_REGEX.match(ents_idx[0].name[pos:]).end() longest = max(ents_idx, key=lambda e: len(e.name))
next_layer_start = pos + NUMBER_REGEX.match(longest.name[pos:]).end()
sub_numbering = unique_hierarchical_prefix_numbering(ents_idx, start_point=next_layer_start) or alphabetic_numbering(ents_idx, next_layer_start) sub_numbering = unique_hierarchical_prefix_numbering(ents_idx, start_point=next_layer_start) or alphabetic_numbering(ents_idx, next_layer_start)
if not sub_numbering: if not sub_numbering:
return None return None