Esempio n. 1
0
        private static Action <StreamWriter, ContextObject> HandleCollectionOpen(TokenPair token, Queue <TokenPair> remainder,
                                                                                 ParserOptions options, InferredTemplateModel scope)
        {
            scope = scope?.GetInferredModelForPath(token.Value, InferredTemplateModel.UsedAs.Collection);

            var innerTemplate = Parse(remainder, options, scope);

            return((builder, context) =>
            {
                //if we're in the same scope, just negating, then we want to use the same object
                var c = context.GetContextForPath(token.Value);

                //"falsey" values by Javascript standards...
                if (!c.Exists())
                {
                    return;
                }

                if (c.Value is IEnumerable enumerable && !(enumerable is string) && !(enumerable is IDictionary <string, object>))
                {
                    var index = 0;
                    var enumerator = enumerable.GetEnumerator();
                    if (enumerator.MoveNext())
                    {
                        var current = enumerator.Current;
                        object next;
                        do
                        {
                            if (enumerator.MoveNext())
                            {
                                next = enumerator.Current;
                            }
                            else
                            {
                                next = null;
                            }
                            var innerContext = new ContextCollection(index, next == null)
                            {
                                Value = current,
                                Key = string.Format("[{0}]", index),
                                Options = options,
                                Parent = c
                            };
                            innerTemplate(builder, innerContext);
                            index++;
                            current = next;
                        } while (current != null);
                    }
                }
Esempio n. 2
0
        private static Action <StreamWriter, ContextObject> HandleFormattingValue(
            TokenPair currentToken,
            ParserOptions options,
            InferredTemplateModel scope)
        {
            return((builder, context) =>
            {
                scope = scope?.GetInferredModelForPath(currentToken.Value, InferredTemplateModel.UsedAs.Scalar);

                if (context == null)
                {
                    return;
                }
                var c = context.GetContextForPath(currentToken.Value, true);

                context.FormattingValue = c.Format(currentToken.FormatAs, c.Value);
            });
        }
Esempio n. 3
0
 private static Action <StreamWriter, ContextObject> PrintFormattedValues(
     TokenPair currentToken,
     ParserOptions options,
     InferredTemplateModel currentScope)
 {
     return((builder, context) =>
     {
         if (context == null)
         {
             return;
         }
         string value = null;
         if (context.FormattingValue != null)
         {
             value = context.FormattingValue.ToString();
             context.FormattingValue = null;
         }
         HandleContent(value)(builder, context);
     });
 }
Esempio n. 4
0
        private static Action <StreamWriter, ContextObject> HandleSingleValue(TokenPair token, ParserOptions options,
                                                                              InferredTemplateModel scope)
        {
            scope = scope?.GetInferredModelForPath(token.Value, InferredTemplateModel.UsedAs.Scalar);

            return((builder, context) =>
            {
                //try to locate the value in the context, if it exists, append it.
                var c = context?.GetContextForPath(token.Value);
                if (c?.Value != null)
                {
                    if (token.Type == TokenType.EscapedSingleValue && !options.DisableContentEscaping)
                    {
                        HandleContent(RtfEncodeString(c.ToString(), options))(builder, c);
                    }
                    else
                    {
                        HandleContent(c.ToString())(builder, c);
                    }
                }
            });
        }
Esempio n. 5
0
        private static Action <StreamWriter, ContextObject> HandleInvertedElementOpen(TokenPair token,
                                                                                      Queue <TokenPair> remainder,
                                                                                      ParserOptions options, InferredTemplateModel scope)
        {
            scope = scope?.GetInferredModelForPath(token.Value, InferredTemplateModel.UsedAs.ConditionalValue);

            var innerTemplate = Parse(remainder, options, scope);

            return((builder, context) =>
            {
                var c = context.GetContextForPath(token.Value);
                //"falsey" values by Javascript standards...
                if (!c.Exists())
                {
                    innerTemplate(builder, c);
                }
            });
        }