Exemple #1
0
        public void config_permalink_sets_relative_file_output_path()
        {
            var context = new SiteContext();

            context.Config = new Dictionary <string, object>();
            context.Config.Add("permalink", "/blog/:year/:month/:day/:title.html");

            var page = new Page()
            {
                Url = "/blog/2010/08/21/title-of-my-post.html"
            };

            var outputPath        = "c:\\temp";
            var defaultOutputPath = "c:\\default";

            var pageContext = PageContext.FromPage(context, page, outputPath, defaultOutputPath);

            Assert.Equal("c:\\temp\\blog\\2010\\08\\21\\title-of-my-post.html", pageContext.OutputPath);
        }
Exemple #2
0
        public void no_permalink_sets_default_output_path_and_page_bag_permalink()
        {
            var context = new SiteContext();

            context.Config = new Dictionary <string, object>();

            var file = "title-of-my-post.html";
            var page = new Page()
            {
                File = file
            };

            page.Bag = new Dictionary <string, object>();

            var outputPath        = "c:\\temp";
            var defaultOutputPath = "c:\\default\\title-of-my-post.html";

            var pageContext = PageContext.FromPage(context, page, outputPath, defaultOutputPath);

            Assert.Equal("c:\\default\\title-of-my-post.html", pageContext.OutputPath);
            Assert.Equal(file, page.Bag["permalink"]);
        }
        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.Content = markdown.Transform(pageContext.Content);
            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());
                    pageContexts.Add(new PageContext(pageContext)
                    {
                        Paginator = newPaginator, OutputPath = path
                    });
                }
            }

            foreach (var context in pageContexts)
            {
                var metadata = page.Bag;
                var failed   = false;
                while (metadata.ContainsKey("layout"))
                {
                    var layout = metadata["layout"];
                    if ((string)layout == "nil" || layout == null)
                    {
                        break;
                    }

                    var path = Path.Combine(Context.SourceFolder, "_layouts", layout + LayoutExtension);

                    if (!FileSystem.File.Exists(path))
                    {
                        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);
            }
        }