2022-02-19 16:42:47 +00:00
|
|
|
using System.Linq.Expressions;
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
namespace QuestReader.Models.ParsedContent;
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
[JsonConverter(typeof(ContentConverter))]
|
|
|
|
public abstract class ContentNode
|
|
|
|
{
|
|
|
|
public string Type { get => GetType().Name.Replace("Node", ""); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class ContainerNode : ContentNode
|
2022-02-19 16:42:47 +00:00
|
|
|
{
|
|
|
|
public IList<ContentNode> Nodes { get; set; }
|
2022-02-20 19:27:27 +00:00
|
|
|
|
|
|
|
public override string ToString() => $"{Type} [ {string.Join(",\n", Nodes)} ]";
|
|
|
|
|
|
|
|
public IEnumerable<int> GetReferences()
|
|
|
|
{
|
|
|
|
return Nodes.SelectMany(n =>
|
|
|
|
n is ContainerNode container
|
|
|
|
? container.GetReferences()
|
|
|
|
: (
|
|
|
|
n is ReferenceNode @ref ? new List<int> { @ref.PostId ?? @ref.ThreadId } : Array.Empty<int>()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-02-19 16:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ContentConverter : JsonConverter<ContentNode>
|
|
|
|
{
|
|
|
|
public override ContentNode Read(
|
|
|
|
ref Utf8JsonReader reader,
|
|
|
|
Type typeToConvert,
|
|
|
|
JsonSerializerOptions options
|
|
|
|
)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write(
|
|
|
|
Utf8JsonWriter writer,
|
|
|
|
ContentNode value,
|
|
|
|
JsonSerializerOptions options)
|
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
switch (value) {
|
2022-02-19 16:42:47 +00:00
|
|
|
case null:
|
|
|
|
JsonSerializer.Serialize(writer, null as ContentNode, options);
|
|
|
|
break;
|
2022-02-20 19:27:27 +00:00
|
|
|
case TextNode textNode:
|
|
|
|
JsonSerializer.Serialize(writer, textNode.Text, options);
|
|
|
|
break;
|
2022-02-19 16:42:47 +00:00
|
|
|
default:
|
2022-02-20 19:27:27 +00:00
|
|
|
var type = value.GetType();
|
|
|
|
JsonSerializer.Serialize(writer, value, type, options);
|
|
|
|
break;
|
|
|
|
};
|
2022-02-19 16:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
public class RootNode : ContainerNode
|
2022-02-19 16:42:47 +00:00
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
public Version Version { get; set; }
|
2022-02-19 16:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class TextNode : ContentNode
|
|
|
|
{
|
|
|
|
public string Text { get; set; }
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
public override string ToString() => $"{Text}";
|
2022-02-19 16:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class NewlineNode : ContentNode
|
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
public override string ToString() => $"\n";
|
2022-02-19 16:42:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
|
2022-02-19 16:42:47 +00:00
|
|
|
public class ReferenceNode : ContentNode
|
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
public int? PostId { get; set; }
|
2022-02-19 16:42:47 +00:00
|
|
|
public int ThreadId { get; set; }
|
|
|
|
public ReferenceType ReferenceType { get; set; }
|
|
|
|
public bool LongReference { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum ReferenceType
|
|
|
|
{
|
|
|
|
QuestActive,
|
|
|
|
QuestArchive,
|
|
|
|
QuestDiscussion
|
|
|
|
}
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
public class YoutubeEmbedNode : ContentNode
|
2022-02-19 16:42:47 +00:00
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
/// <remarks>Todo: Make this a URL</remarks>
|
|
|
|
public string VideoLink { get; set; }
|
|
|
|
};
|
2022-02-19 16:42:47 +00:00
|
|
|
|
|
|
|
public class QuoteNode : ContainerNode { };
|
|
|
|
|
|
|
|
public class BoldNode : ContainerNode { };
|
|
|
|
|
|
|
|
public class ItalicsNode : ContainerNode { };
|
|
|
|
|
|
|
|
public class StrikeoutNode : ContainerNode { };
|
|
|
|
|
|
|
|
public class SpoilerNode : ContainerNode { };
|
|
|
|
|
|
|
|
public class InlineCodeNode : ContainerNode { };
|
|
|
|
|
|
|
|
public class UnderlineNode : ContainerNode { };
|
|
|
|
|
2022-02-20 19:27:27 +00:00
|
|
|
public class SmallFontNode : ContainerNode { };
|
|
|
|
|
|
|
|
public class ColorNode : ContainerNode
|
|
|
|
{
|
|
|
|
public string Color { get; set; }
|
|
|
|
};
|
|
|
|
|
2022-02-19 16:42:47 +00:00
|
|
|
public class ExternalLinkNode : ContainerNode
|
|
|
|
{
|
2022-02-20 19:27:27 +00:00
|
|
|
/// <remarks>Todo: Make this a URL</remarks>
|
2022-02-19 16:42:47 +00:00
|
|
|
public string Destination { get; set; }
|
|
|
|
}
|