feat(html): added identify operations
This commit is contained in:
parent
22ddae11f8
commit
aac4b4373d
8 changed files with 134 additions and 23 deletions
78
src/MfGames.Nitride.Html/IdentifyHtml.cs
Normal file
78
src/MfGames.Nitride.Html/IdentifyHtml.cs
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
using FluentValidation;
|
||||||
|
|
||||||
|
using MfGames.Gallium;
|
||||||
|
using MfGames.Nitride.Contents;
|
||||||
|
using MfGames.Nitride.Generators;
|
||||||
|
|
||||||
|
using Zio;
|
||||||
|
|
||||||
|
namespace MfGames.Nitride.Html;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An operation that identifies Markdown files by their common extensions
|
||||||
|
/// and converts them to text input while also adding the IsMarkdown
|
||||||
|
/// component to identify them.
|
||||||
|
/// </summary>
|
||||||
|
[WithProperties]
|
||||||
|
public partial class IdentifyHtml : IOperation
|
||||||
|
{
|
||||||
|
private readonly IValidator<IdentifyHtml> validator;
|
||||||
|
|
||||||
|
public IdentifyHtml(IValidator<IdentifyHtml> validator)
|
||||||
|
{
|
||||||
|
this.validator = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Func<Entity, UPath, bool> IsHtmlTest { get; set; } = null!;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IEnumerable<Entity> Run(
|
||||||
|
IEnumerable<Entity> input,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
this.validator.ValidateAndThrow(this);
|
||||||
|
|
||||||
|
return input.SelectEntity<UPath, ITextContent>(this.MarkTextEntities)
|
||||||
|
.SelectEntity<UPath, IBinaryContent>(this.MarkBinaryEntities);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Entity MarkBinaryEntities(
|
||||||
|
Entity entity,
|
||||||
|
UPath path,
|
||||||
|
IBinaryContent binary)
|
||||||
|
{
|
||||||
|
// If we aren't a Markdown file, then there is nothing we can do about that.
|
||||||
|
if (!this.IsHtmlTest(entity, path))
|
||||||
|
{
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the file as a binary.
|
||||||
|
if (binary is ITextContentConvertable textConvertable)
|
||||||
|
{
|
||||||
|
entity = entity.SetTextContent(textConvertable.ToTextContent())
|
||||||
|
.SetIsHtml();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
"Cannot convert a binary content to a text without ITextContentConvertable.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Entity MarkTextEntities(
|
||||||
|
Entity entity,
|
||||||
|
UPath path,
|
||||||
|
ITextContent _)
|
||||||
|
{
|
||||||
|
return this.IsHtmlTest(entity, path)
|
||||||
|
? entity.SetIsHtml()
|
||||||
|
: entity;
|
||||||
|
}
|
||||||
|
}
|
29
src/MfGames.Nitride.Html/IdentifyHtmlFromPath.cs
Normal file
29
src/MfGames.Nitride.Html/IdentifyHtmlFromPath.cs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
using FluentValidation;
|
||||||
|
|
||||||
|
using MfGames.Gallium;
|
||||||
|
|
||||||
|
using Zio;
|
||||||
|
|
||||||
|
namespace MfGames.Nitride.Html;
|
||||||
|
|
||||||
|
public class IdentifyHtmlFromPath : IdentifyHtml
|
||||||
|
{
|
||||||
|
public IdentifyHtmlFromPath(IValidator<IdentifyHtml> validator)
|
||||||
|
: base(validator)
|
||||||
|
{
|
||||||
|
this.IsHtmlTest = DefaultIsHtml;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool DefaultIsHtml(
|
||||||
|
Entity entity,
|
||||||
|
UPath path)
|
||||||
|
{
|
||||||
|
return (path.GetExtensionWithDot() ?? string.Empty).ToLowerInvariant()
|
||||||
|
switch
|
||||||
|
{
|
||||||
|
".htm" => true,
|
||||||
|
".html" => true,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
12
src/MfGames.Nitride.Html/Validators/IdentifyHtmlValidator.cs
Normal file
12
src/MfGames.Nitride.Html/Validators/IdentifyHtmlValidator.cs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
using FluentValidation;
|
||||||
|
|
||||||
|
namespace MfGames.Nitride.Html.Validators;
|
||||||
|
|
||||||
|
public class IdentifyHtmlValidator : AbstractValidator<IdentifyHtml>
|
||||||
|
{
|
||||||
|
public IdentifyHtmlValidator()
|
||||||
|
{
|
||||||
|
this.RuleFor(x => x.IsHtmlTest)
|
||||||
|
.NotNull();
|
||||||
|
}
|
||||||
|
}
|
|
@ -71,16 +71,8 @@ public partial class IdentifyMarkdown : IOperation
|
||||||
UPath path,
|
UPath path,
|
||||||
ITextContent _)
|
ITextContent _)
|
||||||
{
|
{
|
||||||
// If we aren't a Markdown file, then there is nothing
|
return this.IsMarkdownTest(entity, path)
|
||||||
// we can do about that.
|
? entity.SetIsMarkdown()
|
||||||
if (!this.IsMarkdownTest(entity, path))
|
: entity;
|
||||||
{
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We are already text, so just mark it as Markdown.
|
|
||||||
entity = entity.Set(IsMarkdown.Instance);
|
|
||||||
|
|
||||||
return entity;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
|
|
||||||
namespace MfGames.Nitride.Markdown;
|
namespace MfGames.Nitride.Markdown.Validators;
|
||||||
|
|
||||||
public class ConvertMarkdownToBaseValidator
|
public class ConvertMarkdownToBaseValidator
|
||||||
: AbstractValidator<ConvertMarkdownToBase>
|
: AbstractValidator<ConvertMarkdownToBase>
|
|
@ -1,6 +1,6 @@
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
|
|
||||||
namespace MfGames.Nitride.Markdown;
|
namespace MfGames.Nitride.Markdown.Validators;
|
||||||
|
|
||||||
public class IdentifyMarkdownValidator : AbstractValidator<IdentifyMarkdown>
|
public class IdentifyMarkdownValidator : AbstractValidator<IdentifyMarkdown>
|
||||||
{
|
{
|
Reference in a new issue