/// <summary> /// Evaluates an inline template string. /// </summary> /// <param name="text">Inline string which will be evaluated.</param> /// <param name="scope">Scope object or JToken.</param> /// <param name="opt">EvaluationOptions in evaluating a template.</param> /// <returns>Evaluated result.</returns> public object EvaluateText(string text, object scope = null, EvaluationOptions opt = null) { var evalOpt = opt != null?opt.Merge(LgOptions) : LgOptions; if (text == null) { throw new ArgumentException("inline string is null."); } CheckErrors(); var inlineTemplateId = $"{InlineTemplateIdPrefix}{Guid.NewGuid():N}"; // wrap inline string with "# name and -" to align the evaluation process var multiLineMark = "```"; text = !text.Trim().StartsWith(multiLineMark, StringComparison.Ordinal) && text.Contains('\n') ? $"{multiLineMark}{text}{multiLineMark}" : text; var newContent = $"# {inlineTemplateId} {_newLine} - {text}"; var newLG = TemplatesParser.ParseTextWithRef(newContent, this); return(newLG.Evaluate(inlineTemplateId, scope, evalOpt)); }
/// <summary> /// Expands a template with given name and scope. /// Return all possible responses instead of random one. /// </summary> /// <param name="templateName">Template name to be evaluated.</param> /// <param name="scope">State visible in the evaluation.</param> /// <param name="opt">EvaluationOptions in expanding a template.</param> /// <returns>Expanded result.</returns> public IList <object> ExpandTemplate(string templateName, object scope = null, EvaluationOptions opt = null) { CheckErrors(); var evalOpt = opt != null?opt.Merge(LgOptions) : LgOptions; var expander = new Expander(this, evalOpt); return(expander.ExpandTemplate(templateName, scope)); }
/// <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)); }
/// <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; }