Skip to content

Commit

Permalink
utils:wip: Add image type
Browse files Browse the repository at this point in the history
  • Loading branch information
CakesTwix committed Aug 2, 2024
1 parent a0979c8 commit 6c42b0e
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions Utils/Json2HTML.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using EpubSharp;
using EpubSharp.Format;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -9,7 +11,12 @@ public class Content
public List<Mark> marks { 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 string type { get; set; }
Expand All @@ -19,29 +26,36 @@ public class TextJson
{
public string type { get; set; }
public List<Content?> content { get; set; }
public Attrs attrs { get; set; }
}

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);
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 = "";

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)
{

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];
if (str != null)
{
Expand All @@ -55,7 +69,19 @@ private static string ConvertTokenToHtml(JToken token)
{
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>";
}
else
Expand Down

0 comments on commit 6c42b0e

Please sign in to comment.