Ejemplo n.º 1
0
        public void Visit(Block block)
        {
            parts.Add(context.GetInnerExpressions(block.Name, value =>
            {
                context.Push(block, value);

                if (typeof(Lambda <string>).BaseType.IsAssignableFrom(value.Type))
                {
                    return
                    (Expression.Call(
                         Expression.Call(value, value.Type.GetMethod("Invoke"),
                                         Expression.Constant(block.InnerSource())),
                         typeof(object).GetMethod("ToString")));
                }

                var visitor = new CompilePartVisitor(context);
                visitor.Visit((Section)block);
                var expression = CompoundExpression.NullCheck(value,
                                                              nullValue: "",
                                                              returnIfNotNull: visitor.Result());

                context.Pop();

                return(expression);
            }));
        }
Ejemplo n.º 2
0
        public void Visit(LiteralText literal)
        {
            var text = literal.Text;

            parts.Add(CompoundExpression.IndentCheck(Expression.Constant(text, typeof(string)), context));

            context._lineEnded = text.Length > 0 && text[text.Length - 1] == '\n';
        }
Ejemplo n.º 3
0
        public void Visit(VariableReference variable)
        {
            var getter = context.CompiledGetter(variable.Path);

            getter = CompoundExpression.NullCheck(getter, "");
            getter = Expression.Call(getter, context.TargetType.GetMethod("ToString"));

            if (variable.Escaped)
            {
                parts.Add(Expression.Call(null, typeof(Encoders).GetMethod("DefaultHtmlEncode"), getter));
            }
            else
            {
                parts.Add(getter);
            }
        }
Ejemplo n.º 4
0
        public void Visit(VariableReference variable)
        {
            var getter = context.CompiledGetter(variable.Path);

            getter = CompoundExpression.NullCheck(getter, "");
            getter = Expression.Call(getter, context.TargetType.GetMethod("ToString"));

            if (variable.Escaped)
            {
                var escaperProperty = typeof(Encoders).GetProperty("HtmlEncode", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
                var escaperMethod   = (Delegate)escaperProperty.GetValue(null, null);
                parts.Add(CompoundExpression.IndentOnLineEnd(Expression.Call(null, escaperMethod.Method, getter), context));
            }
            else
            {
                parts.Add(CompoundExpression.IndentOnLineEnd(getter, context));
            }
        }
Ejemplo n.º 5
0
        internal Expression GetInnerExpressions(string path, Func <Expression, Expression> innerExpression, bool invert = false)
        {
            var value = GetInnerExpression(path);

            if (value.Type == typeof(bool))
            {
                return(Expression.Condition(
                           Expression.Equal(value, Expression.Constant(!invert)),
                           innerExpression(value),
                           Expression.Constant("")));
            }
            else if (value.Type.GetInterface("IEnumerable") != null)
            {
                if (invert)
                {
                    var enumerator = Expression.Call(
                        value,
                        value.Type.GetInterface("IEnumerable").GetMethod("GetEnumerator"));

                    return(Expression.Condition(
                               Expression.Call(enumerator, enumerator.Type.GetMethod("MoveNext")),
                               Expression.Constant(""),
                               innerExpression(value)));
                }
                else
                {
                    return(CompoundExpression.Enumerator(
                               itemCallback: innerExpression,
                               enumerable: value));
                }
            }
            else
            {
                if (invert && !value.Type.IsValueType)
                {
                    return(Expression.Condition(
                               Expression.Equal(value, Expression.Constant(null)),
                               innerExpression(value),
                               Expression.Constant("")));
                }
                else
                {
                    return(innerExpression(value));
                }
            }

            // TODO: support enuerating dictionaries
            //else if (GenericIDictionaryUtil.IsInstanceOfGenericIDictionary(value))
            //{
            //    if ((value as IEnumerable).GetEnumerator().MoveNext())
            //    {
            //        yield return value;
            //    }
            //}
            //else if (value is IDictionary) // Dictionaries also implement IEnumerable
            //                               // so this has to be checked before it.
            //{
            //    if (((IDictionary)value).Count > 0)
            //    {
            //        yield return value;
            //    }
            //}
        }