public void Load(LoadDocumentFlags flags, IEnumerable <string> knownExtensions) { // Parse the document and update our document metadata. // var parser = new ParseDocumentCommand(); parser.DocumentPath = this.SourcePath; parser.SummaryMarker = "\r\n\r\n==="; parser.Execute(); this.Content = parser.Content; if (parser.Date.HasValue) { this.Date = parser.Date.Value; } this.Draft = (parser.Draft || this.Date > DateTime.Now); this.Paginate = parser.Metadata.Get <int>("paginate", 0); parser.Metadata.Remove("paginate"); string output; if (parser.Metadata.TryGet <string>("output", out output)) { this.SetOutputPaths(output); parser.Metadata.Remove("output"); } this.Metadata = parser.Metadata; // The rest of this function is about calculating the correct // name for the file. // var fileName = Path.GetFileName(this.RelativePath); string updateFileName = null; string updateInPath = String.Empty; // See if this file should be processed by any of the // rendering engines. // this.ExtensionsForRendering = new List <string>(); for (; ;) { var extension = Path.GetExtension(fileName).TrimStart('.'); if (knownExtensions.Contains(extension)) { this.ExtensionsForRendering.Add(extension); fileName = Path.GetFileNameWithoutExtension(fileName); updateFileName = fileName; } else { break; } } if (LoadDocumentFlags.DateFromFileName == (flags & LoadDocumentFlags.DateFromFileName)) { var match = DateFromFileName.Match(fileName); if (match.Success) { 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; // If the parser didn't override the date, use the date from the filename. // if (!parser.Date.HasValue) { this.Date = new DateTime(year, month, day, hour, minute, second); } fileName = fileName.Substring(match.Length); updateFileName = fileName; } } if (LoadDocumentFlags.DateInPath == (flags & LoadDocumentFlags.DateInPath) && this.Date != DateTime.MinValue) { updateInPath = String.Join("\\", this.Date.Year, this.Date.Month, this.Date.Day); } if (!this.Metadata.Contains("title")) { this.Metadata.Add("title", Path.GetFileNameWithoutExtension(fileName)); } // Sanitize the filename into a good URL. // var sanitized = SanitizeEntryId(fileName); if (!fileName.Equals(sanitized)) { fileName = sanitized; updateFileName = fileName; } if (LoadDocumentFlags.CleanUrls == (flags & LoadDocumentFlags.CleanUrls) && !"index.html".Equals(fileName, StringComparison.OrdinalIgnoreCase) && ".html".Equals(Path.GetExtension(fileName), StringComparison.OrdinalIgnoreCase)) { updateInPath = Path.Combine(updateInPath, Path.GetFileNameWithoutExtension(fileName)); fileName = "index.html"; updateFileName = fileName; } // If the name or path was updated, update the appropriately parts of the document. // if (!String.IsNullOrEmpty(updateFileName) || !String.IsNullOrEmpty(updateInPath)) { if (String.IsNullOrEmpty(updateFileName)) { updateFileName = fileName; } var updateInUrl = String.IsNullOrEmpty(updateInPath) ? String.Empty : updateInPath.Replace('\\', '/') + '/'; this.RelativePath = Path.Combine(Path.GetDirectoryName(this.RelativePath), updateInPath, updateFileName); this.OutputPath = Path.Combine(Path.GetDirectoryName(this.OutputPath), updateInPath, updateFileName); var lastSlash = this.Url.LastIndexOf('/'); this.Url = String.Concat(this.Url.Substring(0, lastSlash + 1), updateInUrl, updateFileName.Equals("index.html", StringComparison.OrdinalIgnoreCase) ? String.Empty : updateFileName); } }
private async Task <DocumentFile> LoadDocumentAsync(string file, LoadDocumentFlags flags, IEnumerable <string> knownExtensions) { // Parse the document and update our document metadata. // var parser = new ParseDocumentCommand(); parser.DocumentPath = file; parser.SummaryMarker = "\r\n\r\n==="; await parser.ExecuteAsync(); var metadataDate = parser.Date; var order = 0; var relativeDocumentPath = Path.GetFullPath(file).Substring(this.DocumentsPath.Length); var outputRelativePath = parser.Metadata.Get <string>("output", relativeDocumentPath); parser.Metadata.Remove("output"); // 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 (knownExtensions.Contains(extension)) { extensionsForRendering.Add(extension); fileName = Path.GetFileNameWithoutExtension(fileName); } else { break; } } if (LoadDocumentFlags.DateFromFileName == (flags & LoadDocumentFlags.DateFromFileName)) { 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); } } if (LoadDocumentFlags.OrderFromFileName == (flags & LoadDocumentFlags.OrderFromFileName)) { 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); if (LoadDocumentFlags.InsertDateIntoPath == (flags & LoadDocumentFlags.InsertDateIntoPath) && 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; } if (LoadDocumentFlags.SanitizePath == (flags & LoadDocumentFlags.SanitizePath)) { outputRelativeFolder = SanitizePath(outputRelativeFolder); } if (LoadDocumentFlags.CleanUrls == (flags & LoadDocumentFlags.CleanUrls) && !"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 parentId = String.IsNullOrEmpty(id) ? null : Path.GetDirectoryName(id); 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); if (metadataDate.HasValue) { documentFile.Date = metadataDate.Value; } documentFile.Id = parser.Metadata.Get <string>("id", id.Trim('\\')); parser.Metadata.Remove("id"); documentFile.ParentId = parser.Metadata.Get <string>("parent", parentId ?? String.Empty); parser.Metadata.Remove("parent"); documentFile.Draft = (parser.Draft || documentFile.Date > DateTime.Now); documentFile.ExtensionsForRendering = extensionsForRendering; documentFile.Order = parser.Metadata.Get <int>("order", order); parser.Metadata.Remove("order"); documentFile.Paginate = parser.Metadata.Get <int>("paginate", 0); parser.Metadata.Remove("paginate"); documentFile.SourceContent = parser.Content; documentFile.Metadata = parser.Metadata; return(documentFile); }