mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-04 14:14:12 -04:00
35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Kyoo.Controllers.Utility
|
|
{
|
|
public class Slugifier
|
|
{
|
|
public static string ToSlug(string showTitle)
|
|
{
|
|
if (showTitle == null)
|
|
return null;
|
|
|
|
//First to lower case
|
|
showTitle = showTitle.ToLowerInvariant();
|
|
|
|
//Remove all accents
|
|
//var bytes = Encoding.GetEncoding("Cyrillic").GetBytes(showTitle);
|
|
//showTitle = Encoding.ASCII.GetString(bytes);
|
|
|
|
//Replace spaces
|
|
showTitle = Regex.Replace(showTitle, @"\s", "-", RegexOptions.Compiled);
|
|
|
|
//Remove invalid chars
|
|
showTitle = Regex.Replace(showTitle, @"[^\w\s\p{Pd}]", "", RegexOptions.Compiled);
|
|
|
|
//Trim dashes from end
|
|
showTitle = showTitle.Trim('-', '_');
|
|
|
|
//Replace double occurences of - or \_
|
|
showTitle = Regex.Replace(showTitle, @"([-_]){2,}", "$1", RegexOptions.Compiled);
|
|
|
|
return showTitle;
|
|
}
|
|
}
|
|
}
|