Smakolykytl2Epub/Utils/Json2HTML.cs

68 lines
1.5 KiB
C#
Raw Normal View History

2023-08-13 06:59:16 -04:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Smakolykytl2Epub.Utils;
public class Content
{
public string type { get; set; }
public List<Mark> marks { get; set; }
public string text { get; set; }
}
public class Mark
{
public string type { get; set; }
}
public class TextJson
{
public string type { get; set; }
2024-08-01 17:27:02 -04:00
public List<Content?> content { get; set; }
2023-08-13 06:59:16 -04:00
}
public class HtmlConverter
{
public static string ConvertJsonToHtml(string json)
{
2023-08-13 07:44:06 -04:00
var token = JToken.Parse(json);
2023-08-13 06:59:16 -04:00
return ConvertTokenToHtml(token);
}
private static string ConvertTokenToHtml(JToken token)
{
2023-08-13 07:44:06 -04:00
var html = "";
2023-08-13 06:59:16 -04:00
if (token is JArray)
{
2023-08-13 07:44:06 -04:00
foreach (var childToken in token.Children()) html += ConvertTokenToHtml(childToken);
2023-08-13 06:59:16 -04:00
}
else if (token is JObject)
{
2023-08-13 07:44:06 -04:00
var text = JsonConvert.DeserializeObject<TextJson>(token.ToString());
2024-08-01 17:27:02 -04:00
for (int i = 0; i < text?.content?.Count; i++)
2023-08-13 06:59:16 -04:00
{
2024-08-01 17:27:02 -04:00
Content str = text.content[i];
if (str != null)
{
html += str.text;
}
if (str.type == "hardBreak")
{
html += "<br>";
}
if (str.type == "paragraph")
{
html += "<p>";
}
2023-08-13 06:59:16 -04:00
}
html += "<br>";
}
else
{
html += token.ToString();
}
return html;
}
}