-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTechExtensions.cs
More file actions
89 lines (77 loc) · 3.04 KB
/
Copy pathTechExtensions.cs
File metadata and controls
89 lines (77 loc) · 3.04 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
using System;
using System.Data;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Linq;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.Logging;
using ServiceStack.OrmLite;
using TechStacks.ServiceModel;
using TechStacks.ServiceModel.Types;
namespace TechStacks.ServiceInterface;
public static class TechExtensions
{
private static ILog Log = LogManager.GetLogger(typeof(TechExtensions));
public static TechnologyInStack ToTechnologyInStack(this TechnologyChoice technologyChoice)
{
var result = technologyChoice.ConvertTo<TechnologyInStack>();
result.PopulateWith(technologyChoice.Technology);
result.Id = technologyChoice.Id;
return result;
}
private static readonly Regex InvalidCharsRegex = new Regex(@"[^a-z0-9\s-]", RegexOptions.Compiled);
private static readonly Regex CollapseSpacesRegex = new Regex(@"\s+", RegexOptions.Compiled);
private static readonly Regex SpacesRegex = new Regex(@"\s", RegexOptions.Compiled);
private static readonly Regex CollapseHyphensRegex = new Regex("-+", RegexOptions.Compiled);
/// <summary>
/// From http://stackoverflow.com/a/2921135/670151
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
public static string GenerateSlug(this string phrase)
{
var str = phrase.RemoveAccent().ToLower()
.Replace("#", "sharp") // c#, f# => csharp, fsharp
.Replace("++", "pp"); // c++ => cpp
str = InvalidCharsRegex.Replace(str, "-");
//// convert multiple spaces into one space
//str = CollapseSpacesRegex.Replace(str, " ").Trim();
// cut and trim
str = str.Substring(0, str.Length <= 100 ? str.Length : 100).Trim();
str = SpacesRegex.Replace(str, "-");
str = CollapseHyphensRegex.Replace(str, "-");
if (string.IsNullOrEmpty(str))
return null;
if (str[0] == '-')
str = str.Substring(1);
if (str[str.Length - 1] == '-')
str = str.Substring(0, str.Length - 1);
return str;
}
public static string RemoveAccent(this string txt)
{
// https://github.com/dotnet/corefx/issues/9158
// byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
// return System.Text.Encoding.ASCII.GetString(bytes);
return txt;
}
public static async Task<long> RegisterPageViewAsync(this IDbConnection db, string id)
{
var rows = await db.ExecuteSqlAsync("UPDATE page_stats SET view_count = view_count + 1 WHERE id = @id", new { id });
var parts = id.Substring(1).SplitOnFirst('/');
if (rows == 0 && parts.Length == 2)
{
var type = parts[0];
var slug = parts[1];
return await db.InsertAsync(new PageStats {
Id = id,
RefType = type,
RefSlug = slug,
ViewCount = 1,
LastModified = DateTime.UtcNow,
});
}
return 0;
}
}