-
Notifications
You must be signed in to change notification settings - Fork 0
/
TocHelper.cs
234 lines (200 loc) · 6.27 KB
/
TocHelper.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
232
233
234
using Microsoft.DocAsCode.Common;
using Microsoft.DocAsCode.DataContracts.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace TocGenerator
{
public static class TocHelper
{
public static TocViewModel GenerateTocFromDirectory(string folderPath, string name)
{
if (string.IsNullOrEmpty(folderPath))
{
return null;
}
if (!Directory.Exists(folderPath))
{
return null;
}
var item = ConvertToTree(folderPath);
if (!string.IsNullOrEmpty(name))
{
item.Name = name;
}
return new TocViewModel(new[] { item });
}
public static TocViewModel GenerateTocFromDirectory(List<string> folders, string name)
{
var rootItem = new TocItemViewModel
{
Name = name,
Items = new TocViewModel()
};
foreach (var folder in folders)
{
if (!string.IsNullOrEmpty(folder) && Directory.Exists(folder))
{
var item = ConvertToTree(folder);
rootItem.Items.Add(item);
}
}
if (rootItem.Items.Count == 0)
{
return null;
}
return new TocViewModel(new[] { rootItem });
}
public static void DistinctTocItems(TocViewModel model)
{
foreach (var item in model)
{
if (item.Items != null)
{
DistinctTocItems(item.Items);
}
}
UniqueTocItemsCore(model);
}
public static void SortTocItems(TocViewModel model)
{
foreach (var item in model)
{
if (item.Items != null)
{
SortTocItems(item.Items);
}
}
model.Sort(SortByItemsAndName);
}
public static void SaveToc(string tocPath, TocViewModel model)
{
YamlUtility.Serialize(tocPath, model);
}
public static void ResolveHref(string tocPath, TocViewModel model)
{
if (model == null)
{
return;
}
if (string.IsNullOrEmpty(tocPath))
{
return;
}
var baseDir = Path.GetDirectoryName(tocPath);
foreach (var item in model)
{
ResolveHref(baseDir, item);
}
}
private static void ResolveHref(string baseDir, TocItemViewModel item)
{
if (item == null)
{
return;
}
if (!PathUtility.IsRelativePath(item.Href))
{
item.Href = PathUtility.MakeRelativePath(baseDir, item.Href);
}
if (item.Items != null && item.Items.Count > 0)
{
foreach (var child in item.Items)
{
ResolveHref(baseDir, child);
}
}
}
private static int SortByItemsAndName(TocItemViewModel first, TocItemViewModel second)
{
if (first.Items != null && second.Items != null)
{
return first.Name.CompareTo(second.Name);
}
else if (first.Items != null)
{
return -1;
}
else if (second.Items != null)
{
return 1;
}
else
{
return first.Name.CompareTo(second.Name);
}
}
private static void UniqueTocItemsCore(TocViewModel model)
{
var count = model.Count();
var i = 0;
var j = 1;
while (j < count)
{
if (string.Equals(model[i].Name, model[j].Name))
{
model[i].Items.AddRange(model[j].Items);
model[i].Items.Sort(SortByItemsAndName);
j++;
}
else
{
i++;
model[i] = model[j];
j++;
}
}
if (i < count)
{
model.RemoveRange(i + 1, count - i - 1);
}
}
private static TocItemViewModel ConvertToTree(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return null;
}
var name = string.Empty;
if (Directory.Exists(filePath))
{
var folderPath = filePath;
name = Path.GetFileName(folderPath);
var node = new TocItemViewModel
{
Name = name
};
var indexPage = Path.Combine(folderPath, "index.md");
if (File.Exists(indexPage))
{
node.Href = indexPage;
}
var items = (from file in Directory.EnumerateFileSystemEntries(folderPath)
let fileName = Path.GetFileNameWithoutExtension(file)
let ext = Path.GetExtension(file)
where !string.Equals(fileName, "index", StringComparison.OrdinalIgnoreCase)
where ext == "" || string.Equals(".md", ext, StringComparison.OrdinalIgnoreCase)
let item = ConvertToTree(file)
where item != null
select item).ToList();
if (items.Count == 0)
{
return null;
}
node.Items = new TocViewModel(items);
return node;
}
if (File.Exists(filePath))
{
name = Path.GetFileNameWithoutExtension(filePath);
return new TocItemViewModel
{
Name = name,
Href = filePath
};
}
return null;
}
}
}