49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
namespace LehrerApp.Templating;
|
|
|
|
internal static class SystemVariables
|
|
{
|
|
internal const string CurrentPage = "curPage";
|
|
internal const string MaximumPageNumber = "maxPageNum";
|
|
internal const string Today = "today";
|
|
|
|
private static readonly string[] Names = [CurrentPage, MaximumPageNumber, Today];
|
|
|
|
internal static bool IsStandalone(string value) =>
|
|
TryRead(value, 0, out var length) && length == value.Length;
|
|
|
|
internal static bool Contains(string value) => Names.Any(name =>
|
|
value.Contains("$$" + name, StringComparison.Ordinal));
|
|
|
|
internal static bool TryRead(string source, int index, out int length)
|
|
{
|
|
length = 0;
|
|
if (index < 0 || index + 2 > source.Length || source[index] != '$' || source[index + 1] != '$')
|
|
return false;
|
|
foreach (var name in Names)
|
|
{
|
|
var token = "$$" + name;
|
|
if (!source.AsSpan(index).StartsWith(token, StringComparison.Ordinal)) continue;
|
|
var end = index + token.Length;
|
|
if (end < source.Length && (char.IsLetterOrDigit(source[end]) || source[end] is '_' or '-' or '.'))
|
|
continue;
|
|
length = token.Length;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
internal static IEnumerable<(string Text, string? Name)> Split(string source)
|
|
{
|
|
var literalStart = 0;
|
|
for (var index = 0; index < source.Length;)
|
|
{
|
|
if (!TryRead(source, index, out var length)) { index++; continue; }
|
|
if (index > literalStart) yield return (source[literalStart..index], null);
|
|
yield return (source.Substring(index, length), source.Substring(index + 2, length - 2));
|
|
index += length;
|
|
literalStart = index;
|
|
}
|
|
if (literalStart < source.Length) yield return (source[literalStart..], null);
|
|
}
|
|
}
|