using System.Text;
using System.Text.RegularExpressions;
namespace TightWiki.Engine
{
internal static class WikiUtility
{
///
/// Skips the namespace and returns just the page name part of the navigation.
///
///
///
internal static string GetPageNamePart(string navigation)
{
var parts = navigation.Trim(':').Trim().Split("::");
if (parts.Length > 1)
{
return string.Join('_', parts.Skip(1));
}
return navigation.Trim(':');
}
internal static string WarningCard(string header, string exceptionText)
{
var html = new StringBuilder();
html.Append("
");
html.Append($"
{header}
");
html.Append("
");
html.Append($"
{exceptionText}");
html.Append("
");
html.Append("
");
html.Append("
");
return html.ToString();
}
internal static List OrderMatchesByLengthDescending(MatchCollection matches)
{
var result = new List();
foreach (Match match in matches)
{
result.Add(new WikiOrderedMatch
{
Value = match.Value,
Index = match.Index
});
}
return result.OrderByDescending(o => o.Value.Length).ToList();
}
///
/// Gets a list of symbols where the symbol occurs consecutively, more than once. (e.g. "##This##")
///
///
///
internal static HashSet GetApplicableSymbols(string input)
{
var symbolCounts = new Dictionary();
char? previousChar = null;
int consecutiveCount = 0;
for (int i = 0; i < input.Length; i++)
{
char currentChar = input[i];
if (char.IsLetterOrDigit(currentChar) || char.IsWhiteSpace(currentChar))
{
continue;
}
if (previousChar.HasValue && currentChar == previousChar.Value)
{
consecutiveCount++;
if (consecutiveCount > 1)
{
symbolCounts.TryGetValue(previousChar.Value, out int count);
symbolCounts[previousChar.Value] = count + 1;
consecutiveCount = 1;
}
}
else
{
consecutiveCount = 1;
}
previousChar = currentChar;
}
return symbolCounts.Where(o => o.Value > 1).Select(o => o.Key).ToHashSet();
}
}
}