-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCulturesDocGenerator.cs
More file actions
125 lines (112 loc) · 4.82 KB
/
CulturesDocGenerator.cs
File metadata and controls
125 lines (112 loc) · 4.82 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
using commonItems;
using commonItems.Collections;
using commonItems.Colors;
using commonItems.Localization;
using commonItems.Mods;
using ImperatorToCK3.CK3.Cultures;
namespace DocsGenerator;
public static class CulturesDocGenerator {
private static IEnumerable<Culture> LoadCultures(string modPath, ColorFactory colorFactory) {
// Create ModFilesystem, but with just the mod we're analyzing.
// For that reason, treat the mod as base game.
var modFS = new ModFilesystem(modPath, Array.Empty<Mod>());
Logger.Info("Loading cultural pillars...");
var pillars = new PillarCollection();
pillars.LoadPillars(modFS);
Logger.Info("Loading cultures...");
var cultures = new CultureCollection(pillars);
cultures.LoadNameLists(modFS);
cultures.LoadCultures(modFS, colorFactory);
return cultures;
}
private static string GetLocForKey(LocDB locDB, string locKey) {
var locBlock = locDB.GetLocBlockForKey(locKey);
if (locBlock is null) {
Logger.Warn($"No localization found for \"{locKey}\"");
return locKey;
}
var englishLoc = locBlock["english"];
if (englishLoc is null) {
Logger.Warn($"No English localization found for \"{locKey}\"");
return locKey;
}
// Check for nested loc.
var dollarPos = englishLoc.IndexOf('$');
if (dollarPos != -1) {
var secondDollarPos = englishLoc.IndexOf('$', dollarPos + 1);
if (secondDollarPos != -1) {
var nesting = englishLoc.Substring(dollarPos, secondDollarPos - dollarPos + 1);
var nestedLocKey = nesting.Trim('$');
englishLoc = englishLoc.Replace(nesting, GetLocForKey(locDB, nestedLocKey));
}
}
return englishLoc;
}
private static string GetCultureColorForCell(Culture culture) {
return "#" + culture.Color.OutputHex()
.Replace("hex", string.Empty)
.Replace("{", string.Empty)
.Replace("}", string.Empty)
.Trim();
}
private static void OutputCulturesTable(IEnumerable<Culture> cultures, LocDB locDB, bool cultureColorUnderName) {
Logger.Info("Outputting cultures table...");
using var output = new StringWriter();
output.WriteLine("""
<style>
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
overflow:hidden;padding:10px 5px;word-break:normal;text-align:left;vertical-align:center;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;text-align:left;vertical-align:center;}
.color-cell {
min-width: 20px;
text-shadow: 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black, 0 0 1px black;
color: white;
font-weight: bold;
}
</style>
""");
output.WriteLine("<html>");
output.WriteLine("\t<body>");
output.WriteLine("\t\t<table class=\"tg\">");
output.WriteLine($"""
<thead>
<tr>
{(cultureColorUnderName ? "" : "<th></th>")}
<th>Culture</th>
<th>Heritage</th>
<th>Ethos</th>
<th>Traditions</th>
<th>Language</th>
<th>Martial custom</th>
</tr>
</thead>
""");
output.WriteLine("\t\t\t<tbody>");
foreach (var culture in cultures) {
output.WriteLine("\t\t\t\t<tr>");
if (cultureColorUnderName) {
output.WriteLine($"\t\t\t\t\t<td class=\"color-cell\" style=\"background-color: {GetCultureColorForCell(culture)}\">{GetLocForKey(locDB, culture.Id)}</td>");
} else {
output.WriteLine($"\t\t\t\t\t<td class=\"color-cell\" style=\"background-color: {GetCultureColorForCell(culture)}\"></td>");
output.WriteLine($"\t\t\t\t\t<td>{GetLocForKey(locDB, culture.Id)}</td>");
}
output.WriteLine($"\t\t\t\t\t<td>{GetLocForKey(locDB, $"{culture.Heritage.Id}_name")}</td>");
output.WriteLine($"\t\t\t\t\t<td>{GetLocForKey(locDB, $"{culture.EthosId}_name")}</td>");
output.WriteLine($"\t\t\t\t\t<td>{string.Join("<br>", culture.Traditions.Select(t=>GetLocForKey(locDB, $"{t}_name")))}</td>");
output.WriteLine($"\t\t\t\t\t<td>{GetLocForKey(locDB, $"{culture.LanguageId}_name")}</td>");
output.WriteLine($"\t\t\t\t\t<td>{GetLocForKey(locDB, $"{culture.MartialCustomId}_name")}</td>");
output.WriteLine("\t\t\t\t</tr>");
}
output.WriteLine("\t\t\t</tbody>");
output.WriteLine("\t\t</table>");
output.WriteLine("\t</body>");
output.WriteLine("</html>");
File.WriteAllText ("generated_docs/cultures_table.html", output.ToString());
}
public static void GenerateCulturesTable(string modPath, ColorFactory colorFactory, LocDB locDB, bool cultureColorUnderName) {
var cultures = LoadCultures(modPath, colorFactory);
OutputCulturesTable(cultures, locDB, cultureColorUnderName);
}
}