Beispiel #1
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children   = compiler.Compile(Children);
            var expression = MorestachioExpression.Compile();

            return(async(outputStream, context, scopeData) =>
            {
                //try to locate the value in the context, if it exists, append it.
                var contextObject = context != null ? (await expression(context, scopeData)) : null;
                if (contextObject != null)
                {
                    //await contextObject.EnsureValue();
                    if (EscapeValue && !scopeData.ParserOptions.DisableContentEscaping)
                    {
                        outputStream.Write(HtmlEncodeString(await contextObject.RenderToString(scopeData)));
                    }
                    else
                    {
                        outputStream.Write(await contextObject.RenderToString(scopeData));
                    }
                }

                await children(outputStream, contextObject, scopeData);
            });
        }
 /// <param name="compiler"></param>
 /// <inheritdoc />
 public Compilation Compile(IDocumentCompiler compiler)
 {
     return((outputStream, context, scopeData) =>
     {
         CoreAction(outputStream, scopeData);
     });
 }
Beispiel #3
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children = Children.Where(e => e is SwitchCaseDocumentItem || e is SwitchDefaultDocumentItem)
                           .Cast <BlockDocumentItemBase>()
                           .Select(e => new SwitchExecutionContainerCompiledAction()
            {
                Callback   = compiler.Compile(e.Children),
                Expression = (e as SwitchCaseDocumentItem)?.MorestachioExpression.Compile()
            }).ToArray();
            var expression = MorestachioExpression.Compile();

            return(async(outputStream, context, scopeData) =>
            {
                var value = await expression(context, scopeData);

                if (ScopeToValue)
                {
                    context = value;
                }
                var toBeExecuted = await CoreAction(outputStream, value, scopeData,
                                                    children);

                if (toBeExecuted != null)
                {
                    await toBeExecuted.Callback(outputStream, context, scopeData);
                }
            });
        }
Beispiel #4
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children   = compiler.Compile(Children);
            var expression = MorestachioExpression.Compile();

            return(async(outputStream, context, scopeData) =>
            {
                var oldCulture = scopeData.ParserOptions.CultureInfo;
                if (scopeData.CustomData.TryGetValue(LocalizationCultureKey, out var customCulture) &&
                    customCulture is CultureInfo culInfo)
                {
                    oldCulture = culInfo;
                }

                var expValue = (await expression(context, scopeData));

                CultureInfo requestedCulture;
                if (expValue.Value is CultureInfo culture)
                {
                    requestedCulture = culture;
                }
                else
                {
                    requestedCulture = CultureInfo.GetCultureInfo(await expValue.RenderToString(scopeData));
                }

                scopeData.CustomData[LocalizationCultureKey] = requestedCulture;
                await children(outputStream, context, scopeData);

                scopeData.CustomData[LocalizationCultureKey] = oldCulture;
            });
        }
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public Compilation Compile(IDocumentCompiler compiler)
        {
            var value = Value;

            foreach (var textEditDocumentItem in Children.OfType <TextEditDocumentItem>())
            {
                value = textEditDocumentItem.Operation.Apply(value);
            }

            if (value == null)
            {
                return(null);
            }

#if Span
            var memValue = value.AsMemory();
            return((stream, context, scopeData) =>
            {
                stream.Write(memValue.Span);
            });
#else
            return((stream, context, scopeData) =>
            {
                stream.Write(value);
            });
#endif
        }
 /// <param name="compiler"></param>
 /// <inheritdoc />
 public CompilationAsync Compile(IDocumentCompiler compiler)
 {
     return(async(stream, context, scopeData) =>
     {
         scopeData.RemoveVariable(Value, IdVariableScope);
         await AsyncHelper.FakePromise();
     });
 }
Beispiel #7
0
 /// <summary>
 ///		Creates a new Build-In renderer
 /// </summary>
 /// <returns></returns>
 public virtual IRenderer CreateCompiledRenderer(IDocumentCompiler compiler = null)
 {
     if (Errors.Any())
     {
         throw new AggregateException("You cannot Render this Template as there are one or more Errors. See Inner Exception for more infos.", Errors.Select(e => e.GetException())).Flatten();
     }
     return(new CompiledRenderer(Document, ParserOptions, CaptureVariables, compiler ?? new DocumentCompiler()));
 }
 /// <param name="compiler"></param>
 /// <inheritdoc />
 public CompilationAsync Compile(IDocumentCompiler compiler)
 {
     return(async(stream, context, scopeData) =>
     {
         CoreAction(scopeData);
         await AsyncHelper.FakePromise();
     });
 }
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var compilation = compiler.Compile(Children);

            return(async(stream, context, data) =>
            {
                await compilation(stream, context, data);
            });
        }
Beispiel #10
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children = compiler.Compile(Children);

            return(async(stream, context, scopeData) =>
            {
                scopeData.CompiledPartials[PartialName] = children;
                await AsyncHelper.FakePromise();
            });
        }
Beispiel #11
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children = compiler.Compile(Children);

            return(async(stream, context, scopeData) =>
            {
                CoreAction(context, scopeData);
                await children(stream, context, scopeData);
            });
        }
