-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathParseDocumentation.cs
233 lines (223 loc) · 9.44 KB
/
ParseDocumentation.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
using System.Text;
using System.Text.RegularExpressions;
namespace OpenKNXproducer
{
public class ParseDocumentation
{
static readonly Regex sRegexChapterId = new("[^a-zA-Z0-9-_ ÄäÖöÜüß\\n]");
static readonly Regex sRegexChapterWhitespaces = new("--*");
static readonly Regex sRegexChapterName = new("[#*]");
static readonly Regex sRegexLink = new(@"\[([^\]]*)\]\([^)]*\)");
static readonly Dictionary<string, string> sCharReplace = new() { { " ", "-" }, { "\n", "-" }, { "Ä", "Ae" }, { "ä", "ae" }, { "Ö", "Oe" }, { "ö", "oe" }, { "Ü", "Ue" }, { "ü", "ue" }, { "ß", "ss" } };
public static string GetChapterId(string iLine, string iPraefix)
{
// get rid of forbidden characters
string lResult = sRegexChapterId.Replace(iLine, "");
// get rid of whitespaces at start and end
lResult = lResult.Trim();
foreach (var lEntry in sCharReplace)
lResult = lResult.Replace(lEntry.Key, lEntry.Value);
if (iPraefix != "") lResult = iPraefix + "-" + lResult;
lResult = sRegexChapterWhitespaces.Replace(lResult, "-");
return lResult;
}
private static string GetChapterName(string iLine)
{
string lResult = sRegexChapterName.Replace(iLine, "").Trim();
return lResult;
}
private static int CountCharAtStart(string iLine, char iChar)
{
int lResult = 0;
foreach (var lChar in iLine.ToCharArray())
{
if (lChar == iChar)
lResult++;
else
break;
}
if (lResult == 0) lResult = 10;
return lResult;
}
private static void WriteBaggage(string iPath, string iFileName, StringBuilder iBaggage)
{
// ensure right extension
string lBaggage = iBaggage.ToString();
lBaggage = sRegexLink.Replace(lBaggage, "$1");
iFileName = Path.ChangeExtension(iFileName, "md");
File.WriteAllText(Path.Combine(iPath, iFileName), lBaggage, Encoding.UTF8);
}
public static int ExportBaggages(string iWorkingDir, string iBaggageDir, string iDocFileName, string iPraefix)
{
// we read all lines of doc file
string lBaggageFileName = "";
string lChapterName = "";
Match lMatch;
StringBuilder lBaggage = new();
using var lFile = File.OpenText(iDocFileName);
Regex lRegexDocStart = new("<!--\\s*DOC\\s*(HelpContext=\"(.*)\")?\\s*-->");
Regex lRegexDocEnd = new("<!--\\s*DOCEND\\s*-->");
int lActiveDoc = 0;
Regex lRegexDocSkip = new("<!--\\s*DOC\\s*(Skip=\"(.*)\")\\s*-->");
int lActiveSkip = 0;
Regex lRegexDocContentStart = new("<!--\\s*DOCCONTENT");
Regex lRegexDocContentEnd = new("\\s*DOCCONTENT\\s*-->");
bool lActiveContent = false;
Regex lRegexCommentStart = new("^(?=\\s*<!--\\s)(?:(?!DOC).)*$");
Regex lRegexCommentEnd = new(".*-->\\s*");
bool lActiveComment = false;
Regex lRegexCleanupTitle = new(@"##(#?#?#?)\s*\*\*(.*)\*\*");
string lLine = "";
while (!lFile.EndOfStream)
{
if (lActiveSkip < 0)
lActiveSkip = 0;
else
lLine = lFile.ReadLine();
// skip n lines, we use this from external command or internally
if (lActiveSkip > 0)
{
lActiveSkip--;
continue;
}
if (lActiveSkip == 0)
{
lMatch = lRegexDocSkip.Match(lLine);
if (lMatch.Success)
{
lActiveSkip = int.Parse(lMatch.Groups[2].Value);
continue;
}
}
// skip commented lines
if (!lActiveComment)
{
lMatch = lRegexCommentStart.Match(lLine);
lActiveComment = lMatch.Success;
}
if (lActiveComment)
{
if (!lActiveContent)
{
lMatch = lRegexDocContentStart.Match(lLine);
if (lMatch.Success)
{
lActiveContent = true;
continue;
}
}
if (lActiveContent)
{
lMatch = lRegexDocContentEnd.Match(lLine);
if (lMatch.Success)
{
lActiveContent = false;
continue;
}
}
lMatch = lRegexCommentEnd.Match(lLine);
if (lMatch.Success)
{
lActiveComment = false;
lActiveContent = false;
}
if (!lActiveContent)
continue;
}
// process document lines
lMatch = lRegexDocStart.Match(lLine);
if (lMatch.Success)
{
if (lActiveDoc > 0)
{
// we have a new chapter, but the old is still active, we save the old one
lActiveDoc = 0;
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(lBaggageFileName);
WriteBaggage(iBaggageDir, lBaggageFileName, lBaggage);
lBaggage.Clear();
}
// we expect a chapter header line starting with a number of '#'
lLine = lFile.ReadLine();
lActiveDoc = CountCharAtStart(lLine, '#');
// calculate baggages filename
if (lMatch.Groups.Count == 3 && lMatch.Groups[2].Value != "")
{
lBaggageFileName = GetChapterId(lMatch.Groups[2].Value, iPraefix);
// Add chapter start for non chapter blocks
if (lLine.StartsWith("#"))
{
lChapterName = GetChapterName(lLine);
lActiveSkip = 1;
}
else
{
lChapterName = GetChapterName(lMatch.Groups[2].Value);
lActiveSkip = -1;
}
}
else if (lLine.StartsWith("#"))
{
// name is extracted from title
lBaggageFileName = GetChapterId(lLine, iPraefix);
lChapterName = GetChapterName(lLine);
lActiveSkip = 1;
}
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("--> {0}.md", lBaggageFileName);
Console.WriteLine();
string lChapterNameFormatted = $"### {lChapterName}";
lBaggage.AppendLine(lChapterNameFormatted);
lBaggage.AppendLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(lChapterNameFormatted);
Console.WriteLine();
Console.ResetColor();
}
else if (lActiveDoc > 0)
{
lMatch = lRegexDocEnd.Match(lLine);
if (lFile.EndOfStream || lMatch.Success || (lLine.StartsWith("#") && lActiveDoc >= CountCharAtStart(lLine, '#')))
{
// chapter is ended
lActiveDoc = 0;
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
WriteBaggage(iBaggageDir, lBaggageFileName, lBaggage);
lBaggage.Clear();
continue;
}
if (!lActiveContent)
{
lMatch = lRegexDocContentStart.Match(lLine);
if (lMatch.Success)
{
lActiveContent = true;
continue;
}
}
if (lActiveContent)
{
lMatch = lRegexDocContentEnd.Match(lLine);
if (lMatch.Success)
{
lActiveContent = false;
continue;
}
}
}
if (lActiveDoc > 0 && lActiveSkip == 0)
{
lMatch = lRegexCleanupTitle.Match(lLine);
if (lMatch.Success)
lLine = lLine.Replace("**", "");
lBaggage.AppendLine(lLine);
Console.WriteLine(lLine);
}
}
return 0;
}
}
}