-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHtmlTools.cs
242 lines (211 loc) · 9.78 KB
/
HtmlTools.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace WinCachebox
{
public class HtmlTools : IDisposable
{
public HtmlTools()
{
}
public void Dispose()
{
TextBlocks.Clear();
templates.Clear();
}
/// <summary>
/// Liste mit Platzhaltern und dem Text, durch den sie ersetzt werden sollen
/// </summary>
public Dictionary<String, String> TextBlocks = new Dictionary<string, string>();
Dictionary<String, String> templates = new Dictionary<string, string>();
/// <summary>
/// Liefert das durch template angegebene Template und ersetzt
/// alle Vorkommnisse eines Schlüssels durch seinen Wert. Achtung:
/// Ersetzt man einen Schlüssel durch sich selbst, so kann es hier
/// zu Deadlocks kommen!
/// </summary>
/// <param name="template">Name des Templates</param>
/// <returns>Das ersetzte Template</returns>
public String ApplyTemplate(String template)
{
System.Diagnostics.Debug.Assert(templates.ContainsKey(template), "Unbekanntes Template " + template + "!");
String result = templates[template];
foreach (String key in TextBlocks.Keys)
{
//int index;
//while ((index = result.IndexOf(key)) >= 0)
//{
// String left = result.Substring(0, index);
// String right = result.Substring(index + key.Length);
// result = left + TextBlocks[key] + right;
//}
//this takes half the time of the solution above
result = result.Replace(key, TextBlocks[key]);
}
return result;
}
/// <summary>
/// Liest eine Template-Datei ein
/// </summary>
/// <param name="filename">Dateiname des Templates</param>
public void ReadTemplate(String ressourceName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string strRes = "Cachebox." + ressourceName;
StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(strRes));
String curTemplate = "Header";
String line;
while ((line = reader.ReadLine()) != null)
{
// Handelt es sich um einen HTML-Kommentar? Wenn ja,
// neuen Template-Namen übernehmen
if (line.StartsWith("<!-- ") && line.EndsWith(" -->"))
{
curTemplate = line.Substring(5, line.Length - 9);
continue;
}
if (templates.ContainsKey(curTemplate))
templates[curTemplate] += line + "\n";
else
templates.Add(curTemplate, line + "\n");
}
reader.Close();
}
/// <summary>
/// Wandelt in-place bbcode in html um
/// </summary>
/// <param name="html">der zu konvertierende String</param>
public static void BBCodeToHtml(ref String html)
{
html = html.Replace("[b]", "<b> ");
html = html.Replace("[/b]", "</b> ");
html = html.Replace("[i]", "<i> ");
html = html.Replace("[/i]", "</i> ");
html = html.Replace("[u]", "<u> ");
html = html.Replace("[/u]", "</u> ");
}
/// <summary>
/// Wandelt html in text um
/// </summary>
/// <param name="html">der zu konvertierende String</param>
public static string StripHTML(string html)
{
try
{
string result;
// Remove HTML Development formatting
result = html.Replace("\r", string.Empty);
result = result.Replace("\n", string.Empty);
result = result.Replace("\t", string.Empty);
result = System.Text.RegularExpressions.Regex.Replace(result, @"[ ]+", " ");
// Remove the header (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*head[^>]*>", "<head>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*[/][ ]*head[ ]*>", "</head>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
"<head>.*</head>", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// remove all scripts (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*script[^>]*>", "<script>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*[/][ ]*script[ ]*>", "</script>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<script>.*?</script>", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// remove all styles (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*style[^>]*>", "<style>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*[/][ ]*style[ ]*>", "</style>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
"<style>.*?</style>", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// insert tabs in spaces of <td> tags
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*td[^>]*>", "\t",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// insert line breaks in places of <BR> and <LI> tags
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*br[ ]*[/]*>", "\r\n",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*li[ ]*[/]*>", "\r\n * ",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// insert line paragraphs (double line breaks) in place
// if <P>, <DIV> <TR> <H1> tags
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*div[^>]*>", "\r\n\r\n",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*tr[^>]*>", "\r\n\r\n",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*p[^>]*>", "\r\n\r\n",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*h[0-9][^>]*>", "\r\n\r\n",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*/h[0-9][^>]*>", "\r\n\r\n",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// replace horizontal ruler with some dashes
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[ ]*hr[ ]*[/]*>", "\r\n----------\r\n",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// Remove remaining tags like <a>, links, images,
// comments etc - anything that's enclosed inside < >
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[^>]*>", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// replace special characters:
result = System.Text.RegularExpressions.Regex.Replace(result,
@" ", " ",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"•", " * ",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"‹", "<",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"›", ">",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"™", "(tm)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"⁄", "/",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<", "<",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@">", ">",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"©", "(c)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"®", "(r)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"&(.{2,6});", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return result.Trim();
}
catch
{
return html;
}
}
}
}