Repository navigation
Expand file tree
/
Copy pathTocGenerator.cs
More file actions
423 lines (350 loc) · 15.2 KB
/
Copy pathTocGenerator.cs
File metadata and controls
423 lines (350 loc) · 15.2 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
using Humanizer;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using QuadSpinner.Adjunct;
namespace AngryMonkey
{
public static class TocGenerator
{
// -----------------------------
// Public API
// -----------------------------
/// <summary>
/// Builds a TOC for a "Hive" directory and returns JSON array (matching your sample shape).
/// Uses:
/// - folders.txt for ordering child folders + optional folder title override: folderName|Title
/// - getFrontMatter(lines) for page title/order from YAML front matter
/// URL rules:
/// - index.md => "/Base/rel/dir/"
/// - other .md => "/Base/rel/file.html"
/// </summary>
public static string GenerateHiveTocJson(
string hiveRootDir,
string baseUrl,
string manifestFileName = "folders.txt",
string indexFileName = "index.md",
bool indented = false)
{
var toc = GenerateHiveToc(hiveRootDir, baseUrl, manifestFileName, indexFileName);
var jsonOpts = new JsonSerializerOptions
{
WriteIndented = indented,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
return JsonSerializer.Serialize(toc, jsonOpts);
}
/// <summary>
/// Writes a JS file like: window.SITE_TOC = [...];
/// </summary>
public static void WriteHiveTocJs(
string hiveRootDir,
string baseUrl,
string outputJsPath,
string jsVarPath = "window.SITE_TOC",
string manifestFileName = "folders.txt",
string indexFileName = "index.md",
bool indented = false)
{
var json = GenerateHiveTocJson(hiveRootDir, baseUrl, manifestFileName, indexFileName, indented);
var sb = new StringBuilder();
sb.Append(jsVarPath).Append(" = ").Append(json).Append(';').AppendLine();
Directory.CreateDirectory(Path.GetDirectoryName(outputJsPath)!);
File.WriteAllText(outputJsPath, sb.ToString(), Utf8NoBom);
}
/// <summary>
/// Returns TOC object graph (List of TocItem).
/// Root dir is not wrapped in a group; it produces the top-level array.
/// </summary>
public static List<TocItem> GenerateHiveToc(
string hiveRootDir,
string baseUrl,
string manifestFileName = "folders.txt",
string indexFileName = "index.md")
{
if (string.IsNullOrWhiteSpace(hiveRootDir))
throw new ArgumentException("Hive root directory is required.", nameof(hiveRootDir));
var root = new DirectoryInfo(hiveRootDir);
if (!root.Exists)
throw new DirectoryNotFoundException(root.FullName);
baseUrl = NormalizeBaseUrl(baseUrl);
// Root folder becomes the TOC list.
return BuildFolderChildren(
hiveRoot: root,
currentDir: root,
baseUrl: baseUrl,
folderTitle: "",
manifestFileName: manifestFileName,
indexFileName: indexFileName,
includeIndexAsFirstChild: true);
}
// -----------------------------
// Output model (matches your JSON)
// -----------------------------
public sealed class TocItem
{
[JsonPropertyName("title")]
public string Title { get; set; } = "";
[JsonPropertyName("url")]
public string Url { get; set; }
[JsonPropertyName("icon")]
public string Icon { get; set; }
[JsonPropertyName("section")]
public bool? Section { get; set; }
[JsonPropertyName("hidden")]
public bool? Hidden { get; set; }
[JsonPropertyName("children")]
public List<TocItem> Children { get; set; }
}
// -----------------------------
// Build (folders as groups)
// -----------------------------
// 1) change signature to accept iconOverrideFromParent
private static TocItem BuildFolderGroup(
DirectoryInfo hiveRoot,
DirectoryInfo dir,
string baseUrl,
string manifestFileName,
string indexFileName,
string titleOverrideFromParent,
string iconOverrideFromParent)
{
var title = !string.IsNullOrWhiteSpace(titleOverrideFromParent)
? titleOverrideFromParent!
: ToTitle(dir.Name);
var children = BuildFolderChildren(
hiveRoot: hiveRoot,
currentDir: dir,
baseUrl: baseUrl,
folderTitle: title,
manifestFileName: manifestFileName,
indexFileName: indexFileName,
includeIndexAsFirstChild: true);
return new TocItem
{
Title = title.Strip("\""),
Children = children.Count > 0 ? children : null,
Icon = !string.IsNullOrWhiteSpace(iconOverrideFromParent) ? iconOverrideFromParent : null
};
}
private static List<TocItem> BuildFolderChildren(DirectoryInfo hiveRoot,
DirectoryInfo currentDir,
string baseUrl,
string folderTitle,
string manifestFileName,
string indexFileName,
bool includeIndexAsFirstChild)
{
var items = new List<TocItem>();
// ---- index.md => folder landing page entry (Overview / or title from front matter)
if (includeIndexAsFirstChild)
{
var indexPath = Path.Combine(currentDir.FullName, indexFileName);
if (File.Exists(indexPath))
{
var meta = ReadFrontMatter(indexPath);
// bool isAlpha = meta.Title.ToCharArray(0, 1)[0].IsAlphaUpper();
var title = !string.Equals(meta.Title, folderTitle, StringComparison.CurrentCultureIgnoreCase) ? meta.Title : "Overview";
var url = UrlForIndex(hiveRoot.FullName, indexPath, baseUrl);
items.Add(new TocItem { Title = title.Strip("\""), Url = url, Icon = meta.Icon, Hidden = meta.Hidden });
}
}
Program.folderCount++;
// ---- pages (.md excluding index.md)
var pages = currentDir.EnumerateFiles("*.md", SearchOption.TopDirectoryOnly)
.Where(f => !IsHiddenOrInternal(f.Name))
.Where(f => !StringEquals(f.Name, indexFileName))
.Select(f =>
{
var meta = ReadFrontMatter(f.FullName);
return new
{
File = f,
Title = meta.Title.Strip("\"") ?? ToTitle(Path.GetFileNameWithoutExtension(f.Name)),
meta.Order,
meta.Section,
meta.Icon,
meta.Hidden,
meta.Modified
};
})
.Where(x => x.Hidden is null or false)
.OrderByDescending(x => x.Modified)
.ThenBy(x => x.Order)
.ThenBy(x => x.Title, StringComparer.OrdinalIgnoreCase)
.ThenBy(x => x.File.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => new TocItem
{
Title = x.Title,
Url = UrlForPage(hiveRoot.FullName, x.File.FullName, baseUrl),
Section = x.Section,
Icon = x.Icon,
Hidden = x.Hidden
});
items.AddRange(pages);
// ---- subfolders (ordered by folders.txt then alpha)
var manifest = ReadFoldersManifest(currentDir.FullName, manifestFileName);
var subdirs = currentDir.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
.Where(d => !IsHiddenOrInternal(d.Name))
.ToArray();
var sortedSubdirs = SortSubdirs(subdirs, manifest.Order);
foreach (var d in sortedSubdirs)
{
manifest.Titles.TryGetValue(d.Name, out var overrideTitle);
manifest.Icons.TryGetValue(d.Name, out var overrideIcon);
items.Add(BuildFolderGroup(
hiveRoot,
d,
baseUrl,
manifestFileName,
indexFileName,
overrideTitle,
overrideIcon));
}
return items;
}
// -----------------------------
// folders.txt manifest
// -----------------------------
private sealed class FolderManifest
{
public List<string> Order { get; } = [];
public Dictionary<string, string> Titles { get; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, string> Icons { get; } = new(StringComparer.OrdinalIgnoreCase);
}
private static FolderManifest ReadFoldersManifest(string dirPath, string manifestFileName)
{
var m = new FolderManifest();
var path = Path.Combine(dirPath, manifestFileName);
if (!File.Exists(path))
return m;
string[] lines;
try { lines = File.ReadAllLines(path, Utf8NoBom); }
catch { return m; }
foreach (var raw in lines)
{
var line = raw.Trim();
if (line.Length == 0) continue;
if (line.StartsWith("#", StringComparison.Ordinal)) continue;
// name|title|icon (title, icon optional)
var parts = line.Split('|');
var name = parts.Length > 0 ? parts[0].Trim() : "";
var title = parts.Length > 1 ? parts[1].Trim() : "";
var icon = parts.Length > 2 ? parts[2].Trim() : "";
if (name.Length == 0) continue;
m.Order.Add(name);
if (title.Length > 0)
m.Titles[name] = title;
if (icon.Length > 0)
m.Icons[name] = icon;
}
// de-dupe (keep first)
if (m.Order.Count > 1)
{
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
m.Order.RemoveAll(x => !seen.Add(x));
}
return m;
}
private static IEnumerable<DirectoryInfo> SortSubdirs(DirectoryInfo[] subdirs, List<string> orderedNames)
{
if (orderedNames.Count == 0)
return subdirs.OrderBy(d => d.Name, StringComparer.OrdinalIgnoreCase);
var rank = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
for (int i = 0; i < orderedNames.Count; i++)
rank[orderedNames[i]] = i;
return subdirs
.OrderBy(d => rank.GetValueOrDefault(d.Name, int.MaxValue))
.ThenBy(d => d.Name, StringComparer.OrdinalIgnoreCase);
}
// -----------------------------
// Front matter (uses your parser)
// -----------------------------
private sealed class PageMeta
{
public string Title { get; init; }
public string Icon { get; init; }
public int Order { get; init; }
public bool? Section { get; init; }
public bool? Hidden { get; init; }
public DateTime? Modified { get; set; }
}
private static PageMeta ReadFrontMatter(string mdPath)
{
var fm = FrontMatter.GetFrontMatter(File.ReadAllLines(mdPath));
fm.TryGetValue("title", out var title);
fm.TryGetValue("order", out var orderStr);
fm.TryGetValue("icon", out var iconStr);
fm.TryGetValue("modified", out var modifiedStr);
bool? section = fm.ContainsKey("section") ? true : null;
bool? hidden = fm.ContainsKey("hidden") ? true : null;
DateTime? dt = null;
if (modifiedStr.NotEmpty())
{
dt = DateTime.ParseExact(
modifiedStr.Strip("\""),
[
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:ss.FFFFFFF'Z'",
"yyyy-MM-dd'T'HH:mm:ssK",
"yyyy-MM-dd'T'HH:mm:ss.FFFFFFFK"
], CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal
);
}
if (string.IsNullOrWhiteSpace(title))
title = null;
int order = 0;
if (!string.IsNullOrWhiteSpace(orderStr) &&
int.TryParse(orderStr.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var n))
order = n;
return new PageMeta
{
Title = title,
Order = order,
Section = section,
Icon = iconStr,
Hidden = hidden,
Modified = dt
};
}
// -----------------------------
// URL mapping
// -----------------------------
private static string NormalizeBaseUrl(string baseUrl)
{
if (string.IsNullOrWhiteSpace(baseUrl))
throw new ArgumentException("Base URL is required.", nameof(baseUrl));
if (!baseUrl.StartsWith("/", StringComparison.Ordinal))
baseUrl = "/" + baseUrl;
return baseUrl.TrimEnd('/');
}
private static string UrlForIndex(string hiveRootDir, string indexPath, string baseUrl)
{
// index.md -> "/Base/relDir/"
var dir = Path.GetDirectoryName(indexPath)!;
var relDir = Path.GetRelativePath(hiveRootDir, dir).Replace('\\', '/').Trim('/');
return relDir.Length == 0
? $"{baseUrl}/"
: $"{baseUrl}/{relDir}/";
}
private static string UrlForPage(string hiveRootDir, string mdPath, string baseUrl)
{
// page.md -> "/Base/rel/page.html"
var rel = Path.GetRelativePath(hiveRootDir, mdPath).Replace('\\', '/');
rel = rel[..^Path.GetExtension(rel).Length]; // remove ".md"
return $"{baseUrl}/{rel}.html";
}
// -----------------------------
// Helpers
// -----------------------------
private static readonly Encoding Utf8NoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
private static bool IsHiddenOrInternal(string name)
=> name.StartsWith(".", StringComparison.OrdinalIgnoreCase)
|| name.StartsWith("_", StringComparison.OrdinalIgnoreCase);
private static bool StringEquals(string a, string b)
=> string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
private static string ToTitle(string s) => s.Humanize(LetterCasing.Title);
}
}