-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig.wyam
289 lines (234 loc) · 9.53 KB
/
config.wyam
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
#recipe Docs
#n Wyam.Markdown
#n Markdig
//#a **/*
using System.IO;
using System.Text;
using Newtonsoft.Json;
// Customize your settings and add new ones here
Settings[Keys.Host] = "matterhackers.github.io";
Settings[DocsKeys.Title] = "MatterControl Help";
Settings[Keys.LinksUseHttps] = true;
Settings[Keys.LinkRoot] = "/MatterControl-Help";
// Ignore /input/settings in the standard Docs recipe processing
Settings[DocsKeys.IgnoreFolders] = "settings";
// Add any pipeline customizations here
Pipelines.Add("DeployMarkdown",
ReadFiles("docs/**/*.md"), // Read all markdown files
FrontMatter(Yaml()), // Load any frontmatter and parse it as YAML markup,
new TocModule(),
WriteFiles() // Write the post file
);
// Add custom pipeline for new slice/printer settings documents
Pipelines.Add("CompileSettings",
ReadFiles("settings/**/*.md"), // Read all markdown files
FrontMatter(Yaml()), // Load any frontmatter and parse it as YAML markup,
new SettingsModule(),
WriteFiles(".md")
);
public class HelpArticle
{
public string Name { get; set; }
public virtual string Path { get; set; }
public string ArticleKey { get; set; }
public List<HelpArticle> Children { get; set; } = new List<HelpArticle>();
}
public class SettingsArticle : HelpArticle
{
public string Content { get; set; }
public string CanonicalName { get; set; }
[JsonIgnore]
public override string Path { get; set; }
}
public class TocModule : IModule
{
static TocModule()
{
textInfo = new System.Globalization.CultureInfo("en-US", false).TextInfo;
}
public TocModule()
{
}
public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
{
var articles = new Dictionary<string, HelpArticle>();
foreach (var document in inputs)
{
if (document.Metadata.Get("RelativeFilePath") is FilePath filePath)
{
var path = new StringBuilder();
HelpArticle parentContext = null;
var segments = filePath.Directory.FullPath.Split('/');
foreach (var segment in segments)
{
path.AppendFormat("{0}/", segment);
string contextPath = path.ToString();
if (!articles.TryGetValue(contextPath, out HelpArticle helpArticle))
{
helpArticle = new HelpArticle()
{
Name = SanitizeName(segment),
};
if (parentContext != null)
{
parentContext.Children.Add(helpArticle);
}
articles[contextPath] = helpArticle;
parentContext = helpArticle;
}
else
{
parentContext = helpArticle;
}
}
if (filePath.FileName.FullPath == "index.md")
{
parentContext.Path = filePath.FullPath;
}
else
{
string title = document.Metadata.Get("title") as string;
parentContext.Children.Add(new HelpArticle()
{
Name = title ?? SanitizeName(filePath.FileNameWithoutExtension.FullPath),
Path = filePath.FullPath,
ArticleKey = document.Metadata.Get("articleKey") as string
});
}
}
}
if (articles.TryGetValue("docs/", out HelpArticle root))
{
System.IO.File.WriteAllText(
System.IO.Path.Combine(context.FileSystem.RootPath.FullPath, "output", "toc.json"),
JsonConvert.SerializeObject(
root,
Formatting.Indented,
new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
}));
}
return inputs;
}
private static System.Globalization.TextInfo textInfo;
public static string SanitizeName(string name)
{
return textInfo.ToTitleCase(name.Replace(".md", "").Replace("-", " "));
}
}
public class SettingsModule : IModule
{
public IEnumerable<IDocument> Execute(IReadOnlyList<IDocument> inputs, IExecutionContext context)
{
var articles = new Dictionary<string, SettingsArticle>();
foreach (var document in inputs)
{
if (document.Metadata.Get("RelativeFilePath") is FilePath filePath)
{
var path = new StringBuilder();
SettingsArticle parentContext = null;
var segments = filePath.Directory.FullPath.Split('/');
foreach (var segment in segments)
{
path.AppendFormat("{0}/", segment);
string contextPath = path.ToString();
if (!articles.TryGetValue(contextPath, out SettingsArticle helpArticle))
{
helpArticle = new SettingsArticle()
{
Name = segment,
};
if (parentContext != null)
{
parentContext.Children.Add(helpArticle);
}
articles[contextPath] = helpArticle;
parentContext = helpArticle;
}
else
{
parentContext = helpArticle;
}
}
if (filePath.FileName.FullPath != "index.md")
{
parentContext.Children.Add(new SettingsArticle()
{
Name = document.Metadata.Get("title") as string,
Path = filePath.FullPath,
Content = document.Content.Trim(),
CanonicalName = document.Metadata.Get("canonicalName") as string,
ArticleKey = document.Metadata.Get("articleKey") as string
});
}
}
}
var allResults = new List<IDocument>();
if (articles.TryGetValue("settings/", out SettingsArticle root))
{
string outputPath = Path.Combine(context.FileSystem.RootPath.FullPath, "output");
CreateRollupsForChildren(context, root, allResults, outputPath);
System.IO.File.WriteAllText(
System.IO.Path.Combine(outputPath, "all-settings.json"),
JsonConvert.SerializeObject(
root,
Formatting.Indented,
new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
}));
}
// Enable to troubleshoot complications in WriteFiles module (when running under Visual Studio)
// System.Diagnostics.Debugger.Break();
// Empty list unless populated in CreateRollupsForChildren()
return allResults;
}
// Create a document for each root level child, processing descendants into the document body
private void CreateRollupsForChildren(IExecutionContext context, SettingsArticle root, List<IDocument> allResults, string outputPath)
{
string docsPath = Path.Combine(outputPath, "docs");
string settingsPath = Path.Combine(docsPath, "settings");
foreach(var section in root.Children)
{
// Create a file for each child in the section
foreach(var child in section.Children)
{
var sb = new StringBuilder();
DumpChildren(child, sb, 1);
var relativePath = $"docs/settings/{section.Name}/{child.Name}.md";
var testDocument = context.GetDocument(
context.FileSystem.GetInputFile(new FilePath(relativePath)).Path,
new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())),
new MetadataItems { { Keys.RelativeFilePath, new FilePath(relativePath) }});
allResults.Add(testDocument);
}
}
}
// Recursively walk descendants dumping into StringBuilder and ultimately the document body
private void DumpChildren(HelpArticle node, StringBuilder sb, int level)
{
if (node.Children.Any())
{
// Add heading
sb.AppendFormat("{0} {1}\r\n", new string('#', level), node.Name);
// Write table definition if the first child is a leaf node
if (!node.Children.First().Children.Any())
{
sb.AppendFormat("Setting | Details\r\n--- | ---\r\n");
}
foreach(var child in node.Children)
{
DumpChildren(child, sb, level + 1);
}
}
else if (node is SettingsArticle document)
{
sb.AppendFormat("**{0}** | {1}\r\n", document.Name, document.Content);
}
}
}