public static Tuple<bool, string> FormatCode(string lang, string code)
 {
     SourceFormat sf = null;
     switch (lang)
     {
         case "csharp":
         case "cs":
             sf = new Manoli.Utils.CSharpFormat.CSharpFormat();
             break;
         case "c++":
         case "cpp":
             sf = new CPlusPlusFormat();
             break;
         case "js":
         case "javascript":
             sf = new JavaScriptFormat();
             break;
         case "ts":
         case "typescript":
             sf = new TypeScriptFormat();
             break;                  
         case "vb":
         case "basic":
             sf = new VisualBasicFormat();
             break;
         case "sql":
             sf = new TsqlFormat();
             break;
         case "msh":
             sf = new MshFormat();
             break;
         case "haskell":
             sf = new HaskellFormat();
             break;
         case "php":
             sf = new PhpFormat();
             break;
         case "fsharp":
         case "fs":
             sf = new FSharpFormat();
             break;
         case "html":
         case "xml":
         case "aspx":
             sf = new HtmlFormat();
             break;
         case "paket":
             sf = new PaketFormat();
             break;
     }
     if (sf == null)
     {
         return Tuple.Create(false, SourceFormat.EscapeHtml(code, tabSpaces: 2));
     }
     else
     {
         sf.TabSpaces = 2;
         return Tuple.Create(true, sf.FormatCode(code));
     }
 }
		/// <summary/>
		public HtmlFormat()
		{
			const string regJavaScript = @"(?<=&lt;script(?:\s.*?)?&gt;).+?(?=&lt;/script&gt;)";
			const string regComment = @"&lt;!--.*?--&gt;";
			const string regAspTag = @"&lt;%@.*?%&gt;|&lt;%=?|%&gt;|&lt;\?php|&lt;\?|\?&gt;";
			const string regPhpCode = @"(?<=&lt;\?).*?(?=\?&gt;)|(?<=&lt;\?php).*?(?=\?&gt;)";
			const string regAspCode = @"(?<=&lt;%=?).*?(?=%&gt;)";
			const string regTagDelimiter = @"(?:&lt;/?!?\??(?!%)|(?<!%)/?&gt;)+";
			const string regTagName = @"(?<=&lt;/?!?\??(?!%))[\w\.:-]+(?=.*&gt;)";
			const string regAttributes = @"(?<=&lt;(?!%)/?!?\??[\w:-]+).*?(?=(?<!%)/?&gt;)";
			const string regEntity = @"&amp;\w+;";
			const string regAttributeMatch = @"(=?"".*?""|=?'.*?')|([\w:-]+)";
			
			//the regex object will handle all the replacements in one pass
			string regAll = "(" + regJavaScript + ")|(" + regComment + ")|(" 
				+ regAspTag + ")|(" + regAspCode + ")|(" + regPhpCode + ")|(" 
				+ regTagDelimiter + ")|(" + regTagName + ")|("
				+ regAttributes + ")|(" + regEntity + ")";

			CodeRegex = new Regex(regAll, RegexOptions.IgnoreCase | RegexOptions.Singleline);
			attribRegex = new Regex(regAttributeMatch, RegexOptions.Singleline);

			csf = new CSharpFormat();
			jsf = new JavaScriptFormat();
			phf = new PhpFormat();
		}
Exemple #3
0
 private static string FormatCode(string lang, string code)
 {
     SourceFormat sf = null;
     switch (lang)
     {
         case "csharp": case "cs":
       sf = new Manoli.Utils.CSharpFormat.CSharpFormat();
       break;
         case "c++": case "cpp":
             sf = new CPlusPlusFormat();
             break;
         case "js": case "javascript":
             sf = new JavaScriptFormat();
             break;
         case "vb": case "basic":
             sf = new VisualBasicFormat();
             break;
         case "sql":
             sf = new TsqlFormat();
             break;
         case "msh":
             sf = new MshFormat();
             break;
     case "haskell":
       sf = new HaskellFormat();
       break;
     case "php":
             sf = new PhpFormat();
             break;
         case "fsharp": case "fs":
             sf = new FSharpFormat();
             break;
         case "html": case "xml": case "aspx":
             sf = new HtmlFormat();
             break;
     }
     if (sf == null)
         return code;
     else
     {
         string c = code.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"");
     c = c.Replace("<em>", "verynastythingthatnoonewillusebeginem")
      .Replace("</em>", "verynastythingthatnoonewilluseendem");
         sf.TabSpaces = 2;
     return sf.FormatCode(c)
       .Replace("verynastythingthatnoonewilluseendem", "</em>")
       .Replace("verynastythingthatnoonewillusebeginem", "<em>");
     }
 }
