Hi everyone, I have a problem that I need help with.

My current project that I am working on in my independent coding class is a syntax highlighter for html. Just like how there is a syntax highlighter for php using the highlight_file function, I am making a custom function that will highlight the code of an html file with colors that distinguish the different parts of the code.

The part of the highlighter that I am working on now is how the highlighter detects quotes and double quotes and modifies the text on the inside to change into a color.

A basic example would be this:

Input:

Code:
<div align="center">
<h1>Hello World!</h1>
</div>


Output:

Code:
<div align="<font color='purple'>center</font>">
<h1>Hello World!</h1>
</div>


Any ideas?

Thanks.
Well, we had another thread where we discussed this kind of thing, and we eventually decided that Regex was not the correct solution. Regular expressions are great for many things, but in this case, a state machine used to scan through the document would be your tool of choice.
There is this http://qbnz.com/highlighter/ and I think one of the open source pastebin's uses a similar system that allows for one to add support for any language.
TheStorm wrote:
There is this http://qbnz.com/highlighter/ and I think one of the open source pastebin's uses a similar system that allows for one to add support for any language.
Indeed, but he wants to do it for himself. Smile The source of those might be nice for reference, but it might also be sort of cheating, not to mention they're probably way, way too complex for a project of this magnitude, wouldn't you think?
KermMartian wrote:
TheStorm wrote:
There is this http://qbnz.com/highlighter/ and I think one of the open source pastebin's uses a similar system that allows for one to add support for any language.
Indeed, but he wants to do it for himself. Smile The source of those might be nice for reference, but it might also be sort of cheating, not to mention they're probably way, way too complex for a project of this magnitude, wouldn't you think?
Ahh I missed that he wanted to do it himself, carry on then. Razz
Hehe, I figured that that might be the case. Do you happen to remember that syntax highlighting (or maybe it was C parsing?) thread that I'm talking about? I can't seem to find it.
KermMartian wrote:
Regex was not the correct solution. Regular expressions are great for many things, but in this case, a state machine used to scan through the document would be your tool of choice.

Heehee. When you parse HTML with regex, you summon eldritch abominations. State machine is the way to go. It's almost certainly more than you need, but I got a much better grasp on state machine-based parsing from just messing about with yacc. Even just getting a handle on how yacc's input is structured might be useful.
Thank you for all of the replies everyone. I will check out your posts individually tomorrow when I have more time, but they all look helpful. Like Kerm said, I am attempting to make my own syntax highlighter, but I wouldn't mind program references. They can help me out when I need help as well.

Well, night everyone. Thanks again for all of the replies.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement