If I recall correctly, you stated that the code for the code highlighting feature here on Channel 9 would be released. Just wondering how that is coming along, or if it has been put on the back burner for now.
-
-
Charles,
You should come up with a syntax highlighter like this one:
http://www.actiprosoftware.com/Products/DotNet/CodeHighlighter/Default.aspx
Demos here:
http://www.actiprosoftware.com/Products/DotNet/CodeHighlighter/InlineCode.aspx
http://www.actiprosoftware.com/Products/DotNet/CodeHighlighter/PasteCode.aspx
http://www.actiprosoftware.com/Products/DotNet/CodeHighlighter/UploadCodeFile.aspx
-
Tyler Brown wrote:If I recall correctly, you stated that the code for the code highlighting feature here on Channel 9 would be released. Just wondering how that is coming along, or if it has been put on the back burner for now.
I did?
Sure, I'll share that code with you. It's actually a part of the CommunityServer code base, so if you grab the sources from CommunityServer.org, look in the Components project, Transforms.cs, SourceCodeMarkup method. Hey, W3bbo, the transformed mark up is Table-based
C -
Charles wrote:Hey, W3bbo, the transformed mark up is Table-based
I'm converting it as we speak.
-
There are some far, far better code markup tools out there, most of them open source.
EDIT: Charles, could you at least fix the single-line quoting bug, it's the most annoying thing ever.
int n = 5;
// This is what I mean, the comment color continues for the rest of the block
Console.WriteLine(n); -
I personally prefer this one: http://manoli.net/csharpformat/
It may not be most feature-rich one out there, but it does what I need, and generates very clean markup. -
Sven Groot wrote:I personally prefer this one: http://manoli.net/csharpformat/
It may not be most feature-rich one out there, but it does what I need, and generates very clean markup.
Well, if it's only C# we're talking here then a shameless plug is in order

