internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, CompilationContext compilationContext)
        {
            var configuration = compilationContext.Configuration;
            IEnumerable <object> tokens;

            using (var sr = readerFactoryFactory(configuration, templatePath))
            {
                using (var reader = new ExtendedStringReader(sr))
                {
                    tokens = Tokenizer.Tokenize(reader).ToArray();
                }
            }

            var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault();

            var expressions  = ExpressionBuilder.ConvertTokensToExpressions(tokens, configuration);
            var compiledView = FunctionBuilder.Compile(expressions, compilationContext);

            if (layoutToken == null)
            {
                return(compiledView);
            }

            var fs         = configuration.FileSystem;
            var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs");

            if (layoutPath == null)
            {
                throw new InvalidOperationException($"Cannot find layout '{layoutToken.Value}' for template '{templatePath}'");
            }

            var compiledLayout = CompileView(readerFactoryFactory, layoutPath, new CompilationContext(compilationContext));

            return((in EncodedTextWriter writer, BindingContext context) =>
            {
                var config = context.Configuration;
                using var bindingContext = BindingContext.Create(config, null);
                foreach (var pair in context.ContextDataObject)
                {
                    switch (pair.Key.WellKnownVariable)
                    {
                    case WellKnownVariable.Parent:
                    case WellKnownVariable.Root:
                        continue;
                    }

                    bindingContext.ContextDataObject[pair.Key] = pair.Value;
                }

                using var innerWriter = ReusableStringWriter.Get(config.FormatProvider);
                using var textWriter = new EncodedTextWriter(innerWriter, config.TextEncoder, FormatterProvider.Current, true);
                compiledView(textWriter, context);
                var inner = innerWriter.ToString();

                var viewModel = new LayoutViewModel(inner, context.Value);
                bindingContext.Value = viewModel;

                compiledLayout(writer, bindingContext);
            });
Exemplo n.º 2
0
 /// <summary>
 /// Renders the <see cref="T:log4net.Core.LoggingEvent" /> to a string.
 /// </summary>
 /// <param name="loggingEvent">The event to render.</param>
 /// <returns>The event rendered as a string.</returns>
 /// <remarks>
 /// <para>
 /// Helper method to render a <see cref="T:log4net.Core.LoggingEvent" /> to
 /// a string. This appender must have a <see cref="P:log4net.Appender.AppenderSkeleton.Layout" />
 /// set to render the <paramref name="loggingEvent" /> to
 /// a string.
 /// </para>
 /// <para>If there is exception data in the logging event and
 /// the layout does not process the exception, this method
 /// will append the exception text to the rendered string.
 /// </para>
 /// <para>
 /// Where possible use the alternative version of this method
 /// <see cref="M:RenderLoggingEvent(TextWriter,LoggingEvent)" />.
 /// That method streams the rendering onto an existing Writer
 /// which can give better performance if the caller already has
 /// a <see cref="T:System.IO.TextWriter" /> open and ready for writing.
 /// </para>
 /// </remarks>
 protected string RenderLoggingEvent(LoggingEvent loggingEvent)
 {
     if (m_renderWriter == null)
     {
         m_renderWriter = new ReusableStringWriter(CultureInfo.InvariantCulture);
     }
     lock (m_renderWriter)
     {
         m_renderWriter.Reset(1024, 256);
         RenderLoggingEvent(m_renderWriter, loggingEvent);
         return(m_renderWriter.ToString());
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Renders the <see cref="LoggingEvent"/> to a string.
        /// </summary>
        /// <param name="loggingEvent">The event to render.</param>
        /// <returns>The event rendered as a string.</returns>
        /// <remarks>
        /// <para>
        /// Helper method to render a <see cref="LoggingEvent"/> to
        /// a string. This appender must have a <see cref="Layout"/>
        /// set to render the <paramref name="loggingEvent"/> to
        /// a string.
        /// </para>
        /// <para>If there is exception data in the logging event and
        /// the layout does not process the exception, this method
        /// will append the exception text to the rendered string.
        /// </para>
        /// <para>
        /// Where possible use the alternative version of this method
        /// <see cref="RenderLoggingEvent(TextWriter,LoggingEvent)"/>.
        /// That method streams the rendering onto an existing Writer
        /// which can give better performance if the caller already has
        /// a <see cref="TextWriter"/> open and ready for writing.
        /// </para>
        /// </remarks>
        protected string RenderLoggingEvent(LoggingEvent loggingEvent)
        {
            // Create the render writer on first use
            if (m_renderWriter == null)
            {
                m_renderWriter = new ReusableStringWriter(System.Globalization.CultureInfo.InvariantCulture);
            }

            // Reset the writer so we can reuse it
            m_renderWriter.Reset(c_renderBufferMaxCapacity, c_renderBufferSize);

            RenderLoggingEvent(m_renderWriter, loggingEvent);
            return(m_renderWriter.ToString());
        }
Exemplo n.º 4
0
        internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, CompilationContext compilationContext)
        {
            var configuration = compilationContext.Configuration;
            IEnumerable <object> tokens;

            using (var sr = readerFactoryFactory(configuration, templatePath))
            {
                using (var reader = new ExtendedStringReader(sr))
                {
                    tokens = Tokenizer.Tokenize(reader).ToArray();
                }
            }

            var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault();

            var expressions  = ExpressionBuilder.ConvertTokensToExpressions(tokens, configuration);
            var compiledView = FunctionBuilder.Compile(expressions, compilationContext);

            if (layoutToken == null)
            {
                return(compiledView);
            }

            var fs         = configuration.FileSystem;
            var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs");

            if (layoutPath == null)
            {
                throw new InvalidOperationException($"Cannot find layout '{layoutToken.Value}' for template '{templatePath}'");
            }

            var compiledLayout = CompileView(readerFactoryFactory, layoutPath, new CompilationContext(compilationContext));

            return((in EncodedTextWriter writer, BindingContext context) =>
            {
                var config = context.Configuration;
                using var innerWriter = ReusableStringWriter.Get(config.FormatProvider);
                using var textWriter = new EncodedTextWriter(innerWriter, config.TextEncoder, config.UnresolvedBindingFormatter, true);
                compiledView(textWriter, context);
                var inner = innerWriter.ToString();

                var vmContext = new [] { new { body = inner }, context.Value };
                var viewModel = new DynamicViewModel(vmContext);
                using var bindingContext = BindingContext.Create(config, viewModel);

                compiledLayout(writer, bindingContext);
            });
Exemplo n.º 5
0
        internal static Action <TextWriter, object> CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, ICompiledHandlebarsConfiguration configuration)
        {
            IEnumerable <object> tokens;

            using (var sr = readerFactoryFactory(configuration, templatePath))
            {
                using (var reader = new ExtendedStringReader(sr))
                {
                    tokens = Tokenizer.Tokenize(reader).ToList();
                }
            }

            var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault();

            var expressionBuilder = new ExpressionBuilder(configuration);
            var expressions       = expressionBuilder.ConvertTokensToExpressions(tokens);
            var compiledView      = FunctionBuilder.Compile(expressions, configuration, templatePath);

            if (layoutToken == null)
            {
                return(compiledView);
            }

            var fs         = configuration.FileSystem;
            var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs");

            if (layoutPath == null)
            {
                throw new InvalidOperationException("Cannot find layout '" + layoutPath + "' for template '" +
                                                    templatePath + "'");
            }

            var compiledLayout = CompileView(readerFactoryFactory, layoutPath, configuration);

            return((tw, vm) =>
            {
                string inner;
                using (var innerWriter = ReusableStringWriter.Get(configuration.FormatProvider))
                {
                    compiledView(innerWriter, vm);
                    inner = innerWriter.ToString();
                }

                compiledLayout(tw, new DynamicViewModel(new[] { new { body = inner }, vm }));
            });
        }
Exemplo n.º 6
0
 internal sealed override object ReturnInvoke(BindingContext bindingContext, object context, object[] arguments)
 {
     using var writer = ReusableStringWriter.Get(bindingContext.Configuration.FormatProvider);
     WriteInvoke(bindingContext, writer, context, arguments);
     return(writer.ToString());
 }