Beispiel #1
0
        public static Expression Compile(this Template template, CompileContext context)
        {
            var visitor = new CompilePartVisitor(context);

            visitor.Visit(template);
            return(visitor.Result());
        }
Beispiel #2
0
        /// <summary>
        /// Compiles the template into a Lambda function which can be executed later.
        /// </summary>
        /// <typeparam name="T">The type which the template will render against.  Missing
        /// properties/methods/fields will result in CompilationExceptions.</typeparam>
        /// <param name="templateLocator">The delegate to use to locate templates for inclusion.</param>
        /// <returns>A lambda expression representing the compiled template.</returns>
        public static Func <T, string> Compile <T>(this Template template, TemplateLocator templateLocator) where T : class
        {
            var param = Expression.Parameter(typeof(T), "data");

            var context = new CompileContext(template, typeof(T), param, templateLocator);

            var expression = Compile(template, context);

            return((Expression.Lambda <Func <T, string> >(expression, param)).Compile());
        }
Beispiel #3
0
        /// <summary>
        /// Compiles the template into a Lambda function which can be executed later.
        /// This version allows reflective compilation of templates.
        /// </summary>
        /// <param name="compileFor">The type to compile the template for.Missing
        /// properties/methods/fields will result in CompilationExceptions.</param>
        /// <param name="templateLocator">The delegate to use to locate templates for inclusion.</param>
        /// <returns>A lambda expression representing the compiled template.  The lambda takes and object
        /// and immediately casts it to <paramref name="compileFor"/>.</returns>
        public static Func <object, string> Compile(this Template template, Type compileFor, TemplateLocator templateLocator)
        {
            var param = Expression.Parameter(typeof(object), "data");

            var context = new CompileContext(template, compileFor,
                                             Expression.Convert(param, compileFor), templateLocator);

            var expression = Compile(template, context);

            return((Expression.Lambda <Func <object, string> >(expression, param)).Compile());
        }
Beispiel #4
0
        public static Expression IndentOnLineEnd(Expression expression, CompileContext context)
        {
            if (context._indent != null && context._lineEnded)
            {
                var expr = Expression.Call(typeof(String).GetMethod("Concat", new [] { typeof(String), typeof(String) }), new List <Expression> {
                    Expression.Constant(context._indent, typeof(String)),
                    expression
                });
                context._lineEnded = false;
                return(expr);
            }

            return(expression);
        }
        public static Expression IndentOnLineEnd(Expression expression, CompileContext context)
        {
            if (context._indent != null && context._lineEnded)
            {
                var expr =  Expression.Call(typeof(String).GetMethod("Concat", new [] { typeof(String), typeof(String) }), new List<Expression> { 
                    Expression.Constant(context._indent, typeof(String)), 
                    expression 
                });
                context._lineEnded = false;
                return expr;
            }

            return expression;
        }
        public static Expression IndentCheck(Expression expression, CompileContext context) 
        {
            if (context._indent == null) return expression;

            var regex = Expression.Variable(typeof(Regex));

            return Expression.Block(
                new[] { regex },
                Expression.Assign(regex, Expression.New(typeof(Regex).GetConstructor(new[] { typeof(String) }), new List<Expression>() { Expression.Constant("\n(?!$)", typeof(String)) })),
                Expression.Call(regex, typeof(Regex).GetMethod("Replace", new[] { typeof(String), typeof(String) }), new List<Expression>()
                {
                    IndentOnLineEnd(expression, context),
                    Expression.Constant("\n" + context._indent)
                })
            );
        }
Beispiel #7
0
        public static Expression IndentCheck(Expression expression, CompileContext context)
        {
            if (context._indent == null)
            {
                return(expression);
            }

            var regex = Expression.Variable(typeof(Regex));

            return(Expression.Block(
                       new[] { regex },
                       Expression.Assign(regex, Expression.New(typeof(Regex).GetConstructor(new[] { typeof(String) }), new List <Expression>()
            {
                Expression.Constant("\n(?!$)", typeof(String))
            })),
                       Expression.Call(regex, typeof(Regex).GetMethod("Replace", new[] { typeof(String), typeof(String) }), new List <Expression>()
            {
                IndentOnLineEnd(expression, context),
                Expression.Constant("\n" + context._indent)
            })
                       ));
        }
Beispiel #8
0
 public CompilePartVisitor(CompileContext context)
 {
     this.context = context;
 }
 public CompilePartVisitor(CompileContext context)
 {
     this.context = context;
 }