-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
103 lines (85 loc) · 3.16 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
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace GitHubWikiSidbarToDocFX
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 1 || args.Length > 2)
throw new Exception("Invalide number of arguments!");
if (!File.Exists(args[0]))
throw new FileNotFoundException(args[0]);
var file = File.ReadAllText(args[0]);
file = Regex.Replace(file, @"^\s+$[\r\n]*", string.Empty, RegexOptions.Multiline);
var sidelines = file.Split('\n');
StringBuilder toc = new StringBuilder();
for (int i = 0; i < sidelines.Length; i++)
{
//checks and extraction
if (sidelines[i].Length < 3)
continue;
bool href = false;
bool items = false;
int indent = GetUntilOrEmpty(sidelines[i], "-").Length;
string name = "";
if (sidelines[i].Contains("[[") && sidelines[i].Contains("]]"))
href = true;
if (i < sidelines.Length-1 && indent < GetUntilOrEmpty(sidelines[i + 1], "-").Length)
items = true;
if (href)
{
var start = sidelines[i].IndexOf("[[") + 2;
name = sidelines[i].Substring(start, sidelines[i].IndexOf("]]") - start);
}
else
{
var start = sidelines[i].IndexOf("- ") + 2;
name = sidelines[i].Substring(start, sidelines[i].Length - start);
}
//generate toc
//name
toc.Append(Repeat(' ', indent));
toc.Append("- name: ");
toc.Append(name);
toc.Append("\n");
//href
if (href)
{
toc.Append(Repeat(' ', indent + 2));
toc.Append("href: ");
toc.Append(name.Replace(" ", "-"));
toc.Append(".md");
toc.Append("\n");
}
//items
if (items)
{
toc.Append(Repeat(' ', indent + 2));
toc.Append("items: ");
toc.Append("\n");
}
}
var outfilepath = args.Length > 1 ? Path.Combine(args[1], "toc.yml") : "toc.yml";
File.WriteAllText(outfilepath, toc.ToString());
}
public static string GetUntilOrEmpty(string text, string stopAt = "-")
{
if (!String.IsNullOrWhiteSpace(text))
{
int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal);
if (charLocation > 0)
{
return text.Substring(0, charLocation);
}
}
return String.Empty;
}
public static string Repeat(char c, int count)
{
return new String(c, count);
}
}
}