Sign in to follow this  
Dim_Yimma_H

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

Recommended Posts

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 (<iostream> and anything after "Sometext." < isn't displayed):
<pre class="code">
#include <iostream>

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




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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this