I wrote an article on how to use a real compiler's scanner and parser to colorize C# source code. This is way more powerful and precise than a bunch of regexps, but don't take my word for it and head over to Coding Horror weblog for a discussion on the topic.
-
Hold on guys... I've nearly done writing a patch for it.
-
Pseudocode follows:
// get the text from the request
string s = Request.Form.Get("post");
// make a new .cs file
File f = new File("post.cs");
// write the text to it
f.AppendText(s);
f.Close();
// instantiate a new Visual Studio application
VSApp vs = new VSApp("Visual Studio 2003");
// open the file in the app
vs.OpenFile(f);
// take a screenshot of the app
Image i = new Screenshot(vs);
// create an optical character recognition object
OCR o = new OCR();
// parse the syntax-highlighted string, with color
s = o.Parse(OCRParseOptions.IncludeColor);
Mmmm.... tasty
-
Okay, here's my elementary patch. Note that is uses the existing syntax highlighting sytem which keeps all the comment bugs, but at least it uses "more correct" code:
#region Private vars, enums, and helper functions static Language GetLanguageType(string language) { switch (language.ToUpper()) { case "VB" : return Language.VB; case "JS" : return Language.JScript; case "JScript" : return Language.JScript; default : return Language.CS; } } public enum Language { CS, VB, JScript } const int _fontsize = 2; const string TAG_FNTRED = @"<span style='color: Red;'>"; const string TAG_FNTBLUE = @"<span style='color: Blue;'>"; const string TAG_FNTGRN = @"<span style='color: Green;'>"; const string TAG_FNTMRN = @"<span style='color: Maroon;'>"; const string TAG_EFONT = @"</span>"; const string cSyn_Keyword = @"<span class='keyword'>"; const string cSyn_Comment = @"<span class='comment'>"; const string cSyn_End = @"</span>"; public static string FormatSource(string htmlEncodedSource, Language language) { StringWriter textBuffer = new StringWriter(); /* textBuffer.Write("<font face=\"Lucida Console\" size=\"" + _fontsize + "\">"); */ if(language == Language.CS) { textBuffer.Write(FixCSLine(htmlEncodedSource)) ; } else if(language == Language.JScript) { textBuffer.Write(FixJSLine(htmlEncodedSource)) ; } else if(language == Language.VB) { textBuffer.Write(FixVBLine(htmlEncodedSource)) ; } /* textBuffer.Write("</font>"); */ return textBuffer.ToString(); } static string FixCSLine(string source) { if (source == null) return null; source = Regex.Replace(source, @"(\<br(\s*|(\ )*)\/{0,1}\>)", @"<br />"); return source; // TDD I'm commenting this out for now for the 2.0.1 release. At a later point we'll figure out what is wrong with the color // syntax hightlighting. source = Regex.Replace(source, "(?i)(\\t)", " "); source = Regex.Replace(source, "(?i) ", " "); String[] keywords = 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", "finally", "get", "set", "char", "default"}; String CombinedKeywords = "(?<keyword>" + String.Join("|", keywords) + ")"; source = Regex.Replace(source, "\\b" + CombinedKeywords + "\\b(?<!//.*)", cSyn_Keyword + "${keyword}" + cSyn_End); source = Regex.Replace(source, "(?<comment>//.*$)", cSyn_Comment + "${comment}" + cSyn_End); return source; } static string FixVBLine(string source) { if (source == null) return null; source = Regex.Replace(source, @"(\<br(\s*|(\ )*)\/{0,1}\>)", @"<br />"); return source; // TDD I'm commenting this out for now for the 2.0.1 release. At a later point we'll figure out what is wrong with the color // syntax hightlighting. source = Regex.Replace(source, "(?i)(\\t)", " "); String[] keywords = new String[] { "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" }; String CombinedKeywords = "(?<keyword>" + String.Join("|", keywords) + ")"; source = Regex.Replace(source, "(?i)\\b" + CombinedKeywords + "\\b(?<!'.*)", cSyn_Keyword + "${keyword}" + cSyn_End); source = Regex.Replace(source, "(?<comment>'(?![^']*").*$)", cSyn_Comment + "${comment}" + cSyn_End); return source; }
#region SourceCodeMarkup private static string SourceCodeMarkup(string stringToTransform) { StringBuilder formattedSource = new StringBuilder(); string[] table = new string[3]; string containerStart = @"<div class='boxCode'>"; string containerStop = @"</div>" //table[0] = "<table border=\"0\" cellspacing=\"0\" width=\"100%\">"; //table[1] = "<tr><td width=\"15\"></td><td bgcolor=\"lightgrey\" width=\"15\"></td><td bgcolor=\"lightgrey\"><br>"; //table[2] = "<br> </td></tr></table>"; MatchCollection matchs; // Look for code // matchs = Regex.Matches(stringToTransform, "\\[code language=\"(?<lang>(.|\\n)*?)\"\\](?<code>(.|\\n)*?)\\[/code\\]", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled); foreach (Match match in matchs) { // Remove HTML formatting code // string codeToFormat = match.Groups["code"].ToString().Replace("</p>\r\n", "<br />").Replace("<p>",""); // Get the formatted source code // // formattedSource.Append(table[0]); //formattedSource.Append(table[1]); formattedSource.Append(containerStart); formattedSource.Append(FormatSource(codeToFormat, GetLanguageType(match.Groups["lang"].ToString())).Replace(Globals.HtmlNewLine, "")); //formattedSource.Append(table[2]); formattedSource.Append(containerStop); // Update the main string // stringToTransform = stringToTransform.Replace(match.ToString(), formattedSource.ToString()); formattedSource.Remove( 0, formattedSource.Length ); } return stringToTransform; } #endregion
.boxCode { border: 1px solid black; background: #BBB; color: #000; } .keyword { color: Blue; } .comment { color: #666; }
-
I think I know where the comment bug arises:
source = Regex.Replace(source, "(?<comment>//.*$)", cSyn_Comment + "${comment}" + cSyn_End);
As other users have commented, browsers that don't support the RichTextBox get a plain textarea. But no amount of hookery or crookery allows you to post through the plain textarea and preserve newlines.
The above line of code is correct ($ matches end-of-line, and . doesn't match \r or \n) so I conclude some other code before this is stripping newlines. -
The best open source code highlighter:
download the VB source code here:
http://www.actiprosoftware.com/Download/DotNet/Files/CodeHighlighter.zip
Official website:
http://www.actiprosoftware.com/Products/DotNet/CodeHighlighter/Default.aspx -
Thanks for the suggestions. I didn't write the code highlighting code but I will certainly invest some time to make it better according to your feedback (and code
).
C -
so uh... pasting into [code][/code] screws up the white-space (same goes for anything in the editor); any methods for preserving indentation, etc.?
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.