-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.csx
369 lines (331 loc) · 11 KB
/
build.csx
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
#! "net8.0"
#r "nuget: Metalsharp, 0.9.0-rc.6"
#r "nuget: Metalsharp.LiquidTemplates, 0.9.0-rc.5"
#r "nuget: Metalsharp.SimpleBlog, 0.9.0-rc.2"
#r "nuget: System.ServiceModel.Syndication, 8.0.0"
using Metalsharp;
using Metalsharp.LiquidTemplates;
using Metalsharp.SimpleBlog;
using System.Text;
using System.Text.RegularExpressions;
using System.ServiceModel.Syndication;
using System.IO;
using System.Xml;
using System;
using System.Text.Json;
using System.Collections.Generic;
using System.Linq;
IEnumerable<SeriesInfo> seriesInfo = [];
using (var reader = new StreamReader("Config/series.json"))
{
seriesInfo = JsonSerializer.Deserialize<IEnumerable<SeriesInfo>>(reader.ReadToEnd());
}
new MetalsharpProject(new MetalsharpOptions()
{
OutputDirectory = "output",
ClearOutputDirectory = true
})
.AddInput("Site", @".\")
.UseFrontmatter()
.RemoveFiles(file =>
file.Metadata.TryGetValue("draft", out var isDraftObj)
&& isDraftObj is bool isDraft
&& isDraft
)
.Use(project => // Add reading time to posts
{
foreach (var file in project.InputFiles.Where(f => f.Directory.StartsWith(@".\Posts")))
{
file.Metadata.Add("readingTime", (int)Math.Ceiling(file.Text.Split(' ').Length / 200D));
}
})
.UseMarkdown()
.UseSimpleBlog(new()
{
PostsDirectory = @".\Posts",
PostsOrderQuery = f => DateTime.Parse(f.Metadata["date"] as string ?? DateTime.Now.ToString()),
PostMetadata = post =>
{
var postDate = DateTime.Parse(post.Metadata["date"]?.ToString() ?? "");
return new()
{
["template"] = "article",
["year"] = postDate.Year.ToString(),
["month"] = postDate.Month.ToString("D2"),
["day"] = postDate.Day.ToString("D2"),
["isodate"] = postDate.ToUniversalTime().ToString("o", System.Globalization.CultureInfo.InvariantCulture),
["slug"] = post.Name
};
},
BlogFilePath = @".\index.html",
BlogMetadata = new()
{
["title"] = "Software Engineer, Architect, and Team Leader",
["template"] = "archive",
["removeScrollspy"] = true,
["hidePastArticles"] = true,
["fontRequirement"] = "index",
["includeCanonicalIndex"] = true
},
})
.Use(project => // Generate RSS feed
{
var rssItems = project.OutputFiles.Where(f => f.Directory.StartsWith(@".\Posts")).Select(post => new SyndicationItem(
post.Metadata["title"].ToString(),
post.Text,
new Uri($"https://ian.wold.guru/Posts/{post.Name}.html"),
post.Name,
DateTime.Parse(post.Metadata["date"]?.ToString() ?? "")
));
var rssFeedContent = string.Empty;
var rssFeed = new SyndicationFeed(
"Ian Wold",
"Ian Wold's Blog",
new Uri("https://ian.wold.guru"),
rssItems.Select(i =>
{
i.PublishDate = i.LastUpdatedTime;
return i;
})
);
rssFeed.Links.Add(new SyndicationLink(new Uri("https://ian.wold.guru/feed.xml"))
{
RelationshipType = "self"
});
var xmlSettings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8
};
using (var memoryStream = new MemoryStream())
using (var xmlWriter = XmlWriter.Create(memoryStream, xmlSettings))
{
rssFeed.SaveAsRss20(xmlWriter);
xmlWriter.Flush();
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
rssFeedContent = reader.ReadToEnd();
}
}
project.AddOutput(new MetalsharpFile(rssFeedContent, "feed.xml"));
})
.Use(project => // Add SEO and "series" metadata to posts
{
var seriesPosts = new Dictionary<string, IEnumerable<Dictionary<string, object>>>();
var topicPosts = new Dictionary<string, IEnumerable<Dictionary<string, object>>>();
var posts = project.OutputFiles.Where(f => f.Directory.StartsWith(@".\Posts"));
foreach (var post in posts)
{
post.Metadata.Add("structuredData", $$"""
{
"@context": "https://schema.org",
"@type": "Article",
"author": [{
"@type": "Person",
"name": "Ian Wold"
}],
"datePublished": "{{DateTime.Parse(post.Metadata["date"]?.ToString() ?? "")}}",
"image": "https://images.unsplash.com/{{ post.Metadata["hero"]!.ToString()}}",
"headline": "{{post.Metadata["title"]!.ToString()}}",
"description": "{{post.Metadata["description"]!.ToString()}}",
"publisher": {
"@type": "Person",
"name": "Ian Wold",
"logo": {
"@type": "ImageObject",
"url": "https://ian.wold.guru/images/hero1.svg"
}
},
}
"""
);
if (post.Metadata.TryGetValue("series", out var seriesObject) && seriesObject is string seriesName)
{
if (seriesPosts.TryGetValue(seriesName, out var seriesPostsList))
{
seriesPosts[seriesName] = [ ..seriesPostsList, post.Metadata ];
}
else
{
seriesPosts.Add(seriesName, [ post.Metadata ]);
}
}
if (post.Metadata.TryGetValue("topics", out var topicsObject) && topicsObject is IEnumerable<object> topicsObjectList)
{
foreach (var topicName in topicsObjectList.Cast<string>())
{
if (topicPosts.TryGetValue(topicName, out var topicPostsList))
{
topicPosts[topicName] = [ ..topicPostsList, post.Metadata ];
}
else
{
topicPosts.Add(topicName, [ post.Metadata ]);
}
}
}
}
foreach (var series in seriesPosts)
{
project.AddOutput(new MetalsharpFile(string.Empty, $".\\Series\\{seriesInfo.Single(s => s.Title == series.Key).Slug}.html", new Dictionary<string, object>()
{
["title"] = series.Key,
["series"] = series.Key,
["template"] = "archive",
["removeScrollspy"] = true,
["posts"] = series.Value.OrderByDescending(p => DateTime.Parse(p["date"].ToString()))
}));
}
project.AddOutput(new MetalsharpFile(
"Sometimes I write a series of related posts, and I keep those series linked together to be able to easily go back over them. These are all the series I have written (or am in the process of writing):",
".\\series.html",
new Dictionary<string, object>()
{
["title"] = "series",
["template"] = "linkList",
["removeScrollspy"] = true,
["fontRequirement"] = "page",
["links"] = seriesInfo,
["folder"] = "Series"
}
));
var topics = topicPosts.Select(t => new TopicInfo(t.Key, t.Key.ToLowerInvariant(), t.Value.OrderByDescending(p => DateTime.Parse(p["date"].ToString()))));
foreach (var topic in topics)
{
project.AddOutput(new MetalsharpFile(string.Empty, $".\\Topics\\{topic.Slug}.html", new Dictionary<string, object>()
{
["title"] = topic.Title,
["topic"] = topic.Title,
["template"] = "archive",
["removeScrollspy"] = true,
["posts"] = topic.Posts
}));
}
project.AddOutput(new MetalsharpFile(
"These are all of the topics under which I've categorized my posts:",
".\\topics.html",
new Dictionary<string, object>()
{
["title"] = "topics",
["template"] = "linkList",
["removeScrollspy"] = true,
["fontRequirement"] = "page",
["links"] = topics,
["folder"] = "Topics"
}
));
})
.Use(project =>
{
var posts = project.OutputFiles.Where(f => f.Directory.StartsWith(@".\Posts"));
foreach (var post in posts)
{
post.Contents = Encoding.Default.GetBytes(post.Text.Replace("<h2>", "<h3>"));
post.Contents = Encoding.Default.GetBytes(post.Text.Replace("</h2>", "</h3>"));
post.Contents = Encoding.Default.GetBytes(post.Text.Replace("<h1>", "<h2>"));
post.Contents = Encoding.Default.GetBytes(post.Text.Replace("</h1>", "</h2>"));
}
})
.Use(project => // Add table of contents to posts
{
var posts = project.OutputFiles.Where(f => f.Directory.StartsWith(@".\Posts"));// && f.Metadata.TryGetValue("contents", out object isContentsObject) && isContentsObject is bool isContents && isContents);
static string getSectionSlug(string sectionName) =>
string.Join('-', sectionName.Split(' ')).ToLower();
foreach (var post in posts)
{
var postLines = post.Text.Split('\r', '\n').Where(l => !string.IsNullOrWhiteSpace(l));
var postBuilder = new StringBuilder();
var sections = new List<object>();
var isInContainingSection = false;
var isInInnerSection = false;
void addSection(string sectionName, string sectionSlug, int level)
{
sections.Add(new { name = sectionName, id = sectionSlug, level = level });
postBuilder.AppendLine($"<section id=\"{sectionSlug}\">");
}
string getHeaderWithSectionLink(string sectionSlug, string line) =>
$"{line.Substring(0, line.Length - 5)} <a class=\"section-link\" href=\"https://ian.wold.guru/Posts/{post.Name}.html#{sectionSlug}\">#</a>{line.Substring(line.Length - 5, 5)}";
foreach (var line in postLines)
{
var lineToAdd = line;
var trimmedLine = line.Trim();
if (Regex.Match(trimmedLine, @"(?:<h2>)([^<]*)(?:<\/h2>)$") is Match matchContainingHeader && matchContainingHeader.Success)
{
if (isInInnerSection)
{
postBuilder.AppendLine("</section>");
isInInnerSection = false;
}
if (isInContainingSection)
{
postBuilder.AppendLine("</section>");
}
var sectionName = matchContainingHeader.Groups[1].Captures[0].Value;
var sectionSlug = getSectionSlug(sectionName);
addSection(sectionName, sectionSlug, 1);
lineToAdd = getHeaderWithSectionLink(sectionSlug, trimmedLine);
isInContainingSection = true;
}
else if (Regex.Match(trimmedLine, @"(?:<h3>)([^<]*)(?:<\/h3>)$") is Match matchInnerHeader && matchInnerHeader.Success)
{
if (isInInnerSection)
{
postBuilder.AppendLine("</section>");
}
var sectionName = matchInnerHeader.Groups[1].Captures[0].Value;
var sectionSlug = getSectionSlug(sectionName);
addSection(sectionName, sectionSlug, 2);
lineToAdd = getHeaderWithSectionLink(sectionSlug, trimmedLine);
isInInnerSection = true;
}
postBuilder.AppendLine(lineToAdd);
}
if (isInInnerSection)
{
postBuilder.AppendLine("</section>");
isInInnerSection = false;
}
if (isInContainingSection)
{
postBuilder.AppendLine("</section>");
isInContainingSection = false;
}
post.Contents = Encoding.Default.GetBytes(postBuilder.ToString());
post.Metadata.Add("sections", sections);
}
})
.Use(project => // Add slug and description for "series" files
{
foreach (var file in project.InputFiles.Concat(project.OutputFiles))
{
if (file.Metadata.TryGetValue("series", out var seriesNameObj) && seriesNameObj is string seriesName)
{
if (!file.Metadata.ContainsKey("seriesDescription"))
{
file.Metadata.Add("seriesDescription", seriesInfo.Single(s => s.Title == seriesName).Description.Split("\n").Select(d => $"<p>{d}</p>"));
}
if (!file.Metadata.ContainsKey("seriesSlug"))
{
file.Metadata.Add("seriesSlug", seriesInfo.Single(s => s.Title == seriesName).Slug);
}
}
}
})
.UseLeveller()
.UseLiquidTemplates("Templates")
.AddOutput("Static", @".\")
.Use(project => // Add sitemap
{
var builder = new StringBuilder();
builder.AppendLine("https://ian.wold.guru/");
foreach (var page in project.OutputFiles.Where(f => f.Extension.Contains("html") && !f.Name.Contains("index")))
{
builder.AppendLine($"https://ian.wold.guru/{page.FilePath.Replace(".\\", "").Replace("\\", "/")}");
}
project.AddOutput(new MetalsharpFile(builder.ToString(), "sitemap.txt"));
})
.Build();
record SeriesInfo(string Title, string Slug, string Description);
record TopicInfo(string Title, string Slug, IEnumerable<Dictionary<string, object>> Posts);