Exemple #4
0
        protected void DisplayCode()
        {
            string File = this.Page.Server.MapPath(this.ResolveUrl(this.CodeFile));

            File = File.ToLower();

            // Allow only source and aspx files
            string extension = Path.GetExtension(File).ToLower();

            if (!",.cs,.vb,.aspx,.asmx,.js,.ashx,".Contains("," + extension + ","))
            {
                this.Output = "Invalid Filename specified...";
                return;
            }

            if (System.IO.File.Exists(File))
            {
                StreamReader sr         = new StreamReader(File);
                string       FileOutput = sr.ReadToEnd();
                sr.Close();

                if (File.ToLower().EndsWith(".cs") || File.ToLower().EndsWith(".asmx") || File.ToLower().EndsWith(".ashx"))
                {
                    JavaFormat Format = new JavaFormat();
                    this.Output = "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }
                else if (File.ToLower().EndsWith(".js"))
                {
                    JavaScriptFormat Format = new JavaScriptFormat();
                    this.Output = "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }
                else
                {
                    HtmlFormat Format = new HtmlFormat();
                    this.Output = "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }

                //this.txtOutput.Text = "<pre>" + Server.HtmlEncode(FileOutput) + "</pre>";

                this.Page.ClientScript.RegisterStartupScript(typeof(ViewSourceControl), "scroll",
                                                             "var codeContainer = document.getElementById('" + this.btnShowCode.ClientID + "');codeContainer.focus();setTimeout(function() { window.scrollBy(0,200);},100);", true);
            }

            this.btnShowCode.Visible = true;
        }
Exemple #5
0
        /// <summary/>
        public HtmlFormat()
        {
            const string regJavaScript     = @"(?<=&lt;script(?:\s.*?)?&gt;).+?(?=&lt;/script&gt;)";
            const string regComment        = @"&lt;!--.*?--&gt;";
            const string regAspTag         = @"&lt;%@.*?%&gt;|&lt;%|%&gt;";
            const string regAspCode        = @"(?<=&lt;%).*?(?=%&gt;)";
            const string regTagDelimiter   = @"(?:&lt;/?!?\??(?!%)|(?<!%)/?&gt;)+";
            const string regTagName        = @"(?<=&lt;/?!?\??(?!%))[\w\.:-]+(?=.*&gt;)";
            const string regAttributes     = @"(?<=&lt;(?!%)/?!?\??[\w:-]+).*?(?=(?<!%)/?&gt;)";
            const string regEntity         = @"&amp;\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();
        }
        protected void DisplayCode()
        {
            string File = this.Page.Server.MapPath(this.ResolveUrl(this.CodeFile));
            File = File.ToLower();

            // Allow only source and aspx files
            string extension = Path.GetExtension(File).ToLower();

            if ( !",.cs,.vb,.aspx,.asmx,.js,.ashx,".Contains("," + extension + ",") )
            {
                this.Output = "Invalid Filename specified...";
                return;
            }

            if (System.IO.File.Exists(File))
            {
                StreamReader sr = new StreamReader(File);
                string FileOutput = sr.ReadToEnd();
                sr.Close();

                if (File.ToLower().EndsWith(".cs") || File.ToLower().EndsWith(".asmx") || File.ToLower().EndsWith(".ashx"))
                {
                    JavaFormat Format = new JavaFormat();
                    this.Output = "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }
                else if (File.ToLower().EndsWith(".js"))
                {
                    JavaScriptFormat Format = new JavaScriptFormat();
                    this.Output = "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }
                else
                {
                    HtmlFormat Format = new HtmlFormat();
                    this.Output = "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }

                //this.txtOutput.Text = "<pre>" + Server.HtmlEncode(FileOutput) + "</pre>";

                this.Page.ClientScript.RegisterStartupScript(typeof(ViewSourceControl), "scroll",
                    "var codeContainer = document.getElementById('" + this.btnShowCode.ClientID + "');codeContainer.focus();setTimeout(function() { window.scrollBy(0,200);},100);", true);
            }
            
            this.btnShowCode.Visible = true;
            
        }