Ejemplo n.º 1
0
        private async Task <LayoutFile> LoadLayoutAsync(string path)
        {
            var parser = new ParseDocumentCommand();

            parser.DocumentPath = path;
            await parser.ExecuteAsync();

            return(new LayoutFile(path, this.LayoutsPath, parser.Content, parser.Metadata));
        }
Ejemplo n.º 2
0
        public void CanParseTags()
        {
            var path = Path.GetFullPath(@"data\documents\hastags.txt");

            var command = new ParseDocumentCommand(path);
            command.Execute();

            Assert.Equal(new[] { "foo", "bar", "baz" }, command.Metadata.Get<string[]>("tags"));
        }
Ejemplo n.º 3
0
        private async Task<DataFile> LoadLayoutAsync(string path)
        {
            var parser = new ParseDocumentCommand(path);
            await parser.ExecuteAsync();

            var metadataDate = parser.Date;

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

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

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

            // If the parser metadata didn't specify the date, try to use a date from the filename.
            //
            if (!disableDateFromFileName && !metadataDate.HasValue)
            {
                var fileName = Path.GetFileName(path);
                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;

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

            var dataFile = new DataFile(path, this.DataPath, parser.Content, parser.Metadata, parser.Queries);

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

            return dataFile;
        }
Ejemplo n.º 4
0
        public void CanParseNoMetadata()
        {
            var path = Path.GetFullPath(@"data\documents\nometa.txt");
            string expected = "This is text.\r\n   It has no metadata.";

            var command = new ParseDocumentCommand(path);
            command.Execute();

            Assert.Null(command.Date);
            Assert.False(command.Draft);
            Assert.Empty(command.Metadata);
            Assert.Equal(expected.Replace("\r\n", "\n"), command.Content.Replace("\r\n", "\n"));
        }
Ejemplo n.º 5
0
        public void CanParseMetadata()
        {
            var path = Path.GetFullPath(@"data\documents\hasmeta.txt");
            var expected = "This is text.\r\n   It has title metadata.";

            var command = new ParseDocumentCommand(path);
            command.Execute();

            Assert.Null(command.Date);
            Assert.NotEmpty(command.Metadata);
            Assert.Equal("Title from the metadata.", command.Metadata.Get<string>("title"));
            Assert.Equal(expected.Replace("\r\n", "\n"), command.Content.Replace("\r\n", "\n"));
        }
        public void CanParseDraft()
        {
            var path = Path.GetFullPath(@"data\documents\draft.txt");
            string expected = "This is a draft.\r\n   It has metadata.";

            var command = new ParseDocumentCommand(path);
            command.ExecuteAsync().Wait();

            Assert.Null(command.Date);
            Assert.True(command.Draft);
            Assert.Empty(command.Metadata);
            Assert.Equal(expected.Replace("\r\n", "\n"), command.Content.Replace("\r\n", "\n"));
        }
Ejemplo n.º 7
0
        private async Task<LayoutFile> LoadLayoutAsync(string path)
        {
            var parser = new ParseDocumentCommand(path);
            await parser.ExecuteAsync();

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

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

            return new LayoutFile(path, this.LayoutsPath, parser.Content, parser.Metadata, parser.Queries);
        }
Ejemplo n.º 8
0
        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;
        }
Ejemplo n.º 9
0
        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);
        }