]> fortfriendship.online Git - gnargle.github.io.git/blob - SiteTools/ParticleGen/Program.cs
add playdate particle web generation
[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);
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);
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);
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);
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 var file = Path.Combine(Directory.GetCurrentDirectory(), $"../{fileName}");
111 if (!File.Exists(file))
112 {
113 file = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../../{fileName}");
114 }
115
116 return file;
117 }
118
119 string FindDirectory(string folderName)
120 {
121 var folder = Path.Combine(Directory.GetCurrentDirectory(), $"../{folderName}");
122 if (!Directory.Exists(folder))
123 {
124 folder = Path.Combine(Directory.GetCurrentDirectory(), $"../../../../../{folderName}");
125 }
126 return folder;
127 }