File size: 1,275 Bytes
18b6a95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Utility for parsing chapter range expressions like "1-100", "1,5,9", "1-50,70-90".
"""
from __future__ import annotations

import re


def parse_chapter_range(expr: str, max_chapter: int = 99999) -> list[int]:
    """
    Parse expressions like:
      ""          -> all chapters (empty list = no filter)
      "1-100"     -> 1 through 100
      "50"        -> chapter 50 only
      "1,5,9,20"  -> specific chapters
      "1-50,70-90" -> ranges combined
    Returns sorted list of chapter indices, or empty list meaning "all".
    """
    expr = expr.strip()
    if not expr or expr.lower() in ("all", "*"):
        return []

    indices: set[int] = set()
    parts = re.split(r",\s*", expr)
    for part in parts:
        part = part.strip()
        if not part:
            continue
        m = re.match(r"^(\d+)\s*-\s*(\d+)$", part)
        if m:
            start, end = int(m.group(1)), int(m.group(2))
            indices.update(range(min(start, end), max(start, end) + 1))
        elif re.match(r"^\d+$", part):
            indices.add(int(part))
        # ignore invalid parts

    return sorted(indices)


def format_range_hint(total: int) -> str:
    if total == 0:
        return "No chapters found yet"
    return f"e.g. all, 1-{total}, 1-50,70-90, 5,10,15"