forked from sll552/DiscordBee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayoutHandler.cs
80 lines (72 loc) · 2.85 KB
/
LayoutHandler.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
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace MusicBeePlugin
{
class LayoutHandler
{
private readonly Regex _layoutElementRegex;
/// <summary>
/// Create a LayoutHandler instance
/// </summary>
/// <param name="layoutElementRegex">The Regex used to determine layout elemets that will be replaced, the actual element name must be a subgroup e.g. '{(.+?)}'</param>
public LayoutHandler(Regex layoutElementRegex)
{
_layoutElementRegex = layoutElementRegex ?? throw new ArgumentNullException(nameof(layoutElementRegex));
}
private string Clean(string input, Dictionary<string, string> values, string seperators)
{
var emptyValues = new Dictionary<string, string>();
foreach (var entry in values)
{
if (string.IsNullOrWhiteSpace(entry.Value))
{
emptyValues.Add(entry.Key, entry.Value);
}
}
input = Replace(input, emptyValues);
// remove excess whitespace
input = Regex.Replace(input, "\\s+", " ");
if (string.IsNullOrEmpty(seperators)) return input;
foreach (var sep in seperators)
{
var escsep = Regex.Escape(sep.ToString());
// remove double seperators
input = Regex.Replace(input, "[" + escsep + "]{2,}", sep.ToString());
// remove seperator with whitespace in between
input = Regex.Replace(input, "[" + escsep + "]\\s+[" + escsep + "]", sep.ToString());
// remove seperators and whitespace at the beginning
input = Regex.Replace(input, "^\\s*[" + escsep + "]*\\s*", "");
// remove seperators and whitespace at the end
input = Regex.Replace(input, "\\s*[" + escsep + "]+\\s*$", "");
}
return input;
}
private string Replace(string input, Dictionary<string, string> values)
{
var matches = _layoutElementRegex.Matches(input);
foreach (Match match in matches)
{
// complete match is group 0 so we neeed exactly 2 groups to be able to replace correctly
if (match.Groups.Count != 2) continue;
var key = match.Groups[1].Captures[0].Value;
if (values.ContainsKey(key))
{
input = input.Replace(match.Value, values[key]);
}
}
return input;
}
/// <summary>
/// Render the layout elements in the given string using the values from the provided dictionary
/// </summary>
/// <param name="layoutStr">String to render</param>
/// <param name="values">Value disctionary to use</param>
/// <param name="seperators">The seperators used in this string, these will be used as character class in Regex</param>
/// <returns></returns>
public string Render(string layoutStr, Dictionary<string, string> values, string seperators)
{
return Replace(Clean(layoutStr, values, seperators), values);
}
}
}