Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code Color-coding 2

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
A while back earthandfire posted a thread in the VB.NET forum (thread796-1153488) about a color coding program for between the [[]code] [[]/code] tags.

earthandfire said:
ZmrAbdulla posed a problem in thread796-1152284, which both ca8msm and I found very interesting and intriguing. In one of his replies ca8msm sugested that it would be a good idea if we could have a mechanism to embed colour tags between [[]code] [[]/code] tags.

This got me thinking. I can see two possible solutions.

1)
Create a VS AddIn. This would (hopefully) enable us to see the formatting used and then we would be able to pick up the colours and output a document with the appropriate formatting. I don't, however, see this as the easy option.

2)
Create a program that will parse a block of text looking for key features. For example the apostrophe could indicate that what follows should be green. A list of reserved words could be used to match up blue. Obviously this is a very simplistic summary, but, I think far more achievable than option 1. This could then be extended to support the rainbow of colours used in SQL Server.

I intend to experiment with option two.

I've posted this a separate topic in the hope that it will ignite some interest. I was actually quite surprised that so few contributors posted to the other thread.
This was a very cool idea and after finding the CopySourceAsHtml VS Add-In, I choose to experiment with option one. This thread is the C# version (I started with a post in VB.NET)

There is one major advantage of having the Add-In. The Add-In doesn't have to look for keywords to color code. As an Add-In it has access to the code windows, which it sees as a RichTextBox - so it's just a matter of parsing the colors out for TGML.

I have the source, and setup available at - the only difference in how the original and this one works - is a checkbox labeled to TGML when choosing Copy As HTML from the menu (whether context or edit menus).

Here is a sample of the output:
Code:
        [COLOR=blue]public[/color] [COLOR=blue]static[/color] HtmlColor ConvertColor( [COLOR=blue]uint[/color] oleColor )
        {

            [COLOR=blue]uint[/color] refColor = 0;
            [COLOR=blue]int[/color] r;
            [COLOR=blue]int[/color] g;
            [COLOR=blue]int[/color] b;

            [COLOR=blue]if[/color] ( OleTranslateColor( oleColor , 0 , [COLOR=blue]ref[/color] refColor ) != 0 )
            {
                [COLOR=blue]throw[/color] [COLOR=blue]new[/color] Exception( "OleTranslateColor failed." );
            }
            r = ([COLOR=blue]int[/color])( refColor [& 0xFF );]
            g = ([COLOR=blue]int[/color])( ( refColor >> 8 ) [& 0xFF );]
            b = ([COLOR=blue]int[/color])( ( refColor >> 16 ) [& 0xFF );]
            [COLOR=blue]return[/color] [COLOR=blue]new[/color] HtmlColor( r , g , b );

        }

Cheers!
 
