-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
473 lines (385 loc) · 15.5 KB
/
Program.cs
File metadata and controls
473 lines (385 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using AngryMonkey.Objects;
using AngryMonkey.POCO;
using Markdig;
using Markdig.Extensions.AutoIdentifiers;
using Markdig.Extensions.MediaLinks;
using Newtonsoft.Json;
using Spectre.Console;
using System.Collections.Concurrent;
using System.Text;
using System.Text.RegularExpressions;
namespace AngryMonkey;
public static partial class Program
{
//public static string source = "X:\\Gaea2\\Docs\\Source\\Nodes";
//public static string destination = "X:\\Gaea2\\Docs\\staging";
public static string RootFolder;
public static string StagingFolder;
public static string Templates;
public static string HtmlTemplate;
public static string Html;
public static int folderCount;
public static MarkdownPipeline pipeline;
//private static Dictionary<string, string> TOC = [];
//private static Dictionary<string, string> Titles = [];
//private static Dictionary<string, string> mdTitles = [];
internal static List<Link> Links = [];
internal static List<Page> Pages = [];
internal static string[] DataFolders = [];
internal static Dictionary<string, Link> Slugs = [];
internal static ConcurrentBag<SearchObject> SearchObjects = [];
internal static ConcurrentBag<string> RogueAts = [];
internal static ConcurrentBag<string> RogueHeadings = [];
internal static ConcurrentBag<string> ThinPages = [];
internal static ConcurrentBag<string> Linktionary = [];
internal static Dictionary<string, Page> PageByDestMd = new(StringComparer.OrdinalIgnoreCase);
internal static StringBuilder LLMS = new();
internal static Dictionary<string, Flub[]> Flubs = [];
internal static Dictionary<string, NodeMetadata> Meta = [];
internal static bool Fast { get; set; } = false;
private static readonly Regex AtToken = new Regex(@"(?<!\w)@([A-Za-z][A-Za-z0-9_-]*)", RegexOptions.Compiled | RegexOptions.CultureInvariant);
public static void Main(string[] args)
{// Synchronous
SearchObjects = [];
string config = "";
if (args.Length <= 1)
{
if (File.Exists(Environment.CurrentDirectory + "\\hives.json"))
{
config = Environment.CurrentDirectory + "\\hives.json";
}
else
{
AnsiConsole.MarkupLine("[red]Configuration file not found![/] The Monkey is angry!");
return;
}
}
//if (args.Length > 0)
//{
// if (File.Exists(args[0]))
// {
// config = args[0];
// }
// else
// {
// if (Directory.Exists(args[0]))
// {
// if (File.Exists(args[0] + "\\hives.json"))
// {
// config = args[0] + "\\hives.json";
// }
// }
// }
//}
if (config == "")
{
AnsiConsole.MarkupLine("[red]Configuration file not found![/] The Monkey is angry!");
return;
}
AppConfig cfg = LoadConfiguration(config);
DataFolders = cfg.DataFolders;
Hive[] hives = Config.BuildHives(cfg);
//if (Directory.Exists(StagingFolder))
// Directory.Delete(StagingFolder, true);
//Directory.CreateDirectory(StagingFolder);
if (args.Any(a => string.Equals(a, "--fast", StringComparison.OrdinalIgnoreCase)))
{
Fast = true;
}
if (args.Any(a => string.Equals(a, "--assets", StringComparison.OrdinalIgnoreCase)))
{
AnsiConsole.WriteLine();
FileService.CopyDirectory($"{Templates}\\Assets", $"{StagingFolder}\\assets", "*.*");
AnsiConsole.MarkupLine("[Fuchsia][[shows teeth]][/] [white]The Monkey copied assets only[/]");
return;
}
Html = File.ReadAllText(HtmlTemplate);
Links.Clear();
Pages.Clear();
SearchObjects.Clear();
RogueAts.Clear();
Slugs.Clear();
pipeline = new MarkdownPipelineBuilder()
.UseCustomContainers()
.UseAlertBlocks()
.UseAutoIdentifiers(AutoIdentifierOptions.GitHub)
.UseYamlFrontMatter()
.UsePipeTables()
.UseDiagrams()
.UseAutoLinks()
.UseMediaLinks(new MediaOptions()
{
Class = "border rounded ratio ratio-16x9",
Width = "",
Height = ""
})
.UseFigures()
.UseEmojiAndSmiley()
.UseDefinitionLists()
.UseGenericAttributes()
.Build();
foreach (Hive hive in hives)
{
DeleteStagedHive(hive);
}
foreach (Hive hive in hives)
{
//GenerateMissingFolderIndexMarkdown(hive);
CollectPages(hive);
if (hive.Type == HiveType.Videos)
{
new YouTube().MakeYouTubePages(hive);
CollectPages(hive);
}
}
LoadFlubs();
LoadMetadata();
imgs.Clear();
BuildSlugIndex();
if (!Fast)
{
AnsiConsole.WriteLine("Copying Data Folders...");
foreach (string dataFolder in DataFolders)
{
FileService.CopyAll(dataFolder, dataFolder.Replace(cfg.SourceRoot, cfg.StagingRoot));
}
}
foreach (Hive hive in hives)
{
GenerateNavigation(hive);
if (hive.Type == HiveType.Changelogs)
{
CreateChangelog(hive);
}
if (!hive.IsHome)
FileService.CopyDirectory(hive.Source, hive.Destination);
ProcessMarkdown(hive);
CreateLLMS(hive);
}
FileService.CopyDirectory($@"{Templates}\Assets", $@"{StagingFolder}\assets", "*.*");
FileService.CopyDirectory($@"{RootFolder}\Hives", $@"{StagingFolder}\assets\js\", "*.*");
File.WriteAllText($@"{StagingFolder}\search.json", JsonConvert.SerializeObject(SearchObjects, new JsonSerializerSettings { Formatting = Formatting.None }));
File.WriteAllText($@"{StagingFolder}\llms-full.txt", LLMS.ToString());
File.WriteAllText($@"{RootFolder}\.vscode\atlinks.json", JsonConvert.SerializeObject(Links));
StringBuilder sb = new();
foreach (string s in Linktionary)
{
sb.AppendLine(s);
}
File.WriteAllText($@"{RootFolder}\linktionary.csv", sb.ToString());
AnsiConsole.MarkupLine($"[white][[{hives.Length}]][/] hives\n[white][[{Links.Count}]][/] pages\n[white][[{folderCount}]][/] sections");
if (!RogueAts.IsEmpty)
{
var distinct = RogueAts.Distinct().ToArray();
AnsiConsole.MarkupLine($"[DarkOrange][[{distinct.Length}]][/] rogue @s found! See RogueAts.txt");
File.WriteAllText($@"{RootFolder}\rogueAts.txt", string.Join(Environment.NewLine, distinct));
}
if (imgs.Count > 0)
{
var distinct = imgs.OrderBy(x => x).ToArray();
AnsiConsole.MarkupLine($"[DarkOrange][[{distinct.Length}]][/] rogue IMG found! See rogueImgs.txt");
File.WriteAllText($@"{RootFolder}\rogueImgs.txt", string.Join(Environment.NewLine, distinct));
}
if (RogueHeadings.Count > 0)
{
var distinct = RogueHeadings.OrderBy(x => x).ToArray();
AnsiConsole.MarkupLine($"[DarkOrange][[{distinct.Length}]][/] rogue HEADINGS found! See rogueHeadings.txt");
File.WriteAllText($@"{RootFolder}\rogueHeadings.txt", string.Join(Environment.NewLine, distinct));
}
if (ThinPages.Count > 0)
{
var distinct = ThinPages.OrderBy(x => x).ToArray();
AnsiConsole.MarkupLine($"[DarkOrange][[{distinct.Length}]][/] Thin Pages found! See roguePages.txt");
File.WriteAllText($@"{RootFolder}\roguePages.txt", string.Join(Environment.NewLine, distinct));
}
AnsiConsole.MarkupLine("[green][[Success - oo oo aa ahh ahh!]][/] [white]The Monkey is happy.[/]");
}
private static void CreateChangelog(Hive hive)
{
DateTime monthsAgo = DateTime.UtcNow.AddMonths(-12);
var changedPages = Pages.Where(x => x.Modified > monthsAgo).OrderByDescending(x => x.Modified).ThenBy(x => x.Hive.Name);
StringBuilder sb = new();
sb.AppendLine("---");
sb.AppendLine("title: Documentation Changelog");
sb.AppendLine("uid: changelog");
sb.AppendLine("icon: compass-drafting");
sb.AppendLine("---\n");
sb.AppendLine("# Changelog\n");
sb.AppendLine("\n" +
"The following tables list changes to individual pages. To see the exact change, go to any page and select the 'Updated on XXXXX' link in the right sidebar." +
"\n");
string last = "";
int i = 1;
foreach (Page changedPage in changedPages)
{
if (changedPage.UID == "changelog")
continue;
string mod = changedPage.Modified.ToString("Y");
if (mod != last)
{
sb.AppendLine($"## {mod}");
i = 1;
last = mod;
sb.AppendLine("| Id | Page | Section | Last Modified |");
sb.AppendLine("| ---- | ---- | ------- | ------------- |");
}
sb.AppendLine($"| {i:000} | [{changedPage.Title}]({changedPage.Link}) | {changedPage.Hive.Name} | {changedPage.Modified:yyyy-MM-dd} |");
i++;
}
File.WriteAllText($@"{hive.Source}\docs-changelog.md", sb.ToString());
}
private static void CreateLLMS(Hive hive)
{
if (hive.IsHome)
return;
string[] dirs = Pages.Where(x => x.Hive == hive).Select(x => x.Directory).Distinct().ToArray();
foreach (string dir in dirs)
{
Page[] pages = Pages.Where(x => x.Hive == hive && x.Directory.Contains(dir)).ToArray();
StringBuilder llms = new();
llms.AppendLine("## SOURCE: " + hive.Destination.Replace(RootFolder, "https://docs.gaea.app").Replace("\\", "/") + "/llms.txt\n\n");
foreach (Page page in pages)
{
llms.AppendLine($"// from {page.UID}/{Path.GetFileName(page.Filename)}");
llms.AppendLine(page.Contents);
}
File.WriteAllText(dir.Replace(hive.Source, hive.Destination) + "\\llms.txt", llms.ToString());
}
}
private static void LoadFlubs()
{
var files = Directory.GetFiles($@"{RootFolder}\source\.flubs", "*.json");
foreach (var file in files)
{
try
{
var flubArray = JsonConvert.DeserializeObject<Flub[]>(File.ReadAllText(file));
if (flubArray is { Length: > 0 })
{
var key = Path.GetFileNameWithoutExtension(file).ToLower();
Flubs[key] = flubArray;
}
}
catch (Exception ex)
{
Console.WriteLine(file);
}
}
}
private static void LoadMetadata()
{
var files = Directory.GetFiles($@"{RootFolder}\source\.meta", "*.json");
foreach (var file in files)
{
try
{
var flubArray = JsonConvert.DeserializeObject<NodeMetadata>(File.ReadAllText(file));
var key = Path.GetFileNameWithoutExtension(file).ToLower();
Meta[key] = flubArray;
}
catch (Exception ex)
{
Console.WriteLine(file);
}
}
}
private static AppConfig LoadConfiguration(string config)
{
var cfg = Config.LoadConfig(config);
RootFolder = cfg.RootFolder;
StagingFolder = cfg.StagingRoot;
Templates = cfg.TemplatesFolder;
HtmlTemplate = cfg.HtmlTemplate;
return cfg;
}
private static void BuildSlugIndex()
{
var duplicates = Links.GroupBy(l => l.Slug, StringComparer.OrdinalIgnoreCase)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
if (duplicates.Count > 0)
{
AnsiConsole.WriteLine("DUPLICATES:");
foreach (var s in duplicates) AnsiConsole.WriteLine(s);
throw new Exception("Duplicate slugs found");
}
Slugs = Links.ToDictionary(x => x.Slug, StringComparer.OrdinalIgnoreCase);
}
private static HashSet<string> existingPages = [];
private static void CollectPages(Hive hive)
{
string[] md = Directory.GetFiles(hive.Source, "*.md", hive.IsHome ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories);
foreach (var file in md)
{
try
{
if (existingPages.Contains(file))
continue;
var yaml = FrontMatter.GetFrontMatter(File.ReadAllLines(file));
string title = yaml["title"];
var page = new Page()
{
Filename = file,
Hive = hive,
UID = yaml["uid"],
Title = yaml["title"],
// PATHS AND LINKS
Directory = Path.GetDirectoryName(file),
Modified = new FileInfo(file).LastWriteTimeUtc,
Link = file.Replace(hive.Source, hive.URL).Replace("\\", "/").Replace(".md", ".html"),
// FRONT MATTER
Icon = yaml.ContainsKey("icon") ? yaml["icon"] : null,
Tag = yaml.ContainsKey("tag") ? yaml["tag"] : null,
Hidden = yaml.ContainsKey("hidden") && yaml["hidden"] == "true",
StartsSection = yaml.ContainsKey("section") && yaml["section"] == "true"
};
Pages.Add(page);
var destMd = file.Replace(hive.Source, hive.Destination);
PageByDestMd[destMd] = page;
var link = new Link(page.Link, title, page.UID, page.Icon, page.Hidden);
Links.Add(link);
existingPages.Add(file);
}
catch (Exception ex)
{
Console.WriteLine(file);
Console.WriteLine(ex.Message);
}
}
// find duplicates in LINKS by slug
List<string> duplicateSlugs = Links.GroupBy(l => l.Slug)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
if (duplicateSlugs.Any())
{
AnsiConsole.WriteLine("DUPLICATES:");
foreach (string duplicateSlug in duplicateSlugs)
{
AnsiConsole.WriteLine(" - " + duplicateSlug);
}
AnsiConsole.MarkupLine($"[red][[{duplicateSlugs.Count}]] duplicate slugs found![/] The Monkey is angry!");
Environment.Exit(50);
}
Slugs = Links.ToDictionary(x => x.Slug);
}
private static void GenerateNavigation(Hive hive)
{
if (hive.IsHome)
{
File.WriteAllText($@"{RootFolder}\Hives\TOC_{hive.ShortName}.js", "window.SITE_TOC = null;");
return;
}
TocGenerator.WriteHiveTocJs(hive.Source, hive.URL, $@"{RootFolder}\Hives\TOC_{hive.ShortName}.js");
}
public static void DeleteStagedHive(Hive hive)
{
if (hive.IsHome)
return;
if (Directory.Exists(hive.Destination))
{
Directory.Delete(hive.Destination, true);
}
}
}