Exemplo n.º 1
1
        protected override string RenderTemplate(string content, PageContext pageData)
        {
            var serviceConfiguration = new TemplateServiceConfiguration
            {
                TemplateManager = new IncludesResolver(FileSystem, includesPath),
                BaseTemplateType = typeof(ExtensibleTemplate<>),
                DisableTempFileLocking = true,
                CachingProvider = new DefaultCachingProvider(t => { })
            };
            serviceConfiguration.Activator = new ExtensibleActivator(serviceConfiguration.Activator, Filters, _allTags);

            Engine.Razor = RazorEngineService.Create(serviceConfiguration);

            content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");

            var pageContent = pageData.Content;
            pageData.Content = pageData.FullContent;

            try
            {
                content = Engine.Razor.RunCompile(content, pageData.Page.File, typeof(PageContext), pageData);
                pageData.Content = pageContent;
                return content;
            }
            catch (Exception e)
            {
                Tracing.Error(@"Failed to render template, falling back to direct content");
                Tracing.Debug(e.Message);
                Tracing.Debug(e.StackTrace);
                return content;
            }
        }
Exemplo n.º 2
0
 protected override string RenderTemplate(string templateContents, PageContext pageData)
 {
     var data = CreatePageData(pageData);
     var template = Template.Parse(templateContents);
     Template.FileSystem = new Includes(Context.SourceFolder);
     return template.Render(data);
 }
Exemplo n.º 3
0
        private Hash CreatePageData(PageContext pageContext)
        {
            var y = Hash.FromDictionary(pageContext.Bag);

            if (y.ContainsKey("title"))
            {
                if (string.IsNullOrWhiteSpace(y["title"].ToString()))
                {
                    y["title"] = context.Title;
                }
            }
            else
            {
                y.Add("title", context.Title);
            }

            var x = Hash.FromAnonymousObject(new
            {
                site = contextDrop,
                page = y,
                content = pageContext.Content
            });

            return x;
        }
Exemplo n.º 4
0
        private Hash CreatePageData(PageContext pageContext)
        {
            var y = Hash.FromDictionary(pageContext.Bag);

            if (y.ContainsKey("title"))
            {
                if (string.IsNullOrWhiteSpace(y["title"].ToString()))
                {
                    y["title"] = Context.Title;
                }
            }
            else
            {
                y.Add("title", Context.Title);
            }

            var x = Hash.FromAnonymousObject(new
            {
                site = contextDrop.ToHash(),
                wtftime = Hash.FromAnonymousObject(new { date = DateTime.Now }),
                page = y,
                content = pageContext.Content,
                paginator = pageContext.Paginator,
            });

            return x;
        }
Exemplo n.º 5
0
        Hash CreatePageData(PageContext pageContext)
        {
            var y = Hash.FromDictionary(pageContext.Bag);

            if (y.ContainsKey("title"))
            {
                if (string.IsNullOrWhiteSpace(y["title"].ToString()))
                {
                    y["title"] = Context.Title;
                }
            }
            else
            {
                y.Add("title", Context.Title);
            }

            var x = Hash.FromAnonymousObject(new
            {
                site = contextDrop.ToHash(),
                wtftime = Hash.FromAnonymousObject(new { date = DateTime.Now }),
                page = y,
                content = pageContext.Content,//)//Markdown.Transform(contents.ExcludeHeader()),
            });

            if (Context.Config.ContainsKey("paginate") && pageContext.OutputPath.EndsWith("index.html"))
            {
                x.Add("paginator", new Paginator(Context));
            }
            return x;
        }
Exemplo n.º 6
0
        protected override string RenderTemplate(string content, PageContext pageData)
        {
            var includesPath = Path.Combine(pageData.Site.SourceFolder, "_includes");
               var serviceConfig = new TemplateServiceConfiguration { Resolver = new IncludesResolver(FileSystem, includesPath) };
               RazorEngine.Razor.SetTemplateService(new TemplateService(serviceConfig));

               content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");

               return RazorEngine.Razor.Parse(content, pageData);
        }
Exemplo n.º 7
0
        protected override string RenderTemplate(string templateContents, PageContext pageData)
        {
            // Replace all em HTML tags in liquid tags ({{ or {%) by underscores
            templateContents = emHtmlRegex.Replace(templateContents, "_");

            var data = CreatePageData(pageData);
            var template = Template.Parse(templateContents);
            var output = template.Render(data);

            return output;
        }
Exemplo n.º 8
0
 public PageContext(PageContext context)
 {
     Title = context.Title;
     OutputPath = context.OutputPath;
     Bag = context.Bag;
     Content = context.Content;
     Site = context.Site;
     Page = context.Page;
     Previous = context.Previous;
     Next = context.Next;
     Paginator = context.Paginator;
 }
Exemplo n.º 9
0
 public PageContext(PageContext context)
 {
     Title = context.Title;
     OutputPath = context.OutputPath;
     Bag = new Dictionary<string, object>(context.Bag);
     Content = context.Content;
     Site = context.Site;
     Page = context.Page;
     Previous = context.Previous;
     Next = context.Next;
     Paginator = context.Paginator;
 }
