Beispiel #1
0
        public Site(SiteConfig config, IEnumerable<DataFile> data, IEnumerable<DocumentFile> documents, IEnumerable<StaticFile> files, LayoutFileCollection layouts, Site parent = null)
        {
            this.DefaultLayoutForExtension = new Dictionary<string, string>(config.DefaultLayoutForExtension);
            this.IgnoreFiles = config.IgnoreFiles;

            this.SitePath = config.SitePath;

            this.Author = config.Author;
            this.LiveReloadScript = config.LiveReloadScript;
            this.DocumentsPath = config.DocumentsPath;
            this.FilesPath = config.FilesPath;
            this.LayoutsPath = config.LayoutsPath;
            this.OutputPath = config.OutputPath;
            this.Parent = parent;
            this.TimeZone = config.TimeZone;
            this.Url = config.Url.EnsureEndsWith("/");
            this.RootUrl = config.RootUrl.EnsureEndsWith("/");

            var allDocuments = documents.ToList();

            this.Data = data.ToList();

            this.Partials = allDocuments.Where(d => d.Partial).ToList();

            this.Documents = allDocuments.Where(d => !d.Partial).ToList();

            this.Files = files.ToList();

            this.Layouts = new LayoutFileCollection(layouts);

            this.Metadata = config.Metadata;
        }
        public void CanRenderDocument()
        {
            var tinySiteAssembly = typeof(RazorRenderer).Assembly;

            var basePath = Path.GetFullPath(@"data\RenderDocumentsCommand\");

            var outputPath = Path.Combine(Path.GetTempPath(), @"tinysite\");

            var loadLayouts = new LoadLayoutsCommand(Path.Combine(basePath, @"layouts\"), null, null);
            var layouts = loadLayouts.Execute();
            var collection = new LayoutFileCollection(layouts);

            var loadDocuments = new LoadDocumentsCommand();
            loadDocuments.Author = new Author();
            loadDocuments.DocumentsPath = Path.Combine(basePath, @"documents\");
            loadDocuments.Layouts = collection;
            loadDocuments.OutputRootPath = outputPath;
            loadDocuments.RenderedExtensions = new[] { "md", "cshtml" };
            loadDocuments.RootUrl = "http://www.example.com/";
            loadDocuments.ApplicationUrl = "/app/sub";
            var documents = loadDocuments.Execute().OrderBy(d => d.Name).ToList();

            var config = new SiteConfig() { OutputPath = outputPath, Url = "http://example.com", RootUrl = String.Empty, };

            var site = new Site(config, Enumerable.Empty<DataFile>(), documents, Enumerable.Empty<StaticFile>(), collection);

            var engines = RenderingEngine.Load(tinySiteAssembly);

            Statistics.Current = new Statistics();

            var command = new RenderDocumentsCommand(engines, site);
            command.Execute();

            var description = "This is the summary of the document with a link to example.com.";
            var content = "<p>This is the summary of the document with a link to <a href=\"http://example.com\">example.com</a>.</p>\n<p>This is additional content in the document.</p>";
            var summary = "<p>This is the summary of the document with a link to <a href=\"http://example.com\">example.com</a>.</p>";
            var title = "test";

            Assert.Equal(0, command.RenderedData);
            Assert.Equal(3, command.RenderedDocuments);
            Assert.Equal(String.Empty, documents[0].RenderedContent.Trim());

            Assert.Equal("This is the summary of the document with a link to [example.com](http://example.com).\n\nThis is additional content in the document.", documents[2].SourceContent.Replace("\r\n", "\n"));
            Assert.Equal(content, documents[2].Content);
            Assert.Equal(description, documents[2].Description);
            Assert.Equal(summary, documents[2].Summary);
            Assert.Equal(title, documents[2].Metadata.Get<string>("title"));

            Assert.Equal(
                $"<title>{title}</title>\n" +
                $"<description>{description}</description>\n" +
                $"<summary>{summary}</summary>\n" +
                $"<content>{content}</content>",
                documents[2].RenderedContent.Replace("\r\n", "\n"));
        }
        private async Task<DocumentFile> LoadDocumentAsync(string file, IEnumerable<string> knownExtensions, LayoutFileCollection availableLayouts)
        {
            // Parse the document and update our document metadata.
            //
            var parser = new ParseDocumentCommand(file);
            await parser.ExecuteAsync();

            if (this.AdditionalMetadataForFiles != null)
            {
                var rootPath = Path.GetDirectoryName(this.DocumentsPath.TrimEnd('\\'));
                var relativePath = file.Substring(rootPath.Length + 1);

                foreach (var additionalMetadataConfig in this.AdditionalMetadataForFiles)
                {
                    if (additionalMetadataConfig.Match.IsMatch(relativePath))
                    {
                        parser.Metadata.AssignFrom(file, additionalMetadataConfig.Metadata);
                    }
                }
            }

            var metadataDate = parser.Date;

            var partial = false;

            var order = 0;

            var relativeDocumentPath = Path.GetFullPath(file).Substring(this.DocumentsPath.Length);

            var outputRelativePath = parser.Metadata.GetAndRemove("output", relativeDocumentPath);

            // The rest of this function is about calculating the correct
            // name for the file.
            //
            var fileName = Path.GetFileName(outputRelativePath);

            var outputRelativeFolder = Path.GetDirectoryName(outputRelativePath);

            // See if this file should be processed by any of the
            // rendering engines.
            //
            var extensionsForRendering = new List<string>();

            for (;;)
            {
                var extension = Path.GetExtension(fileName).TrimStart('.');
                if (extension.Equals("partial", StringComparison.OrdinalIgnoreCase))
                {
                    partial = true;
                    fileName = Path.GetFileNameWithoutExtension(fileName);
                }
                else if (knownExtensions.Contains(extension))
                {
                    extensionsForRendering.Add(extension);
                    fileName = Path.GetFileNameWithoutExtension(fileName);
                }
                else
                {
                    break;
                }
            }

            var targetExtension = Path.GetExtension(fileName).TrimStart('.');

            var layoutName = parser.Metadata.Get<string>("layout");

            var layouts = GetLayouts(layoutName, targetExtension, file);

            var disableDateFromFileName = parser.Metadata.GetAndRemove("DisableDateFromFileName", false);

            if (!disableDateFromFileName)
            {
                var match = DateFromFileName.Match(fileName);

                if (match.Success)
                {
                    // If the parser metadata didn't specify the date, use the date from the filename.
                    //
                    if (!metadataDate.HasValue)
                    {
                        var year = Convert.ToInt32(match.Groups[1].Value, 10);
                        var month = Convert.ToInt32(match.Groups[2].Value, 10);
                        var day = Convert.ToInt32(match.Groups[3].Value, 10);
                        var hour = match.Groups[4].Success ? Convert.ToInt32(match.Groups[4].Value, 10) : 0;
                        var minute = match.Groups[5].Success ? Convert.ToInt32(match.Groups[5].Value, 10) : 0;
                        var second = match.Groups[6].Success ? Convert.ToInt32(match.Groups[6].Value, 10) : 0;

                        metadataDate = new DateTime(year, month, day, hour, minute, second);
                    }

                    fileName = fileName.Substring(match.Length);
                }
            }

            var disableOrderFromFileName = parser.Metadata.GetAndRemove("DisableOrderFromFileName", false);

            if (!disableOrderFromFileName)
            {
                var match = OrderFromFileName.Match(fileName);

                if (match.Success)
                {
                    order = Convert.ToInt32(match.Groups[1].Value, 10);

                    fileName = fileName.Substring(match.Length);
                }
            }

            var parentId = String.IsNullOrEmpty(outputRelativeFolder) ? null : SanitizePath(outputRelativeFolder);

            var disableInsertDateIntoPath = parser.Metadata.GetAndRemove("DisableInsertDateIntoPath", false);

            if (!disableInsertDateIntoPath && metadataDate.HasValue)
            {
                outputRelativeFolder = Path.Combine(outputRelativeFolder, metadataDate.Value.Year.ToString(), metadataDate.Value.Month.ToString(), metadataDate.Value.Day.ToString());
            }

            if (!parser.Metadata.Contains("title"))
            {
                parser.Metadata.Add("title", Path.GetFileNameWithoutExtension(fileName));
            }

            // Sanitize the filename into a good URL.
            //
            var sanitized = SanitizeEntryId(fileName);

            if (!fileName.Equals(sanitized))
            {
                fileName = sanitized;
            }

            var disableSanitizePath = parser.Metadata.GetAndRemove("DisableSanitizePath", false);

            if (!disableSanitizePath)
            {
                outputRelativeFolder = SanitizePath(outputRelativeFolder);
            }

            var disableCleanUrls = parser.Metadata.GetAndRemove("DisableCleanUrls", false);

            if (!disableCleanUrls && !"index.html".Equals(fileName, StringComparison.OrdinalIgnoreCase) && ".html".Equals(Path.GetExtension(fileName), StringComparison.OrdinalIgnoreCase))
            {
                outputRelativeFolder = Path.Combine(outputRelativeFolder, Path.GetFileNameWithoutExtension(fileName)) + "\\";

                fileName = null;
            }

            var id = String.IsNullOrEmpty(fileName) ? outputRelativeFolder : Path.Combine(outputRelativeFolder, Path.GetFileNameWithoutExtension(fileName));

            var output = Path.Combine(outputRelativeFolder, fileName ?? "index.html");

            var relativeUrl = this.ApplicationUrl.EnsureEndsWith("/") + Path.Combine(outputRelativeFolder, fileName ?? String.Empty).Replace('\\', '/');

            // Finally create the document.
            //
            var documentFile = new DocumentFile(file, this.DocumentsPath, output, this.OutputRootPath, relativeUrl, this.RootUrl, this.Author, parser.Metadata, parser.Queries);

            documentFile.Partial = partial;

            if (metadataDate.HasValue)
            {
                documentFile.SetTimes(metadataDate.Value);
            }

            documentFile.Id = parser.Metadata.GetAndRemove("id", id.Trim('\\'));

            documentFile.ParentId = parser.Metadata.GetAndRemove("parent", parentId ?? String.Empty);

            documentFile.Draft = (parser.Draft || documentFile.Date > DateTime.Now);

            documentFile.ExtensionsForRendering = extensionsForRendering;

            documentFile.AssignLayouts(layouts);

            documentFile.Order = parser.Metadata.GetAndRemove("order", order);

            documentFile.PaginateQuery = parser.Metadata.GetAndRemove<string>("paginate");

            documentFile.SourceContent = parser.Content;

            return documentFile;
        }
Beispiel #4
0
        private Site LoadSite(SiteConfig config, IEnumerable<LastRunDocument> lastRunState, IEnumerable<string> renderedExtensions)
        {
            Site site;

            using (var capture = Statistics.Current.Start(StatisticTiming.LoadedSite))
            {
                // Load layouts.
                LayoutFileCollection layouts;
                {
                    var load = new LoadLayoutsCommand(config.LayoutsPath, config.AdditionalMetadataForFiles, config.IgnoreFiles);
                    var loaded = load.Execute();

                    layouts = new LayoutFileCollection(loaded);
                }

                // Load data files.
                IEnumerable<DataFile> data;
                {
                    var load = new LoadDataFilesCommand(config.DataPath, config.AdditionalMetadataForFiles, config.IgnoreFiles);
                    data = load.Execute();
                }

                // Load documents.
                IEnumerable<DocumentFile> documents;
                {
                    var load = new LoadDocumentsCommand();
                    load.Author = config.Author;
                    load.OutputRootPath = config.OutputPath;
                    load.Layouts = layouts;
                    load.RenderedExtensions = renderedExtensions;
                    load.DefaultLayoutForExtension = config.DefaultLayoutForExtension;
                    load.DocumentsPath = config.DocumentsPath;
                    load.RootUrl = config.RootUrl;
                    load.ApplicationUrl = config.Url;
                    load.AdditionalMetadataForFiles = config.AdditionalMetadataForFiles;
                    load.IgnoreFiles = config.IgnoreFiles;
                    documents = load.Execute();
                }

                // Load files.
                IEnumerable<StaticFile> files;
                {
                    var load = new LoadFilesCommand();
                    load.OutputPath = config.OutputPath;
                    load.FilesPath = config.FilesPath;
                    load.RootUrl = config.RootUrl;
                    load.Url = config.Url;
                    files = load.Execute();
                }

                // Calculate unmodified state.
                {
                    var unmodified = new SetUnmodifiedCommand(config.SitePath, documents, files, this.InitialLastRunState);
                    unmodified.Execute();
                }

                site = new Site(config, data, documents, files, layouts);
            }

            Statistics.Current.SiteFiles = site.Documents.Count + site.Files.Count + site.Layouts.Count;

            return site;
        }