DocSpace-buildtools/common/ASC.Textile/Blocks/NoTextileEncoder.cs

80 lines
3.4 KiB
C#
Raw Normal View History

2019-08-19 12:35:39 +00:00
namespace Textile.Blocks
{
public static class NoTextileEncoder
{
private static readonly string[,] TextileModifiers = {
{ "\"", """ },
{ "%", "%" },
{ "*", "*" },
{ "+", "+" },
{ "-", "-" },
{ "<", "&lt;" }, // or "&#60;"
{ "=", "&#61;" },
{ ">", "&gt;" }, // or "&#62;"
{ "?", "&#63;" },
{ "^", "&#94;" },
{ "_", "&#95;" },
{ "~", "&#126;" },
{ "@", "&#64;" },
{ "'", "&#39;" },
{ "|", "&#124;" },
{ "!", "&#33;" },
{ "(", "&#40;" },
{ ")", "&#41;" },
{ ".", "&#46;" },
{ "x", "&#120;" }
};
public static string EncodeNoTextileZones(string tmp, string patternPrefix, string patternSuffix)
{
return EncodeNoTextileZones(tmp, patternPrefix, patternSuffix, null);
}
public static string EncodeNoTextileZones(string tmp, string patternPrefix, string patternSuffix, string[] exceptions)
{
string evaluator(Match m)
{
var toEncode = m.Groups["notex"].Value;
2022-01-13 12:53:57 +00:00
if (toEncode.Length == 0)
2019-08-19 12:35:39 +00:00
{
return string.Empty;
}
for (var i = 0; i < TextileModifiers.GetLength(0); ++i)
{
if (exceptions == null || Array.IndexOf(exceptions, TextileModifiers[i, 0]) < 0)
{
toEncode = toEncode.Replace(TextileModifiers[i, 0], TextileModifiers[i, 1]);
}
}
return patternPrefix + toEncode + patternSuffix;
}
2022-01-14 13:12:37 +00:00
tmp = Regex.Replace(tmp, "("+ patternPrefix + "(?<notex>.+?)" + patternSuffix + ")*", new MatchEvaluator(evaluator));
2019-08-19 12:35:39 +00:00
return tmp;
}
public static string DecodeNoTextileZones(string tmp, string patternPrefix, string patternSuffix)
{
return DecodeNoTextileZones(tmp, patternPrefix, patternSuffix, null);
}
public static string DecodeNoTextileZones(string tmp, string patternPrefix, string patternSuffix, string[] exceptions)
{
string evaluator(Match m)
{
var toEncode = m.Groups["notex"].Value;
for (var i = 0; i < TextileModifiers.GetLength(0); ++i)
{
if (exceptions == null || Array.IndexOf(exceptions, TextileModifiers[i, 0]) < 0)
{
toEncode = toEncode.Replace(TextileModifiers[i, 1], TextileModifiers[i, 0]);
}
}
return toEncode;
}
2022-01-14 13:12:37 +00:00
tmp = Regex.Replace(tmp, "(" + patternPrefix + "(?<notex>.+?)" + patternSuffix + ")*", new MatchEvaluator(evaluator));
2019-08-19 12:35:39 +00:00
return tmp;
}
}
}