2021-09-07 05:15:45 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Gallium;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Nitride.Contents;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
using Zio;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
namespace MfGames.Nitride.Markdown;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
/// <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 IdentifyMarkdown : IOperation
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
private readonly IValidator<IdentifyMarkdown> validator;
|
|
|
|
|
|
|
|
public IdentifyMarkdown(IValidator<IdentifyMarkdown> validator)
|
|
|
|
{
|
|
|
|
this.validator = validator;
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:32:22 +00:00
|
|
|
public Func<Entity, UPath, bool> IsMarkdownTest { get; set; } = null!;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public IEnumerable<Entity> Run(IEnumerable<Entity> input)
|
|
|
|
{
|
|
|
|
this.validator.ValidateAndThrow(this);
|
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
return input.SelectEntity<UPath, ITextContent>(this.MarkTextEntities)
|
|
|
|
.SelectEntity<UPath, IBinaryContent>(this.MarkBinaryEntities);
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
private Entity MarkBinaryEntities(
|
|
|
|
Entity entity,
|
|
|
|
UPath path,
|
|
|
|
IBinaryContent binary)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
2022-06-07 13:55:13 +00:00
|
|
|
// If we aren't a Markdown file, then there is nothing we can do about that.
|
2022-06-05 18:44:51 +00:00
|
|
|
if (!this.IsMarkdownTest(entity, path))
|
|
|
|
{
|
|
|
|
return entity;
|
|
|
|
}
|
2021-09-07 05:15:45 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
// Convert the file as a binary.
|
|
|
|
if (binary is ITextContentConvertable textConvertable)
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-07-09 04:52:10 +00:00
|
|
|
entity = entity.SetTextContent(textConvertable.ToTextContent())
|
|
|
|
.Set(IsMarkdown.Instance);
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
2022-06-05 18:44:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException(
|
2022-06-06 14:32:22 +00:00
|
|
|
"Cannot convert a binary content to a text without ITextContentConvertable.");
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
2021-09-07 05:15:45 +00:00
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
private Entity MarkTextEntities(
|
|
|
|
Entity entity,
|
|
|
|
UPath path,
|
|
|
|
ITextContent _)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
|
|
|
// If we aren't a Markdown file, then there is nothing
|
|
|
|
// we can do about that.
|
|
|
|
if (!this.IsMarkdownTest(entity, path))
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
return entity;
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
// We are already text, so just mark it as Markdown.
|
|
|
|
entity = entity.Set(IsMarkdown.Instance);
|
|
|
|
|
|
|
|
return entity;
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
|
|
|
}
|