Display C in HTML

A

Adam

Hi,

Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?

Thanks a lot,
Adam
 
A

Army1987

Adam said:
Hi,

Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?

Thanks a lot,
Adam

That's not a question about C.
<ot>
Have you tried the <pre> tag? You'll still need to escape characters such as <, &, etc...
</ot>
 
F

Flash Gordon

Adam wrote, On 30/06/07 12:08:
Hi,

Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?

Almost certainly. Have a look at http://clc-wiki.net/wiki/intro_to_clc
for information on why many will not consider your post topical, and dig
around further on the site for possible enlightenment with your problem
depending on what your real requirements are.
 
C

CBFalconer

Adam said:
Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?

No converter needed. Just display the text.
 
R

Richard Heathfield

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;
}
 
A

Army1987

Adam said:
Hi,

Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?

Thanks a lot,
Adam

Try this:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int status = 0;
int ch;
FILE *in, *out;
in = argc < 2 ? stdin : fopen(argv[1], "r");
if (in == NULL) {
fprintf(stderr, "Cannot open '%s' ", argv[2]);
perror("for input");
return EXIT_FAILURE;
}
out = argc < 3 ? stdout : fopen(argv[2], "w");
if (out == NULL) {
fprintf(stderr, "Cannot create or open '%s' ", argv[2]);
perror("for output");
fclose(in);
return EXIT_FAILURE;
}
fputs("<html><head></head><body><p><pre>", out);
do switch (ch = getc(in)) {
case '"': fputs("&quot;", out); continue;
case '&': fputs("&amp;", out); continue;
case '<': fputs("&lt;", out); continue;
case '>': fputs("&gt;", out); continue;
default : putc(ch, out); continue;
case EOF: if (ferror(in)) perror("Read error"); break;
} while (ch != EOF);
fputs("</pre></p></body></html>\n", out);
status = ferror(in) || ferror(out) ? EXIT_FAILURE : 0;
if (fclose(in) != 0) {
perror("Error closing input file");
status = EXIT_FAILURE;
}
if (fclose(out) != 0) {
perror("Error closing output file");
status = EXIT_FAILURE;
}
return 0;
}

HTH.
 
A

Army1987

CBFalconer said:
No converter needed. Just display the text.

<ot>
What do you expect to happen when you open a file containing less-
than signs with an HTML browser? Have you tried it?
</ot>
 
A

Adam

Adam wrote, On 30/06/07 12:08:



Almost certainly. Have a look at http://clc-wiki.net/wiki/intro_to_clc
for information on why many will not consider your post topical,

Yes, sorry, I guess it is a bit off-topic. Thanks for the link though.
I particularly like the (probably valid) whole section dedicated to a
defence of pedantry ;)
and dig around further on the site for possible enlightenment
with your problem

In my defence I did spend a while browsing around the c.l.c archives
but only found some dud links.

Thanks,
Adam
 
F

Flash Gordon

Adam wrote, On 30/06/07 15:32:
In my defence I did spend a while browsing around the c.l.c archives
but only found some dud links.

You will find the site does full syntax highlighting of C code on it and
there is a section on how the site is configured.
 
M

Malcolm McLean

Adam said:
I guess you're right, sorry.


Yes, but I was looking for something which does nice colouring/
formatting etc. Thanks to a private email I've been pointed at c2HTML
which looks perfect.
That's a perfectly topical subject, if somewhat unusual. Showing C code to
other programmers is an important part of what we do.
 
K

Keith Thompson

Army1987 said:
<ot>
What do you expect to happen when you open a file containing less-
than signs with an HTML browser? Have you tried it?
</ot>

<OT>
I expect it to display them correctly, *unless* the file name has a
".htm" or ".html" suffix or is otherwise marked as HTML.
</OT>
 
R

Richard Tobin

<OT>
I expect it to display them correctly, *unless* the file name has a
".htm" or ".html" suffix or is otherwise marked as HTML.
</OT>

I was on the point of making the same reply, but then I noticed that
Army specified "an HTML browser", rather than "a web browser". I'm
not aware of any HTML-only browsers, so his point is only vacuously
correct.

