Re: Display C in HTML



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("&amp;"); break;
case '<': printf("&lt;"); break;
case '>': printf("&gt;"); break;
case '"': printf("&quot;"); 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
.



Relevant Pages

  • Re: Display C in HTML
    ... #> Does anyone know of an online C-to-HTML converter to display code ... #> nicely formatted in a browser? ... # depending on what your real requirements are. ...
    (comp.lang.c)
  • Re: Display C in HTML
    ... Adam wrote, On 30/06/07 12:08: ... Does anyone know of an online C-to-HTML converter to display code ... nicely formatted in a browser? ...
    (comp.lang.c)
  • [OT] Re: Display C in HTML
    ... Does anyone know of an online C-to-HTML converter to display code ... than signs with an HTML browser? ...
    (comp.lang.c)
  • Re: Display C in HTML
    ... Adam wrote: ... Does anyone know of an online C-to-HTML converter to display code ... nicely formatted in a browser? ...
    (comp.lang.c)