namespace AiAgent.Core.Helpers; using System.Text; using System.Text.Json.Serialization; using System.Text.RegularExpressions; // Models for 'Full Article.json' public class Article { [JsonPropertyName("title")] public string? Title { get; set; } [JsonPropertyName("Summary")] public string? Summary { get; set; } [JsonPropertyName("Sections")] public List
? Sections { get; set; } [JsonPropertyName("Conclusion")] public string? Conclusion { get; set; } [JsonPropertyName("References")] public List? References { get; set; } } public class Section { [JsonPropertyName("Heading")] public string? Heading { get; set; } [JsonPropertyName("Content")] public string? Content { get; set; } } public class Reference { [JsonPropertyName("Reference Number")] public int ReferenceNumber { get; set; } [JsonPropertyName("Reference Text")] public string? ReferenceText { get; set; } [JsonPropertyName("Reference URL")] public string? ReferenceUrl { get; set; } [JsonPropertyName("Reference Justification")] public string? ReferenceJustification { get; set; } } // Model for 'Article Fact List.json' public class FactList { [JsonPropertyName("Historical Context")] public List? HistoricalContext { get; set; } [JsonPropertyName("Recent Findings")] public List? RecentFindings { get; set; } } public static class HtmlBuilder { public static string BuildArticlePage(Article article, FactList factList, string imageLink) { var sb = new StringBuilder(); AppendHtmlHead(sb, article.Title ?? "Article"); AppendHeader(sb, article.Title ?? "Untitled Article", imageLink); AppendMainContent(sb, article, factList); AppendHtmlFooter(sb); return sb.ToString(); } private static string ProcessContentWithCitations(string? content) { if (string.IsNullOrEmpty(content)) { return string.Empty; } string pattern = @"\s?\[Ref:\s*([\d,\s]+)\]"; return Regex.Replace(content, pattern, match => { string numbersPart = match.Groups[1].Value; var numbers = numbersPart.Split(',') .Select(n => n.Trim()) .Where(n => !string.IsNullOrEmpty(n)); var links = numbers.Select(num => $"{num}"); return $"{string.Join(",", links)}"; }); } private static void AppendHtmlHead(StringBuilder sb, string title) { sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine($" {title}"); sb.AppendLine(" "); sb.AppendLine(""); sb.AppendLine(""); } private static void AppendHeader(StringBuilder sb, string title, string imageLink) { sb.AppendLine("
"); sb.AppendLine($" \"Article"); sb.AppendLine($"

{title.Split(":").First()}

"); //sb.AppendLine($"

{title.Split(":").Last()}

"); sb.AppendLine("
"); } private static void AppendMainContent(StringBuilder sb, Article article, FactList factList) { sb.AppendLine("
"); // ### MODIFICATION 2 ### // Removed "Article generated on: " text from this line. sb.AppendLine($"

{DateTime.Now:dddd, dd MMMM yyyy HH:mm}

"); if (!string.IsNullOrWhiteSpace(article.Summary)) { sb.AppendLine("
"); sb.AppendLine("

Summary

"); sb.AppendLine($"

{ProcessContentWithCitations(article.Summary)}

"); sb.AppendLine("
"); } AppendKeyFacts(sb, factList); if (article.Sections != null) { foreach (var section in article.Sections) { if (string.IsNullOrWhiteSpace(section.Heading) || string.IsNullOrWhiteSpace(section.Content)) continue; sb.AppendLine("
"); sb.AppendLine($"

{section.Heading}

"); string processedContent = ProcessContentWithCitations(section.Content); sb.AppendLine($"

{processedContent.Replace("\n", "
")}

"); sb.AppendLine("
"); } } if (!string.IsNullOrWhiteSpace(article.Conclusion)) { sb.AppendLine("
"); sb.AppendLine("

Conclusion

"); sb.AppendLine($"

{ProcessContentWithCitations(article.Conclusion)}

"); sb.AppendLine("
"); } AppendReferences(sb, article.References); sb.AppendLine("
"); } private static void AppendKeyFacts(StringBuilder sb, FactList factList) { bool hasHistorical = factList.HistoricalContext?.Any() ?? false; bool hasRecent = factList.RecentFindings?.Any() ?? false; if (!hasHistorical && !hasRecent) return; sb.AppendLine("
"); if (hasHistorical) { sb.AppendLine("

Historical Context

"); sb.AppendLine("
    "); foreach (var fact in factList.HistoricalContext!) { sb.AppendLine($"
  • {fact}
  • "); } sb.AppendLine("
"); } if (hasRecent) { sb.AppendLine("

Recent Findings

"); sb.AppendLine("
    "); foreach (var fact in factList.RecentFindings!) { sb.AppendLine($"
  • {fact}
  • "); } sb.AppendLine("
"); } sb.AppendLine("
"); } private static void AppendReferences(StringBuilder sb, List? references) { if (references == null || !references.Any()) return; sb.AppendLine("
"); sb.AppendLine("

References

"); sb.AppendLine("
    "); foreach (var reference in references.OrderBy(r => r.ReferenceNumber)) { sb.AppendLine($"
  1. "); sb.AppendLine($" {reference.ReferenceText}"); if (!string.IsNullOrWhiteSpace(reference.ReferenceJustification)) { sb.AppendLine($"

    {reference.ReferenceJustification}

    "); } sb.AppendLine("
  2. "); } sb.AppendLine("
"); sb.AppendLine("
"); } private static void AppendHtmlFooter(StringBuilder sb) { sb.AppendLine(""); sb.AppendLine(""); } }