]> fortfriendship.online Git - gnargle.github.io.git/blob - SiteTools/ParticleGen/Program.cs
new entry
[gnargle.github.io.git] / SiteTools / ParticleGen / Program.cs
1 // See https://aka.ms/new-console-template for more information
2
3 using HtmlAgilityPack;
4 using ParticleGen;
5 using System.Text.Json;
6
7 //we need to get every page then iterate through them to generate particle json pages for them.
8 //there's the index, then for folders theres entries, projects, diversions, diversions/hentaigames
9
10 var fileInfos = new List<Tuple<FileInfo, string>>();
11 var index = FindFile("index.html");
12 fileInfos.Add(new Tuple<FileInfo, string>(new FileInfo(index), "https://athene.gay"));
13
14 var folder = FindDirectory("entries");
15 var filePaths = Directory.EnumerateFiles(folder, "*.html");
16 if (filePaths.Any())
17 {
18 foreach (var path in filePaths)
19 {
20 if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase))
21 continue;
22 fileInfos.Add(new Tuple<FileInfo, string>(new FileInfo(path), "https://athene.gay/entries"));
23 }
24 }
25
26 folder = FindDirectory("projects");
27 filePaths = Directory.EnumerateFiles(folder, "*.html");
28 if (filePaths.Any())
29 {
30 foreach (var path in filePaths)
31 {
32 if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase))
33 continue;
34 fileInfos.Add(new Tuple<FileInfo, string>(new FileInfo(path), "https://athene.gay/projects"));
35 }
36 }
37
38 folder = FindDirectory("diversions");
39 filePaths = Directory.EnumerateFiles(folder, "*.html");
40 if (filePaths.Any())
41 {
42 foreach (var path in filePaths)
43 {
44 if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase))
45 continue;
46 fileInfos.Add(new Tuple<FileInfo, string>(new FileInfo(path), "https://athene.gay/diversions"));
47 }
48 }
49
50 folder = FindDirectory("diversions/hentaigames");
51 filePaths = Directory.EnumerateFiles(folder, "*.html");
52 if (filePaths.Any())
53 {
54 foreach (var path in filePaths)
55 {
56 if (Path.GetFileNameWithoutExtension(path).Equals("template", StringComparison.InvariantCultureIgnoreCase))
57 continue;
58 fileInfos.Add(new Tuple<FileInfo, string>(new FileInfo(path), "https://athene.gay/diversions/hentaigames"));
59 }
60 }
61
62 List<string> textTags = new List<string>() { "p", "h1", "h2", "h3", "h4" };
63
64 foreach (var file in fileInfos)
65 {
66 var doc = new HtmlDocument();
67 doc.Load(file.Item1.FullName);
68 var particleRoot = new ParticleRoot()
69 {
70 Format = "particle",
71 Title = doc.DocumentNode.SelectSingleNode("//title").InnerText,
72 Content = new List<Content>()
73 };
74
75 TraverseHTML(doc.DocumentNode.SelectSingleNode("//body"), particleRoot, file.Item2);
76 File.WriteAllText(file.Item1.FullName.Replace(".html", ".json"), JsonSerializer.Serialize(particleRoot));
77 }
78
79 void TraverseHTML(HtmlNode traversalNode, ParticleRoot particleRoot, string uriPath)
80 {
81 if (textTags.Contains(traversalNode.Name))
82 {
83 particleRoot.Content.Add(new Content()
84 {
85 Type = "paragraph",
86 Text = traversalNode.InnerText.Replace(" ", "").Replace("\n", " ").Replace("\r", " ").Trim(),
87 });
88 }
89
90 if (traversalNode.Name == "a" && traversalNode.GetAttributeValue("href", "").EndsWith("html"))
91 {
92 particleRoot.Content.Add(new Content()
93 {
94 Type = "button",
95 Label = traversalNode.InnerText.Replace(" ", "").Replace("\n", " ").Replace("\r", " ").Trim(),
96 Action = $"{uriPath}/{traversalNode.GetAttributeValue("href", "").Replace(".html", ".json")}"
97 });
98 }
99 if (traversalNode.HasChildNodes)
100 {
101 foreach (var node in traversalNode.ChildNodes)
102 {
103 TraverseHTML(node, particleRoot, uriPath);
104 }
105 }
106 }
107
108 string FindFile(string fileName)
109 {
110 //this is so stupid lmao
111 var file = Path.Combine(Directory.GetCurrentDirectory(), $"../{fileName}");
112 if (!File.Exists(file))
113 {
114 file = Path.Combine(Directory.GetCurrentDirectory(), $"../../{fileName}");
115 }
116 if (!File.Exists(file))
117 {
118 file = Path.Combine(Directory.GetCurrentDirectory(), $"../../../{fileName}");
119 }
120 if (!File.Exists(file))
121 {
122 file = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../{fileName}");
123 }
124 if (!File.Exists(file))
125 {
126 file = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../../{fileName}");
127 }
128
129 return file;
130 }
131
132 string FindDirectory(string folderName)
133 {
134 var folder = Path.Combine(Directory.GetCurrentDirectory(), $"../{folderName}");
135 if (!Directory.Exists(folder))
136 {
137 folder = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../../{folderName}");
138 }
139 if (!Directory.Exists(folder))
140 {
141 folder = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../{folderName}");
142 }
143 return folder;
144 }