A web browser should display a C program correctly if the file is
determined to be plain text, either by the server or by rules in the
browser.

-- Richard
 
S

SM Ryan

# Adam wrote, On 30/06/07 12:08:
# > Hi,
# >
# > Does anyone know of an online C-to-HTML converter to display code
# > nicely formatted in a browser?
#
# Almost certainly. Have a look at http://clc-wiki.net/wiki/intro_to_clc
# for information on why many will not consider your post topical, and dig
# around further on the site for possible enlightenment with your problem
# depending on what your real requirements are.

What's interesting is your smart ass response wasted more electrons
than simply letting other people respond.

And why isn't the presentation of C code is not topical for comp.lang.c?
Try rereading posts from 1990 and see how the group used to be useful.
 
W

Wade Ward

SM Ryan said:
# Adam wrote, On 30/06/07 12:08:
# > Hi,
# >
# > Does anyone know of an online C-to-HTML converter to display code
# > nicely formatted in a browser?
#
# Almost certainly. Have a look at http://clc-wiki.net/wiki/intro_to_clc
# for information on why many will not consider your post topical, and dig
# around further on the site for possible enlightenment with your problem
# depending on what your real requirements are.

What's interesting is your smart ass response wasted more electrons
than simply letting other people respond.

And why isn't the presentation of C code is not topical for comp.lang.c?
Try rereading posts from 1990 and see how the group used to be useful.
It is completely within the grasp of ISO C to do what you want, that is, if
HTML can be represented by C char arrays.

What code do you have so far?
 
W

Walter Roberson

That's a perfectly topical subject, if somewhat unusual.

You can colour within the C standard???
Showing C code to
other programmers is an important part of what we do.

Is there at least an TR that specifies that gets() must be rendered
in extra-large blinking ?
 
R

Richard Heathfield

Walter Roberson said:
You can colour within the C standard???

Sort of, yes. You can write a standard C program that will take C code
as input and produce as output a CSS to control colouring and an HTML
page with the input code translated into valid HTML, nicely formatted
and syntax coloured.
Is there at least an TR that specifies that gets() must be rendered
in extra-large blinking ?

That reminds me of the Far Side cartoon of a (typically Larsonesque) man
emerging from the "Gents" in a restaurant. Above his head is a sign,
flashing the message "DIDN'T WASH HANDS".

<SFX: rummage rummage>

Ah! Here we are:

http://www.2000greetings.com/previewcard.htm?c=197
 
W

Walter Roberson

Walter Roberson said:
Sort of, yes. You can write a standard C program that will take C code
as input and produce as output a CSS to control colouring and an HTML
page with the input code translated into valid HTML, nicely formatted
and syntax coloured.

You can use a standard C program to construct HTML text that
*when interpreted by a program that uses system specific constructs
in an appropriately equipped and configured system* would
display nice colouration -- but the HTML text -itself- produced
by the C program isn't going to be syntax coloured without resorting
to system-specific extensions.

But then for any given program in any text-programmable language
(plus system-extensions plus appropriate hardware), one could write
a C program that produced that program. It doesn't seem to me that
the ability to write a C program that writes text in a different
programming language qualifies the text so produced as topical.
 
R

Richard Heathfield

Walter Roberson said:
You can use a standard C program to construct HTML text that
*when interpreted by a program that uses system specific constructs
in an appropriately equipped and configured system* would
display nice colouration -- but the HTML text -itself- produced
by the C program isn't going to be syntax coloured without resorting
to system-specific extensions.

Yes, sorry, I was of course aware of this, but I should have made it
more explicit in my reply.
But then for any given program in any text-programmable language
(plus system-extensions plus appropriate hardware), one could write
a C program that produced that program. It doesn't seem to me that
the ability to write a C program that writes text in a different
programming language qualifies the text so produced as topical.

I'm not suggesting that the text so produced is topical. I'm suggesting
that the program that produces that text is (or at least can be)
topical.
 
E

Ed Jensen

Adam said:
Hi,

Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?

Thanks a lot,
Adam

The jEdit editor -- http://www.jedit.org/ -- has a plug-in available
that can take your C source code and generate syntax highlighted HTML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top