diff --git a/Jellyfin.Plugin.Webhook/Helpers/HandlebarsFunctionHelpers.cs b/Jellyfin.Plugin.Webhook/Helpers/HandlebarsFunctionHelpers.cs index bf80e2d..6b886ec 100644 --- a/Jellyfin.Plugin.Webhook/Helpers/HandlebarsFunctionHelpers.cs +++ b/Jellyfin.Plugin.Webhook/Helpers/HandlebarsFunctionHelpers.cs @@ -10,6 +10,71 @@ namespace Jellyfin.Plugin.Webhook.Helpers; /// public static class HandlebarsFunctionHelpers { + private static readonly HandlebarsBlockHelper StringEqualityAllHelper = (output, options, context, arguments) => + { + if (arguments.Length < 2) + { + throw new HandlebarsException("{{if_all}} helper requires at least two arguments"); + } + + if (arguments.Length % 2 != 0) + { + throw new HandlebarsException("{{if_all}} helper must have an even number of arguments"); + } + + bool isMatch = true; + for (int i = 0; i < arguments.Length; i += 2) + { + if (!string.Equals(GetStringValue(arguments[i]), GetStringValue(arguments[i + 1]), StringComparison.OrdinalIgnoreCase)) + { + isMatch = false; + break; + } + } + + if (isMatch) + { + options.Template(output, context); + } + else + { + options.Inverse(output, context); + } + }; + + private static readonly HandlebarsBlockHelper StringEqualityAnyHelper = (output, options, context, arguments) => + { + if (arguments.Length < 2) + { + throw new HandlebarsException("{{if_any}} helper requires at least two arguments"); + } + + if (arguments.Length % 2 != 0) + { + throw new HandlebarsException("{{if_any}} helper must have an even number of arguments"); + } + + var argument = GetStringValue(arguments[0]); + bool isMatch = false; + for (int i = 0; i < arguments.Length; i += 2) + { + if (string.Equals(GetStringValue(arguments[i]), GetStringValue(arguments[i + 1]), StringComparison.OrdinalIgnoreCase)) + { + isMatch = true; + break; + } + } + + if (isMatch) + { + options.Template(output, context); + } + else + { + options.Inverse(output, context); + } + }; + private static readonly HandlebarsBlockHelper StringEqualityHelper = (output, options, context, arguments) => { if (arguments.Length != 2) @@ -64,6 +129,8 @@ public static class HandlebarsFunctionHelpers /// public static void RegisterHelpers() { + Handlebars.RegisterHelper("if_all", StringEqualityAllHelper); + Handlebars.RegisterHelper("if_any", StringEqualityAnyHelper); Handlebars.RegisterHelper("if_equals", StringEqualityHelper); Handlebars.RegisterHelper("if_exist", StringExistHelper); Handlebars.RegisterHelper("link_to", (writer, context, parameters) => diff --git a/README.md b/README.md index 719450d..0072bdc 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ See [Templates](Jellyfin.Plugin.Webhook/Templates) for sample templates. #### Helpers: +- if_all + - if _all_ pairs of the following parameters equals each other case insensitive (logical AND) +- if_any + - if _any_ pair of the following parameters equals each other case insensitive (logical OR) - if_equals - if first parameter equals second parameter case insensitive - if_exist