forked from Geeksltd/GCop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentGenerator.csx
148 lines (130 loc) · 4.62 KB
/
DocumentGenerator.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
var folder = new DirectoryInfo(@"./Rules");
var filez = Reader.RemoveSuffix(Reader.RemovePrefix(Reader.ReadFileNames(folder,"_Template.md"),"GCop"),".md");
var lines = MDEngine.GetLines(new FileInfo(@"Rules\GCop101.md"));
Writer.WriteTable(new FileInfo(@"docs\Rules.md"), "/Rules/", filez);
var files = Reader.RemoveSuffix(Reader.ReadFileNames(folder,"_Template.md"),".md");
Writer.WriteList(new FileInfo(@"_sidebar.md"), "/Rules/", files);
public static class Reader
{
public static List<FileItem> ReadFileNames(DirectoryInfo dir, params string[] ignore)
{
var filez = ReadFileNames(dir);
foreach (var item in ignore)
filez.RemoveAll(x => x.Name == item);
return filez;
}
public static List<FileItem> ReadFileNames(DirectoryInfo dir)
{
var files = new List<FileItem>();
foreach (var file in dir.GetFiles())
files.Add(new FileItem(file.Name, file));
return files;
}
public static List<FileItem> RemovePrefix(List<FileItem> strings, string prefix)
{
var result = new List<FileItem>();
foreach (var item in strings)
{
item.Name = item.Name.Substring(prefix.Length);
result.Add(item);
}
return result;
}
public static List<FileItem> RemoveSuffix(List<FileItem> strings, string suffix)
{
var result = new List<FileItem>();
foreach (var item in strings)
{
item.Name = item.Name.Substring(0, item.Name.Length - suffix.Length);
result.Add(item);
}
return result;
}
}
public static class Writer
{
public static void WriteList(FileInfo dest, string relativePath, List<FileItem> contents)
{
string initString =
@"* [Rules List](docs/Rules.md)
* [Template](Rules/_Template.md)
* Rules
";
var str = BuildListString(relativePath, contents, initString);
using (var writer = new StreamWriter(dest.FullName))
{
writer.Write(str);
}
}
private static string BuildListString(string relativePath, List<FileItem> contents, string initString)
{
var builder = new StringBuilder();
builder.Append(initString);
foreach (var content in contents)
{
builder.AppendLine($" * [{content.Name}]({relativePath + content.File.Name})");
}
return builder.ToString();
}
public static void WriteTable(FileInfo dest, string relativePath, List<FileItem> contents)
{
string initString =
@"# Error list
GCop code | Description
--- | ---
";
var str = BuildTableString(relativePath, contents, initString);
using (var writer = new StreamWriter(dest.FullName))
{
writer.Write(str);
}
}
static string BuildTableString(string relativePath, List<FileItem> contents, string initString)
{
var builder = new StringBuilder();
builder.Append(initString);
foreach (var content in contents)
{
builder.AppendLine($"{content.Name} | [{MDEngine.ParseTitle(content.File)}]({relativePath + content.File.Name})");
}
return builder.ToString();
}
}
public static class MDEngine
{
public static string GetLines(FileInfo path)
{
using (StreamReader srt = new StreamReader(path.FullName))
{
return srt.ReadToEnd();
}
}
public static string ParseTitle(FileInfo path) // =>GetLine(text, 3).Substring(4).Substring(GetLine(text, 3).Substring(4).Length - 2);
{
var x = GetLine(MDEngine.GetLines(path), 3);
var z = x.Substring(4);
var y = z.Substring(0, z.Length - 2);
return y;
}
public static string GetLine(string text, int lineNo)
{
string[] lines = text.Replace("\r", "").Split('\n');
return lines.Length >= lineNo ? lines[lineNo - 1] : null;
}
}
public class FileItem
{
public FileItem(string name, FileInfo file)
{
Name = name;
File = file;
}
public string Name { get; set; }
public FileInfo File { get; set; }
}