tags:

views:

158

answers:

5

I have many source/text file, say file.cpp or file.txt . Now, I want to see all my code/text in browser, so that it will be easy for me to navigate many files.

My main motive for doing all this is, I am learning C++ myself, so whenever I learn something new, I create some sample code and then compile and run it. Also, along these codes, there are comments/tips for me to be aware of. And then I create links for each file for easy navigation purpose. Since, there are many such files, I thought it would be easy to navigate it if I use this html method. I am not sure if it is OK or good approach, I would like to have some feedback.

What I did was save file.cpp/file.txt into file.html and then use pre and code html tag for formatting. And, also some more necessare html tags for viewing html files.

But when I use it, everything inside < > is lost eg. #include <iostream> is just seen as #include, and <iostream> is lost.

Is there any way to see it, is there any tag or method that I can use ?

I can use regular HTML escape code < and > for this, to see < > but since I have many include files and changing it for all of them is bit time-consuming, so I want to know if there is any other idea ??

So is there any other solution than s/</&lt; and s/>/&gt;

I would also like to know if there any other ideas/tips than just converting cpp file into html.

What I want to have is, in my main page something like this,

tip1 Do this

tip2 Do that

When I click tip1, it will open tip1.html which has my codes for that tip. And also there is back link in tip1.html, which will take me back to main page on clicking it. Everything is OK just that everything inside < > is lost,not seen.

Thanks.

A: 

A few ideas:

  • If you serve the files as mimetype text/plain, the browser should display the text for you.

  • You could also possibly configure your browser to assume .cpp is text/plain.

  • Instead of opening the files directly in the browser, you could serve them with a web server than can change the characters for you.

Ned Batchelder
IE has a habit of assuming anything that looks like HTML is HTML, even if it's served with a different MIME type.
Rob
+3  A: 

You might want to take a look at online tools such as CodeHtmler, which allows you to copy into the browser, select the appropriate language, and it'll convert to HTML for you, together with keyword colourisation etc.

Brian Agnew
Thanks, it worked. But, is there any way to scriptise this whole process i.e. take file.cpp, paste it-in CodeHtmler and then get file.html and save it. Is it OK to ask here, or should I ask new question ??
seg.server.fault
I suspect you could knock together a (say) Perl script to perform a form submission (most likely a POST). Alrternatively, note that the source code is available (there's a link on the website), so you could compile in a batch-driven fashion, most likely.
Brian Agnew
+1  A: 

Or, do like many other people and put your documentation in Doxygen format (/** */) with code samples in @verbatim/@endverbatim tags. Doxygen is good stuff.

xcramps
A: 

You could also use SyntaxHighlighter to display the code on the client side using JavaScript.

Peter Jansson
A: 

It is pretty much essential that somewhere along the line you use a program to prevent the characters '<>&' from being (mis-)interpreted by your browser (and expand significant repeated blanks into '` '). You have a couple of options for when/how to do that. You could use static HTML, simply converting each file once before putting it into the web server document hierarchy. This has the least conversion overhead if the files are looked at more often than they are modified. Alternatively, you can configure your web server to server the pages via a filter program (CGI, or something more sophisticated) and serve the output of that in lieu of the file. The advantage is that files are only converted when needed; the disadvantage is that the files are converted each time they are needed. You could get fancy and consider a caching solution - convert the file on first demand but retain the converted file for future use. The main downside there is that the web server needs to be able to write to where the converted file is cached - not necessarily a good idea for security reasons. (A minimalist approach to security requires the document hierarchy to be owned by and only writable by one user, say webmaster, and the web server runs as another user, say webserver. Now the web server cannot do any damage because it cannot write anywhere in the document hierarchy. Simple; effective; restrictive.)

The program can be a simple Perl script or a simple C program (the C source for webcode 1.3 is available here).

Jonathan Leffler