Re: Display C in HTML
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Sat, 30 Jun 2007 13:38:39 +0000
Adam said:
Hi,
Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?
The following code is online, in the sense that you can find it in this
'ere Usenet article. It doesn't display a complete page (because I
found that that was usually not what I wanted); rather, it wraps pre
and code tags around the code, and does the "usual HTML conversions",
so to speak. (At least, it does if you spec -c in the command line
args.)
I use it from within vim like this:
:.,+5!text2html -c
(where you replace 5 with however many lines the code is, less 1).
Here's the code:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int code = 0;
int ch;
if(argc > 1 && strcmp(argv[1], "-c") == 0)
{
code = 1;
}
if(!code)
{
printf("<p>\n");
}
else
{
printf("<pre><code>\n");
}
while((ch = getchar()) != EOF)
{
switch(ch)
{
case '&': printf("&"); break;
case '<': printf("<"); break;
case '>': printf(">"); break;
case '"': printf("""); break;
case '\n': if(!code) { printf("</p>\n<p>\n"); }
else { putchar(ch); } break;
default: putchar(ch); break;
}
}
if(!code)
{
printf("</p>\n");
}
else
{
printf("</code></pre>\n");
}
return 0;
}
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- References:
- Display C in HTML
- From: Adam
- Display C in HTML
- Prev by Date: Re: storing related structs
- Next by Date: Machine epsilon: conclusion
- Previous by thread: Re: [OT] Re: Display C in HTML
- Next by thread: Re: Display C in HTML
- Index(es):
Relevant Pages
|