Nice! Looks good!
Code:
[COLOR=blue]public[/color] [COLOR=blue]string[/color] CopyAsTGML( [COLOR=blue]string[/color] rtf)
        {
            StringBuilder tgml;
            [COLOR=blue]string[/color] content;
            [COLOR=blue]int[/color] undentSpacesRemaining;
            [COLOR=blue]int[/color] undentSpaces = 0;
            [COLOR=blue]bool[/color] allowLeadingSpace;
            HtmlStyle blockStyle = [COLOR=blue]null[/color];
            [COLOR=blue]string[/color] foregroundColor;
            [COLOR=blue]string[/color] backgroundColor;

            _htmlColors = [COLOR=blue]new[/color] HtmlColorTable();
            _htmlFile = [COLOR=blue]new[/color] HtmlFile(_preferences.WrapWords , _preferences.TabSize);
            _encoding = Encoding.Default;

            ParseRtf( rtf );

            tgml = [COLOR=blue]new[/color] StringBuilder();

            [COLOR=blue]foreach[/color] ( HtmlLine line [COLOR=blue]in[/color] _htmlFile.Lines )
            {
                undentSpacesRemaining = undentSpaces;
                [COLOR=blue]if[/color]( line.HasContent )
                {
                    allowLeadingSpace = [COLOR=blue]false[/color];
                    [COLOR=blue]foreach[/color] ( HtmlBlock block [COLOR=blue]in[/color] line.Blocks )
                    {
                        [COLOR=blue]if[/color] ( ( block.HasContent ) || ( block.LeadingSpaces > 0 ) )
                        {
                            content = block.GetContent( allowLeadingSpace , undentSpacesRemaining , _preferences ); 
                            undentSpacesRemaining -= block.LeadingSpaces;
                            [COLOR=blue]if[/color] ( undentSpacesRemaining < 0 )
                            {
                                undentSpacesRemaining = 0;
                            }
                            [COLOR=blue]if[/color] ( content.Length > 0 )
                            {
                                allowLeadingSpace = [COLOR=blue]true[/color];
                                blockStyle = [COLOR=blue]new[/color] HtmlStyle();
                                [COLOR=blue]if[/color] ( _htmlColors.Exists( block.ForegroundColor ) )
                                {
                                    foregroundColor = _htmlColors.Get( block.ForegroundColor ).ToString();
                                    [COLOR=blue]if[/color] ( foregroundColor != _preferences.PlainTextStyle.ForegroundColor )
                                    {
                                        blockStyle.AppendFormat( "{0} " , foregroundColor );
                                    }
                                }
                                [COLOR=blue]if[/color] ( _htmlColors.Exists( block.BackgroundColor ) )
                                {
                                    backgroundColor = _htmlColors.Get( block.BackgroundColor ).ToString();
                                    [COLOR=blue]if[/color] ( backgroundColor != _preferences.PlainTextStyle.BackgroundColor )
                                    {
                                        blockStyle.AppendFormat( "{0}" , backgroundColor );
                                    }
                                }
[COLOR=green]//                                if ( ( _preferences.BlockStyle != null ) && ( _preferences.BlockStyle.Length > 0 ) )[/color]
[COLOR=green]//                                {[/color]
[COLOR=green]//                                    blockStyle.Append( _preferences.BlockStyle );[/color]
[COLOR=green]//                                }[/color]
                                [COLOR=blue]if[/color] ( blockStyle.Length > 0 )
                                {
                                    tgml.Append( "[COLOR=" );
                                    tgml.AppendFormat( "{0}" , blockStyle.ToString() );
                                    tgml.Append( "]" );
                                }
                                [COLOR=green]//Regex reg = new Regex("(?<first>[&(.)*;)",System.Text.RegularExpressions.RegexOptions.IgnoreCase);][/color]
                                [COLOR=green]//content = reg.Replace( content , "[{first}]" );[/color]
                                [COLOR=green]//strInput = Regex.Replace(strInput,"(?<first>\S+) (?<last>\S+)","${last},${first}")[/color]
                                content = Regex.Replace(content, "&[^[&]*;", "[$0]");]
                                tgml.Append( content );
                                [COLOR=blue]if[/color] ( blockStyle.Length > 0 )
                                {
                                    tgml.Append( "[/color]" );
                                }
                            }
                        }
                    }
                }
                tgml.Append( "\n" );
            }
            [COLOR=blue]return[/color] tgml.ToString();
        }
 
Found a bug. I copied this line of code:
Code:
Regex regex = [COLOR=blue]new[/color] Regex(@"[@!#$%^&*]",RegexOptions.IgnoreCase);
The real result is below. It adds some brackets (in [red]red[/red]) on it's own.
Code:
Regex regex = [COLOR=blue]new[/color] Regex(@"[@!#$%^[red][[/red]&*]",RegexOptions.IgnoreCase);[red]][/red]
 
Thanks Veep - that would be part of the TGML process, where it's looking for any &???;.

Here's the regex that's causing it - coincidentally it's in the CopyAsTGML function you posted above:
Code:
content = Regex.Replace(content, "&[^&]*;", "[$0]");

I'll have to look into modifying how I trap the html &???; codes.
 
I found the fix to that bug Veep.

Just remove the line that I have outlined above.

I had changed the html parser so that it wouldn't output any character codes in the format &????; when copying as TGML. I should have removed the Regex statement there, but left that in.

Or if you don't want to recompile the code yourself you can wait until I update the download files (very soon - am updating the server they are hosted on).
 
All is well. Thanks
Code:
Regex regex = [COLOR=blue]new[/color] Regex(@"[@!#$%^&*]",RegexOptions.IgnoreCase);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top