• Advertisement
Sign in to follow this  

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

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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.

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
< = &lt;
> = &gt;
& = &amp;

use the <pre> tag around your code to keep your spacing.

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement