-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExporter.cs
More file actions
291 lines (238 loc) · 10.4 KB
/
Copy pathExporter.cs
File metadata and controls
291 lines (238 loc) · 10.4 KB
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
using System;
using System.IO;
using FileSystem;
using Tree;
using Log;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Export
{
class Exporter {
Logger log;
Template template;
public Exporter(String inpath, String outpath, String templatepath) {
log = new Logger("Exporter");
template = new Template(templatepath,inpath,outpath);
scanPath("path",inpath);
template.render("Test");
}
private void scanPath(String place,String real_path) {
log.info($"Looking at directory: {place}");
IEnumerable<String> dirs;
try {
dirs = Directory.EnumerateDirectories(real_path);
}
catch {
log.error($"Cannot load path {real_path}");
return;
}
foreach (String dir in dirs) { // Look for directories
String real_dir = dir.Split(Path.DirectorySeparatorChar)[dir.Split(Path.DirectorySeparatorChar).Length-1];
log.info($"Found {dir}");
if (real_dir.StartsWith("mod_")) {
scanPath(place+"/"+real_dir.Replace("mod_",""),real_path+Path.DirectorySeparatorChar+real_dir);
String modpath = real_path+Path.DirectorySeparatorChar+real_dir+Path.DirectorySeparatorChar+real_dir+".mod.md";
template.addFolder(real_path,real_dir, place+"/"+real_dir, FileTypes.Module);
new MDCompiler(File.ReadAllText(modpath), modpath, place+"/"+real_dir.Replace("mod_","")+modpath.Replace(".mod.md","").Replace("mod_",""), FileTypes.Module, template).compileWithTemplate(template);
}
else {
scanPath(place+"/"+real_dir,real_path+Path.DirectorySeparatorChar+real_dir);
template.addFolder(real_path,real_dir, place+"/"+real_dir, FileTypes.Folder);
}
}
foreach (String page in Directory.EnumerateFiles(real_path,"*.md")) { // Look for pages
String file = page.Split(Path.DirectorySeparatorChar)[page.Split(Path.DirectorySeparatorChar).Length-1];
log.info($"Found File {file}");
if (file.EndsWith(".mod.md")) {
// Ignore - It has already been processed above
}
else if (file.EndsWith(".class.md")) {
log.info($"Loading class at path {real_path+Path.DirectorySeparatorChar+file}");
String cpath = real_path+Path.DirectorySeparatorChar+file;
new MDCompiler(File.ReadAllText(cpath), cpath, place+"/"+file.Replace(".class.md", ""), FileTypes.Class, template).compileWithTemplate(template);
}
else {
String ppath = real_path+Path.DirectorySeparatorChar+file;
new MDCompiler(File.ReadAllText(ppath), ppath, place+"/"+file.Replace(".md", ""), FileTypes.Class, template).compileWithTemplate(template);
}
}
}
}
class MDCompiler {
String result;
String path;
String vpath;
FileTypes type;
Logger log = new Logger("MDCompiler");
Template template;
public MDCompiler(String text, String _path, String _vpath, FileTypes _type, Template _template) {
Regex rx = new Regex(@"@##@(.+?)\(!\):",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
Regex pbs = new Regex(@"\[\[(.+?)\]\]",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
template = _template;
path = _path;
int parts = path.Replace(template.base_path,"root").Split(Path.DirectorySeparatorChar).Length-2;
String reppath = "";
for (int i = 0; i < parts; i++)
{
reppath += "../";
}
///\[(.*?)\]/
MatchCollection matches = rx.Matches(text);
foreach (Match match in matches)
{
text = text.Replace((String)match.Value,"");
}
MatchCollection links = pbs.Matches(text);
foreach (Match match in links)
{
log.info(parsePath((String)match.Value.Replace("[[","").Replace("]]",""),reppath));
log.info((String)match.Value);
text = text.Replace((String)match.Value,parsePath((String)match.Value.Replace("[[","").Replace("]]",""),reppath));
}
result = CommonMark.CommonMarkConverter.Convert(text.Replace("root/",reppath));
vpath = _vpath;
type = _type;
}
public void compileWithTemplate(Template template) {
template.addFile(result,path, vpath, type);
}
public String parsePath(String path, String reppath) {
VFile file = template.getVFileFromPath(path);
return file.path.Replace("root",reppath);
}
}
enum FileTypes
{
Folder, Module,
Class, Page, Function
}
class VFolder
{
public String name;
public List<VFolder> folders;
public List<VFile> file;
public String path;
public FileTypes type;
public String vpath;
public VFolder(String _name, String _path, String _vpath, FileTypes _type) {
folders = new List<VFolder>();
file = new List<VFile>();
name = _name;
path = _path;
vpath = _vpath;
type = _type;
}
}
class VFile
{
public String name;
public String content;
public VFolder parent;
public String path;
public FileTypes type;
public String vpath;
public VFile(String _name, String _content, VFolder _parent, String _path, String _vpath, FileTypes _type) {
name = _name;
content = _content;
parent = _parent;
path = _path;
vpath = _vpath;
type = _type;
}
}
class Template {
String tmpdata;
VFolder root;
public String base_path;
public String out_path;
Logger log = new Logger("Template");
public Template(String path, String inpath, String outpath) {
tmpdata = File.ReadAllText(path);
base_path = inpath;
out_path = outpath;
root = new VFolder("root",out_path,"root",FileTypes.Folder);
}
public String addFile(String _htmdata, String path, String vpath, FileTypes type) {
path = path.Replace(base_path,"root");
String cplate = tmpdata.Replace("{{content}}",_htmdata);
log.info($"Adding file {path} to directory {Path.GetDirectoryName(path)}");
VFolder parent = getFolderFromPath(Path.GetDirectoryName(path));
parent.file.Add(new VFile(Path.GetFileName(path),cplate,parent,path,vpath,type) );
return cplate;
}
public void addFolder(String path, String name, String vpath, FileTypes type) {
path = path.Replace(base_path,"root");
VFolder parent = getFolderFromPath(Path.GetDirectoryName(path));
parent.folders.Add(new VFolder(name,path+Path.DirectorySeparatorChar+name,vpath,type));
}
public VFile getFileFromPath(String path) {
VFolder folder = getFolderFromPath(Path.GetDirectoryName(path));
foreach (VFile f in folder.file) {
if (f.name == Path.GetFileName(path)) {
return f;
}
}
throw new FileNotFoundException($"Cannot find file {path}");
}
public VFolder getFolderFromPath(String path) {
List<String> pathbits = new List<String>(path.Split(Path.DirectorySeparatorChar));
return getFolderRecursive(pathbits, root);
}
private VFolder getFolderRecursive(List<String> pathbits, VFolder searchf) {
foreach (VFolder i in searchf.folders) {
if (i.name == pathbits[0]) {
pathbits.RemoveAt(0);
if (pathbits.Count < 1) {
return i;
}
return getFolderRecursive(pathbits,i);
}
}
throw new FileNotFoundException($"Cannot find folder ");
}
public VFile getVFileFromPath(String path) {
VFolder folder = getFolderFromPath(Path.GetDirectoryName(path));
foreach (VFile f in folder.file) {
if (Path.GetFileName(f.vpath) == Path.GetFileName(path)) {
return f;
}
}
throw new FileNotFoundException($"Cannot find file {path}");
}
public VFolder getVFolderFromPath(String path) {
List<String> pathbits = new List<String>(path.Split("/"));
return getVFolderRecursive(pathbits, root);
}
private VFolder getVFolderRecursive(List<String> pathbits, VFolder searchf) {
foreach (VFolder i in searchf.folders) {
if (Path.GetFileName(i.vpath) == pathbits[0]) {
pathbits.RemoveAt(0);
if (pathbits.Count < 1) {
return i;
}
return getVFolderRecursive(pathbits,i);
}
}
return searchf;
}
public String renderfile(String path) {
return getFileFromPath(path).content;
}
public void render(String title) {
recursiveRender(root,title);
}
private void recursiveRender(VFolder bfol, String title) {
foreach (VFolder f in bfol.folders) {
log.info($"Scanning folder {f.path}");
Directory.CreateDirectory(f.path.Replace("root",out_path));
recursiveRender(f,title);
}
foreach (VFile d in bfol.file) {
log.info($"Rendering file {d.path.Replace("root",out_path)+".html"}");
File.WriteAllText(d.path.Replace("root",out_path)+".html", d.content.Replace("{{title}}",title));
}
}
}
}