utils:wip: Add image type

This commit is contained in:
CakesTwix 2024-08-02 13:03:41 +03:00
parent a0979c8bef
commit 6c42b0e567
Signed by: CakesTwix
GPG key ID: 7B11051D5CE19825

View file

@ -1,3 +1,5 @@
using EpubSharp;
using EpubSharp.Format;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -9,7 +11,12 @@ public class Content
public List<Mark> marks { get; set; } public List<Mark> marks { get; set; }
public string text { get; set; } public string text { get; set; }
} }
public class Attrs
{
public string src { get; set; }
public string alt { get; set; }
public string title { get; set; }
}
public class Mark public class Mark
{ {
public string type { get; set; } public string type { get; set; }
@ -19,29 +26,36 @@ public class TextJson
{ {
public string type { get; set; } public string type { get; set; }
public List<Content?> content { get; set; } public List<Content?> content { get; set; }
public Attrs attrs { get; set; }
} }
public class HtmlConverter public class HtmlConverter
{ {
public static string ConvertJsonToHtml(string json) public static string ConvertJsonToHtml(string json, EpubWriter writer)
{ {
var client = new HttpClient();
var token = JToken.Parse(json); var token = JToken.Parse(json);
return ConvertTokenToHtml(token); return ConvertTokenToHtml(token, client, writer);
} }
private static string ConvertTokenToHtml(JToken token) private static string ConvertTokenToHtml(JToken token, HttpClient client, EpubWriter writer)
{ {
var html = ""; var html = "";
if (token is JArray) if (token is JArray)
{ {
foreach (var childToken in token.Children()) html += ConvertTokenToHtml(childToken); foreach (var childToken in token.Children()) html += ConvertTokenToHtml(childToken, client, writer);
} }
else if (token is JObject) else if (token is JObject)
{ {
var text = JsonConvert.DeserializeObject<TextJson>(token.ToString()); var text = JsonConvert.DeserializeObject<TextJson>(token.ToString());
for (int i = 0; i < text?.content?.Count; i++) var count = (text?.content?.Count != 0) ? text?.content?.Count : 1;
for (int i = 0; i < count; i++)
{ {
Content str = text.content[i]; Content str = text.content[i];
if (str != null) if (str != null)
{ {
@ -55,7 +69,19 @@ public class HtmlConverter
{ {
html += "<p>"; html += "<p>";
} }
} }
if (text.type == "image")
{
//var webRequest = new HttpRequestMessage(HttpMethod.Get, text.attrs.src);
//var response = client.Send(webRequest);
//var type = text.attrs.src.EndsWith(".jpg") ? EpubContentType.ImageJpeg :
//text.attrs.src.EndsWith(".png") ? EpubContentType.ImagePng : throw new Exception($"Unknown image ext {text.attrs.src}");
//using var reader = new StreamReader(response.Content.ReadAsStream());
//writer.AddFile(text.attrs.src, reader.ReadToEnd(), type);
//html += text.attrs.src;
}
html += "<br>"; html += "<br>";
} }
else else