summaryrefslogtreecommitdiffstats
path: root/highlight_comments.c
blob: ad270f469c49f4c1d3db62dfb64429958745f636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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);
  }
}