Files
Flypaper/src/Flypaper.Core/Strategies/HtmlContent.cs
T
2026-11-15 19:05:21 +01:00

149 lines
4.7 KiB
C#

using System.Text;
using Flypaper.Core.Abstractions;
namespace Flypaper.Core.Strategies;
public static class HtmlContent
{
private const string Css = """
body { margin: 0 auto; max-width: 52rem; padding: 1.5rem; font: 16px/1.6 system-ui, sans-serif; color: #222; }
header { border-bottom: 1px solid #ddd; margin-bottom: 1.5rem; }
nav a { margin-right: 1rem; color: #0b5cad; text-decoration: none; }
h1, h2, h3 { line-height: 1.25; }
article { margin-bottom: 2rem; }
a { color: #0b5cad; }
""";
private static readonly string[] Sections =
["docs", "blog", "wiki", "support", "products", "archive", "news", "help", "files", "guides", "kb", "releases"];
private static readonly string[] Titles =
["Welcome", "Documentation", "Knowledge Base", "Support Center", "Release Notes", "Resources", "Archive", "Developer Portal"];
public static IEnumerable<string> Generate(IRandomSource random)
{
var title = $"{Pick(random, Titles)} · {Title(random, 2, 3)}";
yield return $"""
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title}</title>
<style>
{Css}
</style>
</head>
<body>
<header>
<h1>{title}</h1>
""";
yield return Navigation(random);
yield return "</header>\n<main>\n";
while (true)
{
yield return random.Next(0, 10) switch
{
< 5 => Article(random),
< 7 => LinkList(random),
< 9 => Paragraph(random),
_ => $"<h3>{Title(random, 2, 6)}</h3>\n{Paragraph(random)}"
};
}
}
private static string Navigation(IRandomSource random)
{
var builder = new StringBuilder("<nav>\n");
var count = random.Next(3, 7);
for (var i = 0; i < count; i++)
{
builder.Append($"<a href=\"/{Pick(random, Sections)}/\">{LoremText.Capitalize(LoremText.Word(random))}</a>\n");
}
return builder.Append("</nav>\n").ToString();
}
private static string Article(IRandomSource random)
{
var builder = new StringBuilder($"<article>\n<h2>{Title(random, 3, 9)}</h2>\n");
var paragraphs = random.Next(1, 6);
for (var i = 0; i < paragraphs; i++)
{
builder.Append(Paragraph(random));
}
return builder.Append("</article>\n").ToString();
}
private static string LinkList(IRandomSource random)
{
var ordered = random.Next(0, 2) == 0;
var builder = new StringBuilder(ordered ? "<ol>\n" : "<ul>\n");
var items = random.Next(3, 10);
for (var i = 0; i < items; i++)
{
builder.Append($"<li>{Link(random, random.Next(2, 6))}</li>\n");
}
return builder.Append(ordered ? "</ol>\n" : "</ul>\n").ToString();
}
private static string Paragraph(IRandomSource random)
{
var builder = new StringBuilder("<p>");
var sentences = random.Next(2, 9);
for (var i = 0; i < sentences; i++)
{
if (i > 0)
{
builder.Append(' ');
}
builder.Append(Sentence(random));
}
return builder.Append("</p>\n").ToString();
}
private static string Sentence(IRandomSource random)
{
var builder = new StringBuilder();
var words = random.Next(4, 21);
var linkAt = random.Next(0, 8) == 0 ? random.Next(1, words) : -1;
for (var i = 0; i < words; i++)
{
if (i > 0)
{
builder.Append(i > 2 && random.Next(0, 9) == 0 ? ", " : " ");
}
if (i == linkAt)
{
builder.Append(Link(random, random.Next(1, 4)));
continue;
}
var word = LoremText.Word(random);
builder.Append(i == 0 ? LoremText.Capitalize(word) : word);
}
return builder.Append(random.Next(0, 10) == 0 ? '?' : '.').ToString();
}
private static string Link(IRandomSource random, int words)
{
var text = LoremText.Phrase(random, words);
var slug = text.Replace(' ', '-');
return $"<a href=\"/{Pick(random, Sections)}/{slug}\">{text}</a>";
}
private static string Title(IRandomSource random, int min, int max) => LoremText.Capitalize(LoremText.Phrase(random, random.Next(min, max + 1)));
private static string Pick(IRandomSource random, string[] values) => values[random.Next(0, values.Length)];
}