| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Api.Services.ExternalLinks; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Parses blob filenames to derive slugs and titles. |
| | | 8 | | /// Handles SRD filename conventions (e.g., "srd-2024_animated-armor.json"). |
| | | 9 | | /// </summary> |
| | | 10 | | public static partial class BlobFilenameParser |
| | | 11 | | { |
| | | 12 | | // Matches any sequence of non-alphanumeric characters (to replace with single hyphen) |
| | | 13 | | [GeneratedRegex(@"[^a-z0-9]+", RegexOptions.Compiled)] |
| | | 14 | | private static partial Regex NonSlugCharsPattern(); |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Derives a slug from a blob filename. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="filename">Full filename including extension (e.g., "srd-2024_animated-armor.json").</param> |
| | | 20 | | /// <returns>Normalized slug (e.g., "animated-armor"), or empty string if normalization fails.</returns> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// Rules: |
| | | 23 | | /// 1. Remove .json extension |
| | | 24 | | /// 2. If contains underscore, take substring after first underscore |
| | | 25 | | /// 3. Otherwise, use entire base filename |
| | | 26 | | /// 4. Normalize: |
| | | 27 | | /// - Convert to lowercase |
| | | 28 | | /// - Replace runs of non-alphanumeric chars with single hyphen (preserves word boundaries) |
| | | 29 | | /// - Trim leading/trailing hyphens |
| | | 30 | | /// - Collapse multiple consecutive hyphens to single hyphen |
| | | 31 | | /// 5. If result is empty, return empty string (caller should skip and log warning) |
| | | 32 | | /// </remarks> |
| | | 33 | | public static string DeriveSlug(string filename) |
| | | 34 | | { |
| | 0 | 35 | | if (string.IsNullOrWhiteSpace(filename)) |
| | | 36 | | { |
| | 0 | 37 | | return string.Empty; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // Remove .json extension if present |
| | 0 | 41 | | var baseName = filename.EndsWith(".json", StringComparison.OrdinalIgnoreCase) |
| | 0 | 42 | | ? filename[..^5] |
| | 0 | 43 | | : filename; |
| | | 44 | | |
| | | 45 | | // If contains underscore, take substring after first underscore |
| | 0 | 46 | | var underscoreIndex = baseName.IndexOf('_'); |
| | 0 | 47 | | var slug = underscoreIndex >= 0 |
| | 0 | 48 | | ? baseName[(underscoreIndex + 1)..] |
| | 0 | 49 | | : baseName; |
| | | 50 | | |
| | | 51 | | // Normalize slug: preserve word boundaries by replacing non-alphanumeric runs with hyphens |
| | 0 | 52 | | slug = NormalizeSlug(slug); |
| | | 53 | | |
| | | 54 | | // Guard: If normalization produced empty slug, try fallback with full base name |
| | 0 | 55 | | if (string.IsNullOrWhiteSpace(slug)) |
| | | 56 | | { |
| | 0 | 57 | | slug = NormalizeSlug(baseName); |
| | | 58 | | |
| | | 59 | | // If still empty after fallback, return empty (caller MUST skip and log warning) |
| | 0 | 60 | | if (string.IsNullOrWhiteSpace(slug)) |
| | | 61 | | { |
| | 0 | 62 | | return string.Empty; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return slug; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Normalizes a string to a valid slug format. |
| | | 71 | | /// Preserves word boundaries by replacing non-alphanumeric runs with single hyphens. |
| | | 72 | | /// </summary> |
| | | 73 | | private static string NormalizeSlug(string input) |
| | | 74 | | { |
| | 0 | 75 | | if (string.IsNullOrWhiteSpace(input)) |
| | | 76 | | { |
| | 0 | 77 | | return string.Empty; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Convert to lowercase |
| | 0 | 81 | | var normalized = input.ToLowerInvariant(); |
| | | 82 | | |
| | | 83 | | // Replace any run of non-alphanumeric characters with a single hyphen |
| | | 84 | | // This preserves word boundaries: "hello world!" -> "hello-world" |
| | 0 | 85 | | normalized = NonSlugCharsPattern().Replace(normalized, "-"); |
| | | 86 | | |
| | | 87 | | // Trim leading and trailing hyphens |
| | 0 | 88 | | normalized = normalized.Trim('-'); |
| | | 89 | | |
| | | 90 | | // Collapse multiple consecutive hyphens to single hyphen |
| | | 91 | | // (shouldn't happen with regex above, but defensive) |
| | 0 | 92 | | while (normalized.Contains("--", StringComparison.Ordinal)) |
| | | 93 | | { |
| | 0 | 94 | | normalized = normalized.Replace("--", "-"); |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | return normalized; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Prettifies a slug for display as a title. |
| | | 102 | | /// Culture-invariant and deterministic. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="slug">Slug to prettify (e.g., "animated-armor").</param> |
| | | 105 | | /// <returns>Human-readable title (e.g., "Animated Armor").</returns> |
| | | 106 | | public static string PrettifySlug(string slug) |
| | | 107 | | { |
| | 0 | 108 | | if (string.IsNullOrWhiteSpace(slug)) |
| | | 109 | | { |
| | 0 | 110 | | return string.Empty; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | // Replace hyphens with spaces and title case each word (culture-invariant) |
| | 0 | 114 | | var words = slug.Split('-', StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 115 | | var sb = new StringBuilder(); |
| | | 116 | | |
| | 0 | 117 | | foreach (var word in words) |
| | | 118 | | { |
| | 0 | 119 | | if (sb.Length > 0) |
| | | 120 | | { |
| | 0 | 121 | | sb.Append(' '); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // Title case: first letter uppercase (invariant), rest lowercase |
| | 0 | 125 | | if (word.Length > 0) |
| | | 126 | | { |
| | 0 | 127 | | sb.Append(char.ToUpperInvariant(word[0])); |
| | 0 | 128 | | if (word.Length > 1) |
| | | 129 | | { |
| | 0 | 130 | | sb.Append(word[1..].ToLowerInvariant()); |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | return sb.ToString(); |
| | | 136 | | } |
| | | 137 | | } |