Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / MFC
Article

Syntax Highlighter

Rate me:
Please Sign up or sign in to vote.
3.67/5 (6 votes)
5 Feb 20022 min read 69.1K   1.8K   28   5
An application to generate highlighted code RTF files

Description

How often would we like to insert coloured code in our documents? I know it's not that important but, first, it looks much nicer and, second, it's much easy to understand a piece of source code when it is coloured. At least for me.

What this little application does is exporting Rich Text Format syntax coloured code. To do that, special text files are used defining the each of the languages that the application will handle and colour. These files are found in the application folder.

Sample Image

Language files

Let's have now a look into this language files. From all the sections, only the first one is not optional, the rest of them are, though it doesn't make a lot of sense not including them.

Section Description
§ Language extensions File extensions for which the language file will be used
§ Comment line Comment line indicator (e.g.: // in C++)
§ Comment block Opening and closing comment block symbols (in different lines)
§ String marks Characters that can enclose a string (e.g.: ")
§ Operators Single characters that act as operators in the described language in a single line. (blank spaces can be added)
§ Keywords Language keywords.
§ Colours (normal, comment, string, keyword, operator) Colour in hexadecimal that will be used. One per line.

Note that an empty line is required after the last element of each section and the section names must be exactly like they appear in the table above.

Notes

First of all, saying that the application may have bugs. I'm not speaking about memory leaks or something like that, just pointing out the possibility that somebody gets a bad coloured output for a given source file. I haven't so far anyway.

I've only tested the app under Windows 2000, but it should work as well under Windows 98 and I guess also in WindowsXP (?)

Two of those language files are included in both of the downloads. One for C/C++ and the other one for Visual Basic

The code to transfer the rtf information to a CString object was borrowed from the article Painless streaming with CRichEditCtrl, that can be found in the Docs sections in http://www.calcaria.net.

I'm posting the source code along with the application. I hope this article helps, in the same way many of the articles found here, in the Code Project, helped me.

Salu2, Samuel.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Spain Spain
I studied Telecommunication with spezialization in Sound & Image. I was always very interested in programming at university as well and that is how I earn a living.

Since some years ago, I am also succesfully involved in software architecture and design.

Comments and Discussions

 
GeneralThanks for the ref Pin
paulccc31-Oct-05 1:51
paulccc31-Oct-05 1:51 
Hi Samuel,

Thanks for the reference to my RTF article. Nice to know someone used it!

Paul
GeneralCrash if no valid language extension found Pin
UTO7-Jun-04 23:41
UTO7-Jun-04 23:41 
Generalsource files link is not working :-((( Pin
6-Feb-02 21:23
suss6-Feb-02 21:23 
GeneralRe: source files link is not working :-((( Pin
6-Feb-02 23:57
suss6-Feb-02 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.