Repository navigation
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
215 lines (172 loc) · 5.94 KB
/
Copy pathStringExtensions.cs
File metadata and controls
215 lines (172 loc) · 5.94 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
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
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Markdig;
namespace Blogifier.Core.Extensions
{
public static class StringExtensions
{
public static string ToThumb(this string img)
{
if (img.IndexOf('/') < 1) return img;
var first = img.Substring(0, img.LastIndexOf('/'));
var second = img.Substring(img.LastIndexOf('/'));
return $"{first}/thumbs{second}";
}
public static string Capitalize(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
char[] a = str.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
public static string MdToHtml(this string str)
{
var mpl = new MarkdownPipelineBuilder()
.UsePipeTables()
.UseAdvancedExtensions()
.Build();
return Markdown.ToHtml(str, mpl);
}
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source.IndexOf(toCheck, comp) >= 0;
}
private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled);
public static string StripHtml(this string str)
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : RegexStripHtml.Replace(str, string.Empty).Trim();
}
/// <summary>
/// Should extract title (file name) from file path or Url
/// </summary>
/// <param name="str">c:\foo\test.png</param>
/// <returns>test.png</returns>
public static string ExtractTitle(this string str)
{
if (str.Contains("\\"))
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : str.Substring(str.LastIndexOf("\\")).Replace("\\", "");
}
else if (str.Contains("/"))
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : str.Substring(str.LastIndexOf("/")).Replace("/", "");
}
else
{
return str;
}
}
/// <summary>
/// Converts title to valid URL slug
/// </summary>
/// <returns>Slug</returns>
public static string ToSlug(this string title)
{
var str = title.ToLowerInvariant();
str = str.Trim('-', '_');
if (string.IsNullOrEmpty(str))
return string.Empty;
var bytes = Encoding.GetEncoding("utf-8").GetBytes(str);
str = Encoding.UTF8.GetString(bytes);
str = Regex.Replace(str, @"\s", "-", RegexOptions.Compiled);
str = Regex.Replace(str, @"([-_]){2,}", "$1", RegexOptions.Compiled);
str = RemoveIllegalCharacters(str);
return str;
}
public static string Hash(this string source, string salt)
{
var bytes = KeyDerivation.Pbkdf2(
password: source,
salt: Encoding.UTF8.GetBytes(salt),
prf: KeyDerivationPrf.HMACSHA512,
iterationCount: 10000,
numBytesRequested: 256 / 8);
return Convert.ToBase64String(bytes);
}
public static string ReplaceIgnoreCase(this string str, string search, string replacement)
{
string result = Regex.Replace(
str,
Regex.Escape(search),
replacement.Replace("$", "$$"),
RegexOptions.IgnoreCase
);
return result;
}
#region Helper Methods
static string RemoveIllegalCharacters(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
string[] chars = new string[] {
":", "/", "?", "!", "#", "[", "]", "{", "}", "@", "*", ".", ",",
"\"","&", "'", "~", "$"
};
foreach (var ch in chars)
{
text = text.Replace(ch, string.Empty);
}
text = text.Replace("–", "-");
text = text.Replace(" ", "-");
text = RemoveUnicodePunctuation(text);
text = RemoveDiacritics(text);
text = RemoveExtraHyphen(text);
return System.Web.HttpUtility.HtmlEncode(text).Replace("%", string.Empty);
}
static string RemoveUnicodePunctuation(string text)
{
var normalized = text.Normalize(NormalizationForm.FormD);
var sb = new StringBuilder();
foreach (var c in
normalized.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.InitialQuotePunctuation &&
CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.FinalQuotePunctuation))
{
sb.Append(c);
}
return sb.ToString();
}
static string RemoveDiacritics(string text)
{
var normalized = text.Normalize(NormalizationForm.FormD);
var sb = new StringBuilder();
foreach (var c in
normalized.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark))
{
sb.Append(c);
}
return sb.ToString();
}
static string RemoveExtraHyphen(string text)
{
if (text.Contains("--"))
{
text = text.Replace("--", "-");
return RemoveExtraHyphen(text);
}
return text;
}
public static string SanitizePath(this string str)
{
if (string.IsNullOrWhiteSpace(str))
return string.Empty;
str = str.Replace("%2E", ".").Replace("%2F", "/");
if (str.Contains("..") || str.Contains("//"))
throw new ApplicationException("Invalid directory path");
return str;
}
public static string SanitizeFileName(this string str)
{
str = str.SanitizePath();
//TODO: add filename specific validation here
return str;
}
#endregion
}
}