using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace CodeHTML
{
public class TranslateCode
{
private class ProgrammingLanguage
{
public const string VB = "vb";
public const string CSharp = "c#";
public const string JSharp = "js";
}
private bool _ShowFileName;
private int _FontSize;
private string _Language;
private const string TAG_FNTRED = "<font color=\"red\">";
private const string TAG_FNTBLUE = "<font color=\"blue\">";
private const string TAG_FNTGRN = "<font color=\"green\">";
private const string TAG_FNTMRN = "<font color=\"maroon\">";
private const string TAG_EFONT = "</font>";
public bool ShowFileName
{
get
{
return _ShowFileName;
}
set
{
_ShowFileName = value;
}
}
public int FontSize
{
get
{
return _FontSize;
}
set
{
_FontSize = value;
}
}
public string Language
{
get
{
return _Language;
}
set
{
_Language = value;
}
}
public TranslateCode()
{
_ShowFileName = false;
_FontSize = 3;
_Language = "";
}
private void SetLanguageFromFileName(string FilePath)
{
if (FilePath.Split(new char[]{Convert.ToChar(".")}).Length > 0)
{
string str2 = FilePath.Substring(FilePath.LastIndexOf(Convert.ToChar(".")) + 1).ToLower().Trim();
if (StringType.StrCmp(str2, "vb", false) == 0)
{
_Language = "vb";
}
else if (StringType.StrCmp(str2, "c#", false) == 0)
{
_Language = "c#";
}
else if (StringType.StrCmp(str2, "js", false) == 0)
{
_Language = "js";
}
}
}
public string RenderFile(string FilePath)
{
SetLanguageFromFileName(FilePath);
return Render(File.OpenText(FilePath));
}
public void RenderFile(string FilePath, string OutputFilePath)
{
SetLanguageFromFileName(FilePath);
StreamWriter streamWriter = new StreamWriter(OutputFilePath);
streamWriter.Write(Render(File.OpenText(FilePath)));
streamWriter.Flush();
streamWriter.Close();
}
public string Render(StreamReader InputTextReader)
{
return Render(InputTextReader.ReadToEnd());
}
public string Render(string InputString)
{
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
string[] strs1 = InputString.Split(new char[]{13});
stringWriter.WriteLine(String.Concat("<font size=\"", StringType.FromInteger(_FontSize), "\" \\>"));
string str4 = _Language.Trim().ToLower();
if (StringType.StrCmp(str4, "c#", false) == 0)
{
stringWriter.WriteLine("<pre>");
string[] strs5 = strs1;
for (int i2 = 0; i2 < (int)strs5.Length; i2++)
{
string str2 = strs5[i2];
stringWriter.Write(FixCSLine(str2));
}
stringWriter.WriteLine("</pre>");
}
else if (StringType.StrCmp(str4, "js", false) == 0)
{
stringWriter.WriteLine("<pre>");
string[] strs4 = strs1;
for (int k = 0; k < (int)strs4.Length; k++)
{
string str2 = strs4[k];
stringWriter.Write(FixJSLine(str2));
}
stringWriter.WriteLine("</pre>");
}
else if (StringType.StrCmp(str4, "vb", false) == 0)
{
stringWriter.WriteLine("<pre>");
string[] strs3 = strs1;
for (int j = 0; j < (int)strs3.Length; j++)
{
string str2 = strs3[j];
stringWriter.Write(FixVBLine(str2));
}
stringWriter.WriteLine("</pre>");
}
else
{
bool flag2 = false;
bool flag1 = false;
stringWriter.WriteLine("<pre>");
string[] strs2 = strs1;
for (int i1 = 0; i1 < (int)strs2.Length; i1++)
{
string str2 = strs2[i1];
_Language = GetLanguageFromLine(str2, _Language);
if (IsScriptBlockTagStart(str2))
{
stringWriter.Write(FixASPXLine(str2));
flag2 = true;
}
else if (IsScriptBlockTagEnd(str2))
{
stringWriter.Write(FixASPXLine(str2));
flag2 = false;
}
else if ((IsMultiLineTagStart(str2) & flag1 == false) != 0)
{
stringWriter.Write(String.Concat("<font color=blue><b>", HttpUtility.HtmlEncode(str2)));
flag1 = true;
}
else if ((IsMultiLineTagEnd(str2) & flag1) != 0)
{
stringWriter.Write(String.Concat(HttpUtility.HtmlEncode(str2), "</b></font>"));
flag1 = false;
}
else if (flag1)
{
stringWriter.Write(HttpUtility.HtmlEncode(str2));
}
else if (flag2)
{
string str3 = _Language.Trim().ToLower();
if (StringType.StrCmp(str3, "c#", false) == 0)
{
stringWriter.Write(FixCSLine(str2));
}
else if (StringType.StrCmp(str3, "js", false) == 0)
{
stringWriter.Write(FixJSLine(str2));
}
else if (StringType.StrCmp(str3, "vb", false) == 0)
{
stringWriter.Write(FixVBLine(str2));
}
else
{
stringWriter.Write(FixVBLine(str2));
}
}
else
{
stringWriter.Write(FixASPXLine(str2));
}
}
stringWriter.WriteLine("</pre>");
}
stringWriter.WriteLine("</font>");
stringWriter.Flush();
return stringBuilder.ToString();
}
private string GetLanguageFromLine(string sInputLine, string DefaultLanguage)
{
string str1;
string str2 = DefaultLanguage;
if (sInputLine.Length == 0)
{
str1 = str2;
}
else
{
Match match = Regex.Match(sInputLine, "(?i)<%@\\s*Page\\s*.*Language\\s*=\\s*\"(?<lang>[^\"]+)\"");
if (match.Success)
{
str2 = match.Groups["lang"].ToString();
}
match = Regex.Match(sInputLine, "(?i)(?=.*runat\\s*=\\s*\"?server\"?)<script.*language\\s*=\\s*\"(?<lang>[^\"]+)\".*>");
if (match.Success)
{
str2 = match.Groups["lang"].ToString();
}
match = Regex.Match(sInputLine, "(?i)<%@\\s*WebService\\s*.*Language\\s*=\\s*\"?(?<lang>[^\"]+)\"?");
if (match.Success)
{
str2 = match.Groups["lang"].ToString();
}
if (StringType.StrCmp(str2, "CS", false) == 0)
{
str2 = "c#";
}
str1 = str2;
}
return str1;
}
private string FixCSLine(string sInputLine)
{
string str1;
string str3 = sInputLine;
if (sInputLine.Length == 0)
{
str1 = sInputLine;
}
else
{
str3 = Regex.Replace(sInputLine, "(?i)(\\t)", " ");
str3 = HttpUtility.HtmlEncode(str3);
string[] strs1 = new string[]{"private", "protected", "public", "namespace", "class", "break", "for", "if", "else", "while", "switch", "case", "using", "return", "null", "void", "int", "bool", "string", "float",
"this", "new", "true", "false", "const", "static", "base", "foreach", "in", "try", "catch", "get", "set", "char", "default"};
string str2 = String.Concat("(?<keyword>", String.Join("|", strs1), ")");
str3 = Regex.Replace(str3, String.Concat("\\b", str2, "\\b(?<!//.*)"), "<font color=\"blue\">${keyword}</font>");
str3 = Regex.Replace(str3, "(?<comment>//.*$)", "<font color=\"green\">${comment}</font>");
str1 = str3;
}
return str1;
}
private string FixJSLine(string sInputLine)
{
string str1;
string str3 = sInputLine;
if (sInputLine.Length == 0)
{
str1 = sInputLine;
}
else
{
str3 = Regex.Replace(sInputLine, "(?i)(\\t)", " ");
str3 = HttpUtility.HtmlEncode(str3);
string[] strs1 = new string[]{"private", "protected", "public", "namespace", "class", "var", "for", "if", "else", "while", "switch", "case", "using", "get", "return", "null", "void", "int", "string", "float",
"this", "set", "new", "true", "false", "const", "static", "package", "function", "internal", "extends", "super", "import", "default", "break", "try", "catch", "finally"};
string str2 = String.Concat("(?<keyword>", String.Join("|", strs1), ")");
str3 = Regex.Replace(str3, String.Concat("\\b", str2, "\\b(?<!//.*)"), "<font color=\"blue\">${keyword}</font>");
str3 = Regex.Replace(str3, "(?<comment>//.*$)", "<font color=\"green\">${comment}</font>");
str1 = str3;
}
return str1;
}
private string FixVBLine(string sInputLine)
{
string str2;
string str3 = sInputLine;
if (sInputLine.Length == 0)
{
str2 = sInputLine;
}
else
{
str3 = Regex.Replace(sInputLine, "(?i)(\\t)", " ");
str3 = HttpUtility.HtmlEncode(str3);
string[] strs1 = new string[]{"AddressOf", "Delegate", "Optional", "ByVal", "ByRef", "Decimal", "Boolean", "Option", "Compare", "Binary", "Text", "On", "Off", "Explicit", "Strict", "Private", "Protected", "Public", "End Namespace", "Namespace",
"End Class", "Exit", "Class", "Goto", "Try", "Catch", "End Try", "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", "End Enum", "Enum"};
string str1 = String.Concat("(?<keyword>", String.Join("|", strs1), ")");
str3 = Regex.Replace(str3, String.Concat("(?i)\\b", str1, "\\b(?<!\’.*)"), "<font color=\"blue\">${keyword}</font>");
str3 = Regex.Replace(str3, "(?<comment>\’(?![^\’]*").*$)", "<font color=\"green\">${comment}</font>");
str2 = str3;
}
return str2;
}
private string FixASPXLine(string sInputLine)
{
string str1;
string str2 = sInputLine;
if (sInputLine.Length == 0)
{
str1 = sInputLine;
}
else
{
str2 = Regex.Replace(str2, "(?i)(\\t)", " ");
str2 = HttpUtility.HtmlEncode(str2);
string str4 = "(?i)(?<a>(^.*))(?<b>(<!--))(?<c>(.*))(?<d>(-->))(?<e>(.*))";
string str3 = "${a}<font color=\"green\">${b}${c}${d}</font>${e}";
if (Regex.IsMatch(str2, str4))
{
str2 = Regex.Replace(str2, str4, str3);
}
str4 = "(?i)(?<a>(<%@))(?<b>(.*))(?<c>(%>))";
str3 = "<font color=blue><b>${a}${b}${c}</b></font>";
if (Regex.IsMatch(str2, str4))
{
str2 = Regex.Replace(str2, str4, str3);
}
str4 = "(?i)(?<a>(<%#))(?<b>(.*))(?<c>(%>))";
str3 = "${a}<font color=red><b>${b}</b></font>${c}";
if (Regex.IsMatch(str2, str4))
{
str2 = Regex.Replace(str2, str4, str3);
}
str4 = "(?i)(?<a>(<)(?!%)(?!/?asp:)(?!/?template)(?!/?property)(?!/?ibuyspy:)(/|!)?)(?<b>[^;\\s&]+)(?<c>(\\s|>|\\Z))";
str3 = "${a}<font color=\"maroon\">${b}</font>${c}";
if (Regex.IsMatch(str2, str4))
{
str2 = Regex.Replace(str2, str4, str3);
}
str4 = "(?i)(?<a></?)(?<b>(asp:|template|property|IBuySpy:).*)(?<c>>)?";
str3 = "${a}<font color=\"blue\"><b>${b}</b></font>${c}";
if (Regex.IsMatch(str2, str4))
{
str2 = Regex.Replace(str2, str4, str3);
}
str4 = "(?i)(?<a>(<)(/|!|%)?)";
str3 = "<font color=\"blue\">${a}</font>";
if (Regex.IsMatch(str2, str4))
{
str2 = Regex.Replace(str2, str4, str3);
}
str4 = "(?i)(?<a>(/|%)?(>))";
str3 = "<font color=\"blue\">${a}</font>";
if (Regex.IsMatch(str2, str4))
{
str2 = Regex.Replace(str2, str4, str3);
}
str1 = str2;
}
return str1;
}
private bool IsScriptBlockTagStart(string sInputLine)
{
bool flag1 = false;
if (Regex.IsMatch(sInputLine, "<script.*runat=\"?server\"?.*>"))
{
flag1 = true;
}
else if (Regex.IsMatch(sInputLine, "(?i)<%@\\s*WebService"))
{
flag1 = true;
}
bool flag2 = flag1;
return flag2;
}
private bool IsScriptBlockTagEnd(string sInputLine)
{
bool flag1 = false;
if (Regex.IsMatch(sInputLine, "</script.*>"))
{
flag1 = true;
}
bool flag2 = flag1;
return flag2;
}
private bool IsMultiLineTagStart(string sInputLine)
{
bool flag1 = false;
string str2 = "(?i)(?!.*>)(?<a></?)(?<b>(asp:|template|property|IBuySpy:).*)";
if (Regex.IsMatch(HttpUtility.HtmlEncode(sInputLine), str2))
{
flag1 = true;
}
bool flag2 = flag1;
return flag2;
}
private bool IsMultiLineTagEnd(string sInputLine)
{
bool flag1 = false;
string str2 = "(?i)>";
if (Regex.IsMatch(HttpUtility.HtmlEncode(sInputLine), str2))
{
flag1 = true;
}
bool flag2 = flag1;
return flag2;
}
}
}