Ejemplo n.º 1
0
 private Func <IReadOnlyList <object>, object> GlobalTemplateFunction(string templateName)
 => (IReadOnlyList <object> args) =>
 {
     var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, LgOptions);
     var newScope  = evaluator.ConstructScope(templateName, args.ToList());
     return(evaluator.EvaluateTemplate(templateName, newScope));
 };
Ejemplo n.º 2
0
 private Func <IReadOnlyList <object>, object> EvaluateWithTemplates(string templateName, Templates templates)
 => (IReadOnlyList <object> args) =>
 {
     var newScope  = this.ConstructScope(templateName, args.ToList(), templates.AllTemplates);
     var evaluator = new Evaluator(templates, _lgOptions);
     return(evaluator.EvaluateTemplate(templateName, newScope));
 };
Ejemplo n.º 3
0
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <returns>Evaluate result.</returns>
        public object Evaluate(string templateName, object scope = null)
        {
            CheckErrors();

            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, StrictMode);

            return(evaluator.EvaluateTemplate(templateName, scope));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <param name="opt">The EvaluationOptions in evaluating a template.</param>
        /// <returns>Evaluate result.</returns>
        public object Evaluate(string templateName, object scope = null, EvaluationOptions opt = null)
        {
            CheckErrors();
            var evalOpt = opt != null?opt.Merge(LgOptions) : LgOptions;

            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, evalOpt);

            return(evaluator.EvaluateTemplate(templateName, scope));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <returns>Evaluate result.</returns>
        public object EvaluateTemplate(string templateName, object scope = null)
        {
            CheckErrors();

            var memory    = SimpleObjectMemory.Wrap(scope);
            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionEngine);

            return(evaluator.EvaluateTemplate(templateName, new CustomizedMemory(memory)));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <param name="opt">The EvaluationOptions in evaluating a template.</param>
        /// <returns>Evaluate result.</returns>
        public object Evaluate(string templateName, object scope = null, EvaluationOptions opt = null)
        {
            CheckErrors();
            var evalOpt = opt != null ? opt.Merge(LgOptions) : LgOptions;
            var evaluator = new Evaluator(AllTemplates.ToList(), ExpressionParser, evalOpt);
            var result = evaluator.EvaluateTemplate(templateName, scope);
            if (evalOpt.LineBreakStyle == LGLineBreakStyle.Markdown && result is string str)
            {
                result = newLineRegex.Replace(str, "$1$1");
            }

            return result;
        }
Ejemplo n.º 7
0
        private Templates InjectToExpressionFunction()
        {
            var totalTempaltes = new List <Templates> {
                this
            }.Union(References);

            foreach (var curTemplates in totalTempaltes)
            {
                var globalFuncs = curTemplates.GetGlobalFunctionTable(curTemplates.Options);
                foreach (var templateName in globalFuncs)
                {
                    if (curTemplates.Any(u => u.Name == templateName))
                    {
                        var prefix        = string.IsNullOrWhiteSpace(curTemplates.Namespace) ? string.Empty : curTemplates.Namespace + ".";
                        var newGlobalName = prefix + templateName;
                        Expression.Functions.Add(newGlobalName, new ExpressionEvaluator(
                                                     newGlobalName,
                                                     (expression, state, options) =>
                        {
                            object result    = null;
                            var evaluator    = new Evaluator(this, LgOptions);
                            var(args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
                            if (error == null)
                            {
                                var parameters = evaluator.TemplateMap[templateName].Parameters;
                                var newScope   = parameters.Zip(args, (k, v) => new { k, v })
                                                 .ToDictionary(x => x.k, x => x.v);
                                var scope = new CustomizedMemory(state, new SimpleObjectMemory(newScope));
                                try
                                {
                                    result = evaluator.EvaluateTemplate(templateName, scope);
                                }
#pragma warning disable CA1031 // Do not catch general exception types
                                catch (Exception err)
#pragma warning restore CA1031 // Do not catch general exception types
                                {
                                    error = err.Message;
                                }
                            }

                            return(result, error);
                        },
                                                     ReturnType.Object));
                    }
                }
            }

            return(this);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Use to evaluate an inline template str.
        /// </summary>
        /// <param name="inlineStr">inline string which will be evaluated.</param>
        /// <param name="scope">scope object or JToken.</param>
        /// <returns>Evaluate result.</returns>
        public object Evaluate(string inlineStr, object scope = null)
        {
            // wrap inline string with "# name and -" to align the evaluation process
            var fakeTemplateId = "__temp__";

            inlineStr = !inlineStr.Trim().StartsWith("```") && inlineStr.IndexOf('\n') >= 0
                   ? "```" + inlineStr + "```" : inlineStr;
            var wrappedStr = $"# {fakeTemplateId} \r\n - {inlineStr}";

            var lgsource  = LGParser.Parse(wrappedStr, "inline");
            var templates = Templates.Concat(lgsource.Templates).ToList();

            RunStaticCheck(templates);

            var evaluator = new Evaluator(templates, this.expressionEngine);

            return(evaluator.EvaluateTemplate(fakeTemplateId, scope));
        }
Ejemplo n.º 9
0
 public Func <IReadOnlyList <object>, object> TemplateEvaluator(string templateName)
 => (IReadOnlyList <object> args) =>
 {
     var newScope = _evaluator.ConstructScope(templateName, args.ToList());
     return(_evaluator.EvaluateTemplate(templateName, newScope));
 };
Ejemplo n.º 10
0
        /// <summary>
        /// Evaluate a template with given name and scope.
        /// </summary>
        /// <param name="templateName">Template name to be evaluated.</param>
        /// <param name="scope">The state visible in the evaluation.</param>
        /// <returns>Evaluate result.</returns>
        public object EvaluateTemplate(string templateName, object scope = null)
        {
            var evaluator = new Evaluator(Templates, this.expressionEngine);

            return(evaluator.EvaluateTemplate(templateName, scope));
        }
Ejemplo n.º 11
0
        public object EvaluateTemplate(string templateName, IMemory memory)
        {
            var evaluator = new Evaluator(Templates, this.expressionEngine);

            return(evaluator.EvaluateTemplate(templateName, new CustomizedMemory(memory)));
        }