[AD] Documentation HTML index always with same name

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Not sure if this is useful, maybe it is. Modifies the makehtml
behaviour to rename the index file to a fixed name. This allows the
function index to always be available with the same URL. Otherwise,
adding/removing chapters changes its number.
Index: docs/src/makedoc/makehtml.c
===================================================================
RCS file: /cvsroot/alleg/allegro/docs/src/makedoc/makehtml.c,v
retrieving revision 1.38
diff -u -p -r1.38 makehtml.c
--- docs/src/makedoc/makehtml.c	26 Mar 2005 12:25:48 -0000	1.38
+++ docs/src/makedoc/makehtml.c	29 May 2005 14:14:31 -0000
@@ -229,6 +229,7 @@ static const char *_look_up_short_descri
 static char *_scan_line_for_code_tokens(char *line, int *code_scanning);
 static char *_replace_code_tokens(char *line, int column, int *end);
 static char *_find_potential_token(char *line, int column, int end, int *found_size);
+static void _rename_index_file(const char *filename, int index_file_number);
 
 
 
@@ -240,6 +241,7 @@ int write_html(char *filename)
 {
    int block_empty_lines = 0, section_number = 0, ret;
    int last_line_was_a_definition = 0;
+   int index_file_number = -1;
    LINE *line = head;
    char **auto_types;
 
@@ -392,6 +394,11 @@ int write_html(char *filename)
       else if (line->flags & INDEX_FLAG) {
 	 _output_buffered_text();
 	 _output_symbol_index();
+	 if (index_file_number > 0) {
+	    printf("Can't handle more than one index!\n");
+	    abort();
+	 }
+	 index_file_number = section_number - 1;
       }
 
       /* Keep track of continuous definitions */	 
@@ -406,6 +413,7 @@ int write_html(char *filename)
    _close_html_file(_file);
    
    _post_process_pending_refs();
+   _rename_index_file(filename, index_file_number);
    free(html_see_also_text);
    free(html_examples_using_this_text);
    free(html_return_value_text);
@@ -1706,7 +1714,7 @@ static char *_replace_code_tokens(char *
 
 
 /* _find_potential_token:
- * In aline, starting at column and endign at end, tries to find potential
+ * In a line, starting at column and endign at end, tries to find potential
  * tokens, which is words composed of alphanumeric characters and the
  * underscore. If found, a pointer to the token inside the line is
  * returned and the length stored in found_size.
@@ -1745,3 +1753,92 @@ static char *_find_potential_token(char 
    else
       return 0;
 }
+
+
+
+/* _rename_index_file:
+ * If the parameter is greater than zero, it will try to rename the
+ * specified file name to have an `i' instead of a number. If anything
+ * goes wrong, it will spill some warnings, but not abort anything.
+ */
+static void _rename_index_file(const char *filename, int index_file_number)
+{
+   char old_name[256], new_name[256], *s;
+   char *temp_filename, *p;
+   char *line;
+   FILE *f1 = 0, *f2 = 0;
+   
+   assert(filename);
+   assert(strlen(filename) < sizeof(old_name) - 1);
+
+   /* No multiple file output? */
+   if (!(flags & MULTIFILE_FLAG))
+      return;
+
+   /* Source file didn't have an index marker? */
+   if (index_file_number < 1)
+      return;
+
+   /* Prepare the names the old and new names of index. */
+   strcpy(old_name, filename);
+   strcpy(new_name, filename);
+   s = get_extension(old_name)-1;
+   if (s - get_filename(old_name) > 5)
+      s = get_filename(old_name)+5;
+   sprintf(s, "%03d.%s", index_file_number - 1, html_extension);
+   sprintf(new_name + (s - old_name), "i.%s", html_extension);
+
+   /* Try to perform the rename on the file first. */
+   if (rename(old_name, new_name) == -1) {
+      printf("Couldn't rename %s to %s\n", old_name, new_name);
+      return;
+   }
+
+   /* Now rescan the main file and modify the link. */
+   temp_filename = m_strdup(filename);
+   if (!temp_filename || strlen(temp_filename) < 2) goto cancel;
+   temp_filename[strlen(filename)-1] = 'x';
+
+   f1 = fopen(filename, "rt");
+   f2 = fopen(temp_filename, "wt");
+   if (!f1 || !f2) goto cancel;
+
+   printf("post processing %s\n", filename);
+
+   /* Get basename pointers to old and new filenames. */
+   s = get_filename(old_name);
+   p = get_filename(new_name);
+
+   /* Scan for old filename link. */
+   while ((line = m_fgets(f1))) {
+      if (s && strstr(line, s)) {
+	 char *found = strstr(line, s);
+	 *found = 0;
+	 fputs(line, f2);
+	 fputs(p, f2);
+	 fputs(found + strlen(s), f2);
+	 s = NULL;
+      }
+      else
+	 fputs(line, f2);
+      free(line);
+   }
+
+   fclose(f1);
+   fclose(f2);
+   f1 = f2 = 0;
+
+   if (remove(filename)) goto cancel;
+   rename(temp_filename, filename);
+
+   printf("renamed %s to %s\n", old_name, new_name);
+
+   cancel:
+   if (f1) fclose(f1);
+   if (f2) fclose(f2);
+   if (temp_filename) {
+      remove(temp_filename);
+      free(temp_filename);
+   }
+}
+



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/