shoko/Media/GeminiMediaHandler.cs

80 lines
2.5 KiB
C#
Raw Normal View History

2023-10-02 21:17:27 +00:00
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);
}
}
}