Example #1
0
 /// <summary>
 /// Parser to turn lg content into a <see cref="LanguageGeneration.Templates"/>.
 /// </summary>
 /// <param name="filePath">Absolute path of a LG file.</param>
 /// <param name="importResolver">resolver to resolve LG import id to template text.</param>
 /// <param name="expressionParser">expressionEngine Expression engine for evaluating expressions.</param>
 /// <returns>new <see cref="LanguageGeneration.Templates"/> entity.</returns>
 public static Templates ParseFile(
     string filePath,
     ImportResolverDelegate importResolver = null,
     ExpressionParser expressionParser     = null)
 {
     return(TemplatesParser.ParseFile(filePath, importResolver, expressionParser).InjectToExpressionFunction());
 }
Example #2
0
        /// <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>
        /// Use to evaluate an inline template str.
        /// </summary>
        /// <param name="text">inline string which will be evaluated.</param>
        /// <param name="scope">scope object or JToken.</param>
        /// <returns>Evaluate result.</returns>
        public object EvaluateText(string text, object scope = null)
        {
            if (text == null)
            {
                throw new ArgumentException("inline string is null.");
            }

            CheckErrors();

            // wrap inline string with "# name and -" to align the evaluation process
            var fakeTemplateId = "__temp__";
            var multiLineMark  = "```";

            text = !text.Trim().StartsWith(multiLineMark) && text.Contains('\n')
                   ? $"{multiLineMark}{text}{multiLineMark}" : text;

            var newContent = $"# {fakeTemplateId} {newLine} - {text}";

            var newLG = TemplatesParser.ParseTextWithRef(newContent, this);

            return(newLG.Evaluate(fakeTemplateId, scope));
        }
Example #4
0
 /// <summary>
 /// Parser to turn lg content into a <see cref="LanguageGeneration.Templates"/>.
 /// </summary>
 /// <param name="content">Text content contains lg templates.</param>
 /// <param name="id">Id is the identifier of content. If importResolver is null, id must be a full path string. </param>
 /// <param name="importResolver">Resolver to resolve LG import id to template text.</param>
 /// <param name="expressionParser">Expression parser engine for parsing expressions.</param>
 /// <returns>new <see cref="Templates"/> entity.</returns>
 public static Templates ParseText(
     string content,
     string id = "",
     ImportResolverDelegate importResolver = null,
     ExpressionParser expressionParser     = null) => TemplatesParser.ParseText(content, id, importResolver, expressionParser).InjectToExpressionFunction();
 /// <summary>
 /// Parser to turn lg content into a <see cref="LanguageGeneration.Templates"/>.
 /// </summary>
 /// <param name="filePath">Absolute path of a LG file.</param>
 /// <param name="importResolver">resolver to resolve LG import id to template text.</param>
 /// <param name="expressionParser">expressionEngine Expression engine for evaluating expressions.</param>
 /// <returns>new <see cref="LanguageGeneration.Templates"/> entity.</returns>
 public static Templates ParseFile(
     string filePath,
     ImportResolverDelegate importResolver = null,
     ExpressionParser expressionParser     = null) => TemplatesParser.ParseFile(filePath, importResolver, expressionParser);
 /// <summary>
 /// Parser to turn lg content into a <see cref="LanguageGeneration.Templates"/>.
 /// </summary>
 /// <param name="resource">LG resource.</param>
 /// <param name="importResolver">Resolver to resolve LG import id to template text.</param>
 /// <param name="expressionParser">Expression parser engine for parsing expressions.</param>
 /// <returns>new <see cref="Templates"/> entity.</returns>
 public static Templates ParseResource(
     LGResource resource,
     ImportResolverDelegate importResolver = null,
     ExpressionParser expressionParser     = null) => TemplatesParser.ParseResource(resource, importResolver, expressionParser).InjectToExpressionFunction();