-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEx.cs
More file actions
83 lines (71 loc) · 2.82 KB
/
StringEx.cs
File metadata and controls
83 lines (71 loc) · 2.82 KB
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
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
namespace XmlDocToSlateMD
{
public static class StringEx
{
static Dictionary<string, string> AngleBracketsToHTMLSafe = new Dictionary<string, string>
{
{"<", "<"},
{">", ">"}
};
static Dictionary<string, string> BracketsToHTMLSafe = new Dictionary<string, string>
{
{"(", "("},
{")", ")"}
};
static Dictionary<string, string> SquareBracketsToHTMLSafe = new Dictionary<string, string>
{
{"[", "["},
{"]", "]"}
};
public static string GetMemberName(this string methodOrPropOrFieldName)
{
if (!methodOrPropOrFieldName.Contains("("))
return methodOrPropOrFieldName.Substring(methodOrPropOrFieldName.LastIndexOf('.') + 1);
return Regex.Match(methodOrPropOrFieldName, "\\.([#A-z]*)\\(").Groups[1].Captures.Join();
}
public static string GetTypeName(this string methodOrPropOrFieldName)
{
if (!methodOrPropOrFieldName.Contains("("))
return methodOrPropOrFieldName.Substring(2, methodOrPropOrFieldName.LastIndexOf(".") - 2);
string typename = methodOrPropOrFieldName.Substring(0, methodOrPropOrFieldName.IndexOf("("));
return typename.Substring(2, typename.LastIndexOf('.') - 2);
}
public static string HtmlSafe(this string self)
{
return self.HtmlSafeBrakets().HtmlSafeAngleBrakets().HtmlSafeSquareBrakets();
}
public static string HtmlSafeAngleBrakets(this string self)
{
foreach (KeyValuePair<string, string> kvp in AngleBracketsToHTMLSafe)
self = self.Replace(kvp.Key, kvp.Value);
return self;
}
public static string HtmlSafeBrakets(this string self)
{
foreach (KeyValuePair<string, string> kvp in BracketsToHTMLSafe)
self = self.Replace(kvp.Key, kvp.Value);
return self;
}
public static string HtmlSafeSquareBrakets(this string self)
{
foreach (KeyValuePair<string, string> kvp in SquareBracketsToHTMLSafe)
self = self.Replace(kvp.Key, kvp.Value);
return self;
}
public static Stream ToStream(this string self, Encoding encoding = null)
{
return new MemoryStream((encoding ?? Encoding.UTF8).GetBytes(self ?? ""));
}
public static string WrapInHref(this string self)
{
var linkedname = self.Replace('.', '-').ToLowerInvariant();
return $"<a href=\"#{linkedname}\">{self.GetMemberName()}</a>";
}
}
}