Skip to content

Commit

Permalink
Slightly better structured content support
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Oct 19, 2023
1 parent 6baebc8 commit edd174b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 31 deletions.
124 changes: 93 additions & 31 deletions JL.Core/Dicts/EPWING/EpwingYomichan/EpwingYomichanRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public EpwingYomichanRecord(List<JsonElement> jsonElement)
//jsonElement[4].TryGetInt32(out int score);
//Score = score;

List<string> definitionList = GetDefinitionsFromJsonArray(jsonElement[5]);
Definitions = definitionList.TrimStringListToStringArray() ?? Array.Empty<string>();
Definitions = GetDefinitions(jsonElement[5]) ?? Array.Empty<string>();
Definitions.DeduplicateStringsInArray();

//jsonElement[6].TryGetInt32(out int sequence);
Expand Down Expand Up @@ -142,50 +141,113 @@ public int GetFrequency(Freq freq)
return frequency;
}

private static List<string>? GetDefinitionsFromJsonElement(JsonElement jsonElement)
private static string[]? GetDefinitions(JsonElement jsonElement)
{
return jsonElement.ValueKind switch
List<string> definitions = new();
foreach (JsonElement definitionElement in jsonElement.EnumerateArray())
{
JsonValueKind.Array => GetDefinitionsFromJsonArray(jsonElement),
JsonValueKind.Object => GetDefinitionsFromJsonObject(jsonElement),
JsonValueKind.String => GetDefinitionsFromJsonString(jsonElement),
JsonValueKind.Number => null,
JsonValueKind.Undefined => null,
JsonValueKind.True => null,
JsonValueKind.False => null,
JsonValueKind.Null => null,
_ => null
};
string? definition = null;
if (definitionElement.ValueKind is JsonValueKind.String)
{
definition = definitionElement.GetString();
}

else if (definitionElement.ValueKind is JsonValueKind.Array)
{
definition = GetDefinitionsFromJsonArray(definitionElement);
}

else if (definitionElement.ValueKind is JsonValueKind.Object)
{
definition = GetDefinitionsFromJsonObject(definitionElement).Content;
}

if (definition is not null)
{
definitions.Add(definition.GetPooledString());
}
}

return definitions.TrimStringListToStringArray();
}

private static List<string> GetDefinitionsFromJsonArray(JsonElement jsonElement)
private static string? GetDefinitionsFromJsonArray(JsonElement jsonElement, string? parentTag = null)
{
List<string> definitions = new();
StringBuilder stringBuilder = new();

bool first = true;
foreach (JsonElement definitionElement in jsonElement.EnumerateArray())
{
List<string>? defs = GetDefinitionsFromJsonElement(definitionElement);
if (defs is not null)
if (definitionElement.ValueKind is JsonValueKind.String)
{
definitions.AddRange(defs);
_ = stringBuilder.Append(definitionElement.GetString());
}
}

return definitions;
}
else if (definitionElement.ValueKind is JsonValueKind.Array)
{
_ = stringBuilder.Append(GetDefinitionsFromJsonArray(definitionElement));
}

private static List<string>? GetDefinitionsFromJsonObject(JsonElement jsonElement)
{
return jsonElement.TryGetProperty("content", out JsonElement contentElement)
? GetDefinitionsFromJsonElement(contentElement)
else if (definitionElement.ValueKind is JsonValueKind.Object)
{
if (first)
{
first = false;
parentTag = null;
}

YomichanContent contentResult = GetDefinitionsFromJsonObject(definitionElement, parentTag);
if (contentResult.Content is not null)
{
if (contentResult.Tag is null or "span")
{
_ = stringBuilder.Append(contentResult.Content);
}

else if (contentResult.Tag is "ruby" or "rt")
{
_ = stringBuilder.Append(CultureInfo.InvariantCulture, $" ({contentResult.Content}) ");
}

else //if (contentResult.Tag is "div" or "a" or "li" or "ul" or "ol" or "p" or "h1" or "h2" or "h3" or "h4" or "h5" or "h6")
{
_ = stringBuilder.Append(CultureInfo.InvariantCulture, $"\n{contentResult.Content.TrimStart('\n')}");
}
}
}
}

return stringBuilder.Length > 0
? stringBuilder.ToString()
: null;
}

private static List<string> GetDefinitionsFromJsonString(JsonElement jsonElement)
private static YomichanContent GetDefinitionsFromJsonObject(JsonElement jsonElement, string? parentTag = null)
{
return jsonElement.ToString()
.Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(static s => s.Replace("\\\"", "\"", StringComparison.Ordinal))
.ToList();
if (jsonElement.TryGetProperty("content", out JsonElement contentElement))
{
string? tag = null;
if (jsonElement.TryGetProperty("tag", out JsonElement tagElement))
{
tag = tagElement.GetString();
}

if (contentElement.ValueKind is JsonValueKind.String)
{
return new YomichanContent(parentTag ?? tag, contentElement.GetString()?.Trim());
}

else if (contentElement.ValueKind is JsonValueKind.Array)
{
return new YomichanContent(parentTag ?? tag, GetDefinitionsFromJsonArray(contentElement, tag));
}

else if (contentElement.ValueKind is JsonValueKind.Object)
{
return GetDefinitionsFromJsonObject(contentElement, parentTag ?? tag);
}
}

return default;
}
}
12 changes: 12 additions & 0 deletions JL.Core/Dicts/EPWING/EpwingYomichan/YomichanContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace JL.Core.Dicts.EPWING.EpwingYomichan;
internal readonly struct YomichanContent
{
public string? Tag { get; }
public string? Content { get; }

public YomichanContent(string? tag, string? content)
{
Tag = tag;
Content = content;
}
}

0 comments on commit edd174b

Please sign in to comment.