Publishing Code on the Web

by Marshall Farrier|8/2/09

After deciding to create an I.T. website, I realized that I would often be posting solutions in the form of code. This site will frequently offer HTML code, CSS, JavaScript and other source code for creating desired effects on a web browser or for gathering or processing data in some special way. One could of course ask viewers to look at the source code in a separate window by applying the usual method, but I thought it would be a fun challenge (and certainly more elegant) to develop a C++ program that actually translates a page of source code (HTML, for example) into code that displays on a webpage what you see in your text editor. Viewers of a website don't typically see the code for your webpage but rather what their browser renders when it encounters that code.

I'll show here only the function that does the translation. It's the keystone for the program, and it looks like this:

void transHtml(const char * sourcePName, const char * transPName)
{
using namespace std;
fstream inFile;
inFile.open(sourcePName, ios::in);
if (inFile.fail())
{
cout << "Source file doesn't exist c:\\html_trans\\\n";
cout << "Ending program.\n";
return;
}
string trans = "";
char tempCh;

for (int i = 0; i < 5000; i++)
{
inFile.get(tempCh);
if (inFile.eof())
break;
switch (tempCh)
{
case '<':
trans.append("&lt;");
break;
case '>':
trans.append("&gt;");
break;
case '&':
trans.append("&amp;");
break;
case '\n':
trans.append("<br />\n");
break;
default:
trans.append(1, tempCh);
}
}
inFile.clear();
inFile.close();
if (inFile.fail())
{
cout << "Error closing file.\n"
<< "Aborting program.\n";
exit(EXIT_FAILURE);
}

fstream outFile;
outFile.open(transPName, ios::out);
if (outFile.fail())
{
cout << "Error opening file.\n"
<< "Aborting program.\n";
exit(EXIT_FAILURE);
}

for (int i = 0; i < trans.length(); i++)
{
outFile.put(trans.at(i));
}
outFile.close();
if (outFile.fail())
{
cout << "Error closing file.\n"
<< "Aborting program.\n"
<< "Translation file may be corrupt!\n";
exit(EXIT_FAILURE);
}
cout << "Translation complete!\n";
}

Believe it or not, to get the above text, I didn't have to change a single character or line break but just cut and pasted the translation file after running the source code file through the program. The only thing the program didn't do was to make the indentations. I had to go through the text and add those. Still, it does translate a batch of code into usable form for a web-page and takes care of the confusing special symbols. In a pinch, you could present the text without the indentations, but the latter really do improve clarity. So, I think it's worth making the effort to add them. I'm confident that the program will also translate HTML code properly--and will certainly soon have the opportunity to test it on HTML files [Note 8/14/09: I've used it now on HTML, CSS, and JavaScript files, and the program has translated everything correctly].

Complete source code: html_translate.zip

articles

This site is currently being re-organized. As a result, several of the navigation choices above may not yet be active.