HTML Syntax Highlighting

Website: http://vhtml.hypermart.net/

General Info

HTML syntax highlighting can be done easier than other languages because it follows a set pattern. You have tags, attributes, comments, and text. That's about it...So to make highlighting faster, I created a routine that works just for HTML. It's faster because it follows the set pattern for HTML instead of just finding keywords and highlighting those.

Example

It works by first going and putting in the correct RTF color table switches and then storing all text up to an HTML tag (thus making it faster if you have more text b/c it skips everything in between). Then it highlights an individual tag at a time. It goes through and evaluates each tag and its constituients. It's pretty basic...anything to the left of an equal sign in the tag is going to be an attribute name (this color is the same as the tag color...I'll work on creating separate colors for this), and anything to the right will be the attribute value (colored in blue in the example). It will color values even if they're not enclosed in quotes. When you go to the next space, then we know we're at another attribute name EXCEPT in cases when we're in a quote. I had to add some code to make sure it didn't evaluate the next word after a space as an attribute name instead of as part of the quote. But that's all taken care of, so don't fret! (c: It will color comments as well.

Problems

Don't try using TagBold, TagItalics, etc. b/c in some configurations it will cause it to freeze and I don't know why yet. I will work on that. But it does work fine w/ any color configuration. You can use common dialogs to allow the user to select a color and it will work fine w/ those. It automattically interprets the colors and applies them to the RTF.

Requirements

If you use the supplied owner created RichText box, you need to download the SSubTimer.DLL - great subclassing control/activex dll from vbAccelerator. The supplied project uses the standard RichTextBox control that comes with VB - if you want to change the DLL to use the supplied RTF control, you'll need to change the code slightly - see below.

Implementation

It's rather simple, actually...I have it set up w/ an RTB made through code, but you can easily switch it to a normal RTB by changing the "Public RichTxtBox As HSRichEdit" to "Public RichTxtBox As RichTextBox". After that, to highlight, just use two lines of code:

  set syntaxHighlight1.RichTxtBox =  RichTextBox1
  syntaxHighlight1.Highlight

To get it to color properly while typing, use the following code:

  Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
    KeyAscii = syntaxHighlight1.KeyPressEvent(KeyAscii)
  End Sub

That's all it takes to set it up! Good luck! (c;

-David Hoyt

Download zipped project file (51k)



Comments

  • Syntax Highlighting

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: Ramil

    It odesn't work when I download the sample.

    Reply
  • Speed Question and inserting info

    Posted by Legacy on 01/07/2001 12:00am

    Originally posted by: Ackbar

    First off I modified this code a bit but I had a couple of thoughts on it. I noticed that as it got further into the process it got substnatially slower. An example being I ran a test on a 68k html file. What I found was that the first 50% took just over 1 second. But the entire file took 23 seconds to highlight. Any suggestions on what could be causing this? I considered the posibility that it's maybe instr or such not liking to be used on such a huge file. If this is the case my solution would be to have a second variable and strip off everything thats already been checked. IE You have
    
    < html > blah blah blah < /html >

    Well it would first find the < html > and highlight that. So why keep it? Cut that section off the string.


    The second issue is how do you highlight info thats being dropped in? IE if you have table dialog. After it's all setup how would you highlight that?

    Another thing i've been wondering about. I coded it in so it would keep syntax with whatever it is around. But I noticed a few flaws in overall. Like if you type ="blah in tags and then delete that you still tend to end up with the red. This is easy enough to fix.

    Also you should consider adding a script section. Like isScript boolean or something. The reason I suggest this is fairly simple. When you highlight say

    < script >
    if (a < b)

    everything after that < gets highlighted. But this is only meant to be used as a < than sign. So a script coloring would be great. Maybe just leave everything in the script black. Maybe even go as far as highlighting keywords in the script.

    Anyway overall I think this is a good example. And I'm screwing around with it to try and optimize it for speed and quality. If completed it could be a very nice thing for vb programmers.

    If you have any suggestions etc, or if you have a solution to the inserting text problem please send me an email. (Ackbar81@hotmail.com)

    Thanks :)

    -Ackbar

    Reply
  • Color Coding inserted text

    Posted by Legacy on 11/26/1999 12:00am

    Originally posted by: Eric Banker

    I have been able to get this part working great within my program but html editors tend to insert text into the textbox. I can not figure out how to create a new function that will highlight just the string that i want to insert. Do you have any ideas?

    Please email me with an answer if you can

    Thanks

    Eric Banker

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • As a result of corporate expansions, mergers, and acquisitions, the teams and technologies that support an organization's IT service management (ITSM) practices can over time become somewhat dispersed. Supporting an organization's strategic objectives, and providing consistent and quality IT support is essential, but this can be challenging where disconnected support environments exist. While physically centralizing support is not always possible, nor is it always desirable, delivering a consistent and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds