diff options
-rw-r--r-- | highlight_comments.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/highlight_comments.c b/highlight_comments.c new file mode 100644 index 0000000..ad270f4 --- /dev/null +++ b/highlight_comments.c @@ -0,0 +1,93 @@ + +/* Here are all the header files */ +#include <stdio.h> +#include <ctype.h> + +/* Some defines */ +#define false 0 +#define true 1 + +/* Function definitions */ +void putc_html(char c); + + +int main(void) { + unsigned char c = ' ', last_c = ' '; /* TODO */ + int not_whitespace = false; + int do_inline = false; + int comment = false; + int index; + + printf("<html><head><style>\n" /* kind of a big ugly prinf here... */ + "body {background-color: black; color: white; }\n" + "td {vertical-align: top; padding: 0px; padding-left: 10px;}\n" + "pre {margin: 0px; }\n" + ".comment {color: #33FF33; font-weight: bold;}\n" + "</style></head><body>\n" + "<table><tr><td><td><pre>"); + + while((last_c = c) && (c = getc(stdin))) { + if(comment) { + if(last_c == '*' && c == '/') { + comment = false; + if(do_inline) { + printf("*/</span>"); + } else { + printf("</pre><td><pre>"); + } + not_whitespace = true; + c = getc(stdin); + continue; + } + if(not_whitespace) + putc_html(last_c); + if(! (isspace(c) || c == '*')) + not_whitespace = true; + if(last_c == '\n') + not_whitespace = false; + } else { + if(last_c == '/' && c == '*') { + comment = true; + if(not_whitespace) { + do_inline = true; + printf("<span class=\"comment\">/\x2A "); /* heh */ + } else { + do_inline = false; + printf("</pre><tr><td class=\"comment\"><pre>"); + } + not_whitespace = false; + continue; + } + if(! isspace(last_c)) + not_whitespace = true; + if(not_whitespace) + putc_html(last_c); + if(last_c == '\n' && c == '\n' && not_whitespace) { + not_whitespace = false; + putc('\n', stdout); + } + } + + if(feof(stdin) || ferror(stdin)) break; + } + printf("</table></body></html>\n"); + return 0; +} + +/* This function puts some stuff! Great! */ +void putc_html(char c) { + switch(c) { + case '<': + printf("<"); + break; + case '>': + printf(">"); + break; + case '&': + printf("&"); + break; + default: + putc(c,stdout); + } +} + |