-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileSystemOutputHandler.cs
231 lines (176 loc) · 7.57 KB
/
FileSystemOutputHandler.cs
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
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using Bloggen.Net.Config;
using Bloggen.Net.Content;
using Bloggen.Net.Model;
using Bloggen.Net.Output.Implementation;
using Bloggen.Net.Source;
using Bloggen.Net.Template;
using Microsoft.Extensions.Options;
using Slugify;
namespace Bloggen.Net.Output
{
public class FileSystemOutputHandler : IOutputHandler
{
private const string POSTS_DIRECTORY = "posts";
private const string TAGS_DIRECTORY = "tags";
private const string POST_PAGES_DIRECTORY = "pages";
private const string EXTENSION = "html";
private readonly CommandLineOptions commandLineOptions;
private readonly IFileSystem fileSystem;
private readonly IContext<Post, Tag, Page> context;
private readonly ITemplateHandler templateHandler;
private readonly IContentParser contentParser;
private readonly SiteConfig siteConfig;
private readonly ISourceHandler sourceHandler;
private readonly ISlugHelper slugHelper;
private readonly object site;
public FileSystemOutputHandler(
CommandLineOptions commandLineOptions,
IFileSystem fileSystem,
IContext<Post, Tag, Page> context,
ITemplateHandler templateHandler,
IContentParser contentParser,
IOptions<SiteConfig> siteConfig,
ISourceHandler sourceHandler,
ISlugHelper slugHelper)
{
(this.commandLineOptions, this.fileSystem, this.context, this.templateHandler,
this.contentParser, this.siteConfig, this.sourceHandler, this.slugHelper) =
(commandLineOptions, fileSystem, context, templateHandler, contentParser, siteConfig.Value, sourceHandler, slugHelper);
this.site = new { config = this.siteConfig, tags = this.context.Tags.OrderBy(t => t.Name), pages = this.context.Pages };
}
public void Generate()
{
this.ClearOutput();
this.GenerateContextUrls();
this.MergePostMetaproperties();
this.Render(this.context.Posts, "post", p => this.contentParser.RenderPost(p.FileName));
this.Render(this.context.Tags, "tag");
this.Render(this.context.Pages, "page", p => this.contentParser.RenderPage(p.FileName));
this.RenderPostPages();
this.CopyAssets();
}
private void ClearOutput()
{
if (this.fileSystem.Directory.Exists(this.commandLineOptions.OutputDirectory))
{
this.fileSystem.Directory.Delete(this.commandLineOptions.OutputDirectory, true);
}
this.fileSystem.Directory.CreateDirectory(this.commandLineOptions.OutputDirectory);
this.CreateOutputSubDirectory(POSTS_DIRECTORY);
this.CreateOutputSubDirectory(TAGS_DIRECTORY);
this.CreateOutputSubDirectory(POST_PAGES_DIRECTORY);
}
private void CreateOutputSubDirectory(string directory)
{
this.fileSystem.Directory.CreateDirectory(
this.fileSystem.Path.Combine(this.commandLineOptions.OutputDirectory, directory));
}
private void GenerateContextUrls()
{
foreach (var p in this.context.Posts)
{
p.Url = $"{POSTS_DIRECTORY}/{p.FileName}";
}
foreach (var t in this.context.Tags)
{
t.Url = $"{TAGS_DIRECTORY}/{this.slugHelper.GenerateSlug(t.Name)}";
}
foreach (var p in this.context.Pages)
{
p.Url = $"{p.FileName}";
}
}
private void MergePostMetaproperties()
{
foreach (var post in this.context.Posts)
{
foreach (var property in this.siteConfig.MetaProperties)
{
if (!post.MetaProperties.Any(postProp => postProp.Property == property.Property))
{
post.MetaProperties.Add(property);
}
}
}
}
private void Render<T>(IEnumerable<T> items, string layout, Func<T, string>? getContent = null) where T : class, IResource
{
foreach(var item in items)
{
var p = this.fileSystem.Path.Combine(this.commandLineOptions.OutputDirectory, item.Url);
using var sw = this.fileSystem.File.CreateText($"{p}.{EXTENSION}");
this.templateHandler.Write(sw, layout, item, this.site, getContent != null ? getContent(item) : null);
}
}
private void RenderPostPages()
{
var list = this.GeneratePostPages();
this.RenderPostPage(list[0], list.Count, this.commandLineOptions.OutputDirectory, $"index.{EXTENSION}");
var node = list[0].Next;
while (node != null)
{
this.RenderPostPage(node, list.Count, this.commandLineOptions.OutputDirectory, $"{node.Url}.{EXTENSION}");
node = node.Next;
}
}
private void RenderPostPage<T>(PaginationNode<T> node, int totalCount, params string[] pathParts) where T : class, IResource
{
node.TotalCount = totalCount;
using var sw = this.fileSystem.File.CreateText(this.fileSystem.Path.Combine(pathParts));
this.templateHandler.Write(sw, "list", node, this.site);
}
private List<PaginationNode<Post>> GeneratePostPages()
{
var postCount = this.context.Posts.Count();
var list = new List<PaginationNode<Post>>();
var ordered = this.context.Posts.OrderByDescending(p => p.CreatedAt);
for (int i = 0; i * this.siteConfig.PostsPerPage < postCount; i++)
{
list.Add(new PaginationNode<Post>(
i + 1,
ordered.Skip(i * this.siteConfig.PostsPerPage).Take(this.siteConfig.PostsPerPage)));
if (i > 0)
{
list[i].Url = $"{POST_PAGES_DIRECTORY}/{(list[i].PageNumber)}";
list[i].Previous = list[i - 1];
list[i - 1].Next = list[i];
}
else
{
list[i].Url = string.Empty;
}
}
return list;
}
private void CopyAssets()
{
var destination = this.fileSystem.Path.Combine(this.commandLineOptions.OutputDirectory, "assets");
this.CopyDirectory(this.sourceHandler.AssetsPath, destination);
this.CopyDirectory(this.sourceHandler.TemplateAssetsPath, destination);
}
private void CopyDirectory(string source, string destination)
{
var sourceDirectory = this.fileSystem.DirectoryInfo.FromDirectoryName(source);
if (!sourceDirectory.Exists)
{
return;
}
if (!this.fileSystem.DirectoryInfo.FromDirectoryName(destination).Exists)
{
this.fileSystem.Directory.CreateDirectory(destination);
}
foreach (var f in sourceDirectory.GetFiles())
{
f.CopyTo(this.fileSystem.Path.Combine(destination, f.Name), true);
}
foreach (var d in sourceDirectory.GetDirectories())
{
this.CopyDirectory(d.FullName, this.fileSystem.Path.Combine(destination, d.Name));
}
}
}
}