2021-12-20 16:22:40 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Web;
|
|
|
|
using Nettle.Compiler;
|
|
|
|
using Nettle.Functions;
|
|
|
|
|
|
|
|
namespace Wabbajack.Server.Extensions;
|
|
|
|
|
|
|
|
public static class NettleFunctions
|
|
|
|
{
|
|
|
|
public static INettleCompiler RegisterWJFunctions(this INettleCompiler compiler)
|
|
|
|
{
|
|
|
|
compiler.RegisterFunction(new Escape());
|
|
|
|
return compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class UrlEncode : FunctionBase
|
|
|
|
{
|
|
|
|
public UrlEncode() : base()
|
|
|
|
{
|
|
|
|
DefineRequiredParameter("text", "text to encode", typeof(string));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override object GenerateOutput(TemplateContext context, params object[] parameterValues)
|
|
|
|
{
|
|
|
|
var value = GetParameterValue<string>("text", parameterValues);
|
|
|
|
return HttpUtility.UrlEncode(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string Description => "URL encodes a string";
|
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class Escape : FunctionBase
|
|
|
|
{
|
|
|
|
public Escape() : base()
|
|
|
|
{
|
|
|
|
DefineRequiredParameter("text", "text to escape", typeof(string));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override object GenerateOutput(TemplateContext context, params object[] parameterValues)
|
|
|
|
{
|
|
|
|
var value = GetParameterValue<string>("text", parameterValues);
|
2021-12-20 16:46:35 +00:00
|
|
|
return Regex.Escape(value).Replace("'", "\\'");
|
2021-12-20 16:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string Description => "Escapes a string";
|
|
|
|
}
|
|
|
|
}
|