2022-06-05 18:44:51 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using Slugify;
|
|
|
|
|
|
|
|
namespace Nitride.Slugs;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A default implementation of ISlugProvider.
|
|
|
|
/// </summary>
|
|
|
|
public class SimpleSlugConverter : ISlugConverter, IEnumerable<string>
|
|
|
|
{
|
2022-06-05 19:03:28 +00:00
|
|
|
private readonly List<(string, string, StringComparison)> replacements;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
public SimpleSlugConverter()
|
|
|
|
{
|
2022-06-05 19:03:28 +00:00
|
|
|
this.replacements = new List<(string, string, StringComparison)>();
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:03:28 +00:00
|
|
|
public SimpleSlugConverter(
|
|
|
|
IDictionary<string, string> replacements,
|
|
|
|
StringComparison comparison = StringComparison.InvariantCultureIgnoreCase)
|
2022-06-05 18:44:51 +00:00
|
|
|
: this()
|
|
|
|
{
|
|
|
|
foreach (KeyValuePair<string, string> pair in replacements)
|
|
|
|
{
|
2022-06-05 19:03:28 +00:00
|
|
|
this.Add(pair.Key, pair.Value, comparison);
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-06-05 19:03:28 +00:00
|
|
|
/// Adds a replacement for the resulting slug. This is applied before the final
|
|
|
|
/// conversion
|
|
|
|
/// into a slug but after any other added replacements.
|
2022-06-05 18:44:51 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="search">The text to search for.</param>
|
|
|
|
/// <param name="replace">The replacement string.</param>
|
2022-06-05 19:03:28 +00:00
|
|
|
/// <param name="comparison">
|
|
|
|
/// The comparison to use, defaults to invariant ignore
|
|
|
|
/// case.
|
|
|
|
/// </param>
|
|
|
|
public void Add(
|
|
|
|
string search,
|
|
|
|
string replace,
|
|
|
|
StringComparison comparison = StringComparison.InvariantCultureIgnoreCase)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
2022-06-05 19:03:28 +00:00
|
|
|
this.replacements.Add((search, replace, comparison));
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public IEnumerator<string> GetEnumerator()
|
|
|
|
{
|
2022-07-09 04:52:10 +00:00
|
|
|
return this.replacements.Select(x => x.Item1)
|
|
|
|
.GetEnumerator();
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public virtual string ToSlug(string input)
|
|
|
|
{
|
|
|
|
// If we have null or whitespace, we have a problem.
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Cannot have a blank or null input", nameof(input));
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:03:28 +00:00
|
|
|
// We need to do the replacements before we slugify.
|
2022-06-05 18:44:51 +00:00
|
|
|
// Perform any additional replacements.
|
2022-06-05 19:03:28 +00:00
|
|
|
foreach ((string search, string replace, StringComparison comparison) in this.replacements)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
2022-06-05 19:03:28 +00:00
|
|
|
input = input.Replace(search, replace, comparison);
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:03:28 +00:00
|
|
|
// Create a slug.
|
|
|
|
var helper = new SlugHelper();
|
|
|
|
string output = helper.GenerateSlug(input);
|
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
|
|
|
return this.GetEnumerator();
|
|
|
|
}
|
|
|
|
}
|