Exemplo n.º 10
0
        public static PageContext FromDictionary(IDictionary<string, object> metadata, string outputPath, string defaultOutputPath)
        {
            var context = new PageContext
                              {
                                  OutputPath =
                                      metadata.ContainsKey("permalink")
                                          ? Path.Combine(outputPath, metadata["permalink"].ToString().ToRelativeFile())
                                          : defaultOutputPath
                              };

            if (metadata.ContainsKey("title"))
            {
                context.Title = metadata["title"].ToString();
            }

            context.Bag = metadata;

            return context;
        }
Exemplo n.º 11
0
        protected override string RenderTemplate(string content, PageContext pageData)
        {
            var includesPath = Path.Combine(pageData.Site.SourceFolder, "_includes");
               var serviceConfig = new TemplateServiceConfiguration
                               {
                                  Resolver = new IncludesResolver(FileSystem, includesPath),
                                  BaseTemplateType = typeof (ExtensibleTemplate<>)
                               };
               serviceConfig.Activator = new ExtensibleActivator(serviceConfig.Activator, Filters);
               RazorEngine.Razor.SetTemplateService(new TemplateService(serviceConfig));

               content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");
            try
            {
                return RazorEngine.Razor.Parse(content, pageData);
            }
            catch (Exception)
            {
                Console.WriteLine(@"Failed to render template, falling back to direct content");
                return content;
            }
        }
Exemplo n.º 12
0
        public static PageContext FromPage(SiteContext siteContext, Page page, string outputPath, string defaultOutputPath)
        {
            var context = new PageContext(siteContext, page);

            if (page.Bag.ContainsKey("permalink") || siteContext.Config.ContainsKey("permalink"))
            {
                context.OutputPath = Path.Combine(outputPath, page.Url.ToRelativeFile());
            }
            else
            {
                context.OutputPath = defaultOutputPath;
                page.Bag.Add("permalink", page.File);
            }

            if (context.OutputPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                context.OutputPath = Path.Combine(context.OutputPath, "index.html");
            }

            page.OutputFile = context.OutputPath;

            if (page.Bag.ContainsKey("title"))
            {
                context.Title = page.Bag["title"].ToString();
            }

            if (string.IsNullOrEmpty(context.Title))
            {
                context.Title = siteContext.Title;
            }

            context.Content = page.Content;
            context.FullContent = page.Content;
            context.Bag = page.Bag;
            context.Bag["id"] = page.Id;
            context.Bag["url"] = page.Url;
            return context;
        }
Exemplo n.º 13
0
        protected override string RenderTemplate(string content, PageContext pageData)
        {
            var serviceConfiguration = new TemplateServiceConfiguration
            {
                TemplateManager = new IncludesResolver(FileSystem, includesPath),
                BaseTemplateType = typeof(ExtensibleTemplate<>)
            };
            serviceConfiguration.Activator = new ExtensibleActivator(serviceConfiguration.Activator, Filters, Tags);
            Engine.Razor = RazorEngineService.Create(serviceConfiguration);

            content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");

            try
            {
                return Engine.Razor.RunCompile(content, pageData.Page.File, typeof(PageContext), pageData);
            }
            catch (Exception ex)
            {
                Tracing.Debug(ex.Message + Environment.NewLine + ex.StackTrace);
                Console.WriteLine(@"Failed to render template, falling back to direct content");
                return content;
            }
        }
Exemplo n.º 14
0
        public static PageContext FromPage(Page page, string outputPath, string defaultOutputPath)
        {
            var context = new PageContext();

            if (page.Bag.ContainsKey("permalink"))
            {
                context.OutputPath = Path.Combine(outputPath, page.Url.ToRelativeFile());
            }
            else
            {
                context.OutputPath = defaultOutputPath;
                page.Bag.Add("permalink", page.File);
            }
            if (page.Bag.ContainsKey("title"))
            {
                context.Title = page.Bag["title"].ToString();
            }

            context.Content = page.Content;
            context.Bag = page.Bag;
            context.Bag.Add("id", page.Id);
            context.Bag.Add("url", page.Url);
            return context;
        }
Exemplo n.º 15
0
 protected abstract string RenderTemplate(string content, PageContext pageData);
Exemplo n.º 16
0
        private IDictionary<string, object> ProcessTemplate(PageContext pageContext, string path)
        {
            var templateFile = FileSystem.File.ReadAllText(path);
            var metadata = templateFile.YamlHeader();
            var templateContent = templateFile.ExcludeHeader();

            pageContext.Content = RenderTemplate(templateContent, pageContext);
            return metadata;
        }
Exemplo n.º 17
0
        protected override string RenderTemplate(string content, PageContext pageData)
        {
            content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");

            return RazorEngine.Razor.Parse(content, pageData);
        }
