forked from neuecc/MarkdownGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Beautifier.cs
74 lines (63 loc) · 3.23 KB
/
Beautifier.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MarkdownWikiGenerator
{
public static class Beautifier
{
public static string BeautifyType(Type t, bool isFull = false)
{
if (t == null) return "";
if (t == typeof(void)) return "void";
if (!t.IsGenericType) return
((isFull) ? t.FullName : t.Name)
;
var innerFormat = string.Join(", ", t.GetGenericArguments().Select(x => BeautifyType(x)));
return Regex.Replace(isFull ? t.GetGenericTypeDefinition().FullName : t.GetGenericTypeDefinition().Name, @"`.+$", "") + "<" + innerFormat + ">";
}
public static string BeautifyTypeWithLink(Type t,Func<Type,string> generateTypeRelativeLinkPath, bool isFull = false)
{
if (t == null) return "";
if (t == typeof(void)) return "void";
if (!t.IsGenericType)
if (t.IsArray)
{
return
$"[`{((isFull) ? t.FullName : t.Name).Replace("[]","")}`]({generateTypeRelativeLinkPath(t).Replace("[]", "")})`[]`"
;
}
else
{
return
$"[`{((isFull) ? t.FullName : t.Name)}`]({generateTypeRelativeLinkPath(t)})"
;
}
var innerFormat = string.Join(", ", t.GetGenericArguments().Select(x => BeautifyTypeWithLink(x, generateTypeRelativeLinkPath)));
return Regex.Replace(isFull ? t.GetGenericTypeDefinition().FullName : t.GetGenericTypeDefinition().Name, @"`.+$", "") + "<" + innerFormat + ">";
}
public static string ToMarkdownMethodInfo(MethodInfo methodInfo,Func<Type,string> generateRelativeLinkPath)
{
var isExtension = methodInfo.GetCustomAttributes<System.Runtime.CompilerServices.ExtensionAttribute>(false).Any();
var seq = methodInfo.GetParameters().Select(x =>
{
var suffix = x.HasDefaultValue ? (" = " + (x.DefaultValue ?? $"null")) : "";
return $"{BeautifyTypeWithLink(x.ParameterType, generateRelativeLinkPath)} " + x.Name + suffix;
});
return $"[{methodInfo.Name}]({methodInfo.DeclaringType.Name}/{methodInfo.MetadataToken}.md)" + "(" + (isExtension ? "this " : "") + string.Join(", ", seq) + ")";
}
internal static string ToMarkdownConstructorInfo(ConstructorInfo constructorInfo, Func<Type, string> generateTypeRelativeLinkPath)
{
var isExtension = constructorInfo.GetCustomAttributes<System.Runtime.CompilerServices.ExtensionAttribute>(false).Any();
var seq = constructorInfo.GetParameters().Select(x =>
{
var suffix = x.HasDefaultValue ? (" = " + (x.DefaultValue ?? $"null")) : "";
return $"{BeautifyTypeWithLink(x.ParameterType, generateTypeRelativeLinkPath)} " + x.Name + suffix;
});
return $"{constructorInfo.Name}" + "(" + (isExtension ? "this " : "") + string.Join(", ", seq) + ")";
}
}
}