"""The tracked files and folders an item's text names, each as a backticked span such as `bin/rank_todo.py` or `bin/`.""" import re def named_paths(text: str, tracked: list[str]) -> list[tuple[str, int]]: """Each backticked span in text, in order and once, that names a tracked file or folder, with how many of the tracked files it holds: 1 for a file.""" held = ((span, sum(holds(span, path) for path in tracked)) for span in spans(text)) return [(span, count) for span, count in held if count] def named_files(text: str, tracked: list[str]) -> list[str]: """Each tracked file that a backticked span in text names, either the file itself or a folder holding it, once, in tracked's order.""" named = spans(text) return [path for path in tracked if any(holds(span, path) for span in named)] def spans(text: str) -> list[str]: return list(dict.fromkeys(span.removesuffix("/") for span in re.findall(r"`([^`\s]+)`", text))) def holds(span: str, path: str) -> bool: return path == span or path.startswith(f"{span}/")