📄 translatorflex.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ScrewTurn.Wiki.ImportWiki
{
public class TranslatorFlex : ScrewTurn.Wiki.ImportWiki.ITranslator
{
private Regex noWiki = new Regex(@"\<nowiki\>(.|\s)+?\<\/nowiki\>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private Regex noFlex = new Regex(@"\<noflex\>(.|\s)+?\<\/noflex\>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public string Translate(string input)
{
StringBuilder sb = new StringBuilder();
sb.Append(input);
List<int> noWikiBegin = new List<int>(), noWikiEnd = new List<int>(), noFlexBegin = new List<int>(), noFlexEnd = new List<int>();
Match match;
Regex doubleDoubleBrackets = new Regex(@"\""{2}.+?\""{2}");
Regex italic = new Regex(@"_.+?_");
Regex bold = new Regex(@"\*.+?\*");
Regex underline = new Regex(@"\+.+?\+");
Regex strikethrough = new Regex(@"\-.*?\-");
Regex head = new Regex(@"^!{1,6}.+", RegexOptions.Multiline);
Regex wikiTalk = new Regex(@"@@.+?@@");
Regex code = new Regex(@"@.+?@");
Regex pascalCase = new Regex(@"(\""[^""]+?\""\:)?([A-Z][a-z]+){2,}(\.([A-Z][a-z]+){2,})?");
Regex camelCase = new Regex(@"(\""[^""]+?\""\:)?(([A-Z][a-z]+){2,}\.)?\[.+?\]");
Regex exlink = new Regex(@"(\""[^""]+?\""\:)?(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*");
Regex email = new Regex(@"(\""[^""]+?\""\:)?mailto\:.+");
Regex lists = new Regex(@"^(\t|\s{8})+(\*|(1\.))",RegexOptions.Multiline);
Regex table = new Regex(@"^\|{2}", RegexOptions.Multiline);
Regex newlinespace = new Regex(@"^\ .+", RegexOptions.Multiline);
sb.Replace("\r", "");
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = doubleDoubleBrackets.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = doubleDoubleBrackets.Match(sb.ToString(), end);
else
{
string s = "<nowiki>" + match.Value.Substring(2, match.Length - 4) + "</nowiki>";
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, s);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = doubleDoubleBrackets.Match(sb.ToString(), match.Index + s.Length);
}
}
match = exlink.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = exlink.Match(sb.ToString(), end);
else
{
string s;
string[] split = match.Value.Split(new char[] { '"' });
if (split.Length == 1)
{
if (match.Value.EndsWith(".jpeg", StringComparison.CurrentCultureIgnoreCase) | match.Value.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase) | match.Value.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase))
{
string imgName = match.Value.Substring(match.Value.LastIndexOf('/') + 1, match.Length - match.Value.LastIndexOf('/') - 1);
s = "<noflex>[image|" + imgName + "|{UP}" + imgName + "]</noflex>";
}
else s = "[" + match.Value + "]";
}
else
{
if (split[1].EndsWith(".jpeg", StringComparison.CurrentCultureIgnoreCase) | split[1].EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase) | split[1].EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase))
{
string imgName = split[1].Substring(split[1].LastIndexOf('/') + 1, split[1].Length - split[1].LastIndexOf('/') - 1);
s = "<noflex>[image|" + imgName + "|{UP}" + imgName + "|" + split[2].Substring(1, split[2].Length - 1) + "]</noflex>";
}
else s = "<noflex>[" + split[2].Substring(1, split[2].Length - 1 ) + "|" + split[1] + "]</noflex>";
}
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, s);
ComputeNoFlex(sb.ToString(), ref noFlexBegin, ref noFlexEnd);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = exlink.Match(sb.ToString(), match.Index + s.Length);
}
}
match = email.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = email.Match(sb.ToString(), end);
else
{
string mailLink = "";
if (match.Value.StartsWith("mailto:")) mailLink = "[" + match.Value.Replace("mailto:", "") + "]";
else mailLink = "<noflex>[" + match.Value.Split(new char[] { ':' }, 2)[1].Replace("mailto:", "") + "|" + match.Value.Split(new char[] { ':' }, 2)[0].Substring(1, match.Value.Split(new char[] { ':' }, 2)[0].Length - 2) + "]</noflex>";
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, mailLink);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = email.Match(sb.ToString(), match.Index + mailLink.Length);
}
}
match = bold.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = bold.Match(sb.ToString(), end);
else
{
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, "'''" + match.Value.Substring(1, match.Length - 2) + @"'''");
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = bold.Match(sb.ToString());
}
}
match = underline.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = underline.Match(sb.ToString(), end);
else
{
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, "__" + match.Value.Substring(1, match.Length - 2) + @"__");
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = underline.Match(sb.ToString());
}
}
match = strikethrough.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = strikethrough.Match(sb.ToString(), end);
else
{
string s = "";
if (match.Value == "---") s = "---";
else if (match.Value == "--") s = "-";
else s = "--" + match.Value.Substring(1, match.Length - 2) + @"--";
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, s);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = strikethrough.Match(sb.ToString(), match.Index + s.Length);
}
}
match = head.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = head.Match(sb.ToString(), end);
else
{
//Count the number of ! in order to put a corresponding
//number of =
int count = 1;
for (int i = 0; i < match.Length; i++)
{
if (match.Value[i] == '!')
count++;
}
if(match.Value.EndsWith("!")) count--;
string s = "";
for (int i = 0; i < count; i++)
s += "=";
s += match.Value.Substring(count-1, match.Length - (count-1)-1);
for (int i = 0; i < count; i++)
s += "=";
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, s);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = head.Match(sb.ToString());
}
}
match = wikiTalk.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noFlexBegin, noFlexEnd, out end))
match = wikiTalk.Match(sb.ToString(), end);
else
{
string s=@"<span style=""background-color:red; color:white""><nowiki>"+ match.Value + @"</nowiki></span>";
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, s);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = wikiTalk.Match(sb.ToString(), match.Index + s.Length);
}
}
match = code.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = code.Match(sb.ToString(), end);
else
{
string s = "{{<nowiki>" + match.Value.Substring(1, match.Length - 2) + @"</nowiki>}}";
sb.Remove(match.Index, match.Length);
sb.Insert(match.Index, s);
ComputeNoWiki(sb.ToString(), ref noWikiBegin, ref noWikiEnd);
match = code.Match(sb.ToString());
}
}
match = camelCase.Match(sb.ToString());
while (match.Success)
{
int end;
if (IsNoWikied(match.Index, noWikiBegin, noWikiEnd, out end))
match = camelCase.Match(sb.ToString(), end);
else
{
string s;
string[] split = match.Value.Split(new char[] { ':' }, 2);
if (split.Length == 1)
{
string[] split1 = match.Value.Split(new char[] { '.' }, 2);
if (split1.Length == 1) s = match.Value;
else s = split1[1];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -