X-Git-Url: https://fortfriendship.online/gitweb/gnargle.github.io.git/blobdiff_plain/30585874b9f9af32cd96d449d4a21302542b4de1..e2bfabb05463c263f0f903edbab66afe2bb84f7f:/SiteTools/ParticleGen/Program.cs?ds=sidebyside diff --git a/SiteTools/ParticleGen/Program.cs b/SiteTools/ParticleGen/Program.cs new file mode 100644 index 0000000..fb38b87 --- /dev/null +++ b/SiteTools/ParticleGen/Program.cs @@ -0,0 +1,127 @@ +// See https://aka.ms/new-console-template for more information + +using HtmlAgilityPack; +using ParticleGen; +using System.Text.Json; + +//we need to get every page then iterate through them to generate particle json pages for them. +//there's the index, then for folders theres entries, projects, diversions, diversions/hentaigames + +var fileInfos = new List>(); +var index = FindFile("index.html"); +fileInfos.Add(new Tuple(new FileInfo(index), "https://athene.gay")); + +var folder = FindDirectory("entries"); +var filePaths = Directory.EnumerateFiles(folder); +if (filePaths.Any()) +{ + foreach (var path in filePaths) + { + if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase)) + continue; + fileInfos.Add(new Tuple(new FileInfo(path), "https://athene.gay/entries")); + } +} + +folder = FindDirectory("projects"); +filePaths = Directory.EnumerateFiles(folder); +if (filePaths.Any()) +{ + foreach (var path in filePaths) + { + if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase)) + continue; + fileInfos.Add(new Tuple(new FileInfo(path), "https://athene.gay/projects")); + } +} + +folder = FindDirectory("diversions"); +filePaths = Directory.EnumerateFiles(folder); +if (filePaths.Any()) +{ + foreach (var path in filePaths) + { + if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase)) + continue; + fileInfos.Add(new Tuple(new FileInfo(path), "https://athene.gay/diversions")); + } +} + +folder = FindDirectory("diversions/hentaigames"); +filePaths = Directory.EnumerateFiles(folder); +if (filePaths.Any()) +{ + foreach (var path in filePaths) + { + if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase)) + continue; + fileInfos.Add(new Tuple(new FileInfo(path), "https://athene.gay/diversions/hentaigames")); + } +} + +List textTags = new List() {"p", "h1", "h2", "h3", "h4"}; + +foreach (var file in fileInfos) +{ + var doc = new HtmlDocument(); + doc.Load(file.Item1.FullName); + var particleRoot = new ParticleRoot() + { + Format = "particle", + Title = doc.DocumentNode.SelectSingleNode("//title").InnerText, + Content = new List() + }; + + TraverseHTML(doc.DocumentNode.SelectSingleNode("//body"), particleRoot, file.Item2); + File.WriteAllText(file.Item1.FullName.Replace(".html", ".json"), JsonSerializer.Serialize(particleRoot)); +} + +void TraverseHTML(HtmlNode traversalNode, ParticleRoot particleRoot, string uriPath) +{ + if (textTags.Contains(traversalNode.Name)) + { + particleRoot.Content.Add(new Content() + { + Type = "paragraph", + Text = traversalNode.InnerText.Replace(" ", "").Replace("\n", " ").Replace("\r", " ").Trim(), + }); + } + + if (traversalNode.Name == "a" && traversalNode.GetAttributeValue("href", "").EndsWith("html")) + { + particleRoot.Content.Add(new Content() + { + Type = "button", + Label = traversalNode.InnerText.Replace(" ", "").Replace("\n", " ").Replace("\r", " ").Trim(), + Action = $"{uriPath}/{traversalNode.GetAttributeValue("href", "").Replace(".html", ".json")}" + }); + } + if (traversalNode.HasChildNodes) + { + foreach (var node in traversalNode.ChildNodes) + { + TraverseHTML(node, particleRoot, uriPath); + } + } +} + +string FindFile(string fileName) +{ + var file = Path.Combine(Directory.GetCurrentDirectory(), $"../{fileName}"); + if (!File.Exists(file)) + { + file = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../../{fileName}"); + } + + return file; +} + +string FindDirectory(string folderName) +{ + var folder = Path.Combine(Directory.GetCurrentDirectory(), $"../{folderName}"); + if (!Directory.Exists(folder)) + { + folder = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../../{folderName}"); + } + return folder; +} \ No newline at end of file