C to HTML
This program translates one or more C source files into a well-formed, valid XHTML1.0 document. It was used to create amost of the C program listings on this site.
The program works in the following way:
- Output basic HTML header information, such as <head> and <title> information, and the main page heading.
- Copy the specified C source files onto the page, surrounded by <pre> tags to preserve spacing.
- "Markup" keywords, standard library functions, preprocessig directives and comments with appropriate <span> tags for formatting. Note that this assumes you have a separate cascading style sheet file which actually defines the specific format to use for keywords and comments (e.g to display keywords in blue, comments in red etc). If you do not, it is a fairly trivial matter to add style sheet commands to the top of the document, or to explicitly specify background images, or to search and replace the <span> tags with more explicit formatting tags.
- Output basic HTML footer information, such as closing <body> and <html> tags.
The program will currently only correctly translate C89 source files, and does not support any additional keywords, library functions or preprocessing directives from the new C99 standard, although C++ style single line comments are supported.
Programming Issues
String handling like this is fairly complex in C, and most of the issues involved revolve around this. The following are the most significant:
- Retrieving the command line arguments, and opening/closing the specified files in an orderly manner.
- Replacing characters which won't output properly in HTML (such as "<") with suitable alternatives.
- Finding and checking each word, and determining whether or not it is a keyword, and whether or not it is inside a comment or a string (in which case it should not be marked up)
- If it is a keyword, comment, standard library function or comment, inserting markup commands at the appropriate points.
Usage
Use the following syntax:
ctohtml file1.c file2.c file3.c > output.html