Exemplo n.º 18
0
        private void ProcessFile(string outputDirectory, Page page, Page previous, Page next, bool skipFileOnError, string relativePath = "")
        {
            if (string.IsNullOrWhiteSpace(relativePath))
                relativePath = MapToOutputPath(page.File);

            page.OutputFile = Path.Combine(outputDirectory, relativePath);
            var extension = Path.GetExtension(page.File);

            if (extension.IsImageFormat())
            {
                CreateOutputDirectory(page.OutputFile);
                CopyFileIfSourceNewer(page.File, page.OutputFile, true);
                return;
            }

            if (page is NonProcessedPage)
            {
                CreateOutputDirectory(page.OutputFile);
                CopyFileIfSourceNewer(page.File, page.OutputFile, true);
                return;
            }

            if (extension.IsMarkdownFile() || extension.IsRazorFile())
                page.OutputFile = page.OutputFile.Replace(extension, ".html");

            var pageContext = PageContext.FromPage(Context, page, outputDirectory, page.OutputFile);

            pageContext.Previous = previous;
            pageContext.Next = next;

            var pageContexts = new List<PageContext> { pageContext };
            object paginateObj;
            if (page.Bag.TryGetValue("paginate", out paginateObj))
            {
                var paginate = Convert.ToInt32(paginateObj);
                var totalPages = (int)Math.Ceiling(Context.Posts.Count / Convert.ToDouble(paginateObj));
                var paginator = new Paginator(Context, totalPages, paginate, 1);
                pageContext.Paginator = paginator;

                var paginateLink = "/page/:page/index.html";
                if (page.Bag.ContainsKey("paginate_link"))
                    paginateLink = Convert.ToString(page.Bag["paginate_link"]);

                var prevLink = page.Url;
                for (var i = 2; i <= totalPages; i++)
                {
                    var newPaginator = new Paginator(Context, totalPages, paginate, i) { PreviousPageUrl = prevLink };
                    var link = paginateLink.Replace(":page", Convert.ToString(i));
                    paginator.NextPageUrl = link;

                    paginator = newPaginator;
                    prevLink = link;

                    var path = Path.Combine(outputDirectory, link.ToRelativeFile());
                    if (path.EndsWith(FileSystem.Path.DirectorySeparatorChar.ToString())) {
                        path = Path.Combine(path, "index.html");
                    }
                    var context = new PageContext(pageContext) { Paginator = newPaginator, OutputPath = path };
                    context.Bag["url"] = link;
                    pageContexts.Add(context);
                }
            }

            foreach (var context in pageContexts)
            {
                var metadata = page.Bag;
                var failed = false;

                var excerptSeparator = context.Bag.ContainsKey("excerpt_separator")
                    ? context.Bag["excerpt_separator"].ToString()
                    : Context.ExcerptSeparator;
                try
                {
                    context.Bag["excerpt"] = GetContentExcerpt(RenderTemplate(context.Content, context), excerptSeparator);
                }
                catch (Exception ex)
                {
                    if (!skipFileOnError)
                    {
                        var message = string.Format("Failed to process {0}, see inner exception for more details", context.OutputPath);
                        throw new PageProcessingException(message, ex);
                    }

                    Console.WriteLine(@"Failed to process {0}, see inner exception for more details", context.OutputPath);
                    continue;
                }

                while (metadata.ContainsKey("layout"))
                {
                    var layout = metadata["layout"];
                    if ((string)layout == "nil" || layout == null)
                        break;

                    var path = FindLayoutPath(layout.ToString());

                    if (path == null)
                        break;

                    try
                    {
                        metadata = ProcessTemplate(context, path);
                    }
                    catch (Exception ex)
                    {
                        if (!skipFileOnError)
                        {
                            var message = string.Format("Failed to process layout {0} for {1}, see inner exception for more details", layout, context.OutputPath);
                            throw new PageProcessingException(message, ex);
                        }

                        Console.WriteLine(@"Failed to process layout {0} for {1} because '{2}'. Skipping file", layout, context.OutputPath, ex.Message);
                        failed = true;
                        break;
                    }
                }
                if (failed)
                    continue;

                try
                {
                    context.Content = RenderTemplate(context.Content, context);
                }
                catch (Exception ex)
                {
                    if (!skipFileOnError)
                    {
                        var message = string.Format("Failed to process {0}, see inner exception for more details", context.OutputPath);
                        throw new PageProcessingException(message, ex);
                    }

                    Console.WriteLine(@"Failed to process {0}, see inner exception for more details", context.OutputPath);
                    continue;
                }

                CreateOutputDirectory(context.OutputPath);
                FileSystem.File.WriteAllText(context.OutputPath, context.Content);
            }
        }
Exemplo n.º 19
0
 protected override string RenderTemplate(string content, PageContext pageData)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 20
0
        private static Hash CreatePageData(SiteContext context, PageContext pageContext)
        {
            var title = string.IsNullOrWhiteSpace(pageContext.Title) ? context.Title : pageContext.Title;

            var drop = new SiteContextDrop(context);

            return Hash.FromAnonymousObject(new
            {
                site = drop,
                page = new { title },
                content = pageContext.Content
            });
        }