pre2html

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/pre2html.git/
Log | Files | Refs

pre2html.c (3298B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 
      4 enum {
      5   BLEN = 10240
      6 };
      7 
      8 static inline void write_char(char x) {
      9   write(1, &x, sizeof(char));
     10 }
     11 
     12 static inline void write_string(char *x) {
     13   write(1, x, strlen(x));
     14 }
     15 
     16 static int line(char *buf, char *end) {
     17   int n = 0;
     18   while(buf < end) {
     19     char c;
     20     int m = read(0, &c, sizeof(char));
     21     if(m <= 0) break;
     22     if(m != sizeof(char)) exit(-1);
     23     *buf++ = c;
     24     n++;
     25     if('\n' == c) break;
     26   }
     27   return n;
     28 }
     29 
     30 static int head(char *what, char *buf, char *end) {
     31   while(*what && buf < end) {
     32     if(*what++ != *buf++) return 0;
     33   }
     34   return !*what;
     35 }
     36 
     37 static int tail(char *what, char *buf, char *end) {
     38   while(*what && buf < --end) {
     39     if(*what++ != *end) return 0;
     40   }
     41   return !*what;
     42 }
     43 
     44 static char *find(char what, char *buf, char *end) {
     45   for(; buf < end; buf++)
     46     if(what == *buf) return buf;
     47   return NULL;
     48 }
     49 
     50 static void attr(char *buf, char *end) {
     51   for(; buf < end; buf++)
     52     switch(*buf) {
     53     case '"': write_string("&quot;"); break;
     54     default: write_char(*buf);
     55     }
     56 }
     57 
     58 static escape(char c) {
     59   switch(c) {
     60   case '<': write_string("&lt;"); break;
     61   case '>': write_string("&gt;"); break;
     62   case '&': write_string("&amp;"); break;
     63   case '"': write_string("&quot;"); break;
     64   default: write_char(c);
     65   }
     66 }
     67 
     68 static void text(char *buf, char *end) {
     69   for(; buf < end; buf++)
     70     escape(*buf);
     71 }
     72 
     73 static void rest(char *b, char *e) {
     74   while(b < e) {
     75     if(head("<http:", b, e) || head("<https:", b, e)) {
     76       escape(*b++);
     77       char *n = find('>', b, e);
     78       write_string("<a href=\"");
     79       attr(b, n);
     80       write_string("\">");
     81       text(b, n);
     82       write_string("</a>");
     83       b = n;
     84       escape(*b++);
     85     }
     86     else if(head("<#", b, e)) {
     87       b += 2;
     88       char *n = find(' ', b, e);
     89       write_string("<a href=\"#");
     90       attr(b, n);
     91       write_string("\">");
     92       b = n + 1;
     93       n = find('>', b, e);
     94       text(b, n);
     95       write_string("</a>");
     96       b = n + 1;
     97     }
     98     else escape(*b++);
     99   }
    100 }
    101 
    102 int main() {
    103   char buf[BLEN];
    104   char *end = buf + BLEN;
    105   int n;
    106   int bold = 0;
    107   write_string("<pre>\n");
    108   while(0 < (n = line(buf, end))) {
    109     char *b = buf;
    110     char *e = buf + n;
    111     if(head("CODE", b, e))
    112       write_string("<div style=\"color:#080\">");
    113     else if(head("END", b, e))
    114       write_string("</div>");
    115     else if(head("\f", b, e)) {
    116       if(bold) write_string("</div>");
    117       write_string("<div style=\"font-weight:bold\">");
    118       b++;
    119       bold = 1;
    120       if('\n' != *b) {
    121         write_string("<a name=\"");
    122         char *n = find('\n', b, e);
    123         attr(b, n);
    124         write_string("\"></a>");
    125         b = n;
    126       }
    127       escape(*b++);
    128     }
    129     else if(bold && (tail("\n=", b, e) || tail("\n-", b, e))) {
    130       rest(b, e);
    131       write_string("</div>");
    132       bold = 0;
    133     }
    134     else if(head("> ", b, e)) {
    135       write_string("<span style=\"color:red\">");
    136       rest(b, e);
    137       write_string("</span>");
    138     }
    139     else if(head(">> ", b, e)) {
    140       write_string("<span style=\"color:green\">");
    141       rest(b, e);
    142       write_string("</span>");
    143     }
    144     else if(head(">>> ", b, e)) {
    145       write_string("<span style=\"color:blue\">");
    146       rest(b, e);
    147       write_string("</span>");
    148     }
    149     else rest(b, e);
    150   }
    151   write_string("</pre>\n");
    152   return 0;
    153 }