/* * Filter to change some troff requests into the corresponding * HTML or wiki requests. * $Id: setfont.c,v 1.3 2006/05/19 05:39:31 grog Exp grog $ */ #include #include #include #include #include #include #include #include #define BIGBUF 1024 #define OUTPUT(x) { char *foo = x; while (*foo) *op++ = *foo++; } char inbuf [BIGBUF]; char outbuf [BIGBUF]; char filename [MAXPATHLEN]; /* name of current file */ char lastfilename [MAXPATHLEN]; /* name of current file */ char *op; struct xfont { char *trofffont; char *htmlfont; char *htmlofffont; } htmlfonts [] = { {"CW", "", ""}, {"R", "", ""}, {"B", "", ""}, {"I", "", ""}, {"CB", "", ""}, {"CI", "", ""}, {"BI", "", ""} }, wikifonts [] = { {"CW", "", ""}, {"R", "", ""}, {"B", "'''", "'''"}, {"I", "''", "''"}, {"CB", " '''", " '''"}, {"CI", " ''", " ''"}, {"BI", "'''''", "'''''"} }, *fonts = htmlfonts; int fontentries = sizeof (htmlfonts) / sizeof (struct xfont); struct escapees { char c; char *replacement; } escapee [] = { {'<', "<"}, {'>', ">"}, {'æ', "æ"}, {'Æ', "&Aelig;"}, {'é', "é"}, {'É', "É"}, {'&', "&"}}; char *lastfont; int lastfontsize = 0; /* previous troff font size */ int fontsize = 12; /* current font size */ int htmlfontsize; /* corresponding HTML size */ int centrecount = 0; /* number of lines yet to centre */ char *findspecial (char c) { int i; for (i = 0; i < sizeof (escapee) / sizeof (struct escapees); i++) if (c == escapee [i].c) return escapee [i].replacement; return NULL; } char * dofonts (char *cp) { while (isspace (*cp)) /* skip spaces */ cp++; if (*cp == '+') fontsize += atoi (&cp [1]); else if (*cp == '-') fontsize -= atoi (&cp [1]); else fontsize = atoi (&*cp); if (fontsize == 0) /* reset */ { OUTPUT (""); fontsize = lastfontsize; } else { char temp [64]; if (fontsize >= 20) htmlfontsize = 7; else if (fontsize >= 17) htmlfontsize = 6; else if (fontsize >= 14) htmlfontsize = 5; else if (fontsize >= 12) htmlfontsize = 4; else if (fontsize >= 10) htmlfontsize = 3; else if (fontsize >= 8) htmlfontsize = 2; else htmlfontsize = 1; sprintf (temp, "", htmlfontsize); OUTPUT (temp); } lastfontsize = fontsize; while (isdigit (*++cp)); return cp; } int main (int argc, char *argv []) { int line = 0; /* line number */ int doskip = 0; /* skip tables or the rest */ int skipping = 0; /* set to 1 to skip output */ int tabs = 0; /* set to 1 to output only tables and pics */ int wiki = 0; /* output in wiki format, not HTML */ char *cp; char *ep; char *estring; /* escape string */ int i; /* loop index */ for (i = 1; i < argc; i++) { if (argv [i] [0] == '-') { switch (argv [i] [1]) { /* Skip tables, or skip the rest */ case 's': doskip = 1; break; /* -t: output for tables only */ case 't': tabs = 1; skipping = 1; doskip = 1; break; case 'w': wiki = 1; fonts = wikifonts; /* point to correct font conversion table */ fontentries = sizeof (wikifonts) / sizeof (struct xfont); /* and number of entries */ break; default: fprintf (stderr, "Usage:\n\n\t%s [-s] [-t] [-w]\n", argv [0]); exit (1); } } else { fprintf (stderr, "Usage:\n\n\t%s [-s] [-t] [-w]\n", argv [0]); exit (1); } } while (fgets (inbuf, BIGBUF, stdin)) { line++; op = outbuf; cp = inbuf; ep = &inbuf [strlen (inbuf) - 1]; *ep = '\0'; /* chop off this damn trailing \n */ if (centrecount) { if ((*inbuf != '\0') /* something there */ && (*inbuf != '.') ) /* and not a command */ { puts ("
"); if (--centrecount == 0) /* finished */ puts (""); } } if (skipping) { if (tabs) { if (! memcmp (inbuf, ".TS", 3) /* table start: turn output on again */ || (! memcmp (inbuf, ".PS", 3)) ) skipping = 0; /* stop skipping and output this line */ else continue; } else /* no tables, */ { if (! memcmp (inbuf, ".TE", 3) /* table start: turn output on again */ || (! memcmp (inbuf, ".PE", 3)) ) skipping = 0; continue; /* don't output this line, though */ } } else /* not skipping */ /* Check if we should start skipping */ { #ifdef UNDERSTOOD /* What is this crap? */ if (! memcmp (inbuf, ".ps", 3)) /* found a size spec */ { puts ("
"); continue; } #endif if (! memcmp (inbuf, ".ps", 3)) /* found a size spec */ cp = dofonts (&inbuf [3]); else if (! memcmp (inbuf, ".ce", 3)) /* found a center thing */ { cp = &inbuf [3]; while (isspace (*cp)) /* skip spaces */ cp++; centrecount = atoi (cp); centrecount += 1; /* default centre one line */ puts ("
"); continue; } else /* no tables, */ { if (! memcmp (inbuf, ".TS", 3) /* table start: turn output on again */ || (! memcmp (inbuf, ".PS", 3)) ) { skipping = 1; continue; /* don't output this line, though */ } } } while (*cp) /* look for things */ { if (! memcmp (cp, "\\f", 2)) /* found a font */ { int i; char fontname [3]; if (cp [2] == '(') /* two-letter font */ { memcpy (fontname, &cp [3], 2); fontname [2] = '\0'; cp += 5; } else if (cp [2] == '[') { int i = 0; cp = &cp [3]; /* look at it */ while ((*cp != ']') && (i < sizeof (fontname) - 1) ) fontname [i++] = *cp++; fontname [i] = '\0'; i = sizeof (fontname) - 1; if (i) fprintf (stderr, "Font name starting with %s is too long\n:%s\n", fontname, inbuf); } else { fontname [0] = cp [2]; fontname [1] = '\0'; cp += 3; } if (! strcmp (fontname, "P")) /* previous font? */ { if (lastfont) OUTPUT (lastfont); lastfont = NULL; } else { for (i = 0; i < fontentries; i++) { if (! (strcmp (fontname, fonts [i].trofffont))) /* found the font */ { if (lastfont) /* turn off previous font */ OUTPUT (lastfont); lastfont = fonts [i].htmlofffont; /* note how to turn off this one */ OUTPUT (fonts [i].htmlfont); break; } } if (i == fontentries) /* didn't find it */ fprintf (stderr, "can't find font %s:\n%s\n", fontname, inbuf); } } else if (! memcmp (cp, "\\s", 2)) /* found a size spec */ cp = dofonts (&cp [2]); else if (estring = findspecial (*cp)) { while (*estring) *op++ = *estring++; cp++; } else /* nothing special, just copy */ *op++ = *cp++; } *op = '\0'; puts (outbuf); if ((tabs) && (! memcmp (inbuf, ".TE", 3) /* table start: turn output on again */ || (! memcmp (inbuf, ".PE", 3)) ) ) skipping = 1; /* stop skipping and output this line */ } return 0; }