Improve the TitleNumberFixup rule

This commit is contained in:
Zoe Roux 2024-02-02 18:35:29 +01:00
parent bba1fd964d
commit 5264214eb3

View File

@ -113,21 +113,36 @@ class TitleNumberFixup(Rule):
if not episodes or len(episodes) < 2: if not episodes or len(episodes) < 2:
return return
episode = episodes[0] to_remove = []
prevs: List[Match] = matches.previous(episode) # type: ignore to_add = []
title = prevs[0] if len(prevs) > 0 and prevs[0].name == "title" else None for episode in episodes:
if not title: prevs: List[Match] = matches.previous(episode) # type: ignore
return title = prevs[0] if len(prevs) > 0 and prevs[0].tagged("title") else None
if not title:
continue
# do not fixup if there was a - or any separator between the title and the episode number # do not fixup if there was a - or any separator between the title and the episode number
holes = matches.holes(title.end, episode.start) holes = matches.holes(title.end, episode.start)
if holes: if holes:
return continue
newTitle = copy(title) to_remove.extend([title, episode])
newTitle.end = episode.end newTitle = copy(title)
newTitle.value = f"{title.value} {episode.value}" newTitle.end = episode.end
return [[title, episode], [newTitle]] newTitle.value = f"{title.value} {episode.value}"
# If an hole was created to parse the episode at the current pos, merge it back into the title
holes = matches.holes(episode.end)
if holes and holes[0].start == episode.end:
val: str = holes[0].value
if "-" in val:
val, *_ = val.split("-")
val = val.rstrip()
newTitle.value = f"{newTitle.value}{val}"
newTitle.end += len(val)
to_add.append(newTitle)
return [to_remove, to_add]
class MultipleSeasonRule(Rule): class MultipleSeasonRule(Rule):