-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
executable file
·94 lines (72 loc) · 3.2 KB
/
Extensions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2010 Henry A Schimke.
// See LICENSE for details.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace HAS.Util
{
public static class Extensions
{
#region Required Local Functions
private const string template_leadin = "{{";
private const string template_leadout = "}}";
private static StringBuilder SetApplyTemplate(StringBuilder target, string template, string value, string prefix)
{
target.Replace(prefix + template_leadin + template + template_leadout, value);
return target;
}
#endregion
#region Extension Methods
public static string ApplyTemplate(this String str, string template_name, string template_value, string template_prefix)
{
StringBuilder sb = new StringBuilder(str);
SetApplyTemplate(sb, template_name, template_value, template_prefix);
return sb.ToString();
}
public static string ApplyTemplate(this String str, Dictionary<string, object> template_applicables, string template_prefix)
{
StringBuilder sb = new StringBuilder(str);
foreach (var item in template_applicables)
{
SetApplyTemplate(sb, item.Key, System.Convert.ToString(item.Value), template_prefix);
}
return sb.ToString();
}
public static string ApplyTemplate(this String str, object template_applicables, string template_prefix)
{
System.Reflection.PropertyInfo[] prop_list = template_applicables.GetType().GetProperties();
StringBuilder sb = new StringBuilder(str);
foreach (var prop in prop_list)
{
SetApplyTemplate(sb, prop.Name, System.Convert.ToString(prop.GetValue(template_applicables, null)), template_prefix);
}
return sb.ToString();
}
public static string ApplyTemplate(this String str, string template_name, string template_value)
{
return ApplyTemplate(str, template_name, template_value, String.Empty);
}
public static string ApplyTemplate(this String str, Dictionary<string, object> template_applicables)
{
return ApplyTemplate(str, template_applicables, String.Empty);
}
public static string ApplyTemplate(this String str, object template_applicables)
{
return ApplyTemplate(str, template_applicables, String.Empty);
}
public static string StripHtml(this String str, bool aggressive)
{
string pattern = aggressive ? @"<(.|\n)*?>" : @"</?(?i:script|embed|object|frameset|frame|iframe|meta|link|style|param|comment|listing|noscript|plaintext|xmp)(.|\n)*?>";
return Regex.Replace(str, pattern, String.Empty);
}
#endregion
#region Required External Functions
public static Dictionary<string, object> CreateTemplateSet()
{
return new Dictionary<string, object>();
}
#endregion
}
}