[CHANGE] Reworked parsers/converters. Decipher operation from script do not work!

This commit is contained in:
max
2025-10-24 00:23:09 +02:00
parent de28591d24
commit a84195aefa
18 changed files with 285 additions and 77 deletions

View File

@@ -12,45 +12,58 @@ public static class JsonParser
.Select(image => new WebImage { Width = image.GetProperty("width").GetInt32(), Height = image.GetProperty("height").GetInt32(), Url = image.GetProperty("url").GetString() ?? "" })
.ToList();
public static string ExtractTextOrHtml(JsonElement element)
public static string ExtractTextOrHtml(JsonNode? node)
{
if (node is not JsonObject nodeObj)
{
return "";
}
// Case 1: Simple text (no formatting)
if (element.TryGetProperty("simpleText", out var simpleText))
return simpleText.GetString() ?? string.Empty;
if (nodeObj.TryGetPropertyValue("simpleText", out var simpleText))
return simpleText?.GetValue<string>() ?? string.Empty;
// Case 2: Runs (formatted text segments)
if (element.TryGetProperty("runs", out var runs) && runs.ValueKind == JsonValueKind.Array)
if (nodeObj.TryGetPropertyValue("runs", out var runs) && runs != null && runs.GetValueKind() == JsonValueKind.Array)
{
var sb = new StringBuilder();
foreach (var run in runs.EnumerateArray())
foreach (var runNode in runs.AsArray())
{
var text = run.GetProperty("text").GetString() ?? string.Empty;
if (runNode is not JsonObject run)
{
continue;
}
var text = runNode["text"]?.GetValue<string>() ?? string.Empty;
var formatted = System.Net.WebUtility.HtmlEncode(text);
var bold = run.TryGetProperty("bold", out var boldProp) && boldProp.GetBoolean();
var italic = run.TryGetProperty("italic", out var italicProp) && italicProp.GetBoolean();
var underline = run.TryGetProperty("underline", out var underlineProp) && underlineProp.GetBoolean();
var strikethrough = run.TryGetProperty("strikethrough", out var strikeProp) && strikeProp.GetBoolean();
var bold = run.TryGetPropertyValue("bold", out var boldNode) && boldNode is JsonValue bv && bv.GetValue<bool>();
var italic = run.TryGetPropertyValue("italic", out var italicNode) && italicNode is JsonValue iv && iv.GetValue<bool>();
var underline = run.TryGetPropertyValue("underline", out var underlineNode) && underlineNode is JsonValue uv && uv.GetValue<bool>();
var strikethrough = run.TryGetPropertyValue("strikethrough", out var strikeNode) && strikeNode is JsonValue sv && sv.GetValue<bool>();
if (bold) formatted = $"<b>{formatted}</b>";
if (italic) formatted = $"<i>{formatted}</i>";
if (underline) formatted = $"<u>{formatted}</u>";
if (strikethrough) formatted = $"<s>{formatted}</s>";
if (run.TryGetProperty("navigationEndpoint", out var nav) &&
nav.TryGetProperty("url", out var urlProp))
if (run.TryGetPropertyValue("navigationEndpoint", out var nav) && nav is JsonObject navObj &&
navObj.TryGetPropertyValue("url", out var urlProp))
{
var url = urlProp.GetString();
var url = urlProp?.GetValue<string>();
if (!string.IsNullOrEmpty(url))
formatted = $"<a href=\"{url}\">{formatted}</a>";
}
if (run.TryGetProperty("emoji", out var emoji) && emoji.ValueKind == JsonValueKind.Object)
if (run.TryGetPropertyValue("emoji", out var emoji) && emoji is JsonObject emojiObj)
{
if (emoji.TryGetProperty("url", out var emojiUrl))
if (emojiObj.TryGetPropertyValue("url", out var emojiUrl))
{
var src = emojiUrl.GetString();
var src = emojiUrl?.GetValue<string>();
if (!string.IsNullOrEmpty(src))
formatted = $"<img src=\"{src}\" alt=\"{text}\" class=\"emoji\" />";
}
@@ -64,10 +77,15 @@ public static class JsonParser
return string.Empty;
}
public static List<WebImage> ExtractWebImages(JsonElement element)
public static List<WebImage> ExtractWebImages(JsonNode? node)
{
var thumbnailsArray = element.GetProperty("thumbnail").GetProperty("thumbnails");
return thumbnailsArray.Deserialize<List<WebImage>>() ?? [];
if (node == null)
{
return [];
}
var thumbnailsArray = node["thumbnails"];
return thumbnailsArray?.Deserialize<List<WebImage>>() ?? [];
}
}