📄 formatter.cs
字号:
sb.Append("</a>");
}
else {
sb.Append(@"<a href=""");
sb.Append(a);
sb.Append(@"""");
if(!isImage) {
sb.Append(@" class=""externallink"" title=""");
sb.Append(n);
sb.Append(@"""");
}
sb.Append(@" target=""_blank"">");
sb.Append(n);
sb.Append("</a>");
}
}
else if(a.StartsWith(@"\\") || a.StartsWith("//")) {
// The link is a UNC path
if(n.Length == 0) {
sb.Append(@"<a class=""externallink"" href=""file://///");
sb.Append(a.Substring(2));
sb.Append(@""" title=""");
sb.Append(a);
sb.Append(@""" target=""_blank"">");
sb.Append(a);
sb.Append("</a>");
}
else {
sb.Append(@"<a href=""file://///");
sb.Append(a.Substring(2));
sb.Append(@"""");
if(!isImage) {
sb.Append(@" class=""externallink"" title=""");
sb.Append(n);
sb.Append(@"""");
}
sb.Append(@" target=""_blank"">");
sb.Append(n);
sb.Append("</a>");
}
}
else if(a.IndexOf("@") != -1 && a.IndexOf(".") != -1) {
// Email
if(n.Length == 0) {
sb.Append(@"<a class=""emaillink""");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""mailto:");
sb.Append(a);
sb.Append(@""" title=""");
sb.Append(a);
sb.Append(@""">");
sb.Append(a);
sb.Append("</a>");
}
else {
sb.Append(@"<a");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""mailto:");
sb.Append(a);
sb.Append(@"""");
if(!isImage) {
sb.Append(@" class=""emaillink"" title=""");
sb.Append(n);
sb.Append(@"""");
}
sb.Append(@">");
sb.Append(n);
sb.Append("</a>");
}
}
else if(((a.IndexOf(".") != -1 && !a.ToLower().EndsWith(".aspx")) || a.EndsWith("/")) && Pages.Instance.FindPage(a) == null) {
// Link to an internal file or subdirectory
if(n.Length == 0) {
sb.Append(@"<a class=""internallink""");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@""" title=""");
sb.Append(a);
sb.Append(@""">");
sb.Append(a);
sb.Append("</a>");
}
else {
sb.Append(@"<a");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@"""");
if(!isImage) {
sb.Append(@" class=""emaillink"" title=""");
sb.Append(n);
sb.Append(@"""");
}
sb.Append(@">");
sb.Append(n);
sb.Append("</a>");
}
}
else {
if(a.IndexOf(".aspx") != -1) {
// The link points to a "system" page
if(n.Length == 0) {
sb.Append(@"<a class=""systemlink""");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@""" title=""");
sb.Append(a);
sb.Append(@""">");
sb.Append(a);
sb.Append("</a>");
}
else {
sb.Append(@"<a");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@"""");
if(!isImage) {
sb.Append(@" class=""systemlink"" title=""");
sb.Append(n);
sb.Append(@"""");
}
sb.Append(@">");
sb.Append(n);
sb.Append("</a>");
}
}
else {
if(a.StartsWith("c:") || a.StartsWith("C:")) {
if(n.Length == 0) {
sb.Append(@"<a href=""AllPages.aspx?Cat=");
sb.Append(Tools.UrlEncode(a.Substring(2)));
sb.Append(@""">");
sb.Append(a.Substring(2));
sb.Append("</a>");
}
else {
sb.Append(@"<a href=""AllPages.aspx?Cat=");
sb.Append(Tools.UrlEncode(a.Substring(2)));
sb.Append(@""">");
sb.Append(n);
sb.Append("</a>");
}
}
else if(a.Contains(":") || a.ToLower().Contains("%3a")) {
sb.Append(@"<b style=""color: #FF0000;"">FORMATTER ERROR ("":"" not supported in Page Names)</b>");
}
else {
// The link points to a wiki page
PageInfo info;
if(n.Length == 0) {
string tempLink = a;
if(a.IndexOf("#") != -1) {
tempLink = a.Substring(0, a.IndexOf("#"));
a = Tools.UrlEncode(a.Substring(0, a.IndexOf("#"))) + Settings.PageExtension + a.Substring(a.IndexOf("#"));
}
else {
a += Settings.PageExtension;
a = Tools.UrlEncode(a);
}
info = Pages.Instance.FindPage(tempLink);
if(info == null) {
sb.Append(@"<a class=""unknownlink""");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@""" title=""");
sb.Append(tempLink);
sb.Append(@""">");
sb.Append(tempLink);
sb.Append("</a>");
}
else {
if(!lp.Contains(info)) {
lp.Add(info);
}
sb.Append(@"<a class=""pagelink""");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@""" title=""");
sb.Append(Content.GetPageContent(info, false).Title);
sb.Append(@""">");
sb.Append(tempLink);
sb.Append("</a>");
}
}
else {
string tempLink = a;
if(a.IndexOf("#") != -1) {
tempLink = a.Substring(0, a.IndexOf("#"));
a = Tools.UrlEncode(a.Substring(0, a.IndexOf("#"))) + Settings.PageExtension + a.Substring(a.IndexOf("#"));
}
else {
a += Settings.PageExtension;
a = Tools.UrlEncode(a);
}
info = Pages.Instance.FindPage(tempLink);
if(info == null) {
sb.Append(@"<a");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@"""");
if(!isImage) {
sb.Append(@" class=""unknownlink"" title=""");
sb.Append(n);
sb.Append(@"""");
}
sb.Append(@">");
sb.Append(n);
sb.Append("</a>");
}
else {
if(!lp.Contains(info)) {
lp.Add(info);
}
sb.Append(@"<a");
if(blank) sb.Append(@" target=""_blank""");
sb.Append(@" href=""");
sb.Append(a);
sb.Append(@"""");
if(!isImage) {
sb.Append(@" class=""pagelink"" title=""");
sb.Append(Content.GetPageContent(info, false).Title);
sb.Append(@"""");
}
sb.Append(@">");
sb.Append(n);
sb.Append("</a>");
}
}
}
}
}
return sb.ToString();
}
/// <summary>
/// Detects all the Headers in a block of text (H1, H2, H3, H4).
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The List of Header objects, in the same order as they are in the text.</returns>
public static List<HPosition> DetectHeaders(string text) {
Match match;
string h;
int end = 0;
List<int> noWikiBegin = new List<int>(), noWikiEnd = new List<int>();
List<HPosition> hPos = new List<HPosition>();
StringBuilder sb = new StringBuilder(text);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
int count = 0;
match = h4.Match(sb.ToString());
while(match.Success) {
if(!IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end)) {
h = match.Value.Substring(5, match.Value.Length - 10);
hPos.Add(new HPosition(match.Index, h, 4, count));
end = match.Index + match.Length;
count++;
}
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = h4.Match(sb.ToString(), end);
}
match = h3.Match(sb.ToString());
while(match.Success) {
if(!IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end)) {
h = match.Value.Substring(4, match.Value.Length - 8);
bool found = false;
for(int i = 0; i < hPos.Count; i++) {
if(match.Index == hPos[i].Index) {
found = true;
break;
}
}
if(!found) {
hPos.Add(new HPosition(match.Index, h, 3, count));
count++;
}
end = match.Index + match.Length;
}
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = h3.Match(sb.ToString(), end);
}
match = h2.Match(sb.ToString());
while(match.Success) {
if(!IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end)) {
h = match.Value.Substring(3, match.Value.Length - 6);
bool found = false;
for(int i = 0; i < hPos.Count; i++) {
if(match.Index == hPos[i].Index) {
found = true;
break;
}
}
if(!found) {
hPos.Add(new HPosition(match.Index, h, 2, count));
count++;
}
end = match.Index + match.Length;
}
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = h2.Match(sb.ToString(), end);
}
match = h1.Match(sb.ToString());
while(match.Success) {
if(!IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end)) {
h = match.Value.Substring(2, match.Value.Length - 4);
bool found = false;
for(int i = 0; i < hPos.Count; i++) {
// A special treatment is needed in this case
// because =====xxx===== matches also 2 H1 headers (=='='==)
if(match.Index >= hPos[i].Index && match.Index <= hPos[i].Index + hPos[i].Text.Length + 5) {
found = true;
break;
}
}
if(!found) {
hPos.Add(new HPosition(match.Index, h, 1, count));
count++;
}
end = match.Index + match.Length;
}
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = h1.Match(sb.ToString(), end);
}
return hPos;
}
private static string BuildEditSectionLink(int id, string page) {
StringBuilder sb = new StringBuilder();
sb.Append(@"<a href=""Edit.aspx?Page=");
sb.Append(Tools.UrlEncode(page));
sb.Append("&Section=");
sb.Append(id.ToString());
sb.Append(@""" class=""editsectionlink"">");
sb.Append(editSectionPlaceHolder);
sb.Append("</a>");
return sb.ToString();
}
private static string GenerateList(string[] lines, int line, int level, ref int currLine) {
StringBuilder sb = new StringBuilder();
if(lines[currLine][level] == '*') sb.Append("<ul>");
else if(lines[currLine][level] == '#') sb.Append("<ol>");
while(currLine <= lines.Length - 1 && CountBullets(lines[currLine]) >= level + 1) {
if(CountBullets(lines[currLine]) == level + 1) {
sb.Append("<li>");
sb.Append(lines[currLine].Substring(CountBullets(lines[currLine])).Trim());
sb.Append("</li>");
currLine++;
}
else {
sb.Remove(sb.Length - 5, 5);
sb.Append(GenerateList(lines, currLine, level + 1, ref currLine));
sb.Append("</li>");
}
}
if(lines[line][level] == '*') sb.Append("</ul>");
else if(lines[line][level] == '#') sb.Append("</ol>");
return sb.ToString();
}
private static int CountBullets(string line) {
int res = 0, count = 0;
while(line[count] == '*' || line[count] == '#') {
res++;
count++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -