-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
158 lines (120 loc) · 4.9 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.IO.SearchOption;
using static System.Text.RegularExpressions.RegexOptions;
namespace ApiReferenceCleanup
{
class Program
{
private static readonly Regex XmlLine = new Regex("^((?: )+)(<[^>]+>)(.*)(<[^>]+>)$", Compiled);
// TODO: recognize self-clsoing tags like <see cref="whatever" /> as one word
private static readonly Regex XmlWord = new Regex(@"<([^ ]+)[^>]*?>[^<]*?</\1>[\S]*|<[^<]*?/>|[\S]+", Compiled);
private static readonly Regex MdWord = new Regex(@"\[[^]]*?\]\([^)]*?\)[\S]*|[\S]+", Compiled);
private static readonly Regex Prefix = new Regex(@"^ *(?:[-*>]|\d+\.|) *", Compiled);
static void Main()
{
string path = @"E:\Users\Svick\git\core-docs\xml";
int lineLengthLimit = 120;
Console.WindowWidth = lineLengthLimit + 1;
var files = Directory.EnumerateFiles(path, "*.xml", AllDirectories);
foreach (var file in files)
{
//Console.WriteLine(file);
bool changed = false;
var inputLines = File.ReadAllLines(file);
var outputLines = new List<string>();
bool docs = false;
foreach (var line in inputLines)
{
if (!docs)
{
outputLines.Add(line);
if (line.Contains("<Docs>"))
docs = true;
continue;
}
if (line.Contains("</Docs>"))
{
docs = false;
outputLines.Add(line);
continue;
}
if (line.Length <= lineLengthLimit)
{
outputLines.Add(line);
continue;
}
changed = true;
Console.WriteLine(line);
void Add(string part)
{
Console.WriteLine(part);
outputLines.Add(part);
}
var xmlLineMatch = XmlLine.Match(line);
// TODO: XmlLine → XML; CDATA → MD; else → report
if (xmlLineMatch.Success)
{
string indent1 = xmlLineMatch.Groups[1].Value;
string indent2 = indent1 + " ";
string opening = xmlLineMatch.Groups[2].Value;
string text = xmlLineMatch.Groups[3].Value;
string closing = xmlLineMatch.Groups[4].Value;
Add(indent1 + opening);
foreach (var part in Split(text, lineLengthLimit - indent2.Length, XmlWord))
{
Add(indent2 + part);
}
Add(indent1 + closing);
}
else
{
string prefix1 = Prefix.Match(line).Value;
string prefix2;
if (prefix1.TrimStart().StartsWith(">"))
prefix2 = prefix1;
else
prefix2 = new string(' ', prefix1.Length);
string text = line.Substring(prefix1.Length);
bool first = true;
foreach (var part in Split(text, lineLengthLimit - prefix1.Length, MdWord))
{
string prefix = first ? prefix1 : prefix2;
first = false;
Add(prefix + part);
}
}
Console.WriteLine();
}
/*if (changed)
File.WriteAllLines(file, outputLines);*/
}
}
static IEnumerable<string> Split(string s, int lengthLimit, Regex wordRegex)
{
var words = wordRegex.Matches(s).Cast<Match>().Select(m => m.Value);
var result = new StringBuilder();
foreach (var word in words)
{
if (result.Length != 0)
{
if (result.Length + 1 + word.Length > lengthLimit)
{
yield return result.ToString();
result.Clear();
}
else
{
result.Append(' ');
}
}
result.Append(word);
}
yield return result.ToString();
}
}
}