Example #1
0
        /// <inheritdoc/>
        protected override async Task WriteAsync(StringRender renderer, InterpolationToken obj, Context context)
        {
            var value = context.Lookup(obj.Content.ToString());

            var functionValueDynamic = value as Func <dynamic, object>;
            var functionValue        = value as Func <object>;

            if (functionValueDynamic != null || functionValue != null)
            {
                object functionResult = functionValueDynamic != null?functionValueDynamic.Invoke(context.View) : functionValue.Invoke();

                var resultString = functionResult.ToString();
                if (resultString.Contains("{{"))
                {
                    await renderer.RenderAsync(context.RendererSettings.Parser.Parse(resultString), context);

                    return;
                }

                value = resultString;
            }

            if (obj.EscapeResult && value != null)
            {
                value = WebUtility.HtmlEncode(value.ToString());
            }

            if (obj.Indent > 0)
            {
                renderer.Write(' ', obj.Indent);
            }

            renderer.Write(value?.ToString());
        }
Example #2
0
        /// <inheritdoc/>
        protected override void Write(StringRender renderer, InterpolationToken obj, Context context)
        {
            var value = context.Lookup(obj.Content.ToString());

            var functionValueDynamic = value as Func <dynamic, object>;
            var functionValue        = value as Func <object>;

            if (functionValueDynamic != null || functionValue != null)
            {
                object functionResult = functionValueDynamic != null?functionValueDynamic.Invoke(context.View) : functionValue.Invoke();

                var resultString = ConvertToStringInCulture(functionResult, context.RenderSettings.CultureInfo);
                if (resultString.Contains("{{"))
                {
                    renderer.Render(context.RendererSettings.Parser.Parse(resultString), context);
                    return;
                }

                value = resultString;
            }

            if (!context.RenderSettings.SkipHtmlEncoding && obj.EscapeResult && value != null)
            {
                value = context.RendererSettings.EncodingFuction(ConvertToStringInCulture(value, context.RenderSettings.CultureInfo));
            }

            if (obj.Indent > 0)
            {
                renderer.Write(' ', obj.Indent);
            }

            renderer.Write(ConvertToStringInCulture(value, context.RenderSettings.CultureInfo));
        }
        protected override void Write(StringRender renderer, HelperToken obj, Context context)
        {
            if (renderer is null)
            {
                throw new ArgumentNullException(nameof(renderer));
            }

            if (obj is null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (_helperCache.TryGetValue(obj.Name, out var helper))
            {
                var helperContext = new HelperContext(context);
                var args          = obj.Args;

                var argumentTypes = helper.ArgumentTypes;
                if ((argumentTypes.Length - 1) == args.Length)
                {
                    var arr = new object[args.Length + 1];
                    arr[0] = helperContext;

                    for (var i = 0; i < args.Length; i++)
                    {
                        var arg = args[i].ShouldAttemptContextLoad
                            ? context.Lookup(args[i].Value)
                            : args[i].Value;

                        arg = TryConvertTypeIfRequired(arg, args[i].Value, argumentTypes[i + 1]);

                        if (arg is null)
                        {
                            return;
                        }

                        arr[i + 1] = arg;
                    }

                    var result = helper.Delegate.Method.Invoke(helper.Delegate.Target, arr);
                    if (result is string str)
                    {
                        renderer.Write(str);
                    }
                    else if (result is object)
                    {
                        renderer.Write(Convert.ToString(result, context.RenderSettings.CultureInfo));
                    }
                }
            }
        }
Example #4
0
        /// <inheritdoc/>
        protected override void Write(StringRender renderer, LiteralToken obj, Context context)
        {
            for (var i = 0; i < obj.Content.Length; i++)
            {
                var item = obj.Content[i];
                if (obj.Indent > 0 && !item.IsEmptyOrWhitespace())
                {
                    renderer.Write(' ', obj.Indent);
                }

                renderer.Write(ref item);
            }
        }
        /// <inheritdoc />
        protected override void Write(StringRender renderer, InterpolationToken obj, Context context)
        {
            var    content = obj.Content.ToString();
            string format  = null;

            var pos = content.IndexOf(':');

            if (pos > 0)
            {
                format  = content.Substring(pos + 1);
                content = content.Substring(0, pos);
            }

            var value = context.Lookup(content);

            var functionValueDynamic = value as Func <dynamic, object>;
            var functionValue        = value as Func <object>;

            if (functionValueDynamic != null || functionValue != null)
            {
                object functionResult = functionValueDynamic != null?functionValueDynamic.Invoke(context.View) : functionValue.Invoke();

                var resultString = functionResult.ToString();
                if (resultString.Contains("{{"))
                {
                    renderer.Render(context.RendererSettings.Parser.Parse(resultString), context);
                    return;
                }

                value = resultString;
            }

            var formattedValue = ApplyFormat(value, format);

            if (!context.RenderSettings.SkipHtmlEncoding && obj.EscapeResult && value != null)
            {
                formattedValue = WebUtility.HtmlEncode(formattedValue);
            }

            if (obj.Indent > 0)
            {
                renderer.Write(' ', obj.Indent);
            }

            renderer.Write(formattedValue);
        }