예제 #1
0
        private PageTemplate TemplateResolver(string templateName)
        {
            string location        = null;
            string templateContent = GetTemplate(templateName, out location);
            var    template        = Razorizer.Compile(templateName, templateContent, location);

            template.Model = this;
            return(template);
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TemplateContext"/> class.
 /// </summary>
 public TemplateContext(Razorizer razorizer)
 {
     this.Razorizer = razorizer;
     StyleDirectories = new List<string>();
     _regexItems = new List<TagExpandItem>();
     _msnRegistry = new MsdnRegistry();
     Param = new DynamicParam();
     Style = new DynamicParam();
     razorizer.TemplateResolver += TemplateResolver;
 }
예제 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TemplateContext"/> class.
 /// </summary>
 public TemplateContext(Razorizer razorizer)
 {
     this.Razorizer              = razorizer;
     StyleDirectories            = new List <string>();
     _regexItems                 = new List <TagExpandItem>();
     _msnRegistry                = new MsdnRegistry();
     Param                       = new DynamicParam();
     Style                       = new DynamicParam();
     razorizer.TemplateResolver += TemplateResolver;
 }
예제 #4
0
        /// <summary>
        /// Parses the specified template name.
        /// </summary>
        /// <param name="templateName">Name of the template.</param>
        /// <returns></returns>
        public string Parse(string templateName)
        {
            string location = null;

            try
            {
                var template = Razorizer.FindTemplate(templateName);
                template.Model = this;
                return(template.Run());
            }
            catch (TemplateCompilationException ex)
            {
                foreach (var compilerError in ex.Errors)
                {
                    Logger.PushLocation(location, compilerError.Line, compilerError.Column);
                    if (compilerError.IsWarning)
                    {
                        Logger.Warning("{0}: {1}", compilerError.ErrorNumber, compilerError.ErrorText);
                    }
                    else
                    {
                        Logger.Error("{0}: {1}", compilerError.ErrorNumber, compilerError.ErrorText);
                    }
                    Logger.PopLocation();
                }
                Logger.PopLocation();
                Logger.Fatal("Error when compiling template [{0}]", templateName);
            }
            catch (TemplateParsingException ex)
            {
                foreach (var parserError in ex.Errors)
                {
                    Logger.PushLocation(ex.Location, parserError.Location.LineIndex, parserError.Location.CharacterIndex);
                    Logger.Error("{0}: {1}", "R0000", parserError.Message);
                    Logger.PopLocation();
                }
                Logger.PopLocation();
                Logger.Fatal("Error when compiling template [{0}]", templateName);
            }
            catch (Exception ex)
            {
                Logger.PushLocation(location);
                Logger.Error("Unexpected exception", ex);
                Logger.PopLocation();
                throw;
            }
            return("");
        }
예제 #5
0
        /// <summary>
        /// Runs this instance.
        /// </summary>
        public void Run()
        {
            var clock = Stopwatch.StartNew();

            var razorizer = new Razorizer(typeof(PageTemplateDoc))
            {
                EnableDebug = true
            };

            // New instance of a template context used by the RazorEngine
            var context = new TemplateContext(razorizer)
            {
                Config       = Config,
                StyleManager = StyleManager,
            };

            // Create web documentation
            if (Config.WebDocumentationUrl != null)
            {
                context.WebDocumentation = new WebDocumentation(Config.WebDocumentationUrl, Config.WebDocumentationLogin);
            }

            // Setup the context based on the config and StyleManager
            context.Initialize();

            // Verify the validity of the style
            foreach (var styleName in Config.StyleNames)
            {
                Logger.Message("-------------------------------------------------------------------------------");
                Logger.Message("Generating documentation using [{0}] style", styleName);
                Logger.Message("-------------------------------------------------------------------------------");
                context.UseStyle(styleName);
                context.Parse(StyleDefinition.DefaultBootableTemplateName);
            }

            Logger.Message("Total time: {0:F1}s", clock.ElapsedMilliseconds / 1000.0f);
            //Logger.Message("Time for assembly processing: {0:F1}s", timeForModelProcessor/1000.0f);
            //Logger.Message("Time for writing content: {0:F1}s", timeForWriting/1000.0f);

            if ((Config.OutputType & OutputType.DocPak) != 0)
            {
                GenerateDocPak();
            }
        }
예제 #6
0
        public void TestLayout()
        {
            var layout = @"@{   
    this.Layout = @""layoutRendering"";
}
@section Head
{<head>
    <title>@Model[""mykey""]</title>
</head>}
@section Body
{<body>
my body
</body>}
";

            var layoutRendering = @"<!DOCTYPE html>
<html>
@RenderSection(""Head"")
@RenderSection(""Body"")
</html>
";
            // Create razorizer
            var razorizer = new Razorizer {EnableDebug = true};

            var model = new Dictionary<string, object>() {{"mykey", "This is the title"}};

            // Pre-generate the layoutRendering page (and cache it using its name)
            var layoutRenderingPage = razorizer.Compile("layoutRendering", layoutRendering,
                typeof (IDictionary<string, object>));

            // Run the layout (using indirectly the previous page compiled)
            var result = razorizer.Parse(layout, model);

            Assert.AreEqual(@"<!DOCTYPE html>
<html>
<head>
    <title>This is the title</title>
</head>
<body>
my body
</body>
</html>
", result);
        }
예제 #7
0
        /// <summary>
        /// Runs this instance.
        /// </summary>
        public void Run()
        {
            var clock = Stopwatch.StartNew();

            var razorizer = new Razorizer(typeof (PageTemplateDoc)) { EnableDebug = true };

            // New instance of a template context used by the RazorEngine
            var context = new TemplateContext(razorizer)
            {
                Config = Config,
                StyleManager = StyleManager,
            };

            // Create web documentation
            if (Config.WebDocumentationUrl != null)
            {
                context.WebDocumentation = new WebDocumentation(Config.WebDocumentationUrl, Config.WebDocumentationLogin);
            }

            // Setup the context based on the config and StyleManager
            context.Initialize();

            // Verify the validity of the style
            foreach (var styleName in Config.StyleNames)
            {
                Logger.Message("-------------------------------------------------------------------------------");
                Logger.Message("Generating documentation using [{0}] style", styleName);
                Logger.Message("-------------------------------------------------------------------------------");
                context.UseStyle(styleName);
                context.Parse(StyleDefinition.DefaultBootableTemplateName);
            }

            Logger.Message("Total time: {0:F1}s", clock.ElapsedMilliseconds / 1000.0f);
            //Logger.Message("Time for assembly processing: {0:F1}s", timeForModelProcessor/1000.0f);
            //Logger.Message("Time for writing content: {0:F1}s", timeForWriting/1000.0f);

            if ((Config.OutputType & OutputType.DocPak) != 0 )
                GenerateDocPak();
        }
예제 #8
0
 public void Setup()
 {
     razorizer = new Razorizer();
 }