-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.wyam
349 lines (290 loc) · 10.9 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
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
#n Wyam.Markdown
#n Wyam.Razor
#n Wyam.Yaml
#n Wyam.Html
#n Wyam.Feeds
using System.IO;
using System.Xml;
using System.Threading.Tasks;
// Normalize the culture for the examples so they produce the same output regardless of system culture
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("ru-RU");
//System.Text.Encodings.Web.HtmlEncoder.Default = new System.Text.Encodings.Web.HtmlEncoder(UnicodeRanges.All);
Settings[Keys.Host] = "DotNet.Ru";
Settings[Keys.LinksUseHttps] = true;
//////////////////////////////////////////////////////////////////////
// Pipelines
//////////////////////////////////////////////////////////////////////
Pipelines.Add("News",
ReadFiles("News/*.md"),
FrontMatter(Yaml()),
Markdown(),
Execute((inputs, ctx, _) => EnrichByPersonInfo(inputs, ctx))
);
Pipelines.Add("Pages",
ReadFiles("{!_,}*.cshtml"),
FrontMatter(Yaml()),
Meta("Id", (doc, ctx) => doc.Source.FileNameWithoutExtension.FullPath.ToLower()),
Meta(Keys.RelativeFilePath, (doc, ctx) => doc.String("Id") + ".html"),
Razor()
.WithLayout("/_Layout.cshtml"),
WriteFiles((doc, ctx) => "Www/" + doc.String(Keys.RelativeFilePath))
);
Pipelines.Add("Resources",
CopyFiles("Resources/**")
.To((source, dest) => dest.Path.FullPath.Replace("/Resources/", "/Www/"))
);
//////////////////////////////////////////////////////////////////////
// Seeds
//////////////////////////////////////////////////////////////////////
Pipelines.Add("Communities",
ReadFiles(ctx => ResolveAuditPath(ctx) + "/db/communities/*.xml"),
Xml(),
Execute((inputs, ctx, _) => EnrichCommunity(inputs, ctx)),
Meta("RedirectUrl", (doc, ctx) => doc.String("VkUrl"))
);
Pipelines.Add("CustomRedirects",
ReadFiles("Redirects/*.yml"),
Yaml(),
Meta("SubDomain", (doc, ctx) => doc.Source.FileNameWithoutExtension.FullPath)
);
// Pipelines.Add("Dump",
// ReadFiles("Diagnostics/Dump.cshtml"),
// Meta(Keys.RelativeFilePath, (doc, ctx) => "dump.html"),
// Razor(),
// WriteFiles()
// );
Pipelines.Add("SubDomains",
Documents("Communities"),
Concat(
Documents("CustomRedirects")
),
Meta("Url", (doc, ctx) => BuildSubHostUrl(doc.String("SubDomain"))),
Meta("SiteImage", (doc, ctx) => TryFindSiteImage(doc))
);
Pipelines.Add("SubDomainName",
Documents("SubDomains"),
Content((doc, ctx) => new Uri(doc.String("Url")).Host.ToLower()),
WriteFiles((doc, ctx) => doc.String("SubDomain") + "/CNAME")
);
Pipelines.Add("SubDomainRedirect",
Documents("SubDomains"),
Razor()
.WithLayout("/_Redirect.cshtml"),
WriteFiles((doc, ctx) => doc.String("SubDomain") + "/index.html")
);
Pipelines.Add("SubDomainImage",
Documents("SubDomains"),
Where((doc, ctx) => doc.String("SiteImage") != null),
CopyFiles((doc, ctx) => doc.String("SiteImage"))
.To((source, dest) => ResolveSiteImageDestinationPathBecauseWyamSucks(source, dest))
);
/*
Pipelines.Add("News",
ReadFiles("News/*.cshtml"),
FrontMatter(Yaml()),
//Content(String.Empty),
Meta("Id", (doc, ctx) => doc.Source.Directory.Name.ToLower()),
Meta(Keys.RelativeFilePath, (doc, ctx) => doc.String("id") + ".html"),
Razor()
.WithLayout("/_Layout.cshtml"),
WriteFiles(),
Branch(
// Put the GenerateFeeds module inside a Branch module so that the feed
// documents don't get added to the set of output documents for this pipeline
GenerateFeeds(),
WriteFiles()
)
);
Pipelines.Add("BuildSitemap",
Documents(@ctx.Documents),
Sitemap(),
WriteFiles((d, c) => "sitemap.xml")
);
*/
//////////////////////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////////////////////
public string ResolveAuditPath(IExecutionContext context)
{
var auditDirPath = context.FileSystem.RootPath.Parent.Combine("Audit").FullPath;
if (!Directory.Exists(auditDirPath))
{
throw new ArgumentException("Can't find Audit at " + auditDirPath);
}
Info($"Audit directory not found at {auditDirPath}");
return auditDirPath;
}
public string TryFindSiteImage(IDocument input)
{
var imageDir = input.Source?.Directory?.FullPath;
if (imageDir == null)
{
return null;
}
var id = input.String("Id");
var imageName = String.Format("{0}-1280x640.png", id);
var imageFullName = Path.Combine(imageDir, imageName);
return File.Exists(imageFullName) ? imageFullName : null;
}
public FilePath ResolveSiteImageDestinationPathBecauseWyamSucks(IFile source, IFile destination)
{
// Dirty hacks, tested only for redirect images
var imageName = source.Path.FileName.FullPath;
var idParts = imageName.Split('-');
if (idParts.Length == 0 || String.IsNullOrWhiteSpace(idParts[0]))
{
throw new ArgumentException("Can't extract ID for " + imageName);
}
var id = idParts[0];
var searchPattern = "Id: " + id;
var yamlFile = source
.Directory
.GetFiles()
.Where(file => file.Path.Extension == ".yml")
.Where(file => file.ReadAllText().Contains(searchPattern))
.Single();
var shortName = yamlFile.Path.FileNameWithoutExtension.FullPath;
var destinationDirectory = destination.Directory.Parent.GetDirectory(shortName);
var destinationFile = destinationDirectory.GetFile(destination.Path.FileName.FullPath.ToLowerInvariant());
return destinationFile.Path.FullPath;
}
public IReadOnlyList<IDocument> EnrichCommunity(IReadOnlyList<IDocument> inputs, IExecutionContext context)
{
var outputs = new List<IDocument>(inputs.Count);
var shortNameParser = new System.Text.RegularExpressions.Regex(@"(?<Prefix>\w{3})DotNet");
foreach(var input in inputs)
{
var output = input;
var name = input.String("Name");
string subDomain = null;
var match = shortNameParser.Match(name);
if (match.Success)
{
subDomain = match.Groups["Prefix"].Value;
}
else
{
throw new ArgumentException("Can't resolve short name for " + name);
}
var metadata = new MetadataItems
{
{ "SubDomain", subDomain },
{ "Title", "Сообщество " + name },
{ "Description", input.String("City") }
};
output = context.GetDocument(input, metadata);
outputs.Add(output);
}
return outputs;
}
public string BuildSubHostUrl(string subDomain)
{
var host = subDomain + "." + Settings[Keys.Host];
var proto = (bool)Settings[Keys.LinksUseHttps] ? "https" : "http";
return $"{proto}://{host}/";
}
public IReadOnlyList<IDocument> EnrichByPersonInfo(IReadOnlyList<IDocument> inputs, IExecutionContext context)
{
var aliases = ResolvePersonAliases(context.FileSystem);
var outputs = new List<IDocument>(inputs.Count);
foreach(var input in inputs)
{
var output = input;
var authorName = input.String("Author");
if (!String.IsNullOrWhiteSpace(authorName) && aliases.TryGetValue(authorName, out var author))
{
var metadata = new MetadataItems
{
{ "Author", author.Name },
{ "AuthorImage", author.Image },
// TODO: MarkDown
{ "AuthorDescription", author.Description },
};
// TODO: Resolve Publisher name from Aliases map
output = context.GetDocument(input, metadata);
}
outputs.Add(output);
}
return outputs;
}
public IReadOnlyDictionary<string, Person> ResolvePersonAliases(IReadOnlyFileSystem fileSystem)
{
var auditDirPath = fileSystem.RootPath.Parent.Combine("Audit");
var auditDir = fileSystem.GetDirectory(auditDirPath);
if (!auditDir.Exists)
{
Info($"Audit directory not found at {auditDir.Path}");
return new Dictionary<string, Person>();
}
Info($"Audit directory found at {auditDir.Path}");
var people = auditDir
.GetDirectory("./db/speakers")
.GetFiles(SearchOption.AllDirectories)
.Where(file => file.Path.FileName.FullPath == "index.xml")
.Select(ReadPerson)
.ToList();
Info($"Found {people.Count} people");
var aliases = people
.SelectMany(person => person.Aliases.Concat(new[] { person.Name }), (person, alias) => new { Alias = alias, Person = person })
// Skip duplicates
.Where(aliased => aliased.Alias != "Александр Иванов" && aliased.Alias != "Евгений Пешков")
.ToDictionary(aliased => aliased.Alias, aliased => aliased.Person);
/*
// Find duplicates
var people2 = people
.SelectMany(person => person.Aliases.Concat(new[] { person.Name }), (person, alias) => new { Alias = alias, Person = person })
// Skip duplicates
.Where(aliased => aliased.Alias != "Александр Иванов" && aliased.Alias != "Евгений Пешков");
var aliases = new Dictionary<string, Person>();
foreach (var person2 in people2)
{
if (aliases.ContainsKey(person2.Alias))
{
Info($"!!! Found duplicate alias: {person2.Alias}, {person2.Person.Image} !!!");
}
else
{
aliases.Add(person2.Alias, person2.Person);
}
}
*/
return aliases;
}
public Person ReadPerson(IFile personFile)
{
using (var personStream = personFile.OpenRead())
{
var xmlPerson = new XmlDocument();
xmlPerson.Load(personStream);
var xmlRoot = xmlPerson.DocumentElement;
var name = xmlRoot.SelectSingleNode("/Speaker/Name").InnerText;
var description = xmlRoot.SelectSingleNode("/Speaker/Description").InnerText;
var aliases = new List<string>();
var id = xmlRoot.SelectSingleNode("/Speaker/Id").InnerText;
var latinName = id.Replace("-", " ");
if (latinName != name)
{
aliases.Add(latinName);
}
var imageUrl = new Uri($"https://raw.githubusercontent.com/DotNetRu/Audit/master/db/speakers/{id}/avatar.jpg");
return new Person(name, aliases, imageUrl, description);
}
}
public void Info(string message)
{
Wyam.Common.Tracing.Trace.Information(message);
}
public sealed class Person
{
public Person(string name, IReadOnlyList<string> aliases, Uri image, string description)
{
Name = name;
Aliases = aliases.ToList();
Image = image;
Description = description;
}
public string Name { get; }
public IReadOnlyList<string> Aliases { get; }
public Uri Image { get; }
public string Description { get; }
}