Sanitize text for use in filenames. - Convert to lowercase - Remove quotes, colons, backticks - Replace other unsafe characters with dashes - Convert any whitespace to single space - Strip leading/trailing spaces and dashes
(text: str)
| 382 | |
| 383 | @staticmethod |
| 384 | def _sanitize_filename_part(text: str) -> str: |
| 385 | """ |
| 386 | Sanitize text for use in filenames. |
| 387 | - Convert to lowercase |
| 388 | - Remove quotes, colons, backticks |
| 389 | - Replace other unsafe characters with dashes |
| 390 | - Convert any whitespace to single space |
| 391 | - Strip leading/trailing spaces and dashes |
| 392 | """ |
| 393 | # Convert to lowercase |
| 394 | text = text.lower() |
| 395 | |
| 396 | # Normalize all whitespace to single spaces |
| 397 | text = re.sub(r'\s+', ' ', text) |
| 398 | |
| 399 | # Remove quotes, colons, backticks entirely (don't replace with dash) |
| 400 | text = re.sub(r'["\':´`]', '', text) |
| 401 | |
| 402 | # Replace other unsafe characters (from UNSAFE_CHARS_PATTERN) with dash |
| 403 | # This covers: < > " / \ | ? * and control characters |
| 404 | # Note: we already removed quotes and colons above |
| 405 | text = SlugGenerator.UNSAFE_CHARS_PATTERN.sub('-', text) |
| 406 | |
| 407 | # Replace other non-alphanumeric (except space, dash, and dot) with dash |
| 408 | text = re.sub(r'[^a-z0-9\s.\-]+', '-', text) |
| 409 | |
| 410 | # Replace multiple consecutive dashes with single dash (but preserve spaces) |
| 411 | text = re.sub(r'-+', '-', text) |
| 412 | |
| 413 | # Remove trailing dashes before we clean up space-dash sequences |
| 414 | text = text.rstrip('-') |
| 415 | |
| 416 | # Handle " -" and "- " sequences: collapse to single space |
| 417 | text = re.sub(r'\s*-\s*', ' ', text) |
| 418 | |
| 419 | # Replace multiple consecutive spaces with single space |
| 420 | text = re.sub(r'\s+', ' ', text) |
| 421 | |
| 422 | # Strip leading/trailing spaces |
| 423 | text = text.strip(' ') |
| 424 | |
| 425 | return text |
| 426 | |
| 427 | |
| 428 | class VersionSection: |
no test coverage detected