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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,7 +9,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MfGames.Nitride\MfGames.Nitride.csproj"/>
|
<ProjectReference Include="..\MfGames.Nitride\MfGames.Nitride.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Include the source generator -->
|
<!-- Include the source generator -->
|
||||||
|
@ -25,7 +25,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MfGames.Gallium" Version="0.4.0"/>
|
<PackageReference Include="MfGames.Gallium" Version="0.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,17 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MfGames.Nitride.Gemtext\MfGames.Nitride.Gemtext.csproj"/>
|
<ProjectReference Include="..\MfGames.Nitride.Gemtext\MfGames.Nitride.Gemtext.csproj" />
|
||||||
<ProjectReference Include="..\MfGames.Nitride.Html\MfGames.Nitride.Html.csproj"/>
|
<ProjectReference Include="..\MfGames.Nitride.Html\MfGames.Nitride.Html.csproj" />
|
||||||
<ProjectReference Include="..\MfGames.Nitride.Slugs\MfGames.Nitride.Slugs.csproj"/>
|
<ProjectReference Include="..\MfGames.Nitride.Slugs\MfGames.Nitride.Slugs.csproj" />
|
||||||
<ProjectReference Include="..\MfGames.Nitride\MfGames.Nitride.csproj"/>
|
<ProjectReference Include="..\MfGames.Nitride\MfGames.Nitride.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MfGames.Gallium" Version="0.4.0"/>
|
<PackageReference Include="MfGames.Gallium" Version="0.4.0" />
|
||||||
<PackageReference Include="Markdig" Version="0.30.3"/>
|
<PackageReference Include="Markdig" Version="0.30.3" />
|
||||||
<PackageReference Include="MfGames.Markdown.Gemtext" Version="1.2.2"/>
|
<PackageReference Include="MfGames.Markdown.Gemtext" Version="1.2.2" />
|
||||||
<PackageReference Include="Zio" Version="0.15.0"/>
|
<PackageReference Include="Zio" Version="0.15.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Include the source generator -->
|
<!-- Include the source generator -->
|
||||||
|
|
|
@ -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