Question code to be displayed using colours

pitaridis

Well-known member
Joined
Nov 18, 2005
Messages
63
Programming Experience
10+
I am going to publish some VB.NET classes that I have develop and I am going to make it like everyone will be able to suggest improvements or add some members and classes. I am going to start a project which will host the management of the system. I want the code to be displayed using colours like the VB.NET IDE shows the code. Is there a class which will allow me to convert the code to HTML coloured VB.NET code?

Thanks in advance
 
I didn't find any when searching so I wrote one myself. Actually I've written several code colorizers before, but this time I attempted Regex methods, which seems to work out well. The VBNetToHtml class is attached, try it, there is also a sample output.htm of the class itself. The class is written and tested in short time, so I'm sure there is bugs, things that could be done better, and features to be added :cool:

Usage sample:
VB.NET:
Dim vb As New VBNetToHtml
vb.ColorMap(VBNetToHtml.ColorKey.Comment) = Color.Green '(to override element color)
Dim s As String = vb.Colorize(Clipboard.GetText)
My.Computer.FileSystem.WriteAllText("output.htm", s, False, System.Text.Encoding.Default)
 

Attachments

  • VBNetToHtml.zip
    5 KB · Views: 23
Thank you very much for your reply. I started doing it myself by filtering the code line by line and it was very difficult. I have never used Regex in my projects. Your code is a state of the art.
 
I did some more work on this class and thought I'd share it. This version is a bit more complex, so if you just want to grab the essence the class I posted first will do. The basic concept of the original code is not changed at all, even if it may now look a little overwhelming and the line count has doubled, that is mostly the comments and more whitespace :)

These are the changes:
  • fixed some minor bugs and a bigger one for the keywords captures (now "." is left out as separator at beginning).
  • general cleanup, refactored some calls, put everything in a Tools namespace, moved out all enum types to namespace level. This adds a bit more abstraction, but also makes other places in code more clean; better organization not cramping everything into one place is OOP.
  • added support for coloring xml code comments, this led to changing the captures loop into a recursive call, in order to capture and loop the xml markup tags in those comments.
  • added public options for setting document type (full|body) and style type (css|style).
  • added xml code document to nearly all class members, also added some more inline code comments to explain more. Adding comments can really expand the amount of text, which can make it more difficult to get the overview, but too little comments can also make things difficult to understand.
 

Attachments

  • VBNetToHtml.zip
    8.5 KB · Views: 27
Thank you very much for your help. I have to make some changes to your code in order to work for me. I do not know why function does not work the way that you used it but I changed it to be a normal function in order to make the code to work.

I suppose that the way that you use the function is like you extend the type in order to have a new member function. I have never used such a function and I suppose that I have to do something in order to work. Please tell me what I should do.

Thanks.
 
Do you mean the added namespace?
VB.NET:
Dim vb As New Tools.VBNetToHtml
Dim s As String = vb.Colorize(Clipboard.GetText)
I suppose that the way that you use the function is like you extend the type in order to have a new member function.
I don't know what you mean by this.
 
I am talking about the following function

VB.NET:
        <Runtime.CompilerServices.Extension()> _
        Function IsBetween(ByVal i As Integer, ByVal start As Integer, ByVal length As Integer) As Boolean
            Return i >= start AndAlso i < start + length
        End Function
 
Just forget it. I found what is the problem. Thank you for your help.
 
Back
Top