handle cases where the first item isn't numbered
This commit is contained in:
parent
61a7eb07f9
commit
85ac6bc35c
|
@ -245,15 +245,19 @@ 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:
|
||||||
|
if pos >= len(e.stem):
|
||||||
|
i = 0
|
||||||
|
else:
|
||||||
n = NUMBER_REGEX.match(e.name[pos:])
|
n = NUMBER_REGEX.match(e.name[pos:])
|
||||||
if n is None:
|
if n is None:
|
||||||
return None
|
return None
|
||||||
|
@ -264,7 +268,8 @@ def unique_hierarchical_prefix_numbering(entries, start_point=0):
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue