CSV to HTML translation: Difference between revisions
Content added Content deleted
(→{{header|TXR}}: The comment was next to the {{incorrect}} message; if it moves to the talk page, then fewer readers can find it.) |
(→{{header|C}}: decruft) |
||
Line 232: | Line 232: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Extra credit is a little pointless for this task. One could always do <code>tr:first-child > td {}</code> in CSS anyway. |
|||
This produces a full bare html (tidy gives 1 warning) with styles embedded but not |
|||
"inlined"; it does not escape characters that does not need to be escaped (provided |
|||
that the correct encoding matching the encoding of the input is given; currently |
|||
hard-endoded UTF-8). |
|||
<lang c>#include <stdio.h> |
<lang c>#include <stdio.h> |
||
#include <stddef.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
char input[] = |
|||
#define BUF_LEN 12 |
|||
"Character,Speech\n" |
|||
"The multitude,The messiah! Show us the messiah!\n" |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; " |
|||
"he's a very naughty boy! Now go away!</angry>\n" |
|||
"The multitude,Who are you?\n" |
|||
"Brians mother,I'm his mother; that's who!\n" |
|||
"The multitude,Behold his mother! Behold his mother!"; |
|||
int main() |
|||
void html_min_header(const char *enc, const char *title) |
|||
{ |
{ |
||
char *s = input; |
|||
printf("<html><head><title>%s</title>" |
|||
printf("<table><tr><td>"); |
|||
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" |
|||
for (s = input; *s; s++) { |
|||
"<style type=\"text/css\"><!--" |
|||
switch(*s) { |
|||
"th {font-weight:bold;text-align:left;background-color:yellow}" |
|||
case '\n': printf("</td></tr><tr><td>"); break; |
|||
"table,td,th {border:1px solid #000;border-collapse:collapse}" |
|||
case ',': printf("</td><td>"); break; |
|||
"td {background-color:cyan}" |
|||
case '<': printf("<"); break; |
|||
"td,th {padding:5px}//-->" |
|||
case '>': printf(">"); break; |
|||
"</style></head><body>", title, enc); |
|||
case '&': printf("&"); break; |
|||
} |
|||
default: putchar(*s); |
|||
} |
|||
} |
|||
puts("</td></tr></table>"); |
|||
return 0; |
|||
void html_min_footer(void) |
|||
}</lang>output: [snipped: pre/nowiki tag unescapes &-escaped chars, run the code yourself to see the output.] |
|||
{ |
|||
printf("</body></html>"); |
|||
} |
|||
void escape_html(char *o, int c) |
|||
{ |
|||
static const char *specials = "<>&"; |
|||
static const char *map[] = { "<", ">", "&"}; |
|||
ptrdiff_t pos; |
|||
char *p; |
|||
if ( (p = strchr(specials, c)) != NULL ) { |
|||
pos = p - specials; |
|||
if (o != NULL) strcpy(o, map[pos]); |
|||
} else { |
|||
o[0] = c; |
|||
o[1] = '\0'; |
|||
} |
|||
} |
|||
void add_column(const char *type, int c) |
|||
{ |
|||
char buf[BUF_LEN]; |
|||
if ( c == '\n' ) return; |
|||
printf("<%s>", type); |
|||
for(; c != EOF && c != '\n'; c = getchar()) { |
|||
if (c == ',') { |
|||
printf("</%s><%s>", type, type); |
|||
continue; |
|||
} |
|||
escape_html(buf, c); |
|||
printf("%s", buf); |
|||
} |
|||
printf("</%s>", type); |
|||
} |
|||
enum mode { |
|||
FIRST = 1, NEXT |
|||
}; |
|||
int main(int argc, char **argv) |
|||
{ |
|||
int c; |
|||
enum mode status = FIRST; |
|||
html_min_header("utf-8", "CSV converted into HTML"); |
|||
printf("<table summary=\"data\">"); |
|||
while( (c = getchar()) != EOF ) { |
|||
printf("<tr>"); |
|||
switch(status) { |
|||
case FIRST: |
|||
add_column("th", c); |
|||
status = NEXT; |
|||
break; |
|||
case NEXT: |
|||
default: |
|||
add_column("td", c); |
|||
break; |
|||
} |
|||
printf("</tr>"); |
|||
} |
|||
printf("</table>"); |
|||
html_min_footer(); |
|||
return EXIT_SUCCESS; |
|||
}</lang> |
|||
Output: |
|||
<lang html5><html><head><title>CSV converted into HTML</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css"><!--th {font-weight:bold;text-align:left;background-color:yellow}table,td,th {border:1px solid #000;border-collapse:collapse}td {background-color:cyan}td,th {padding:5px}//--></style></head><body><table summary="data"><tr><th>Character</th><th>Speech</th></tr><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr><tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table></body></html></lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |