Skip to main content
gamedev.net
🔒 Locked

[web] Displaying C++ code using HTML/CSS

Started by Dim_Yimma_H Jul 8, 2005 at 9:09 AM 5 replies 8.7k views
Original Post
Dim_Yimma_H
Dim_Yimma_H
I'm writing some C++ tutorials to put up on a website but having trouble getting some code elements to display properly since they're interpreted as HTML tags. I'm using the pre tag so it'll be easy to paste the code within the tag without having to reformat it. But since it allows tags within it some elements which contains < and > gets messed up. Example on messed up code ( and anything after "Sometext." < isn't displayed):

#include <iostream>

int main(int argc, char** argv)
{
	cout <<"Sometext." <<endl;
	return 0;
}
pre.code definition:

pre.code
{
	background-color:#FFF1CD;
	font-family:courier, serif;
	font-size:10px;
	font-style:normal;
	margin:10px;
	padding:0px
}




I was glad to find the xmp tag which basically did what I wanted but it was also deprecated [depressed] I find it strange the xmp tag was replaced with the pre tag because to me it seems like the pre tag has different features? Do you know of a better way to do this? Thanks for any suggestions.
Prototype
Prototype
Take a look at PSPad.
It can output HTML code from any source with syntax highlighting just like Visual Studio (or any other format as you like). It's also free.
Promit
Promit
You'll need to replace < with <, > with >, and other html special characters with their HTML entity equivalents. If you can run PHP or ASP on your host, it's pretty easy to do.

[EDIT] Apparently the forum converts entities. Hit edit or quote to see the actual text of this post.

[Edited by - Promit on July 8, 2005 10:54:32 AM]
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Dim_Yimma_H
Dim_Yimma_H
Quote:
Original post by Prototype
Take a look at PSPad.
It can output HTML code from any source with syntax highlighting just like Visual Studio (or any other format as you like). It's also free.

Wow PSPad sure has many features! Thanks for the suggestion Prototype. I figured you meant the File->Export->Export to Clipboard as HTML so it converts the code to an exact look alike when viewed in a browser? It's better than my currently messed up HTML code. I haven't decided yet if I'm going for syntax highlighting though it does make the C++ code more readable but also makes the HTML code a bit harder to overview.
Quote:
Original post by Prototype
You'll need to replace < with <, > with >, and other html special characters with their HTML entity equivalents. If you can run PHP or ASP on your host, it's pretty easy to do.

I might actually consider learning PHP since it seems to be useful in other ways as well. If PHP supports reading files (would be strange otherwise [grin]) it should be possible to even keep the C++ code separate from the HTML code and put it in there when the page is loaded and at the same time do like you said, replace the < and > with their entity equivalents.

In the meantime i can either use PSPad or "do bad things" and use the xmp tag (it seems to be supported although deprecated).

Thanks for your help!
smr
smr
< = <
> = >
& = &

use the
 tag around your code to keep your spacing.
Rob Loach
Rob Loach
There's a PHP library out there called Geshi that pretty much formats almost any language in a nice HTML format. You might want to have a look at it.
Rob Loach [Website] [Projects] [
Dim_Yimma_H
Dim_Yimma_H
The GeSHi library output looks pretty neat, another reason to learn PHP it seems. I'll make sure to take a look at it. Thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.