Add Gemini media handler

This commit is contained in:
Yuki 2023-10-02 17:17:27 -04:00
parent 54aeb257c9
commit ee82b1bef4
2 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,80 @@
using System.Numerics;
using ImGuiNET;
namespace Shoko;
[MediaType("text/gemini")]
class GeminiMediaHandler : MediaHandler
{
List<string> lines;
public GeminiMediaHandler(ProtoHandler content)
{
Content = content;
lines = new List<string>();
}
public override void Load()
{
Title = new UriBuilder(Content.URL).Path;
var reader = new StreamReader(Content.Content);
string line;
while((line = reader.ReadLine()) is not null)
{
lines.Add(line);
}
}
public override void Render()
{
var formatting = true;
foreach(var line in lines)
{
if(line.StartsWith("```"))
{
formatting = !formatting;
}
else if(formatting)
{
if(line.StartsWith("=>"))
{
var parts = line[2..].Trim().Split(new char[]{' ', '\t'}, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var title = parts[parts.Length > 1 ? 1 : 0];
var linkurl = new Uri(Content.URL, parts[0]).ToString();
ImGui.TextColored(new Vector4(0,0,255,255), title.Trim());
ImGui.SetItemTooltip(linkurl);
if(ImGui.IsItemClicked(ImGuiMouseButton.Left))
Content.CurrentTab.Load(linkurl);
}
else if(line.StartsWith("#"))
{
var scale = 2f;
var heading = 1;
if(line.StartsWith("##")) (scale, heading) = (1.5f, 2);
if(line.StartsWith("###")) (scale, heading) = (1.25f, 3);
var font = ImGui.GetFont();
float oldScale = font.Scale;
font.Scale *= scale;
ImGui.PushFont(font);
ImGui.TextUnformatted(line[heading..].Trim());
font.Scale = oldScale;
ImGui.PopFont();
}
else if(line.StartsWith("* "))
{
ImGui.BulletText(line[2..].Trim());
}
else if(line.StartsWith(">"))
{
ImGui.TextDisabled(" "+line.Trim());
}
else
ImGui.TextWrapped(line.Trim());
}
else
ImGui.TextUnformatted(line);
}
}
}

View File

@ -44,7 +44,14 @@ class GeminiProtoHandler : ProtoHandler
{
byte[] content = new byte[]{};
if(meta.Length > 1)
{
if(Status.StartsWith('3'))
{
meta[1] = "=> "+meta[1];
MediaType = "text/gemini";
}
content = Encoding.UTF8.GetBytes(meta[1]);
}
Content = new MemoryStream(content);
}