using System.Net; using System.Text; /// /// Generates HTML for PubLeaflet content. /// static class PubLeafletHtmlGenerator { public static string Generate(SiteStandard.Document document, PubLeaflet.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("
"); foreach (var page in content.Pages) { switch (page) { case PubLeaflet.Pages.LinearDocument linearDoc: foreach (var block in linearDoc.Blocks) { RenderLeafletBlock(html, block.Block, block.Alignment); } break; case PubLeaflet.Pages.Canvas canvas: foreach (var block in canvas.Blocks) { RenderLeafletBlock(html, block.Block, alignment: null); } break; } } html.AppendLine("
"); html.AppendLine(""); html.AppendLine(""); return html.ToString(); } static void RenderLeafletBlock(StringBuilder html, object block, string? alignment) { var alignStyle = alignment switch { PubLeaflet.Pages.LinearDocumentTextAlignCenterToken.Value => " style=\"text-align:center\"", PubLeaflet.Pages.LinearDocumentTextAlignRightToken.Value => " style=\"text-align:right\"", PubLeaflet.Pages.LinearDocumentTextAlignJustifyToken.Value => " style=\"text-align:justify\"", _ => string.Empty, }; switch (block) { case PubLeaflet.Blocks.Text text: html.AppendLine($"{RenderLeafletRichText(text.Plaintext, text.Facets)}

"); break; case PubLeaflet.Blocks.Header header: var level = Math.Clamp(header.Level ?? 1, 1, 6); html.AppendLine($"{RenderLeafletRichText(header.Plaintext, header.Facets)}"); break; case PubLeaflet.Blocks.Code code: var langAttr = !string.IsNullOrEmpty(code.Language) ? $" class=\"language-{WebUtility.HtmlEncode(code.Language)}\"" : string.Empty; html.AppendLine($"
{WebUtility.HtmlEncode(code.Plaintext)}
"); break; case PubLeaflet.Blocks.Image image: var altAttr = image.Alt is not null ? $" alt=\"{WebUtility.HtmlEncode(image.Alt)}\"" : " alt=\"\""; html.AppendLine("
"); html.AppendLine($""); if (image.Alt is not null) { html.AppendLine($"
{WebUtility.HtmlEncode(image.Alt)}
"); } html.AppendLine("
"); break; case PubLeaflet.Blocks.UnorderedList unorderedList: RenderLeafletUnorderedList(html, unorderedList); break; case PubLeaflet.Blocks.Blockquote blockquote: html.AppendLine("
"); html.AppendLine($"

{RenderLeafletRichText(blockquote.Plaintext, blockquote.Facets)}

"); html.AppendLine("
"); break; case PubLeaflet.Blocks.Website website: html.AppendLine("
"); html.AppendLine($"{WebUtility.HtmlEncode(website.Title ?? website.Src)}"); if (website.Description is not null) { html.AppendLine($"

{WebUtility.HtmlEncode(website.Description)}

"); } html.AppendLine("
"); break; case PubLeaflet.Blocks.BskyPost bskyPost: html.AppendLine($"
"); html.AppendLine("

View on Bluesky

"); html.AppendLine("
"); break; case PubLeaflet.Blocks.Iframe iframe: var heightAttr = iframe.Height.HasValue ? $" height=\"{iframe.Height.Value}\"" : string.Empty; html.AppendLine($""); break; case PubLeaflet.Blocks.HorizontalRule: html.AppendLine("
"); break; case PubLeaflet.Blocks.Math math: html.AppendLine($"
{WebUtility.HtmlEncode(math.Tex)}
"); break; case PubLeaflet.Blocks.Button button: html.AppendLine($"{WebUtility.HtmlEncode(button.Text)}"); break; case PubLeaflet.Blocks.Page page: html.AppendLine($"{WebUtility.HtmlEncode(page.Id)}"); break; case PubLeaflet.Blocks.Poll poll: html.AppendLine($"
"); break; } } static void RenderLeafletUnorderedList(StringBuilder html, PubLeaflet.Blocks.UnorderedList list) { html.AppendLine(""); } static void RenderLeafletListItemRecursive(StringBuilder html, PubLeaflet.Blocks.UnorderedListListItem item) { html.Append("
  • "); RenderLeafletListItemContent(html, item.Content); if (item.Children is { Count: > 0 }) { html.AppendLine(); html.AppendLine("
      "); foreach (var child in item.Children) { RenderLeafletListItemRecursive(html, child); } html.AppendLine("
    "); } html.AppendLine("
  • "); } static void RenderLeafletListItemContent(StringBuilder html, PubLeaflet.Blocks.IUnorderedListListItemContent content) { switch (content) { case PubLeaflet.Blocks.Text text: html.Append(RenderLeafletRichText(text.Plaintext, text.Facets)); break; case PubLeaflet.Blocks.Header header: var level = Math.Clamp(header.Level ?? 1, 1, 6); html.Append($"{RenderLeafletRichText(header.Plaintext, header.Facets)}"); break; case PubLeaflet.Blocks.Image image: var altAttr = image.Alt is not null ? $" alt=\"{WebUtility.HtmlEncode(image.Alt)}\"" : " alt=\"\""; html.Append($""); break; } } static string RenderLeafletRichText(string plaintext, List? facets) { if (string.IsNullOrEmpty(plaintext)) { return string.Empty; } if (facets is null || facets.Count == 0) { return WebUtility.HtmlEncode(plaintext); } var utf8Bytes = Encoding.UTF8.GetBytes(plaintext); var sortedFacets = facets.OrderBy(f => f.Index.ByteStart).ToList(); var sb = new StringBuilder(); long currentByte = 0; foreach (var facet in sortedFacets) { var byteStart = Math.Min(facet.Index.ByteStart, utf8Bytes.Length); var byteEnd = Math.Min(facet.Index.ByteEnd, utf8Bytes.Length); if (byteStart < currentByte) { continue; } if (byteStart > currentByte) { var before = Encoding.UTF8.GetString(utf8Bytes, (int)currentByte, (int)(byteStart - currentByte)); sb.Append(WebUtility.HtmlEncode(before)); } if (byteEnd > byteStart) { var facetText = Encoding.UTF8.GetString(utf8Bytes, (int)byteStart, (int)(byteEnd - byteStart)); var encoded = WebUtility.HtmlEncode(facetText); foreach (var feature in facet.Features) { encoded = feature switch { PubLeaflet.Richtext.FacetBold => $"{encoded}", PubLeaflet.Richtext.FacetItalic => $"{encoded}", PubLeaflet.Richtext.FacetCode => $"{encoded}", PubLeaflet.Richtext.FacetStrikethrough => $"{encoded}", PubLeaflet.Richtext.FacetUnderline => $"{encoded}", PubLeaflet.Richtext.FacetHighlight => $"{encoded}", PubLeaflet.Richtext.FacetLink link => $"{encoded}", PubLeaflet.Richtext.FacetDidMention didMention => $"{encoded}", PubLeaflet.Richtext.FacetAtMention atMention => $"{encoded}", _ => encoded, }; } sb.Append(encoded); } currentByte = byteEnd; } if (currentByte < utf8Bytes.Length) { var remaining = Encoding.UTF8.GetString(utf8Bytes, (int)currentByte, utf8Bytes.Length - (int)currentByte); sb.Append(WebUtility.HtmlEncode(remaining)); } return sb.ToString(); } }