using System.Globalization; using System.Net; using System.Text; using System.Text.RegularExpressions; using Flypaper.Core.Abstractions; namespace Flypaper.Core.Strategies; public static partial class MazeContent { public static readonly string[] Roots = ["wiki", "docs", "blog", "backup", "archive", "files", "kb", "guides", "releases", "news", "help", "support", "products"]; private const int MaxDepth = 6; public static bool IsDirectory(string? path) => path is { Length: > 1 } && path[^1] == '/'; public static IEnumerable Page(IRandomSource random, string? path, string? host, int spacing) { var site = new Site(path, host); var siteName = site.Host ?? "Portal"; var title = $"{LoremText.Capitalize(LoremText.Phrase(random, random.Next(2, 4)))} ยท {siteName}"; yield return $$""" {{title}}

{{title}}

"""; yield return Navigation(random, site); yield return "
\n
\n

Related pages

\n" + List(random, site, random.Next(7, 11)); yield return "

"; var minFiller = Math.Max(1, spacing / 2); var maxFiller = Math.Max(minFiller, spacing * 3 / 2); while (true) { var words = Math.Max(2, random.Next(minFiller, maxFiller + 1) / 7); yield return $"{LoremText.Capitalize(LoremText.Phrase(random, words))}. {Anchor(random, site)} "; switch (random.Next(0, 8)) { case 0: yield return $"

\n{List(random, site, random.Next(3, 6))}

"; break; case 1: yield return $"

\n

{LoremText.Capitalize(LoremText.Phrase(random, random.Next(2, 6)))}

\n

"; break; } } } public static IEnumerable Listing(IRandomSource random, string? path, string? host) { var site = new Site(path, host); var shown = WebUtility.HtmlEncode(site.DisplayPath); var wide = site.Section is "backup" or "files" or "archive"; yield return $$""" Index of {{shown}}

Index of {{shown}}

Name{{new string(' ', 46)}}Last modified      Size
            
Parent Directory - """; while (true) { yield return Entry(random, site, wide); } } private static string Entry(IRandomSource random, Site site, bool wide) { var slug = Slug(random, site); var directory = random.Next(0, 3) == 0; var name = directory ? slug + "/" : wide ? $"{slug}-{Date(random)[..10]}{Pick(random, [".tar.gz", ".zip", ".bak", ".gz"])}" : $"{slug}{Pick(random, [".html", ".html", ".htm", ".txt"])}"; var size = directory ? "-" : random.Next(0, 4) == 0 ? string.Create(CultureInfo.InvariantCulture, $"{random.Next(1, 900)}.{random.Next(0, 10)}M") : string.Create(CultureInfo.InvariantCulture, $"{random.Next(1, 900)}K"); var href = random.Next(0, 5) == 0 ? Absolute(random, site) : name; var padding = new string(' ', Math.Max(1, 50 - name.Length)); return $"{name}{padding}{Date(random)} {size}\n"; } private static string Date(IRandomSource random) => string.Create(CultureInfo.InvariantCulture, $"{random.Next(2022, 2027)}-{random.Next(1, 13):00}-{random.Next(1, 29):00} {random.Next(0, 24):00}:{random.Next(0, 60):00}"); private static string Navigation(IRandomSource random, Site site) { var builder = new StringBuilder("\n").ToString(); } private static string List(IRandomSource random, Site site, int items) { var builder = new StringBuilder("
    \n"); for (var i = 0; i < items; i++) { builder.Append($"
  • {Anchor(random, site)}
  • \n"); } return builder.Append("
\n").ToString(); } private static string Anchor(IRandomSource random, Site site) => $"{LoremText.Capitalize(LoremText.Phrase(random, random.Next(2, 5)))}"; private static string Href(IRandomSource random, Site site, bool rootOnly = false) { if (rootOnly) { return $"/{Pick(random, Roots)}/"; } var kind = random.Next(0, 10); if (site.Section is null) { kind = 5 + kind % 5; } else if (site.Depth >= MaxDepth && kind is 0 or 3) { kind = 4; } return kind switch { 0 => $"{Slug(random, site)}/", 1 or 2 => Slug(random, site), 3 => $"{LoremText.Word(random)}/{Slug(random, site)}", 4 => random.Next(0, 2) == 0 ? "../" : $"../{Slug(random, site)}", < 8 => Absolute(random, site), _ => site.Host is { } host ? $"http://{host}{Absolute(random, site)}" : Absolute(random, site) }; } private static string Absolute(IRandomSource random, Site site) { var root = site.Section is { } section && random.Next(0, 10) < 7 ? section : Pick(random, Roots); var slug = Slug(random, site); return root switch { "docs" => $"/docs/v{random.Next(1, 5)}/{slug}", "blog" => string.Create(CultureInfo.InvariantCulture, $"/blog/{random.Next(2022, 2027)}/{random.Next(1, 13):00}/{slug}"), "backup" => string.Create(CultureInfo.InvariantCulture, $"/backup/{random.Next(2022, 2027)}/{random.Next(1, 13):00}/"), _ => random.Next(0, 3) == 0 ? $"/{root}/{slug}/" : $"/{root}/{slug}" }; } private static string Slug(IRandomSource random, Site site) { var words = new List(); var count = random.Next(1, 4); for (var i = 0; i < count; i++) { words.Add(LoremText.Word(random)); } if (site.Topic is { } topic && random.Next(0, 4) == 0) { words[0] = topic; } return string.Join('-', words); } private static string Pick(IRandomSource random, string[] values) => values[random.Next(0, values.Length)]; [GeneratedRegex("^[A-Za-z0-9.-]{1,253}$")] private static partial Regex HostPattern(); [GeneratedRegex("^[a-z]{3,}$")] private static partial Regex TopicPattern(); private sealed class Site { public Site(string? path, string? host) { var normalized = (path ?? "/").ToLowerInvariant(); var segments = normalized.Split('/', StringSplitOptions.RemoveEmptyEntries); var endsWithSlash = normalized.EndsWith('/'); Section = segments.Length > 0 && Array.IndexOf(Roots, segments[0]) >= 0 ? segments[0] : null; Depth = endsWithSlash ? segments.Length : Math.Max(0, segments.Length - 1); Host = host is not null && HostPattern().IsMatch(host) ? host : null; DisplayPath = normalized.Length > 200 ? normalized[..200] : normalized; if (!endsWithSlash && segments.Length > 0) { var word = segments[^1].Split('-', '.', '_')[0]; Topic = TopicPattern().IsMatch(word) ? word : null; } } public string? Section { get; } public int Depth { get; } public string? Host { get; } public string? Topic { get; } public string DisplayPath { get; } } }