CVS revision numbers, stored as `cvs2svn:cvs-rev' properties, are a
[sgt/halibut] / bk_html.c
index d0e555e..1bd573a 100644 (file)
--- a/bk_html.c
+++ b/bk_html.c
@@ -10,9 +10,6 @@
  *    sensible. Perhaps for the topmost section in the file, no
  *    fragment should be used? (Though it should probably still be
  *    _there_ even if unused.)
- * 
- *  - free up all the data we have allocated while running this
- *    backend.
  */
 
 #include <stdio.h>
@@ -270,13 +267,9 @@ static htmlconfig html_configure(paragraph *source) {
                k++;                /* treat `xhtml-' and `html-' the same */
 
            if (!ustricmp(k, L"html-restrict-charset")) {
-               char *csname = utoa_dup(uadv(k), CS_ASCII);
-               ret.restrict_charset = charset_from_localenc(csname);
-               sfree(csname);
+               ret.restrict_charset = charset_from_ustr(&p->fpos, uadv(k));
            } else if (!ustricmp(k, L"html-output-charset")) {
-               char *csname = utoa_dup(uadv(k), CS_ASCII);
-               ret.output_charset = charset_from_localenc(csname);
-               sfree(csname);
+               ret.output_charset = charset_from_ustr(&p->fpos, uadv(k));
            } else if (!ustricmp(k, L"html-version")) {
                wchar_t *vername = uadv(k);
                static const struct {
@@ -465,11 +458,13 @@ paragraph *html_config_filename(char *filename)
 }
 
 void html_backend(paragraph *sourceform, keywordlist *keywords,
-                 indexdata *idx, void *unused) {
+                 indexdata *idx, void *unused)
+{
     paragraph *p;
     htmlconfig conf;
     htmlfilelist files = { NULL, NULL, NULL, NULL, NULL };
     htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL };
+    int has_index;
 
     IGNORE(unused);
 
@@ -538,16 +533,20 @@ void html_backend(paragraph *sourceform, keywordlist *keywords,
                                                        sect->fragment);
            }
 
-       /* And the index. */
-       sect = html_new_sect(&sects, NULL);
-       sect->text = NULL;
-       sect->type = INDEX;
-       sect->parent = topsect;
-       html_file_section(&conf, &files, sect, 0);   /* peer of chapters */
-       sect->fragment = utoa_dup(conf.index_text, CS_ASCII);
-       sect->fragment = html_sanitise_fragment(&files, sect->file,
-                                               sect->fragment);
-       files.index = sect->file;
+       /* And the index, if we have one. */
+       has_index = (count234(idx->entries) > 0);
+       if (has_index) {
+           sect = html_new_sect(&sects, NULL);
+           sect->text = NULL;
+           sect->type = INDEX;
+           sect->parent = topsect;
+            sect->contents_depth = 0;
+           html_file_section(&conf, &files, sect, 0);   /* peer of chapters */
+           sect->fragment = utoa_dup(conf.index_text, CS_ASCII);
+           sect->fragment = html_sanitise_fragment(&files, sect->file,
+                                                   sect->fragment);
+           files.index = sect->file;
+       }
     }
 
     /*
@@ -868,13 +867,15 @@ void html_backend(paragraph *sourceform, keywordlist *keywords,
 
                html_text(&ho, conf.nav_separator);
 
-               if (f != files.index) {
-                   element_open(&ho, "a");
-                   element_attr(&ho, "href", files.index->filename);
+               if (has_index) {
+                   if (f != files.index) {
+                       element_open(&ho, "a");
+                       element_attr(&ho, "href", files.index->filename);
+                   }
+                   html_text(&ho, conf.index_text);
+                   if (f != files.index)
+                       element_close(&ho, "a");
                }
-               html_text(&ho, conf.index_text);
-               if (f != files.index)
-                   element_close(&ho, "a");
 
                html_text(&ho, conf.nav_separator);
 
@@ -1401,8 +1402,73 @@ void html_backend(paragraph *sourceform, keywordlist *keywords,
     }
 
     /*
-     * FIXME: Free all the working data.
+     * Free all the working data.
      */
+    sfree(conf.asect);
+    sfree(conf.single_filename);
+    sfree(conf.contents_filename);
+    sfree(conf.index_filename);
+    sfree(conf.template_filename);
+    sfree(conf.template_fragment);
+    {
+       htmlfragment *frag;
+       while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) {
+           /*
+            * frag->fragment is dynamically allocated, but will be
+            * freed when we process the htmlsect structure which
+            * it is attached to.
+            */
+           sfree(frag);
+       }
+       freetree234(files.frags);
+    }
+    {
+       htmlsect *sect, *tmp;
+       sect = sects.head;
+       while (sect) {
+           tmp = sect->next;
+           sfree(sect->fragment);
+           sfree(sect);
+           sect = tmp;
+       }
+       sect = nonsects.head;
+       while (sect) {
+           tmp = sect->next;
+           sfree(sect->fragment);
+           sfree(sect);
+           sect = tmp;
+       }
+    }
+    {
+       htmlfile *file, *tmp;
+       file = files.head;
+       while (file) {
+           tmp = file->next;
+           sfree(file->filename);
+           sfree(file);
+           file = tmp;
+       }
+    }
+    {
+       int i;
+       indexentry *entry;
+       for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
+           htmlindex *hi = (htmlindex *)entry->backend_data;
+           sfree(hi);
+       }
+    }
+    {
+       paragraph *p;
+       word *w;
+       for (p = sourceform; p; p = p->next)
+           for (w = p->words; w; w = w->next)
+               if (w->type == word_IndexRef) {
+                   htmlindexref *hr = (htmlindexref *)w->private_data;
+                   assert(hr != NULL);
+                   sfree(hr->fragment);
+                   sfree(hr);
+               }
+    }
 }
 
 static void html_file_section(htmlconfig *cfg, htmlfilelist *files,