Exemplo n.º 1
0
    public void Test_FilemetadataParser_EmptyFileWithoutConfigOnlyGetsDefaultValues()
    {
        // Arange
        var optionsMock          = new MetadataParserOptions();
        var metadataProviderMock = new Mock <IMetadataProvider>();

        metadataProviderMock
        .Setup(x => x.Retrieve <FileMetaData>(It.Is <string>(p => p.Equals(string.Empty))))
        .Returns(new Metadata <FileMetaData> {
        });

        var loggerMock = new LoggerMock <FileMetadataParser>();
        var sut        = new FileMetadataParser(loggerMock.Object, metadataProviderMock.Object, optionsMock);
        var criteria   = new MetadataCriteria
        {
            Content  = string.Empty,
            FileName = "file.html"
        };

        // Act
        var result = sut.Parse(criteria);

        // Assert
        result.Should().NotBeNull();
        result.Data.Should().NotBeNull();
        result.Data.Count.Should().Be(1, "Only URI is added by default");
        result.Data.Uri.Should().NotBeNull();
        result.Data.Uri.Should().Be("file.html");
    }
Exemplo n.º 2
0
    public Metadata <FileMetaData> Parse(MetadataCriteria criteria)
    {
        var result = _metadataProvider.Retrieve <FileMetaData>(criteria.Content);

        _logger.LogInformation("Metadata count before '{MetadataCount}'", result.Data?.Count);
        var outputLocation = DetermineOutputLocation(criteria.FileName, criteria.Permalink, result.Data);
        var paths          = DetermineFilters(outputLocation);

        var fileMetaData = ApplyDefaults(paths);

        _logger.LogInformation("Metadata ApplyDefaults '{MetadataCount}'", fileMetaData.Count);
        OverwriteMetaData(fileMetaData, result.Data, "file");
        _logger.LogInformation("Metadata Merged '{MetadataCount}'", fileMetaData.Count);

        if (fileMetaData.Date != null && string.IsNullOrEmpty(fileMetaData.PublishedDate))
        {
            fileMetaData.PublishedDate = fileMetaData.Date.GetValueOrDefault().ToString("yyyy-MM-dd");
        }

        if (!string.IsNullOrEmpty(fileMetaData.PublishedDate) && !string.IsNullOrEmpty(fileMetaData.PublishedTime))
        {
            var publishedDateTimeString = $"{fileMetaData.PublishedDate} {fileMetaData.PublishedTime}";
            var publishedDate           = System.DateTimeOffset.ParseExact(publishedDateTimeString, "yyyy-MM-dd HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            fileMetaData.Date = publishedDate;
        }

        if (!string.IsNullOrEmpty(fileMetaData.PublishedDate) && string.IsNullOrEmpty(fileMetaData.ModifiedDate))
        {
            fileMetaData.ModifiedDate = fileMetaData.PublishedDate;
        }
        if (!string.IsNullOrEmpty(fileMetaData.PublishedTime) && string.IsNullOrEmpty(fileMetaData.ModifiedTime))
        {
            fileMetaData.ModifiedTime = fileMetaData.PublishedTime;
        }

        if (!string.IsNullOrEmpty(fileMetaData.ModifiedDate))
        {
            var dateTimeString   = !string.IsNullOrEmpty(fileMetaData.ModifiedTime) ? $"{fileMetaData.ModifiedDate} {fileMetaData.ModifiedTime}" : fileMetaData.ModifiedDate;
            var dateTimePattern  = !string.IsNullOrEmpty(fileMetaData.ModifiedTime) ? "yyyy-MM-dd HH:mm" : "yyyy-MM-dd";
            var modifiedDateTime = System.DateTimeOffset.ParseExact(dateTimeString, dateTimePattern, System.Globalization.CultureInfo.InvariantCulture);
            fileMetaData.Modified = modifiedDateTime;
        }

        // we now have applied all the defaults that match this document and combined it with the retrieved data, store it.
        result.Data = fileMetaData;

        // TODO: is this the right moment to store it back in the metadata?
        result.Data.Uri = outputLocation;

        return(result);
    }
Exemplo n.º 3
0
    public void Test_FilemetadataParser_EmptyFileIfMultipleConfigurationsApplyLastOneWins()
    {
        // Arange
        var options = new MetadataParserOptions
        {
            Defaults = new DefaultMetadatas {
                new DefaultMetadata {
                    Path   = string.Empty,
                    Values = new FileMetaData {
                        Layout = "default.html"
                    }
                },
                new DefaultMetadata {
                    Path   = "test",
                    Values = new FileMetaData {
                        Layout     = "other.html",
                        Collection = "test"
                    }
                }
            }
        };
        var metadataProviderMock = new Mock <IMetadataProvider>();

        metadataProviderMock
        .Setup(x => x.Retrieve <FileMetaData>(It.Is <string>(p => p.Equals(string.Empty))))
        .Returns(new Metadata <FileMetaData> {
        });

        var loggerMock = new LoggerMock <FileMetadataParser>();
        var sut        = new FileMetadataParser(loggerMock.Object, metadataProviderMock.Object, options);
        var criteria   = new MetadataCriteria
        {
            Content   = string.Empty,
            FileName  = Path.Combine("test", "file.html"),
            Permalink = Path.Combine("test", ":name:ext")
        };

        // Act
        var result = sut.Parse(criteria);

        // Assert
        result.Should().NotBeNull();
        result.Data.Should().NotBeNull();
        result.Data.Count.Should().Be(3, "Defaults = 1 + Applied Config = 2, Makes 3 values");
        result.Data.Uri.Should().NotBeNull();
        result.Data.Uri.Should().Be(Path.Combine("test", "file.html"));
        result.Data.Layout.Should().NotBeNull();
        result.Data.Layout.Should().Be("other.html");
        result.Data.Collection.Should().NotBeNull();
        result.Data.Collection.Should().Be("test");
    }
Exemplo n.º 4
0
    public void Test_FilemetadataParser_MultipleLayers()
    {
        // Arange
        var options = new MetadataParserOptions
        {
            Defaults = new DefaultMetadatas {
                new DefaultMetadata {
                    Path   = string.Empty,
                    Values = new FileMetaData {
                        Layout = "default.html"
                    }
                },
                new DefaultMetadata {
                    Path   = "test",
                    Values = new FileMetaData {
                        Collection = "test"
                    }
                }
            }
        };
        var metadataProviderMock = new Mock <IMetadataProvider>();

        metadataProviderMock
        .Setup(x => x.Retrieve <FileMetaData>(It.Is <string>(p => p.Equals(string.Empty))))
        .Returns(new Metadata <FileMetaData> {
        });

        var loggerMock = new Mock <ILogger <FileMetadataParser> >();
        var sut        = new FileMetadataParser(loggerMock.Object, metadataProviderMock.Object, options);
        var criteria   = new MetadataCriteria
        {
            Content   = string.Empty,
            FileName  = "posts/2021/file.html",
            Permalink = "posts/2021/:name:ext"
        };

        // Act
        var result = sut.Parse(criteria);

        // Assert
        result.Should().NotBeNull();
        result.Data.Should().NotBeNull();
        result.Data.Count.Should().Be(2, "Defaults = 1 + Applied Config = 1, Makes 2 values");
        result.Data.Uri.Should().NotBeNull();
        result.Data.Uri.Should().Be("posts/2021/file.html");
        result.Data.Layout.Should().NotBeNull();
        result.Data.Layout.Should().Be("default.html");
    }