-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilities.cs
121 lines (106 loc) · 2.97 KB
/
Utilities.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Personal_Genome_Explorer
{
delegate bool CancelDelegate();
class Utilities
{
public static bool ArrayCompare<T>(T[] A, T[] B)
{
if (A.Length != B.Length)
{
return false;
}
for (int index = 0; index < A.Length; index++)
{
if (!A[index].Equals(B[index]))
{
return false;
}
}
return true;
}
public static string GetSingleRegexMatch(string searchString,Regex regex, string unmatchedResult)
{
var regexMatch = regex.Match(searchString);
if (regexMatch.Success)
{
return regexMatch.Groups[1].Value;
}
else
{
return unmatchedResult;
}
}
public delegate string ProcessDelimitedItemDelegate(string itemText);
public static string ProcessDelimitedItems(string text, string beginningDelimiter, string endDelimiter, ProcessDelimitedItemDelegate processDelimitedItemDelegate)
{
while (true)
{
// Find the next delimited item in the text.
int itemBeginIndex = text.IndexOf(beginningDelimiter);
if (itemBeginIndex == -1)
{
break;
}
int itemEndIndex = text.IndexOf(endDelimiter, itemBeginIndex + beginningDelimiter.Length);
if (itemEndIndex == -1)
{
break;
}
// Process the item.
int itemTextBeginIndex = itemBeginIndex + beginningDelimiter.Length;
string itemText = text.Substring(itemTextBeginIndex, itemEndIndex - itemTextBeginIndex);
string replacedItemText = processDelimitedItemDelegate(itemText);
// Replace the item with the result of processDelimitedItemDelegate.
text = text.Substring(0, itemBeginIndex) + replacedItemText + text.Substring(itemEndIndex + endDelimiter.Length);
};
return text;
}
public static string ConvertWikiTextToPlainText(string wikiText)
{
var result = wikiText;
// Strip out the category text.
result = Utilities.ProcessDelimitedItems(result, "{{", "}}", delegate(string itemText)
{
return "";
});
// Replace internal links and images.
result = Utilities.ProcessDelimitedItems(result, "[[", "]]", delegate(string itemText)
{
if (itemText.ToLowerInvariant().StartsWith("image"))
{
// Strip out images.
return "";
}
else
{
// Replace links with their friendly text.
string friendlyText = itemText;
int friendlySplitIndex = itemText.IndexOf('|');
if (friendlySplitIndex != -1)
{
friendlyText = itemText.Substring(friendlySplitIndex + 1);
}
return friendlyText;
}
});
// Replace external links.
result = Utilities.ProcessDelimitedItems(result, "[", "]", delegate(string itemText)
{
return itemText;
});
// Coalesce multi-line breaks into single-line breaks.
while (result.Contains("\n\n"))
{
result = result.Replace("\n\n", "\n");
};
// Replace single-line breaks with double-line breaks.
result = result.Replace("\n", "\n\n");
return result;
}
};
}