using System.Net;
using System.Text;
///
/// Generates HTML for BlogPckt content.
///
static class BlogPcktHtmlGenerator
{
public static string Generate(SiteStandard.Document document, BlogPckt.Content content)
{
var html = new StringBuilder();
html.AppendLine("");
html.AppendLine("");
html.AppendLine("
");
html.AppendLine("");
html.AppendLine("");
html.AppendLine($"{WebUtility.HtmlEncode(document.Title)}");
if (document.Description is not null)
{
html.AppendLine($"");
}
html.AppendLine("");
html.AppendLine("");
html.AppendLine("");
html.AppendLine("");
html.AppendLine($"
{WebUtility.HtmlEncode(document.Title)}
");
html.AppendLine($"");
if (document.Tags is { Count: > 0 })
{
html.Append("
");
foreach (var tag in document.Tags)
{
html.Append($"{WebUtility.HtmlEncode(tag)} ");
}
html.AppendLine("
");
}
html.AppendLine("");
if (content.Items is not null)
{
foreach (var item in content.Items)
{
RenderBlock(html, item);
}
}
html.AppendLine("");
html.AppendLine("");
html.AppendLine("");
return html.ToString();
}
static void RenderBlock(StringBuilder html, System.Text.Json.JsonElement element)
{
var type = element.TryGetProperty("$type", out var tp) ? tp.GetString() : null;
switch (type)
{
case BlogPckt.Block.Text.TypeId:
var text = BlogPckt.Block.Text.FromJson(element);
if (text is not null)
{
html.AppendLine($"
{RenderRichText(text.Plaintext, text.Facets)}
");
}
break;
case BlogPckt.Block.Heading.TypeId:
var heading = BlogPckt.Block.Heading.FromJson(element);
if (heading is not null)
{
var level = Math.Clamp(heading.Level ?? 1, 1, 6);
html.AppendLine($"{RenderRichText(heading.Plaintext, heading.Facets)}");
}
break;
case BlogPckt.Block.CodeBlock.TypeId:
var code = BlogPckt.Block.CodeBlock.FromJson(element);
if (code is not null)
{
var langAttr = !string.IsNullOrEmpty(code.Language)
? $" class=\"language-{WebUtility.HtmlEncode(code.Language)}\""
: string.Empty;
html.AppendLine($"
{WebUtility.HtmlEncode(code.Plaintext)}
");
}
break;
case BlogPckt.Block.Image.TypeId:
var image = BlogPckt.Block.Image.FromJson(element);
if (image?.Attrs is not null)
{
var attrs = image.Attrs;
var altAttr = attrs.Alt is not null
? $" alt=\"{WebUtility.HtmlEncode(attrs.Alt)}\""
: " alt=\"\"";
var titleAttr = attrs.Title is not null
? $" title=\"{WebUtility.HtmlEncode(attrs.Title)}\""
: string.Empty;
html.AppendLine($"");
html.AppendLine($"");
if (attrs.Title is not null)
{
html.AppendLine($"{WebUtility.HtmlEncode(attrs.Title)}");
}
html.AppendLine("");
}
break;
case BlogPckt.Block.BulletList.TypeId:
var bulletList = BlogPckt.Block.BulletList.FromJson(element);
if (bulletList is not null)
{
RenderBulletList(html, bulletList);
}
break;
case BlogPckt.Block.OrderedList.TypeId:
var orderedList = BlogPckt.Block.OrderedList.FromJson(element);
if (orderedList is not null)
{
RenderOrderedList(html, orderedList);
}
break;
case BlogPckt.Block.Blockquote.TypeId:
var blockquote = BlogPckt.Block.Blockquote.FromJson(element);
if (blockquote is not null)
{
html.AppendLine("
");
}
break;
case BlogPckt.Block.Table.TypeId:
var table = BlogPckt.Block.Table.FromJson(element);
if (table is not null)
{
RenderTable(html, table);
}
break;
case BlogPckt.Block.HorizontalRule.TypeId:
html.AppendLine("");
break;
case BlogPckt.Block.HardBreak.TypeId:
html.AppendLine(" ");
break;
case BlogPckt.Block.Website.TypeId:
var website = BlogPckt.Block.Website.FromJson(element);
if (website is not null)
{
html.AppendLine("
");
}
break;
case BlogPckt.Block.BlueskyEmbed.TypeId:
var bskyEmbed = BlogPckt.Block.BlueskyEmbed.FromJson(element);
if (bskyEmbed?.PostRef is not null)
{
html.AppendLine($"
");
}
break;
case BlogPckt.Block.Iframe.TypeId:
var iframe = BlogPckt.Block.Iframe.FromJson(element);
if (iframe is not null)
{
var heightAttr = iframe.Height.HasValue ? $" height=\"{iframe.Height.Value}\"" : string.Empty;
html.AppendLine($"");
}
break;
case BlogPckt.Block.Gallery.TypeId:
var gallery = BlogPckt.Block.Gallery.FromJson(element);
if (gallery is not null)
{
html.AppendLine($"");
}
break;
case BlogPckt.Block.Mention.TypeId:
var mention = BlogPckt.Block.Mention.FromJson(element);
if (mention is not null)
{
html.AppendLine($"@{WebUtility.HtmlEncode(mention.Handle.ToString())}");
}
break;
case BlogPckt.Block.TaskList.TypeId:
var taskList = BlogPckt.Block.TaskList.FromJson(element);
if (taskList is not null)
{
RenderTaskList(html, taskList);
}
break;
}
}
static void RenderTypedContent(StringBuilder html, object content, bool inline)
{
switch (content)
{
case BlogPckt.Block.Text text:
if (inline)
{
html.Append(RenderRichText(text.Plaintext, text.Facets));
}
else
{
html.AppendLine($"