Beispiel #12
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public virtual CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var elseChildren = GetNestedElseConditions()
                               .Select(e => new IfExecutionContainer()
            {
                Callback   = compiler.Compile(e.Children),
                Expression = e.MorestachioExpression.Compile()
            }).ToArray();

            var elseDocument           = GetNestedElse();
            CompilationAsync elseBlock = null;

            if (elseDocument != null)
            {
                elseBlock = compiler.Compile(new[] { elseDocument });
            }

            var children = compiler.Compile(GetIfContents());

            var expression = MorestachioExpression.Compile();

            return(async(stream, context, scopeData) =>
            {
                var c = await expression(context, scopeData);

                context = context.IsNaturalContext || context.Parent == null ? context : context.Parent;
                if (c.Exists() != Inverted)
                {
                    await children(stream, context, scopeData);

                    return;
                }
                else if (elseChildren.Length > 0)
                {
                    foreach (var ifExecutionContainer in elseChildren)
                    {
                        if ((await ifExecutionContainer.Expression(context, scopeData)).Exists() == Inverted)
                        {
                            continue;
                        }

                        await ifExecutionContainer.Callback(stream, context, scopeData);

                        return;
                    }
                }

                if (elseBlock != null)
                {
                    await elseBlock(stream, context, scopeData);

                    return;
                }
            });
        }
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var expression = MorestachioExpression.Compile();

            return(async(stream, context, scopeData) =>
            {
                context = await expression(context, scopeData);

                scopeData.AddVariable(Value, context, IdVariableScope);
            });
        }
Beispiel #14
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var expression = MorestachioExpression.Compile();
            var children   = compiler.Compile(Children);

            return(async(outputStream, context, scopeData) =>
            {
                await CoreAction(outputStream, await expression(context, scopeData), scopeData,
                                 async o =>
                {
                    await children(outputStream, o, scopeData);
                });
            });
        }
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var expression = MorestachioExpression.Compile();
            var children   = compiler.Compile(Children);

            return(async(stream, context, scopeData) =>
            {
                var c = await expression(context, scopeData);

                if (c.Exists())
                {
                    await children(stream, c, scopeData);
                }
            });
        }
Beispiel #16
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children = compiler.Compile(Children);

            if (ScopeIsolationExpression != null)
            {
                var compiledExpression = ScopeIsolationExpression.Compile();
                return(async(stream, context, data) =>
                {
                    context = await compiledExpression(context, data);

                    context = new ContextObject(context.Key, null, context.Value);
                    await children(stream, context, data);
                });
            }


            return(children);
        }
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children   = compiler.Compile(Children);
            var expression = MorestachioExpression.Compile();

            return(async(stream, context, scopeData) =>
            {
                var c = await expression(context, scopeData);

                if (!c.Exists())
                {
                    return;
                }

                if (!(Number.IsIntegralNumber(c.Value)))
                {
                    var path = new Stack <string>();
                    var parent = context.Parent;
                    while (parent != null)
                    {
                        path.Push(parent.Key);
                        parent = parent.Parent;
                    }

                    throw new IndexedParseException(CharacterLocationExtended.Empty,
                                                    string.Format(
                                                        "{1}'{0}' is expected to return a integral number but did not." +
                                                        " Complete Expression until Error:{2}",
                                                        MorestachioExpression, base.ExpressionStart,
                                                        (path.Count == 0 ? "Empty" : path.Aggregate((e, f) => e + "\r\n" + f))));
                }

                var nr = new Number(c.Value as IConvertible);
                for (int i = 0; i < nr; i++)
                {
                    var contextCollection = new ContextCollection(i, i + 1 == nr, $"[{i}]", context,
                                                                  context.Value);
                    await children(stream, contextCollection, scopeData);
                }
            });
        }
Beispiel #18
0
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var children   = compiler.Compile(Children);
            var expression = MorestachioExpression.Compile();

            return(async(outputStream, context, scopeData) =>
            {
                var index = 0;

                var collectionContext = new ContextCollection(index, false, context.Key,
                                                              context.Parent,
                                                              context.Value);

                while (ContinueBuilding(outputStream, scopeData) &&
                       (await expression(collectionContext, scopeData)).Exists())
                {
                    await children(outputStream, collectionContext, scopeData);

                    collectionContext = new ContextCollection(++index, false, context.Key,
                                                              context.Parent, context.Value);
                }
            });
        }
        /// <param name="compiler"></param>
        /// <inheritdoc />
        public CompilationAsync Compile(IDocumentCompiler compiler)
        {
            var doneAction = new RenderPartialDoneDocumentItem().Compile(compiler);
            var expression = MorestachioExpression.Compile();

            return(async(stream, context, scopeData) =>
            {
                var partialName = await(await expression(context, scopeData)).RenderToString(scopeData);

                if (partialName == null)
                {
                    throw new MorestachioRuntimeException($"Get partial requested by the expression: '{MorestachioExpression.ToString()}' returned null and is therefor not valid");
                }

                var toExecute = await CoreAction(context, scopeData, async (pn, cnxt) =>
                {
                    if (scopeData.CompiledPartials.TryGetValue(pn, out var partialWithContext))
                    {
                        await partialWithContext(stream, cnxt, scopeData);
                        await doneAction(stream, cnxt, scopeData);
                        return true;
                    }

                    return false;
                }, partialName);

                if (toExecute != null)
                {
                    await compiler.Compile(new IDocumentItem[]
                    {
                        toExecute.Item1
                    })(stream, toExecute.Item2, scopeData);
                    await doneAction(stream, toExecute.Item2, scopeData);
                }
            });
        }
Beispiel #20
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="document"></param>
 /// <param name="parserOptions"></param>
 /// <param name="captureVariables"></param>
 /// <param name="compiler"></param>
 public CompiledRenderer(IDocumentItem document, ParserOptions parserOptions, bool captureVariables, IDocumentCompiler compiler)
     : base(document, parserOptions, captureVariables)
 {
     _compiler = compiler;
 }