📄 transforms.cs
字号:
"For", "End If", "If", "Else", "ElseIf", "Next", "While", "And",
"Do", "Loop", "Dim", "As", "End Select", "Select", "Case", "Or",
"Imports", "Then", "Integer", "Long", "String", "Overloads", "True",
"Overrides", "End Property", "End Sub", "End Function", "Sub", "Me",
"Function", "End Get", "End Set", "Get", "Friend", "Inherits",
"Implements","Return", "Not", "New", "Shared", "Nothing", "Finally",
"False", "Me", "My", "MyBase" };
String CombinedKeywords = "(?<keyword>" + String.Join("|", keywords) + ")";
source = Regex.Replace(source, "(?i)\\b" + CombinedKeywords + "\\b(?<!'.*)", TAG_FNTBLUE + "${keyword}" + TAG_EFONT);
source = Regex.Replace(source, "(?<comment>'(?![^']*").*$)", TAG_FNTGRN + "${comment}" + TAG_EFONT);
return source;
*/
}
// *********************************************************************
// ToDelimitedString
//
/// <summary>
/// Private helper function to convert a collection to delimited string array
/// </summary>
///
// ********************************************************************/
public static string ToDelimitedString(ICollection collection, string delimiter) {
StringBuilder delimitedString = new StringBuilder();
// Hashtable is perfomed on Keys
//
if (collection is Hashtable) {
foreach (object o in ((Hashtable) collection).Keys) {
delimitedString.Append( o.ToString() + delimiter);
}
}
// ArrayList is performed on contained item
//
if (collection is ArrayList) {
foreach (object o in (ArrayList) collection) {
delimitedString.Append( o.ToString() + delimiter);
}
}
// String Array is performed on value
//
if (collection is String[]) {
foreach (string s in (String[]) collection) {
delimitedString.Append( s + delimiter);
}
}
return delimitedString.ToString().TrimEnd(Convert.ToChar(delimiter));
}
#endregion
#region HtmlToBBCode
/// <summary>
/// This method will transform a HTML tagged string into a bbcoded string. We first convert all HTML tags to bbcode prior
/// to performing our bbcode translations to strip out any dangerous attributes.
/// </summary>
/// <param name="bbCodedString"></param>
/// <returns></returns>
static string HtmlToBBCode( string htmlTaggedString ) {
return Regex.Replace( htmlTaggedString, @"(?'openingTag'<)(.*?)(?>(?!=[\/\?]?>)(?'closingSlash'\/\?)?(?'closingTag'>))", @"[$1${closingSlash}]", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace );
}
#endregion
#region EncodeCodeBlocks
public static string EncodeCodeBlocks( string taggedString ) {
StringBuilder sb = new StringBuilder();
string output = "";
bool encodeThisBlock = false;
// string[] blocks = Regex.Split(taggedString, @"\[(?:code|pre).*\].*\[\/(?:code|pre).*\]", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace );
string[] blocks = Regex.Split(taggedString, @"(\[(?:\/)?code\s*(?:language\s*=\s*"".*"")?\s*\])", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace );
string language = "";
foreach( string block in blocks ) {
output = block;
if( encodeThisBlock ) {
// temporary fix for FTB putting in </pre><pre class=source> tags in place of two \n\n
output = Regex.Replace(output, @"\<\/pre\s*?\>\<pre\s+class=""?source""?\s*?\>", @"<br/><br/>", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
output = HttpUtility.HtmlEncode( Regex.Unescape(output) );//Regex.Replace( block, @"(?'openingTag'<)(.*?)(?>(?!=[\/\?]?>)(?'closingSlash'\/\?)?(?'closingTag'>))", @"<$1${closingSlash}]", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace );
output = Regex.Replace(output, @"(\<|\<)br?(\s*|(\ )*)(?:\/)?(\>|\>)", "<br/>", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
output = FormatSource( output, GetLanguageType( language ) );
encodeThisBlock = false;
}
// dont write out the code blocks
if( block.StartsWith("[code") ) {
encodeThisBlock = true;
language = "CS";
}
else if( block.StartsWith("[/code") ) {
// skip this block
encodeThisBlock = false;
language = "";
}
else {
sb.Append( output );
}
}
return sb.ToString();
}
#endregion
#region ScrubHtml
/// <summary>
///
/// </summary>
/// <param name="encodedString"></param>
/// <param name="postType"></param>
/// <returns></returns>
static string ScrubHtml( string encodedString, PostType postType ) {
System.Collections.Hashtable ht = GetAllowedTags();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
encodedString = Regex.Replace(encodedString, @"(<!--)", @"<!--", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace );
encodedString = Regex.Replace(encodedString, @"(-->)", @"-->", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace );
// string[] blocks = Regex.Split(encodedString, @"((<(?:\/)?\w+(?:\s+\w+(?:\s*=\s*""?\w+""?)?)*\s*(?:\/)?>)|(<\!--)|(-->))", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace );
// string[] blocks = Regex.Split(encodedString, @"\b(<(?:\/)?\w+(?:\s+\w+(?:\s*=\s*""?(?:[\w-_:#\'\s%]*|(?:(?:http|ftp|https):\/\/)?[\w-_]+(?:\.[\w-_]+)+(?:[\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)""?)?)*\s*(?:\/)?>)", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace );
// string[] blocks = Regex.Split(encodedString, @"(<(?:\/)?\w+\s*(?:\w+)*\s*(?:\/)?>)", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace );
string[] blocks = Regex.Split(encodedString, @"\b<(\/?\w*)(\s*(\w*)(?:\s*=\s*(?:""[^""]*""|'[^']'|[^>]*))?)*\/?>\b", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace );
string output = "";
int keyStart = 1;
foreach( string block in blocks ) {
output = block;
if( output.StartsWith("<") ) {
if( output[1] == '/' )
keyStart = 2;
else
keyStart = 1;
int keyEnd = output.IndexOfAny(new char[]{' ', '/', '>'}, keyStart) - keyStart;
string key = output.Substring(keyStart, keyEnd).ToLower();
StringBuilder sbAttr = new StringBuilder();
if( ht.ContainsKey( key )) {
string slashClosing = output.EndsWith("/>") ? "/" : "";
string slashOpening = output.StartsWith("</") ? "/" : "";
string attributes = (string)ht[key];
if( attributes != null && attributes.Length > 0 ) {
string[] allowedAttr = ((string)ht[key]).Split(',');
//foreach( Match match in Regex.Matches(output, String.Format(@"<{0}((?:\s+(\w+)(?:\s*=""?\w+""?)?)*)?\s*[\/\?]?>", key), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace )) {
//foreach( Match match in Regex.Matches(output, String.Format(@"<{0}((?:\s+(\w+)(?:\s*=\s*""?(?:\w*|(?:(?:http|ftp|https):\/\/)?[\w-_]+(?:\.[\w-_]+)+(?:[\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)""?)?)*)\s*[\/\?]?>", key), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace )) {
string attrPattern = String.Format(@"<{0}(\s*(\w*)(?:\s*=\s*(?:""[^""]*""|'[^']'|[^>]*))?)*\/?>", key);
MatchCollection matches = Regex.Matches(output, attrPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace );
foreach( Match match in matches ) {
foreach( string attr in allowedAttr ) {
if( match.Groups[2].Captures[0].Value == attr ) {
sbAttr.Append( match.Groups[1].Captures[0].Value );
}
}
}
}
sb.AppendFormat("<{0}{1}{2}{3}>", slashOpening, key, sbAttr.ToString(), slashClosing );//Regex.Replace( output, (string)ht[key], "<$1 $2>$3</$1>", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace ) );
}
else {
sb.Append( HttpUtility.HtmlEncode( output ) );
}
}
else {
sb.Append( output );
}
}
return sb.ToString();
}
static System.Collections.Hashtable GetAllowedTags() {
if( CSCache.Get("allowedTags") == null ) {
System.Collections.Hashtable ht = new System.Collections.Hashtable();
// TDD TODO eventually move these out to an external file
ht.Add("h1", "align" );
ht.Add("h2", "align" );
ht.Add("h3", "align" );
ht.Add("h4", "align" );
ht.Add("h5", "align" );
ht.Add("h6", "align" );
ht.Add("strong", "" );
ht.Add("em", "" );
ht.Add("u", "");
ht.Add("b", "");
ht.Add("i", "");
ht.Add("strike", "");
ht.Add("sup", "");
ht.Add("sub", "");
ht.Add("font", "color,size,face");
ht.Add("blockquote", "dir");
ht.Add("ul", "");
ht.Add("ol", "");
ht.Add("li", "");
ht.Add("p", "align,dir");
ht.Add("address", "");
ht.Add("pre", "class");
ht.Add("div", "align");
ht.Add("hr", "id");
ht.Add("br", "");
ht.Add("a", "href,target,name");
ht.Add("span", "align");
ht.Add("img", "src,alt,title");
if( CSCache.Get("allowedTags") == null ) {
CSCache.Insert("allowedTags", ht, 5);
}
}
return (System.Collections.Hashtable)HttpContext.Current.Cache["allowedTags"];
}
#endregion
}
}
#region Old Code
/*
// replace all carraige returns with <br> tags
strBody = strBody.Replace("\n", "\n" + Globals.HtmlNewLine + "\n");
// Ensure we have safe anchors
Match m = Regex.Match(strBody, "href=\"((.|\\n)*?)\"");
foreach(Capture capture in m.Captures) {
if ( (!capture.ToString().StartsWith("href=\"http://")) )
strBody = strBody.Replace(capture.ToString(), "");
// TODO if ( (!capture.ToString().StartsWith("href=\"http://")) && (!capture.ToString().StartsWith("href=\"/")) )
// strBody = strBody.Replace(capture.ToString(), "");
}
// Create mailto links with any words containing @
// strBody = Regex.Replace(strBody, "\\b((?<!<a\\s+href\\s*=\\s*\\S+)\\w+@([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?)", "$1", RegexOptions.IgnoreCase | RegexOptions.Multiline);
// replace all whitespace with
//strBody = strBody.Replace(" ", " ");
return strBody;
*/
#endregion
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -