home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / languages / ami_nuweb0.87c.lha / html.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-12  |  13.1 KB  |  362 lines

  1. #include "global.h"
  2. static void copy_scrap(FILE *);
  3.   /* formats the body of a scrap */
  4. static void display_scrap_ref(FILE *, int);
  5.   /* formats a scrap reference */
  6. static void display_scrap_numbers(FILE *, Scrap_Node *);
  7.   /* formats a list of scrap numbers */
  8. static void print_scrap_numbers(FILE *, Scrap_Node *);
  9.   /* pluralizes scrap formats list */
  10. static void format_entry(Name *name, FILE *html_file, int file_flag);
  11.   /* formats an index entry */
  12. static void format_user_entry(Name *, FILE *);
  13. void write_html(char *file_name, char *html_name)
  14. {
  15.   FILE *html_file = fopen(html_name, "w");
  16.   if (html_file) {
  17.     if (verbose_flag)
  18. #ifdef _AMIGA
  19.       fprintf(stderr, get_string(MSG_VERBOSE_17A), html_name);
  20. #else
  21.       fprintf(stderr, "writing %s\n", html_name);
  22. #endif
  23.     source_open(file_name);
  24.     {
  25.       int scraps = 1;
  26.       int c = source_get();
  27.       while (c != EOF) {
  28.         if (c == '@')
  29.           {
  30.             c = source_get();
  31.             switch (c) {
  32.               case 'O':
  33.               case 'o': {
  34.                           Name *name = collect_file_name();
  35.                           {
  36.                             fputs("\\begin{rawhtml}\n", html_file);
  37.                             fputs("<pre>\n", html_file);
  38.                           }
  39.                             fputs("<a name=\"nuweb", html_file);
  40.                             write_single_scrap_ref(html_file, scraps);
  41.                             fprintf(html_file, "\"><code>\"%s\"</code> ", name->spelling);
  42.                             write_single_scrap_ref(html_file, scraps);
  43.                             fputs("</a> =\n", html_file);
  44.                           
  45.                           scraps++;
  46.                           {
  47.                             copy_scrap(html_file);
  48.                             fputs("<></pre>\n", html_file);
  49.                           }
  50.                           {
  51.                             if (name->defs->next) {
  52.                           #ifdef _AMIGA
  53.                               fputs(get_string(MSG_HTML_31C), html_file);
  54.                           #else
  55.                               fputs("File defined by ", html_file);
  56.                           #endif
  57.                               print_scrap_numbers(html_file, name->defs);
  58.                               fputs("<br>\n", html_file);
  59.                             }
  60.                           }
  61.                           {
  62.                             fputs("\\end{rawhtml}\n", html_file);
  63.                             c = source_get(); /* Get rid of current at command. */
  64.                           }
  65.                         }
  66.                         break;
  67.               case 'D':
  68.               case 'd': {
  69.                           Name *name = collect_macro_name();
  70.                           {
  71.                             fputs("\\begin{rawhtml}\n", html_file);
  72.                             fputs("<pre>\n", html_file);
  73.                           }
  74.                             fputs("<a name=\"nuweb", html_file);
  75.                             write_single_scrap_ref(html_file, scraps);
  76.                             fprintf(html_file, "\"><%s ", name->spelling);
  77.                             write_single_scrap_ref(html_file, scraps);
  78.                             fputs("></a> =\n", html_file);
  79.                           
  80.                           scraps++;
  81.                           {
  82.                             copy_scrap(html_file);
  83.                             fputs("<></pre>\n", html_file);
  84.                           }
  85.                           {
  86.                             if (name->defs->next) {
  87.                           #ifdef _AMIGA
  88.                               fputs(get_string(MSG_HTML_31D), html_file);
  89.                           #else
  90.                               fputs("Macro defined by ", html_file);
  91.                           #endif
  92.                               print_scrap_numbers(html_file, name->defs);
  93.                               fputs("<br>\n", html_file);
  94.                             }
  95.                           }
  96.                           {
  97.                             if (name->uses) {
  98.                           #ifdef _AMIGA
  99.                               fputs(get_string(MSG_HTML_31E1), html_file);
  100.                           #else
  101.                               fputs("Macro referenced in ", html_file);
  102.                           #endif
  103.                               print_scrap_numbers(html_file, name->uses);
  104.                             }
  105.                             else {
  106.                           #ifdef _AMIGA
  107.                               fputs(get_string(MSG_HTML_31E2), html_file);
  108.                               fprintf(stderr, get_string(MSG_WARNING_21A),
  109.                           #else
  110.                               fputs("Macro never referenced.\n", html_file);
  111.                               fprintf(stderr, "%s: <%s> never referenced.\n",
  112.                           #endif
  113.                                       command_name, name->spelling);
  114.                             }
  115.                             fputs("<br>\n", html_file);
  116.                           }
  117.                           {
  118.                             fputs("\\end{rawhtml}\n", html_file);
  119.                             c = source_get(); /* Get rid of current at command. */
  120.                           }
  121.                         }
  122.                         break;
  123.               case 'f': {
  124.                           if (file_names) {
  125.                             fputs("\\begin{rawhtml}\n", html_file);
  126.                             fputs("<dl compact>\n", html_file);
  127.                             format_entry(file_names, html_file, TRUE);
  128.                             fputs("</dl>\n", html_file);
  129.                             fputs("\\end{rawhtml}\n", html_file);
  130.                           }
  131.                           c = source_get();
  132.                         }
  133.                         break;
  134.               case 'm': {
  135.                           if (macro_names) {
  136.                             fputs("\\begin{rawhtml}\n", html_file);
  137.                             fputs("<dl compact>\n", html_file);
  138.                             format_entry(macro_names, html_file, FALSE);
  139.                             fputs("</dl>\n", html_file);
  140.                             fputs("\\end{rawhtml}\n", html_file);
  141.                           }
  142.                           c = source_get();
  143.                         }
  144.                         break;
  145.               case 'u': {
  146.                           if (user_names) {
  147.                             fputs("\\begin{rawhtml}\n", html_file);
  148.                             fputs("<dl compact>\n", html_file);
  149.                             format_user_entry(user_names, html_file);
  150.                             fputs("</dl>\n", html_file);
  151.                             fputs("\\end{rawhtml}\n", html_file);
  152.                           }
  153.                           c = source_get();
  154.                         }
  155.                         break;
  156.               case '@': putc(c, html_file);
  157.               default:  c = source_get();
  158.                         break;
  159.             }
  160.           }
  161.         else {
  162.           putc(c, html_file);
  163.           c = source_get();
  164.         }
  165.       }
  166.     }
  167.     fclose(html_file);
  168.   }
  169.   else
  170. #ifdef _AMIGA
  171.     fprintf(stderr, get_string(MSG_WARNING_17A), command_name, html_name);
  172. #else
  173.     fprintf(stderr, "%s: can't open %s\n", command_name, html_name);
  174. #endif
  175. } static void display_scrap_ref(FILE *html_file, int num)
  176. {
  177.   fputs("<a href=\"#nuweb", html_file);
  178.   write_single_scrap_ref(html_file, num);
  179.   fputs("\">", html_file);
  180.   write_single_scrap_ref(html_file, num);
  181.   fputs("</a>", html_file);
  182. } static void display_scrap_numbers(FILE *html_file, Scrap_Node *scraps)
  183. {
  184.   display_scrap_ref(html_file, scraps->scrap);
  185.   scraps = scraps->next;
  186.   while (scraps) {
  187.     fputs(", ", html_file);
  188.     display_scrap_ref(html_file, scraps->scrap);
  189.     scraps = scraps->next;
  190.   }
  191. } static void print_scrap_numbers(FILE *html_file, Scrap_Node *scraps)
  192. {
  193. #ifdef _AMIGA
  194.   if (scraps->next) fputs(get_string(MSG_HTML_32C1), html_file);
  195.   else fputs(get_string(MSG_HTML_32C2), html_file);
  196. #else
  197.   fputs("scrap", html_file);
  198.   if (scraps->next) fputc('s', html_file);
  199. #endif
  200.   fputc(' ', html_file);
  201.   display_scrap_numbers(html_file, scraps);
  202.   fputs(".\n", html_file);
  203. } static void copy_scrap(FILE *file)
  204. {
  205.   int indent = 0;
  206.   int c = source_get();
  207.   while (1) {
  208.     switch (c) {
  209.       case '@':  {
  210.                    c = source_get();
  211.                    switch (c) {
  212.                      case '@': fputc(c, file);
  213.                                break;
  214.                      case '|': {
  215.                                  do {
  216.                                    do
  217.                                      c = source_get();
  218.                                    while (c != '@');
  219.                                    c = source_get();
  220.                                  } while (c != '}');
  221.                                }
  222.                      case '}': return;
  223.                      case '<': {
  224.                                  Name *name = collect_scrap_name();
  225.                                  fprintf(file, "<%s ", name->spelling);
  226.                                  if (name->defs)
  227.                                    {
  228.                                      Scrap_Node *p = name->defs;
  229.                                      display_scrap_ref(file, p->scrap);
  230.                                      if (p->next)
  231.                                        fputs(", ... ", file);
  232.                                    }
  233.                                  else {
  234.                                    putc('?', file);
  235.                                    fprintf(stderr, "%s: scrap never defined <%s>\n",
  236.                                            command_name, name->spelling);
  237.                                  }
  238.                                  fputs(">", file);
  239.                                }
  240.                                break;
  241.                      default:  /* ignore these since pass1 will have warned about them */
  242.                                break;
  243.                    }
  244.                  }
  245.                  break;
  246.       case '<' : fputs("<", file);
  247.                  indent++;
  248.                  break;
  249.       case '>' : fputs(">", file);
  250.                  indent++;
  251.                  break;
  252.       case '&' : fputs("&", file);
  253.                  indent++;
  254.                  break;
  255.       case '\n': fputc(c, file);
  256.                  indent = 0;
  257.                  break;
  258.       case '\t': {
  259.                    int delta = 8 - (indent % 8);
  260.                    indent += delta;
  261.                    while (delta > 0) {
  262.                      putc(' ', file);
  263.                      delta--;
  264.                    }
  265.                  }
  266.                  break;
  267.       default:   putc(c, file);
  268.                  indent++;
  269.                  break;
  270.     }
  271.     c = source_get();
  272.   }
  273. } static void format_entry(Name *name, FILE *html_file, int file_flag)
  274. {
  275.   while (name) {
  276.     format_entry(name->llink, html_file, file_flag);
  277.     {
  278.       fputs("<dt> ", html_file);
  279.       if (file_flag) {
  280.         fprintf(html_file, "<code>\"%s\"</code>\n<dd> ", name->spelling);
  281.         {
  282.         #ifdef _AMIGA
  283.           fputs(get_string(MSG_HTML_35C), html_file);
  284.         #else
  285.           fputs("Defined by ", html_file);
  286.         #endif
  287.           print_scrap_numbers(html_file, name->defs);
  288.         }
  289.       }
  290.       else {
  291.         fprintf(html_file, "<%s ", name->spelling);
  292.         {
  293.           if (name->defs)
  294.             display_scrap_numbers(html_file, name->defs);
  295.           else
  296.             putc('?', html_file);
  297.         }
  298.         fputs(">\n<dd> ", html_file);
  299.         {
  300.           Scrap_Node *p = name->uses;
  301.           if (p) {
  302.         #ifdef _AMIGA
  303.             fputs(get_string(MSG_HTML_35E1), html_file);
  304.         #else
  305.             fputs("Referenced in ", html_file);
  306.         #endif
  307.             print_scrap_numbers(html_file, p);
  308.           }
  309.           else
  310.         #ifdef _AMIGA
  311.             fputs(get_string(MSG_HTML_35E2), html_file);
  312.         #else
  313.             fputs("Not referenced.\n", html_file);
  314.         #endif
  315.         }
  316.       }
  317.       putc('\n', html_file);
  318.     }
  319.     name = name->rlink;
  320.   }
  321. } static void format_user_entry(Name *name, FILE *html_file)
  322. {
  323.   while (name) {
  324.     format_user_entry(name->llink, html_file);
  325.     {
  326.       Scrap_Node *uses = name->uses;
  327.       if (uses) {
  328.         Scrap_Node *defs = name->defs;
  329.         fprintf(html_file, "<dt><code>%s</code>:\n<dd> ", name->spelling);
  330.         if (uses->scrap < defs->scrap) {
  331.           display_scrap_ref(html_file, uses->scrap);
  332.           uses = uses->next;
  333.         }
  334.         else {
  335.           if (defs->scrap == uses->scrap)
  336.             uses = uses->next;
  337.           fputs("<strong>", html_file);
  338.           display_scrap_ref(html_file, defs->scrap);
  339.           fputs("</strong>", html_file);
  340.           defs = defs->next;
  341.         }
  342.         while (uses || defs) {
  343.           fputs(", ", html_file);
  344.           if (uses && (!defs || uses->scrap < defs->scrap)) {
  345.             display_scrap_ref(html_file, uses->scrap);
  346.             uses = uses->next;
  347.           }
  348.           else {
  349.             if (uses && defs->scrap == uses->scrap)
  350.               uses = uses->next;
  351.             fputs("<strong>", html_file);
  352.             display_scrap_ref(html_file, defs->scrap);
  353.             fputs("</strong>", html_file);
  354.             defs = defs->next;
  355.           }
  356.         }
  357.         fputs(".\n", html_file);
  358.       }
  359.     }
  360.     name = name->rlink;
  361.   }