public static string HighlightToHTML(string source, LangType type, bool CustomProtectionTags) { SourceFormat sf = null; switch(type) { case LangType.C: case LangType.CPP: sf = new CppFormat(); break; case LangType.CS: sf = new CSharpFormat(); break; case LangType.Html: case LangType.Xml: case LangType.Asp: sf = new HtmlFormat(); break; case LangType.JS: sf = new JavaScriptFormat(); break; case LangType.Msh: sf = new MshFormat(); break; case LangType.TSql: sf = new TsqlFormat(); break; case LangType.VB: sf = new VisualBasicFormat(); break; } if (sf == null) return source; sf.CustomProtectedTags = CustomProtectionTags; return sf.FormatCode(source); }
/// <summary/> public HtmlFormat() { const string regJavaScript = @"(?<=<script(?:\s.*?)?>).+?(?=</script>)"; const string regComment = @"<!--.*?-->"; const string regAspTag = @"<%@.*?%>|<%|%>"; const string regAspCode = @"(?<=<%).*?(?=%>)"; const string regTagDelimiter = @"(?:</?!?\??(?!%)|(?<!%)/?>)+"; const string regTagName = @"(?<=</?!?\??(?!%))[\w\.:-]+(?=.*>)"; const string regAttributes = @"(?<=<(?!%)/?!?\??[\w:-]+).*?(?=(?<!%)/?>)"; const string regEntity = @"&\w+;"; const string regAttributeMatch = @"(=?"".*?""|=?'.*?')|([\w:-]+)"; //the regex object will handle all the replacements in one pass string regAll = "(" + regJavaScript + ")|(" + regComment + ")|(" + regAspTag + ")|(" + regAspCode + ")|(" + regTagDelimiter + ")|(" + regTagName + ")|(" + regAttributes + ")|(" + regEntity + ")"; CodeRegex = new Regex(regAll, RegexOptions.IgnoreCase | RegexOptions.Singleline); attribRegex = new Regex(regAttributeMatch, RegexOptions.Singleline); csf = new CSharpFormat(); jsf = new JavaScriptFormat(); }