summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2011-01-24 20:57:02 -0500
committerbnewbold <bnewbold@robocracy.org>2011-01-24 20:57:02 -0500
commitd5e1adf060a23e6c739d0dd78fe9321fb0eb7582 (patch)
tree51196fe755ba36c0058fbfa487bbc0519ed76bc3
downloadlearning_c-d5e1adf060a23e6c739d0dd78fe9321fb0eb7582.tar.gz
learning_c-d5e1adf060a23e6c739d0dd78fe9321fb0eb7582.zip
crude c comment highlighter
-rw-r--r--highlight_comments.c93
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("&lt;");
+ break;
+ case '>':
+ printf("&gt;");
+ break;
+ case '&':
+ printf("&amp;");
+ break;
+ default:
+ putc(c,stdout);
+ }
+}
+