Explicitly constify a bunch of static data declarations which were
[sgt/halibut] / bk_html.c
CommitLineData
78c73085 1/*
2 * HTML backend for Halibut
3 */
4
5/*
6 * TODO:
7 *
8 * - I'm never entirely convinced that having a fragment link to
9 * come in at the start of the real text in the file is
10 * sensible. Perhaps for the topmost section in the file, no
11 * fragment should be used? (Though it should probably still be
12 * _there_ even if unused.)
78c73085 13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <assert.h>
18#include <limits.h>
19#include "halibut.h"
20
21#define is_heading_type(type) ( (type) == para_Title || \
22 (type) == para_Chapter || \
23 (type) == para_Appendix || \
24 (type) == para_UnnumberedChapter || \
25 (type) == para_Heading || \
26 (type) == para_Subsect)
27
28#define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
29 (p)->type == para_Heading ? 1 : \
30 (p)->type == para_Title ? -1 : 0 )
31
32typedef struct {
33 int just_numbers;
34 wchar_t *number_suffix;
35} sectlevel;
36
37typedef struct {
38 int nasect;
39 sectlevel achapter, *asect;
40 int *contents_depths; /* 0=main, 1=chapter, 2=sect etc */
41 int ncdepths;
42 int address_section, visible_version_id;
43 int leaf_contains_contents, leaf_smallest_contents;
44 char *contents_filename;
45 char *index_filename;
46 char *template_filename;
47 char *single_filename;
12f0ee84 48 char **template_fragments;
49 int ntfragments;
78c73085 50 char *head_end, *body_start, *body_end, *addr_start, *addr_end;
51 char *body_tag, *nav_attr;
52 wchar_t *author, *description;
56a99eb6 53 wchar_t *index_text, *contents_text, *preamble_text, *title_separator;
54 wchar_t *nav_prev_text, *nav_next_text, *nav_separator;
55 wchar_t *index_main_sep, *index_multi_sep;
56 wchar_t *pre_versionid, *post_versionid;
78c73085 57 int restrict_charset, output_charset;
58 enum {
27bdc5ab 59 HTML_3_2, HTML_4, ISO_HTML,
78c73085 60 XHTML_1_0_TRANSITIONAL, XHTML_1_0_STRICT
61 } htmlver;
62 wchar_t *lquote, *rquote;
63 int leaf_level;
64} htmlconfig;
65
66#define contents_depth(conf, level) \
67 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
68
69#define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
70
71typedef struct htmlfile htmlfile;
72typedef struct htmlsect htmlsect;
73
74struct htmlfile {
75 htmlfile *next;
76 char *filename;
77 int last_fragment_number;
78 int min_heading_depth;
79 htmlsect *first, *last; /* first/last highest-level sections */
80};
81
82struct htmlsect {
83 htmlsect *next, *parent;
84 htmlfile *file;
85 paragraph *title, *text;
86 enum { NORMAL, TOP, INDEX } type;
87 int contents_depth;
12f0ee84 88 char **fragments;
78c73085 89};
90
91typedef struct {
92 htmlfile *head, *tail;
93 htmlfile *single, *index;
3e82de8f 94 tree234 *frags;
78c73085 95} htmlfilelist;
96
97typedef struct {
98 htmlsect *head, *tail;
99} htmlsectlist;
100
101typedef struct {
3e82de8f 102 htmlfile *file;
103 char *fragment;
104} htmlfragment;
105
106typedef struct {
78c73085 107 int nrefs, refsize;
108 word **refs;
109} htmlindex;
110
111typedef struct {
112 htmlsect *section;
113 char *fragment;
1b7bf715 114 int generated, referenced;
78c73085 115} htmlindexref;
116
117typedef struct {
118 /*
119 * This level deals with charset conversion, starting and
120 * ending tags, and writing to the file. It's the lexical
121 * level.
122 */
123 FILE *fp;
b7309494 124 int charset, restrict_charset;
78c73085 125 charset_state cstate;
126 int ver;
127 enum {
128 HO_NEUTRAL, HO_IN_TAG, HO_IN_EMPTY_TAG, HO_IN_TEXT
129 } state;
130 /*
131 * Stuff beyond here deals with the higher syntactic level: it
132 * tracks how many levels of <ul> are currently open when
133 * producing a contents list, for example.
134 */
135 int contents_level;
136} htmloutput;
137
3e82de8f 138static int html_fragment_compare(void *av, void *bv)
139{
140 htmlfragment *a = (htmlfragment *)av;
141 htmlfragment *b = (htmlfragment *)bv;
142 int cmp;
143
144 if ((cmp = strcmp(a->file->filename, b->file->filename)) != 0)
145 return cmp;
146 else
147 return strcmp(a->fragment, b->fragment);
148}
149
78c73085 150static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
151 htmlsect *sect, int depth);
152
153static htmlfile *html_new_file(htmlfilelist *list, char *filename);
12f0ee84 154static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
155 htmlconfig *cfg);
78c73085 156
157/* Flags for html_words() flags parameter */
158#define NOTHING 0x00
159#define MARKUP 0x01
160#define LINKS 0x02
161#define INDEXENTS 0x04
162#define ALL 0x07
163static void html_words(htmloutput *ho, word *words, int flags,
164 htmlfile *file, keywordlist *keywords, htmlconfig *cfg);
165static void html_codepara(htmloutput *ho, word *words);
166
167static void element_open(htmloutput *ho, char const *name);
168static void element_close(htmloutput *ho, char const *name);
169static void element_empty(htmloutput *ho, char const *name);
170static void element_attr(htmloutput *ho, char const *name, char const *value);
171static void element_attr_w(htmloutput *ho, char const *name,
172 wchar_t const *value);
173static void html_text(htmloutput *ho, wchar_t const *str);
35b123a0 174static void html_text_nbsp(htmloutput *ho, wchar_t const *str);
78c73085 175static void html_text_limit(htmloutput *ho, wchar_t const *str, int maxlen);
176static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
35b123a0 177 int maxlen, int quote_quotes, int nbsp);
78c73085 178static void html_nl(htmloutput *ho);
179static void html_raw(htmloutput *ho, char *text);
180static void html_raw_as_attr(htmloutput *ho, char *text);
181static void cleanup(htmloutput *ho);
182
183static void html_href(htmloutput *ho, htmlfile *thisfile,
184 htmlfile *targetfile, char *targetfrag);
27bdc5ab 185static void html_fragment(htmloutput *ho, char const *fragment);
78c73085 186
187static char *html_format(paragraph *p, char *template_string);
3e82de8f 188static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
189 char *text);
78c73085 190
191static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
192 htmlfile *thisfile, keywordlist *keywords,
193 htmlconfig *cfg);
194static void html_section_title(htmloutput *ho, htmlsect *s,
195 htmlfile *thisfile, keywordlist *keywords,
23c9bbc2 196 htmlconfig *cfg, int real);
78c73085 197
198static htmlconfig html_configure(paragraph *source) {
199 htmlconfig ret;
200 paragraph *p;
201
202 /*
203 * Defaults.
204 */
205 ret.leaf_level = 2;
206 ret.achapter.just_numbers = FALSE;
207 ret.achapter.number_suffix = L": ";
208 ret.nasect = 1;
f1530049 209 ret.asect = snewn(ret.nasect, sectlevel);
78c73085 210 ret.asect[0].just_numbers = TRUE;
211 ret.asect[0].number_suffix = L" ";
212 ret.ncdepths = 0;
213 ret.contents_depths = 0;
214 ret.visible_version_id = TRUE;
215 ret.address_section = TRUE;
216 ret.leaf_contains_contents = FALSE;
217 ret.leaf_smallest_contents = 4;
218 ret.single_filename = dupstr("Manual.html");
219 ret.contents_filename = dupstr("Contents.html");
220 ret.index_filename = dupstr("IndexPage.html");
221 ret.template_filename = dupstr("%n.html");
12f0ee84 222 ret.ntfragments = 1;
223 ret.template_fragments = snewn(ret.ntfragments, char *);
224 ret.template_fragments[0] = dupstr("%b");
78c73085 225 ret.head_end = ret.body_tag = ret.body_start = ret.body_end =
226 ret.addr_start = ret.addr_end = ret.nav_attr = NULL;
227 ret.author = ret.description = NULL;
b7309494 228 ret.restrict_charset = CS_UTF8;
78c73085 229 ret.output_charset = CS_ASCII;
230 ret.htmlver = HTML_4;
56a99eb6 231 ret.index_text = L"Index";
232 ret.contents_text = L"Contents";
233 ret.preamble_text = L"Preamble";
234 ret.title_separator = L" - ";
235 ret.nav_prev_text = L"Previous";
236 ret.nav_next_text = L"Next";
237 ret.nav_separator = L" | ";
238 ret.index_main_sep = L": ";
239 ret.index_multi_sep = L", ";
240 ret.pre_versionid = L"[";
241 ret.post_versionid = L"]";
78c73085 242 /*
243 * Default quote characters are Unicode matched single quotes,
244 * falling back to ordinary ASCII ".
245 */
246 ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0";
247 ret.rquote = uadv(ret.lquote);
248
249 /*
250 * Two-pass configuration so that we can pick up global config
251 * (e.g. `quotes') before having it overridden by specific
252 * config (`html-quotes'), irrespective of the order in which
253 * they occur.
254 */
255 for (p = source; p; p = p->next) {
256 if (p->type == para_Config) {
257 if (!ustricmp(p->keyword, L"quotes")) {
258 if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
259 ret.lquote = uadv(p->keyword);
260 ret.rquote = uadv(ret.lquote);
261 }
262 }
263 }
264 }
265
266 for (p = source; p; p = p->next) {
267 if (p->type == para_Config) {
268 wchar_t *k = p->keyword;
269
270 if (!ustrnicmp(k, L"xhtml-", 6))
271 k++; /* treat `xhtml-' and `html-' the same */
272
b7309494 273 if (!ustricmp(k, L"html-restrict-charset")) {
0960a3d8 274 ret.restrict_charset = charset_from_ustr(&p->fpos, uadv(k));
b7309494 275 } else if (!ustricmp(k, L"html-output-charset")) {
0960a3d8 276 ret.output_charset = charset_from_ustr(&p->fpos, uadv(k));
27bdc5ab 277 } else if (!ustricmp(k, L"html-version")) {
278 wchar_t *vername = uadv(k);
279 static const struct {
280 const wchar_t *name;
281 int ver;
282 } versions[] = {
283 {L"html3.2", HTML_3_2},
284 {L"html4", HTML_4},
285 {L"iso-html", ISO_HTML},
286 {L"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL},
287 {L"xhtml1.0strict", XHTML_1_0_STRICT}
288 };
289 int i;
290
291 for (i = 0; i < (int)lenof(versions); i++)
292 if (!ustricmp(versions[i].name, vername))
293 break;
294
295 if (i == lenof(versions))
296 error(err_htmlver, &p->fpos, vername);
297 else
298 ret.htmlver = versions[i].ver;
78c73085 299 } else if (!ustricmp(k, L"html-single-filename")) {
300 sfree(ret.single_filename);
301 ret.single_filename = dupstr(adv(p->origkeyword));
302 } else if (!ustricmp(k, L"html-contents-filename")) {
303 sfree(ret.contents_filename);
304 ret.contents_filename = dupstr(adv(p->origkeyword));
305 } else if (!ustricmp(k, L"html-index-filename")) {
306 sfree(ret.index_filename);
307 ret.index_filename = dupstr(adv(p->origkeyword));
308 } else if (!ustricmp(k, L"html-template-filename")) {
309 sfree(ret.template_filename);
310 ret.template_filename = dupstr(adv(p->origkeyword));
311 } else if (!ustricmp(k, L"html-template-fragment")) {
12f0ee84 312 char *frag = adv(p->origkeyword);
313 if (*frag) {
314 while (ret.ntfragments--)
315 sfree(ret.template_fragments[ret.ntfragments]);
316 sfree(ret.template_fragments);
317 ret.template_fragments = NULL;
318 ret.ntfragments = 0;
319 while (*frag) {
320 ret.ntfragments++;
321 ret.template_fragments =
322 sresize(ret.template_fragments,
323 ret.ntfragments, char *);
324 ret.template_fragments[ret.ntfragments-1] =
325 dupstr(frag);
326 frag = adv(frag);
327 }
328 } else
329 error(err_cfginsufarg, &p->fpos, p->origkeyword, 1);
78c73085 330 } else if (!ustricmp(k, L"html-chapter-numeric")) {
331 ret.achapter.just_numbers = utob(uadv(k));
332 } else if (!ustricmp(k, L"html-chapter-suffix")) {
333 ret.achapter.number_suffix = uadv(k);
334 } else if (!ustricmp(k, L"html-leaf-level")) {
335 ret.leaf_level = utoi(uadv(k));
336 } else if (!ustricmp(k, L"html-section-numeric")) {
337 wchar_t *q = uadv(k);
338 int n = 0;
339 if (uisdigit(*q)) {
340 n = utoi(q);
341 q = uadv(q);
342 }
343 if (n >= ret.nasect) {
344 int i;
f1530049 345 ret.asect = sresize(ret.asect, n+1, sectlevel);
78c73085 346 for (i = ret.nasect; i <= n; i++)
347 ret.asect[i] = ret.asect[ret.nasect-1];
348 ret.nasect = n+1;
349 }
350 ret.asect[n].just_numbers = utob(q);
351 } else if (!ustricmp(k, L"html-section-suffix")) {
352 wchar_t *q = uadv(k);
353 int n = 0;
354 if (uisdigit(*q)) {
355 n = utoi(q);
356 q = uadv(q);
357 }
358 if (n >= ret.nasect) {
359 int i;
f1530049 360 ret.asect = sresize(ret.asect, n+1, sectlevel);
78c73085 361 for (i = ret.nasect; i <= n; i++) {
362 ret.asect[i] = ret.asect[ret.nasect-1];
363 }
364 ret.nasect = n+1;
365 }
366 ret.asect[n].number_suffix = q;
367 } else if (!ustricmp(k, L"html-contents-depth") ||
368 !ustrnicmp(k, L"html-contents-depth-", 20)) {
369 /*
370 * Relic of old implementation: this directive used
371 * to be written as \cfg{html-contents-depth-3}{2}
372 * rather than the usual Halibut convention of
373 * \cfg{html-contents-depth}{3}{2}. We therefore
374 * support both.
375 */
376 wchar_t *q = k[19] ? k+20 : uadv(k);
377 int n = 0;
378 if (uisdigit(*q)) {
379 n = utoi(q);
380 q = uadv(q);
381 }
382 if (n >= ret.ncdepths) {
383 int i;
f1530049 384 ret.contents_depths =
385 sresize(ret.contents_depths, n+1, int);
78c73085 386 for (i = ret.ncdepths; i <= n; i++) {
387 ret.contents_depths[i] = i+2;
388 }
389 ret.ncdepths = n+1;
390 }
391 ret.contents_depths[n] = utoi(q);
392 } else if (!ustricmp(k, L"html-head-end")) {
393 ret.head_end = adv(p->origkeyword);
394 } else if (!ustricmp(k, L"html-body-tag")) {
395 ret.body_tag = adv(p->origkeyword);
396 } else if (!ustricmp(k, L"html-body-start")) {
397 ret.body_start = adv(p->origkeyword);
398 } else if (!ustricmp(k, L"html-body-end")) {
399 ret.body_end = adv(p->origkeyword);
400 } else if (!ustricmp(k, L"html-address-start")) {
401 ret.addr_start = adv(p->origkeyword);
402 } else if (!ustricmp(k, L"html-address-end")) {
403 ret.addr_end = adv(p->origkeyword);
404 } else if (!ustricmp(k, L"html-navigation-attributes")) {
405 ret.nav_attr = adv(p->origkeyword);
406 } else if (!ustricmp(k, L"html-author")) {
407 ret.author = uadv(k);
408 } else if (!ustricmp(k, L"html-description")) {
409 ret.description = uadv(k);
410 } else if (!ustricmp(k, L"html-suppress-address")) {
411 ret.address_section = !utob(uadv(k));
412 } else if (!ustricmp(k, L"html-versionid")) {
413 ret.visible_version_id = utob(uadv(k));
414 } else if (!ustricmp(k, L"html-quotes")) {
415 if (*uadv(k) && *uadv(uadv(k))) {
416 ret.lquote = uadv(k);
417 ret.rquote = uadv(ret.lquote);
418 }
419 } else if (!ustricmp(k, L"html-leaf-contains-contents")) {
420 ret.leaf_contains_contents = utob(uadv(k));
421 } else if (!ustricmp(k, L"html-leaf-smallest-contents")) {
422 ret.leaf_smallest_contents = utoi(uadv(k));
75a96e91 423 } else if (!ustricmp(k, L"html-index-text")) {
424 ret.index_text = uadv(k);
425 } else if (!ustricmp(k, L"html-contents-text")) {
426 ret.contents_text = uadv(k);
427 } else if (!ustricmp(k, L"html-preamble-text")) {
428 ret.preamble_text = uadv(k);
429 } else if (!ustricmp(k, L"html-title-separator")) {
430 ret.title_separator = uadv(k);
431 } else if (!ustricmp(k, L"html-nav-prev-text")) {
432 ret.nav_prev_text = uadv(k);
433 } else if (!ustricmp(k, L"html-nav-next-text")) {
434 ret.nav_next_text = uadv(k);
435 } else if (!ustricmp(k, L"html-nav-separator")) {
436 ret.nav_separator = uadv(k);
437 } else if (!ustricmp(k, L"html-index-main-separator")) {
438 ret.index_main_sep = uadv(k);
439 } else if (!ustricmp(k, L"html-index-multiple-separator")) {
440 ret.index_multi_sep = uadv(k);
441 } else if (!ustricmp(k, L"html-pre-versionid")) {
442 ret.pre_versionid = uadv(k);
443 } else if (!ustricmp(k, L"html-post-versionid")) {
444 ret.post_versionid = uadv(k);
78c73085 445 }
446 }
447 }
448
449 /*
450 * Now process fallbacks on quote characters.
451 */
452 while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) &&
453 (!cvt_ok(ret.restrict_charset, ret.lquote) ||
454 !cvt_ok(ret.restrict_charset, ret.rquote))) {
455 ret.lquote = uadv(ret.rquote);
456 ret.rquote = uadv(ret.lquote);
457 }
458
459 return ret;
460}
461
462paragraph *html_config_filename(char *filename)
463{
464 /*
465 * If the user passes in a single filename as a parameter to
466 * the `--html' command-line option, then we should assume it
467 * to imply _two_ config directives:
468 * \cfg{html-single-filename}{whatever} and
469 * \cfg{html-leaf-level}{0}; the rationale being that the user
470 * wants their output _in that file_.
471 */
472 paragraph *p, *q;
473
474 p = cmdline_cfg_simple("html-single-filename", filename, NULL);
475 q = cmdline_cfg_simple("html-leaf-level", "0", NULL);
476 p->next = q;
477 return p;
478}
479
480void html_backend(paragraph *sourceform, keywordlist *keywords,
529a6c83 481 indexdata *idx, void *unused)
482{
78c73085 483 paragraph *p;
484 htmlconfig conf;
3e82de8f 485 htmlfilelist files = { NULL, NULL, NULL, NULL, NULL };
78c73085 486 htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL };
03fcb340 487 int has_index;
78c73085 488
489 IGNORE(unused);
490
491 conf = html_configure(sourceform);
492
493 /*
494 * We're going to make heavy use of paragraphs' private data
495 * fields in the forthcoming code. Clear them first, so we can
496 * reliably tell whether we have auxiliary data for a
497 * particular paragraph.
498 */
499 for (p = sourceform; p; p = p->next)
500 p->private_data = NULL;
501
3e82de8f 502 files.frags = newtree234(html_fragment_compare);
503
78c73085 504 /*
505 * Start by figuring out into which file each piece of the
506 * document should be put. We'll do this by inventing an
507 * `htmlsect' structure and stashing it in the private_data
508 * field of each section paragraph; we also need one additional
509 * htmlsect for the document index, which won't show up in the
510 * source form but needs to be consistently mentioned in
511 * contents links.
512 *
12f0ee84 513 * While we're here, we'll also invent the HTML fragment name(s)
78c73085 514 * for each section.
515 */
516 {
517 htmlsect *topsect, *sect;
518 int d;
519
12f0ee84 520 topsect = html_new_sect(&sects, NULL, &conf);
78c73085 521 topsect->type = TOP;
522 topsect->title = NULL;
523 topsect->text = sourceform;
524 topsect->contents_depth = contents_depth(conf, 0);
525 html_file_section(&conf, &files, topsect, -1);
78c73085 526
527 for (p = sourceform; p; p = p->next)
528 if (is_heading_type(p->type)) {
529 d = heading_depth(p);
530
531 if (p->type == para_Title) {
532 topsect->title = p;
533 continue;
534 }
535
12f0ee84 536 sect = html_new_sect(&sects, p, &conf);
78c73085 537 sect->text = p->next;
538
539 sect->contents_depth = contents_depth(conf, d+1) - (d+1);
540
541 if (p->parent) {
542 sect->parent = (htmlsect *)p->parent->private_data;
543 assert(sect->parent != NULL);
544 } else
545 sect->parent = topsect;
546 p->private_data = sect;
547
548 html_file_section(&conf, &files, sect, d);
549
12f0ee84 550 {
551 int i;
552 for (i=0; i < conf.ntfragments; i++) {
553 sect->fragments[i] =
554 html_format(p, conf.template_fragments[i]);
555 sect->fragments[i] =
556 html_sanitise_fragment(&files, sect->file,
557 sect->fragments[i]);
558 }
559 }
78c73085 560 }
561
03fcb340 562 /* And the index, if we have one. */
563 has_index = (count234(idx->entries) > 0);
564 if (has_index) {
12f0ee84 565 sect = html_new_sect(&sects, NULL, &conf);
03fcb340 566 sect->text = NULL;
567 sect->type = INDEX;
568 sect->parent = topsect;
cdb986cc 569 sect->contents_depth = 0;
03fcb340 570 html_file_section(&conf, &files, sect, 0); /* peer of chapters */
12f0ee84 571 sect->fragments[0] = utoa_dup(conf.index_text, CS_ASCII);
572 sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
573 sect->fragments[0]);
03fcb340 574 files.index = sect->file;
575 }
78c73085 576 }
577
578 /*
579 * Go through the keyword list and sort out fragment IDs for
580 * all the potentially referenced paragraphs which _aren't_
581 * headings.
582 */
583 {
584 int i;
585 keyword *kw;
586 htmlsect *sect;
587
588 for (i = 0; (kw = index234(keywords->keys, i)) != NULL; i++) {
589 paragraph *q, *p = kw->para;
590
591 if (!is_heading_type(p->type)) {
592 htmlsect *parent;
593
594 /*
595 * Find the paragraph's parent htmlsect, to
596 * determine which file it will end up in.
597 */
598 q = p->parent;
599 if (!q) {
600 /*
601 * Preamble paragraphs have no parent. So if we
602 * have a non-heading with no parent, it must
603 * be preamble, and therefore its parent
604 * htmlsect must be the preamble one.
605 */
606 assert(sects.head &&
607 sects.head->type == TOP);
608 parent = sects.head;
609 } else
610 parent = (htmlsect *)q->private_data;
611
612 /*
613 * Now we can construct an htmlsect for this
614 * paragraph itself, taking care to put it in the
615 * list of non-sections rather than the list of
616 * sections (so that traverses of the `sects' list
617 * won't attempt to add it to the contents or
618 * anything weird like that).
619 */
12f0ee84 620 sect = html_new_sect(&nonsects, p, &conf);
78c73085 621 sect->file = parent->file;
622 sect->parent = parent;
623 p->private_data = sect;
624
625 /*
04781c84 626 * Fragment IDs for these paragraphs will simply be
627 * `p' followed by an integer.
78c73085 628 */
12f0ee84 629 sect->fragments[0] = snewn(40, char);
630 sprintf(sect->fragments[0], "p%d",
04781c84 631 sect->file->last_fragment_number++);
12f0ee84 632 sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
633 sect->fragments[0]);
78c73085 634 }
635 }
636 }
637
638 /*
04781c84 639 * Reset the fragment numbers in each file. I've just used them
640 * to generate `p' fragment IDs for non-section paragraphs
641 * (numbered list elements, bibliocited), and now I want to use
642 * them for `i' fragment IDs for index entries.
643 */
644 {
645 htmlfile *file;
646 for (file = files.head; file; file = file->next)
647 file->last_fragment_number = 0;
648 }
649
650 /*
78c73085 651 * Now sort out the index. This involves:
652 *
653 * - For each index term, we set up an htmlindex structure to
654 * store all the references to that term.
655 *
656 * - Then we make a pass over the actual document, finding
657 * every word_IndexRef; for each one, we actually figure out
658 * the HTML filename/fragment pair we will use to reference
659 * it, store that information in the private data field of
660 * the word_IndexRef itself (so we can recreate it when the
661 * time comes to output our HTML), and add a reference to it
662 * to the index term in question.
663 */
664 {
665 int i;
666 indexentry *entry;
667 htmlsect *lastsect;
668 word *w;
669
670 /*
671 * Set up the htmlindex structures.
672 */
673
674 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
f1530049 675 htmlindex *hi = snew(htmlindex);
78c73085 676
677 hi->nrefs = hi->refsize = 0;
678 hi->refs = NULL;
679
680 entry->backend_data = hi;
681 }
682
683 /*
684 * Run over the document inventing fragments. Each fragment
685 * is of the form `i' followed by an integer.
78c73085 686 */
56a99eb6 687 lastsect = sects.head; /* this is always the top section */
78c73085 688 for (p = sourceform; p; p = p->next) {
56a99eb6 689 if (is_heading_type(p->type) && p->type != para_Title)
78c73085 690 lastsect = (htmlsect *)p->private_data;
691
692 for (w = p->words; w; w = w->next)
693 if (w->type == word_IndexRef) {
f1530049 694 htmlindexref *hr = snew(htmlindexref);
78c73085 695 indextag *tag;
696 int i;
697
1b7bf715 698 hr->referenced = hr->generated = FALSE;
78c73085 699 hr->section = lastsect;
78c73085 700 {
701 char buf[40];
702 sprintf(buf, "i%d",
703 lastsect->file->last_fragment_number++);
704 hr->fragment = dupstr(buf);
3e82de8f 705 hr->fragment =
706 html_sanitise_fragment(&files, hr->section->file,
707 hr->fragment);
78c73085 708 }
709 w->private_data = hr;
710
711 tag = index_findtag(idx, w->text);
712 if (!tag)
713 break;
714
715 for (i = 0; i < tag->nrefs; i++) {
716 indexentry *entry = tag->refs[i];
717 htmlindex *hi = (htmlindex *)entry->backend_data;
718
719 if (hi->nrefs >= hi->refsize) {
720 hi->refsize += 32;
f1530049 721 hi->refs = sresize(hi->refs, hi->refsize, word *);
78c73085 722 }
723
724 hi->refs[hi->nrefs++] = w;
725 }
726 }
727 }
728 }
729
730 /*
731 * Now we're ready to write out the actual HTML files.
732 *
733 * For each file:
734 *
735 * - we open that file and write its header
736 * - we run down the list of sections
737 * - for each section directly contained within that file, we
738 * output the section text
739 * - for each section which is not in the file but which has a
740 * parent that is, we output a contents entry for the
741 * section if appropriate
742 * - finally, we output the file trailer and close the file.
743 */
744 {
745 htmlfile *f, *prevf;
746 htmlsect *s;
747 paragraph *p;
748
749 prevf = NULL;
750
751 for (f = files.head; f; f = f->next) {
752 htmloutput ho;
753 int displaying;
754 enum LISTTYPE { NOLIST, UL, OL, DL };
755 enum ITEMTYPE { NOITEM, LI, DT, DD };
756 struct stackelement {
757 struct stackelement *next;
758 enum LISTTYPE listtype;
759 enum ITEMTYPE itemtype;
760 } *stackhead;
761
762#define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
763#define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
764
765 ho.fp = fopen(f->filename, "w");
766 ho.charset = conf.output_charset;
b7309494 767 ho.restrict_charset = conf.restrict_charset;
78c73085 768 ho.cstate = charset_init_state;
769 ho.ver = conf.htmlver;
770 ho.state = HO_NEUTRAL;
771 ho.contents_level = 0;
772
773 /* <!DOCTYPE>. */
774 switch (conf.htmlver) {
775 case HTML_3_2:
776 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
777 "HTML 3.2 Final//EN\">\n");
778 break;
779 case HTML_4:
780 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
781 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
782 "strict.dtd\">\n");
783 break;
27bdc5ab 784 case ISO_HTML:
785 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
786 "15445:2000//DTD HTML//EN\">\n");
787 break;
78c73085 788 case XHTML_1_0_TRANSITIONAL:
27bdc5ab 789 fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
790 charset_to_mimeenc(conf.output_charset));
78c73085 791 fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
792 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
793 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
794 break;
795 case XHTML_1_0_STRICT:
27bdc5ab 796 fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
797 charset_to_mimeenc(conf.output_charset));
78c73085 798 fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
799 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
800 "DTD/xhtml1-strict.dtd\">\n");
801 break;
802 }
803
804 element_open(&ho, "html");
805 if (is_xhtml(conf.htmlver)) {
806 element_attr(&ho, "xmlns", "http://www.w3.org/1999/xhtml");
807 }
808 html_nl(&ho);
809
810 element_open(&ho, "head");
811 html_nl(&ho);
812
813 element_empty(&ho, "meta");
814 element_attr(&ho, "http-equiv", "content-type");
815 {
816 char buf[200];
817 sprintf(buf, "text/html; charset=%.150s",
818 charset_to_mimeenc(conf.output_charset));
819 element_attr(&ho, "content", buf);
820 }
821 html_nl(&ho);
822
823 if (conf.author) {
824 element_empty(&ho, "meta");
825 element_attr(&ho, "name", "author");
826 element_attr_w(&ho, "content", conf.author);
827 html_nl(&ho);
828 }
829
830 if (conf.description) {
831 element_empty(&ho, "meta");
832 element_attr(&ho, "name", "description");
833 element_attr_w(&ho, "content", conf.description);
834 html_nl(&ho);
835 }
836
837 element_open(&ho, "title");
838 if (f->first && f->first->title) {
839 html_words(&ho, f->first->title->words, NOTHING,
840 f, keywords, &conf);
841
842 assert(f->last);
843 if (f->last != f->first && f->last->title) {
56a99eb6 844 html_text(&ho, conf.title_separator);
78c73085 845 html_words(&ho, f->last->title->words, NOTHING,
846 f, keywords, &conf);
847 }
848 }
849 element_close(&ho, "title");
850 html_nl(&ho);
851
852 if (conf.head_end)
853 html_raw(&ho, conf.head_end);
854
9acfce4f 855 /*
856 * Add any <head> data defined in specific sections
857 * that go in this file. (This is mostly to allow <meta
858 * name="AppleTitle"> tags for Mac online help.)
859 */
860 for (s = sects.head; s; s = s->next) {
861 if (s->file == f && s->text) {
862 for (p = s->text;
6f0bdcde 863 p && (p == s->text || p->type == para_Title ||
864 !is_heading_type(p->type));
9acfce4f 865 p = p->next) {
866 if (p->type == para_Config) {
867 if (!ustricmp(p->keyword, L"html-local-head")) {
868 html_raw(&ho, adv(p->origkeyword));
869 }
870 }
871 }
872 }
873 }
874
78c73085 875 element_close(&ho, "head");
876 html_nl(&ho);
877
78c73085 878 if (conf.body_tag)
879 html_raw(&ho, conf.body_tag);
880 else
881 element_open(&ho, "body");
882 html_nl(&ho);
883
884 if (conf.body_start)
885 html_raw(&ho, conf.body_start);
886
887 /*
888 * Write out a nav bar. Special case: we don't do this
889 * if there is only one file.
890 */
891 if (files.head != files.tail) {
892 element_open(&ho, "p");
893 if (conf.nav_attr)
894 html_raw_as_attr(&ho, conf.nav_attr);
895
896 if (prevf) {
897 element_open(&ho, "a");
898 element_attr(&ho, "href", prevf->filename);
899 }
56a99eb6 900 html_text(&ho, conf.nav_prev_text);
78c73085 901 if (prevf)
902 element_close(&ho, "a");
903
56a99eb6 904 html_text(&ho, conf.nav_separator);
78c73085 905
906 if (f != files.head) {
907 element_open(&ho, "a");
908 element_attr(&ho, "href", files.head->filename);
909 }
56a99eb6 910 html_text(&ho, conf.contents_text);
78c73085 911 if (f != files.head)
912 element_close(&ho, "a");
913
03fcb340 914 if (has_index) {
771d3da3 915 html_text(&ho, conf.nav_separator);
03fcb340 916 if (f != files.index) {
917 element_open(&ho, "a");
918 element_attr(&ho, "href", files.index->filename);
919 }
920 html_text(&ho, conf.index_text);
921 if (f != files.index)
922 element_close(&ho, "a");
78c73085 923 }
78c73085 924
56a99eb6 925 html_text(&ho, conf.nav_separator);
78c73085 926
927 if (f->next) {
928 element_open(&ho, "a");
929 element_attr(&ho, "href", f->next->filename);
930 }
56a99eb6 931 html_text(&ho, conf.nav_next_text);
78c73085 932 if (f->next)
933 element_close(&ho, "a");
934
935 element_close(&ho, "p");
936 html_nl(&ho);
937 }
938 prevf = f;
939
940 /*
f5dee642 941 * Write out a prefix TOC for the file (if a leaf file).
78c73085 942 *
943 * We start by going through the section list and
944 * collecting the sections which need to be added to
945 * the contents. On the way, we also test to see if
946 * this file is a leaf file (defined as one which
947 * contains all descendants of any section it
948 * contains), because this will play a part in our
949 * decision on whether or not to _output_ the TOC.
950 *
951 * Special case: we absolutely do not do this if we're
952 * in single-file mode.
953 */
954 if (files.head != files.tail) {
955 int ntoc = 0, tocsize = 0;
956 htmlsect **toc = NULL;
957 int leaf = TRUE;
958
959 for (s = sects.head; s; s = s->next) {
960 htmlsect *a, *ac;
961 int depth, adepth;
962
963 /*
964 * Search up from this section until we find
965 * the highest-level one which belongs in this
966 * file.
967 */
968 depth = adepth = 0;
969 a = NULL;
970 for (ac = s; ac; ac = ac->parent) {
971 if (ac->file == f) {
972 a = ac;
973 adepth = depth;
974 }
975 depth++;
976 }
977
978 if (s->file != f && a != NULL)
979 leaf = FALSE;
980
981 if (a) {
982 if (adepth <= a->contents_depth) {
983 if (ntoc >= tocsize) {
984 tocsize += 64;
f1530049 985 toc = sresize(toc, tocsize, htmlsect *);
78c73085 986 }
987 toc[ntoc++] = s;
988 }
989 }
990 }
991
992 if (leaf && conf.leaf_contains_contents &&
993 ntoc >= conf.leaf_smallest_contents) {
994 int i;
995
996 for (i = 0; i < ntoc; i++) {
997 htmlsect *s = toc[i];
998 int hlevel = (s->type == TOP ? -1 :
999 s->type == INDEX ? 0 :
1000 heading_depth(s->title))
1001 - f->min_heading_depth + 1;
1002
1003 assert(hlevel >= 1);
1004 html_contents_entry(&ho, hlevel, s,
1005 f, keywords, &conf);
1006 }
1007 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1008 }
1009 }
1010
1011 /*
1012 * Now go through the document and output some real
1013 * text.
1014 */
1015 displaying = FALSE;
1016 for (s = sects.head; s; s = s->next) {
1017 if (s->file == f) {
1018 /*
1019 * This section belongs in this file.
1020 * Display it.
1021 */
1022 displaying = TRUE;
1023 } else {
f5dee642 1024 /*
1025 * Doesn't belong in this file, but it may be
1026 * a descendant of a section which does, in
1027 * which case we should consider it for the
1028 * main TOC of this file (for non-leaf files).
1029 */
78c73085 1030 htmlsect *a, *ac;
1031 int depth, adepth;
1032
1033 displaying = FALSE;
1034
1035 /*
1036 * Search up from this section until we find
1037 * the highest-level one which belongs in this
1038 * file.
1039 */
1040 depth = adepth = 0;
1041 a = NULL;
1042 for (ac = s; ac; ac = ac->parent) {
1043 if (ac->file == f) {
1044 a = ac;
1045 adepth = depth;
1046 }
1047 depth++;
1048 }
1049
1050 if (a != NULL) {
1051 /*
1052 * This section does not belong in this
1053 * file, but an ancestor of it does. Write
1054 * out a contents table entry, if the depth
1055 * doesn't exceed the maximum contents
1056 * depth for the ancestor section.
1057 */
1058 if (adepth <= a->contents_depth) {
1059 html_contents_entry(&ho, adepth, s,
1060 f, keywords, &conf);
1061 }
1062 }
1063 }
1064
1065 if (displaying) {
1066 int hlevel;
1067 char htag[3];
1068
1069 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1070
1071 /*
1072 * Display the section heading.
1073 */
1074
1075 hlevel = (s->type == TOP ? -1 :
1076 s->type == INDEX ? 0 :
1077 heading_depth(s->title))
1078 - f->min_heading_depth + 1;
1079 assert(hlevel >= 1);
1080 /* HTML headings only go up to <h6> */
1081 if (hlevel > 6)
1082 hlevel = 6;
1083 htag[0] = 'h';
1084 htag[1] = '0' + hlevel;
1085 htag[2] = '\0';
1086 element_open(&ho, htag);
1087
1088 /*
12f0ee84 1089 * Provide anchor(s) for cross-links to target.
78c73085 1090 *
78c73085 1091 * (Also we'll have to do this separately in
1092 * other paragraph types - NumberedList and
1093 * BiblioCited.)
1094 */
12f0ee84 1095 {
1096 int i;
1097 for (i=0; i < conf.ntfragments; i++)
1098 if (s->fragments[i])
1099 html_fragment(&ho, s->fragments[i]);
1100 }
78c73085 1101
23c9bbc2 1102 html_section_title(&ho, s, f, keywords, &conf, TRUE);
78c73085 1103
1104 element_close(&ho, htag);
1105
1106 /*
1107 * Now display the section text.
1108 */
1109 if (s->text) {
f1530049 1110 stackhead = snew(struct stackelement);
78c73085 1111 stackhead->next = NULL;
1112 stackhead->listtype = NOLIST;
1113 stackhead->itemtype = NOITEM;
1114
1115 for (p = s->text;; p = p->next) {
1116 enum LISTTYPE listtype;
1117 struct stackelement *se;
1118
1119 /*
1120 * Preliminary switch to figure out what
1121 * sort of list we expect to be inside at
1122 * this stage.
1123 *
1124 * Since p may still be NULL at this point,
1125 * I invent a harmless paragraph type for
1126 * it if it is.
1127 */
1128 switch (p ? p->type : para_Normal) {
1129 case para_Rule:
1130 case para_Normal:
1131 case para_Copyright:
1132 case para_BiblioCited:
1133 case para_Code:
1134 case para_QuotePush:
1135 case para_QuotePop:
1136 case para_Chapter:
1137 case para_Appendix:
1138 case para_UnnumberedChapter:
1139 case para_Heading:
1140 case para_Subsect:
1141 case para_LcontPop:
1142 listtype = NOLIST;
1143 break;
1144
1145 case para_Bullet:
1146 listtype = UL;
1147 break;
1148
1149 case para_NumberedList:
1150 listtype = OL;
1151 break;
1152
1153 case para_DescribedThing:
1154 case para_Description:
1155 listtype = DL;
1156 break;
1157
1158 case para_LcontPush:
f1530049 1159 se = snew(struct stackelement);
78c73085 1160 se->next = stackhead;
1161 se->listtype = NOLIST;
1162 se->itemtype = NOITEM;
1163 stackhead = se;
1164 continue;
1165
1166 default: /* some totally non-printing para */
1167 continue;
1168 }
1169
1170 html_nl(&ho);
1171
1172 /*
1173 * Terminate the most recent list item, if
1174 * any. (We left this until after
1175 * processing LcontPush, since in that case
1176 * the list item won't want to be
1177 * terminated until after the corresponding
1178 * LcontPop.)
1179 */
1180 if (stackhead->itemtype != NOITEM) {
1181 element_close(&ho, itemname(stackhead->itemtype));
1182 html_nl(&ho);
1183 }
1184 stackhead->itemtype = NOITEM;
1185
1186 /*
1187 * Terminate the current list, if it's not
1188 * the one we want to be in.
1189 */
1190 if (listtype != stackhead->listtype &&
1191 stackhead->listtype != NOLIST) {
1192 element_close(&ho, listname(stackhead->listtype));
1193 html_nl(&ho);
1194 }
1195
1196 /*
1197 * Leave the loop if our time has come.
1198 */
1199 if (!p || (is_heading_type(p->type) &&
1200 p->type != para_Title))
1201 break; /* end of section text */
1202
1203 /*
1204 * Start a fresh list if necessary.
1205 */
1206 if (listtype != stackhead->listtype &&
1207 listtype != NOLIST)
1208 element_open(&ho, listname(listtype));
1209
1210 stackhead->listtype = listtype;
1211
1212 switch (p->type) {
1213 case para_Rule:
1214 element_empty(&ho, "hr");
1215 break;
1216 case para_Code:
1217 html_codepara(&ho, p->words);
1218 break;
1219 case para_Normal:
1220 case para_Copyright:
1221 element_open(&ho, "p");
1222 html_nl(&ho);
1223 html_words(&ho, p->words, ALL,
1224 f, keywords, &conf);
1225 html_nl(&ho);
1226 element_close(&ho, "p");
1227 break;
1228 case para_BiblioCited:
1229 element_open(&ho, "p");
1230 if (p->private_data) {
1231 htmlsect *s = (htmlsect *)p->private_data;
12f0ee84 1232 int i;
1233 for (i=0; i < conf.ntfragments; i++)
1234 if (s->fragments[i])
1235 html_fragment(&ho, s->fragments[i]);
78c73085 1236 }
1237 html_nl(&ho);
1238 html_words(&ho, p->kwtext, ALL,
1239 f, keywords, &conf);
1240 html_text(&ho, L" ");
1241 html_words(&ho, p->words, ALL,
1242 f, keywords, &conf);
1243 html_nl(&ho);
1244 element_close(&ho, "p");
1245 break;
1246 case para_Bullet:
1247 case para_NumberedList:
1248 element_open(&ho, "li");
1249 if (p->private_data) {
1250 htmlsect *s = (htmlsect *)p->private_data;
12f0ee84 1251 int i;
1252 for (i=0; i < conf.ntfragments; i++)
1253 if (s->fragments[i])
1254 html_fragment(&ho, s->fragments[i]);
78c73085 1255 }
1256 html_nl(&ho);
1257 stackhead->itemtype = LI;
1258 html_words(&ho, p->words, ALL,
1259 f, keywords, &conf);
1260 break;
1261 case para_DescribedThing:
1262 element_open(&ho, "dt");
1263 html_nl(&ho);
1264 stackhead->itemtype = DT;
1265 html_words(&ho, p->words, ALL,
1266 f, keywords, &conf);
1267 break;
1268 case para_Description:
1269 element_open(&ho, "dd");
1270 html_nl(&ho);
1271 stackhead->itemtype = DD;
1272 html_words(&ho, p->words, ALL,
1273 f, keywords, &conf);
1274 break;
1275
1276 case para_QuotePush:
1277 element_open(&ho, "blockquote");
1278 break;
1279 case para_QuotePop:
1280 element_close(&ho, "blockquote");
1281 break;
1282
1283 case para_LcontPop:
1284 se = stackhead;
1285 stackhead = stackhead->next;
1286 assert(stackhead);
1287 sfree(se);
1288 break;
1289 }
1290 }
1291
1292 assert(stackhead && !stackhead->next);
1293 sfree(stackhead);
1294 }
1295
1296 if (s->type == INDEX) {
1297 indexentry *entry;
1298 int i;
1299
1300 /*
1301 * This section is the index. I'll just
1302 * render it as a single paragraph, with a
1303 * colon between the index term and the
1304 * references, and <br> in between each
1305 * entry.
1306 */
1307 element_open(&ho, "p");
1308
1309 for (i = 0; (entry =
1310 index234(idx->entries, i)) != NULL; i++) {
1311 htmlindex *hi = (htmlindex *)entry->backend_data;
1312 int j;
1313
1314 if (i > 0)
1315 element_empty(&ho, "br");
1316 html_nl(&ho);
1317
1318 html_words(&ho, entry->text, MARKUP|LINKS,
1319 f, keywords, &conf);
1320
56a99eb6 1321 html_text(&ho, conf.index_main_sep);
78c73085 1322
1323 for (j = 0; j < hi->nrefs; j++) {
1324 htmlindexref *hr =
1325 (htmlindexref *)hi->refs[j]->private_data;
1326 paragraph *p = hr->section->title;
1327
1328 if (j > 0)
56a99eb6 1329 html_text(&ho, conf.index_multi_sep);
78c73085 1330
1331 html_href(&ho, f, hr->section->file,
1332 hr->fragment);
1b7bf715 1333 hr->referenced = TRUE;
78c73085 1334 if (p && p->kwtext)
1335 html_words(&ho, p->kwtext, MARKUP|LINKS,
1336 f, keywords, &conf);
1337 else if (p && p->words)
1338 html_words(&ho, p->words, MARKUP|LINKS,
1339 f, keywords, &conf);
56a99eb6 1340 else {
1341 /*
1342 * If there is no title at all,
1343 * this must be because our
1344 * target section is the
1345 * preamble section and there
1346 * is no title. So we use the
1347 * preamble_text.
1348 */
1349 html_text(&ho, conf.preamble_text);
1350 }
78c73085 1351 element_close(&ho, "a");
1352 }
1353 }
1354 element_close(&ho, "p");
1355 }
1356 }
1357 }
1358
1359 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1360 html_nl(&ho);
1361
1362 {
1363 /*
1364 * Footer.
1365 */
1366 int done_version_ids = FALSE;
1367
1368 element_empty(&ho, "hr");
1369
1370 if (conf.body_end)
1371 html_raw(&ho, conf.body_end);
1372
1373 if (conf.address_section) {
27bdc5ab 1374 int started = FALSE;
1375 if (conf.htmlver == ISO_HTML) {
1376 /*
1377 * The ISO-HTML validator complains if
1378 * there isn't a <div> tag surrounding the
1379 * <address> tag. I'm uncertain of why this
1380 * should be - there appears to be no
1381 * mention of this in the ISO-HTML spec,
1382 * suggesting that it doesn't represent a
1383 * change from HTML 4, but nonetheless the
1384 * HTML 4 validator doesn't seem to mind.
1385 */
1386 element_open(&ho, "div");
1387 }
78c73085 1388 element_open(&ho, "address");
1389 if (conf.addr_start) {
1390 html_raw(&ho, conf.addr_start);
1391 html_nl(&ho);
27bdc5ab 1392 started = TRUE;
78c73085 1393 }
1394 if (conf.visible_version_id) {
78c73085 1395 for (p = sourceform; p; p = p->next)
1396 if (p->type == para_VersionID) {
27bdc5ab 1397 if (started)
78c73085 1398 element_empty(&ho, "br");
1399 html_nl(&ho);
56a99eb6 1400 html_text(&ho, conf.pre_versionid);
78c73085 1401 html_words(&ho, p->words, NOTHING,
1402 f, keywords, &conf);
56a99eb6 1403 html_text(&ho, conf.post_versionid);
78c73085 1404 started = TRUE;
1405 }
78c73085 1406 done_version_ids = TRUE;
1407 }
27bdc5ab 1408 if (conf.addr_end) {
1409 if (started)
1410 element_empty(&ho, "br");
78c73085 1411 html_raw(&ho, conf.addr_end);
27bdc5ab 1412 }
78c73085 1413 element_close(&ho, "address");
27bdc5ab 1414 if (conf.htmlver == ISO_HTML)
1415 element_close(&ho, "div");
78c73085 1416 }
1417
1418 if (!done_version_ids) {
1419 /*
1420 * If the user didn't want the version IDs
1421 * visible, I think we still have a duty to put
1422 * them in an HTML comment.
1423 */
1424 int started = FALSE;
1425 for (p = sourceform; p; p = p->next)
1426 if (p->type == para_VersionID) {
1427 if (!started) {
1428 html_raw(&ho, "<!-- version IDs:\n");
1429 started = TRUE;
1430 }
1431 html_words(&ho, p->words, NOTHING,
1432 f, keywords, &conf);
1433 html_nl(&ho);
1434 }
1435 if (started)
1436 html_raw(&ho, "-->\n");
1437 }
1438 }
1439
1440 element_close(&ho, "body");
1441 html_nl(&ho);
1442 element_close(&ho, "html");
1443 html_nl(&ho);
1444 cleanup(&ho);
1445 }
1446 }
1447
1448 /*
1b7bf715 1449 * Go through and check that no index fragments were referenced
1450 * without being generated, or indeed vice versa.
1451 *
1452 * (When I actually get round to freeing everything, this can
1453 * probably be the freeing loop as well.)
1454 */
1455 for (p = sourceform; p; p = p->next) {
1456 word *w;
1457 for (w = p->words; w; w = w->next)
1458 if (w->type == word_IndexRef) {
1459 htmlindexref *hr = (htmlindexref *)w->private_data;
1460
1461 assert(!hr->referenced == !hr->generated);
1462 }
1463 }
1464
1465 /*
529a6c83 1466 * Free all the working data.
78c73085 1467 */
529a6c83 1468 {
1469 htmlfragment *frag;
1470 while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) {
1471 /*
1472 * frag->fragment is dynamically allocated, but will be
1473 * freed when we process the htmlsect structure which
1474 * it is attached to.
1475 */
1476 sfree(frag);
1477 }
1478 freetree234(files.frags);
1479 }
1480 {
1481 htmlsect *sect, *tmp;
1482 sect = sects.head;
1483 while (sect) {
12f0ee84 1484 int i;
529a6c83 1485 tmp = sect->next;
12f0ee84 1486 for (i=0; i < conf.ntfragments; i++)
1487 sfree(sect->fragments[i]);
1488 sfree(sect->fragments);
529a6c83 1489 sfree(sect);
1490 sect = tmp;
1491 }
1492 sect = nonsects.head;
1493 while (sect) {
12f0ee84 1494 int i;
529a6c83 1495 tmp = sect->next;
12f0ee84 1496 for (i=0; i < conf.ntfragments; i++)
1497 sfree(sect->fragments[i]);
1498 sfree(sect->fragments);
529a6c83 1499 sfree(sect);
1500 sect = tmp;
1501 }
1502 }
1503 {
1504 htmlfile *file, *tmp;
1505 file = files.head;
1506 while (file) {
1507 tmp = file->next;
1508 sfree(file->filename);
1509 sfree(file);
1510 file = tmp;
1511 }
1512 }
1513 {
1514 int i;
1515 indexentry *entry;
1516 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
1517 htmlindex *hi = (htmlindex *)entry->backend_data;
1518 sfree(hi);
1519 }
1520 }
1521 {
1522 paragraph *p;
1523 word *w;
1524 for (p = sourceform; p; p = p->next)
1525 for (w = p->words; w; w = w->next)
1526 if (w->type == word_IndexRef) {
1527 htmlindexref *hr = (htmlindexref *)w->private_data;
1528 assert(hr != NULL);
1529 sfree(hr->fragment);
1530 sfree(hr);
1531 }
1532 }
12f0ee84 1533 sfree(conf.asect);
1534 sfree(conf.single_filename);
1535 sfree(conf.contents_filename);
1536 sfree(conf.index_filename);
1537 sfree(conf.template_filename);
1538 while (conf.ntfragments--)
1539 sfree(conf.template_fragments[conf.ntfragments]);
1540 sfree(conf.template_fragments);
78c73085 1541}
1542
1543static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
1544 htmlsect *sect, int depth)
1545{
1546 htmlfile *file;
1547 int ldepth;
1548
1549 /*
1550 * `depth' is derived from the heading_depth() macro at the top
1551 * of this file, which counts title as -1, chapter as 0,
1552 * heading as 1 and subsection as 2. However, the semantics of
1553 * cfg->leaf_level are defined to count chapter as 1, heading
1554 * as 2 etc. So first I increment depth :-(
1555 */
1556 ldepth = depth + 1;
1557
1558 if (cfg->leaf_level == 0) {
1559 /*
1560 * leaf_level==0 is a special case, in which everything is
1561 * put into a single file.
1562 */
1563 if (!files->single)
1564 files->single = html_new_file(files, cfg->single_filename);
1565
1566 file = files->single;
1567 } else {
1568 /*
1569 * If the depth of this section is at or above leaf_level,
1570 * we invent a fresh file and put this section at its head.
1571 * Otherwise, we put it in the same file as its parent
1572 * section.
1573 */
1574 if (ldepth > cfg->leaf_level) {
1575 /*
1576 * We know that sect->parent cannot be NULL. The only
1577 * circumstance in which it can be is if sect is at
1578 * chapter or appendix level, i.e. ldepth==1; and if
1579 * that's the case, then we cannot have entered this
1580 * branch unless cfg->leaf_level==0, in which case we
1581 * would be in the single-file case above and not here
1582 * at all.
1583 */
1584 assert(sect->parent);
1585
1586 file = sect->parent->file;
1587 } else {
1588 if (sect->type == TOP) {
1589 file = html_new_file(files, cfg->contents_filename);
1590 } else if (sect->type == INDEX) {
1591 file = html_new_file(files, cfg->index_filename);
1592 } else {
1593 char *title;
1594
1595 assert(ldepth > 0 && sect->title);
1596 title = html_format(sect->title, cfg->template_filename);
1597 file = html_new_file(files, title);
1598 sfree(title);
1599 }
1600 }
1601 }
1602
1603 sect->file = file;
1604
1605 if (file->min_heading_depth > depth) {
1606 /*
1607 * This heading is at a higher level than any heading we
1608 * have so far placed in this file; so we set the `first'
1609 * pointer.
1610 */
1611 file->min_heading_depth = depth;
1612 file->first = sect;
1613 }
1614
1615 if (file->min_heading_depth == depth)
1616 file->last = sect;
1617}
1618
1619static htmlfile *html_new_file(htmlfilelist *list, char *filename)
1620{
f1530049 1621 htmlfile *ret = snew(htmlfile);
78c73085 1622
1623 ret->next = NULL;
1624 if (list->tail)
1625 list->tail->next = ret;
1626 else
1627 list->head = ret;
1628 list->tail = ret;
1629
1630 ret->filename = dupstr(filename);
1631 ret->last_fragment_number = 0;
1632 ret->min_heading_depth = INT_MAX;
1633 ret->first = ret->last = NULL;
1634
1635 return ret;
1636}
1637
12f0ee84 1638static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
1639 htmlconfig *cfg)
78c73085 1640{
f1530049 1641 htmlsect *ret = snew(htmlsect);
78c73085 1642
1643 ret->next = NULL;
1644 if (list->tail)
1645 list->tail->next = ret;
1646 else
1647 list->head = ret;
1648 list->tail = ret;
1649
1650 ret->title = title;
1651 ret->file = NULL;
1652 ret->parent = NULL;
1653 ret->type = NORMAL;
1654
12f0ee84 1655 ret->fragments = snewn(cfg->ntfragments, char *);
1656 {
1657 int i;
1658 for (i=0; i < cfg->ntfragments; i++)
1659 ret->fragments[i] = NULL;
1660 }
1661
78c73085 1662 return ret;
1663}
1664
1665static void html_words(htmloutput *ho, word *words, int flags,
1666 htmlfile *file, keywordlist *keywords, htmlconfig *cfg)
1667{
1668 word *w;
1669 char *c;
1670 int style, type;
1671
1672 for (w = words; w; w = w->next) switch (w->type) {
1673 case word_HyperLink:
1674 if (flags & LINKS) {
1675 element_open(ho, "a");
1676 c = utoa_dup(w->text, CS_ASCII);
1677 element_attr(ho, "href", c);
1678 sfree(c);
1679 }
1680 break;
1681 case word_UpperXref:
1682 case word_LowerXref:
1683 if (flags & LINKS) {
1684 keyword *kwl = kw_lookup(keywords, w->text);
2b0986a3 1685 paragraph *p;
1686 htmlsect *s;
1687
1688 assert(kwl);
1689 p = kwl->para;
1690 s = (htmlsect *)p->private_data;
78c73085 1691
1692 assert(s);
1693
12f0ee84 1694 html_href(ho, file, s->file, s->fragments[0]);
78c73085 1695 }
1696 break;
1697 case word_HyperEnd:
1698 case word_XrefEnd:
1699 if (flags & LINKS)
1700 element_close(ho, "a");
1701 break;
1702 case word_IndexRef:
1703 if (flags & INDEXENTS) {
1704 htmlindexref *hr = (htmlindexref *)w->private_data;
27bdc5ab 1705 html_fragment(ho, hr->fragment);
1b7bf715 1706 hr->generated = TRUE;
78c73085 1707 }
1708 break;
1709 case word_Normal:
1710 case word_Emph:
1711 case word_Code:
1712 case word_WeakCode:
1713 case word_WhiteSpace:
1714 case word_EmphSpace:
1715 case word_CodeSpace:
1716 case word_WkCodeSpace:
1717 case word_Quote:
1718 case word_EmphQuote:
1719 case word_CodeQuote:
1720 case word_WkCodeQuote:
1721 style = towordstyle(w->type);
1722 type = removeattr(w->type);
1723 if (style == word_Emph &&
1724 (attraux(w->aux) == attr_First ||
1725 attraux(w->aux) == attr_Only) &&
1726 (flags & MARKUP))
1727 element_open(ho, "em");
1728 else if ((style == word_Code || style == word_WeakCode) &&
1729 (attraux(w->aux) == attr_First ||
1730 attraux(w->aux) == attr_Only) &&
1731 (flags & MARKUP))
1732 element_open(ho, "code");
1733
1734 if (type == word_WhiteSpace)
1735 html_text(ho, L" ");
1736 else if (type == word_Quote) {
1737 if (quoteaux(w->aux) == quote_Open)
1738 html_text(ho, cfg->lquote);
1739 else
1740 html_text(ho, cfg->rquote);
1741 } else {
35b123a0 1742 if (!w->alt || cvt_ok(ho->restrict_charset, w->text))
1743 html_text_nbsp(ho, w->text);
78c73085 1744 else
1745 html_words(ho, w->alt, flags, file, keywords, cfg);
1746 }
1747
1748 if (style == word_Emph &&
1749 (attraux(w->aux) == attr_Last ||
1750 attraux(w->aux) == attr_Only) &&
1751 (flags & MARKUP))
1752 element_close(ho, "em");
1753 else if ((style == word_Code || style == word_WeakCode) &&
1754 (attraux(w->aux) == attr_Last ||
1755 attraux(w->aux) == attr_Only) &&
1756 (flags & MARKUP))
1757 element_close(ho, "code");
1758
1759 break;
1760 }
1761}
1762
1763static void html_codepara(htmloutput *ho, word *words)
1764{
1765 element_open(ho, "pre");
1766 element_open(ho, "code");
1767 for (; words; words = words->next) if (words->type == word_WeakCode) {
1768 char *open_tag;
1769 wchar_t *t, *e;
1770
1771 t = words->text;
1772 if (words->next && words->next->type == word_Emph) {
1773 e = words->next->text;
1774 words = words->next;
1775 } else
1776 e = NULL;
1777
1778 while (e && *e && *t) {
1779 int n;
1780 int ec = *e;
1781
1782 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
1783
1784 open_tag = NULL;
1785 if (ec == 'i')
1786 open_tag = "em";
1787 else if (ec == 'b')
1788 open_tag = "b";
1789 if (open_tag)
1790 element_open(ho, open_tag);
1791
1792 html_text_limit(ho, t, n);
1793
1794 if (open_tag)
1795 element_close(ho, open_tag);
1796
1797 t += n;
1798 e += n;
1799 }
1800 html_text(ho, t);
1801 html_nl(ho);
1802 }
1803 element_close(ho, "code");
1804 element_close(ho, "pre");
1805}
1806
1807static void html_charset_cleanup(htmloutput *ho)
1808{
1809 char outbuf[256];
1810 int bytes;
1811
1812 bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf),
1813 ho->charset, &ho->cstate, NULL);
1814 if (bytes > 0)
1815 fwrite(outbuf, 1, bytes, ho->fp);
1816}
1817
35b123a0 1818static void return_mostly_to_neutral(htmloutput *ho)
78c73085 1819{
35b123a0 1820 if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) {
78c73085 1821 fprintf(ho->fp, " />");
1822 } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) {
1823 fprintf(ho->fp, ">");
1824 }
1825
1826 ho->state = HO_NEUTRAL;
1827}
1828
35b123a0 1829static void return_to_neutral(htmloutput *ho)
1830{
1831 if (ho->state == HO_IN_TEXT) {
1832 html_charset_cleanup(ho);
1833 }
1834
1835 return_mostly_to_neutral(ho);
1836}
1837
78c73085 1838static void element_open(htmloutput *ho, char const *name)
1839{
1840 return_to_neutral(ho);
1841 fprintf(ho->fp, "<%s", name);
1842 ho->state = HO_IN_TAG;
1843}
1844
1845static void element_close(htmloutput *ho, char const *name)
1846{
1847 return_to_neutral(ho);
1848 fprintf(ho->fp, "</%s>", name);
1849 ho->state = HO_NEUTRAL;
1850}
1851
1852static void element_empty(htmloutput *ho, char const *name)
1853{
1854 return_to_neutral(ho);
1855 fprintf(ho->fp, "<%s", name);
1856 ho->state = HO_IN_EMPTY_TAG;
1857}
1858
1859static void html_nl(htmloutput *ho)
1860{
1861 return_to_neutral(ho);
1862 fputc('\n', ho->fp);
1863}
1864
1865static void html_raw(htmloutput *ho, char *text)
1866{
1867 return_to_neutral(ho);
1868 fputs(text, ho->fp);
1869}
1870
1871static void html_raw_as_attr(htmloutput *ho, char *text)
1872{
1873 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
1874 fputc(' ', ho->fp);
1875 fputs(text, ho->fp);
1876}
1877
1878static void element_attr(htmloutput *ho, char const *name, char const *value)
1879{
1880 html_charset_cleanup(ho);
1881 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
1882 fprintf(ho->fp, " %s=\"%s\"", name, value);
1883}
1884
1885static void element_attr_w(htmloutput *ho, char const *name,
1886 wchar_t const *value)
1887{
1888 html_charset_cleanup(ho);
1889 fprintf(ho->fp, " %s=\"", name);
35b123a0 1890 html_text_limit_internal(ho, value, 0, TRUE, FALSE);
78c73085 1891 html_charset_cleanup(ho);
1892 fputc('"', ho->fp);
1893}
1894
1895static void html_text(htmloutput *ho, wchar_t const *text)
1896{
35b123a0 1897 return_mostly_to_neutral(ho);
1898 html_text_limit_internal(ho, text, 0, FALSE, FALSE);
1899}
1900
1901static void html_text_nbsp(htmloutput *ho, wchar_t const *text)
1902{
1903 return_mostly_to_neutral(ho);
1904 html_text_limit_internal(ho, text, 0, FALSE, TRUE);
78c73085 1905}
1906
1907static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen)
1908{
35b123a0 1909 return_mostly_to_neutral(ho);
1910 html_text_limit_internal(ho, text, maxlen, FALSE, FALSE);
78c73085 1911}
1912
1913static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
35b123a0 1914 int maxlen, int quote_quotes, int nbsp)
78c73085 1915{
1916 int textlen = ustrlen(text);
1917 char outbuf[256];
1918 int bytes, err;
1919
1920 if (maxlen > 0 && textlen > maxlen)
1921 textlen = maxlen;
1922
1923 while (textlen > 0) {
1924 /* Scan ahead for characters we really can't display in HTML. */
1925 int lenbefore, lenafter;
1926 for (lenbefore = 0; lenbefore < textlen; lenbefore++)
1927 if (text[lenbefore] == L'<' ||
1928 text[lenbefore] == L'>' ||
1929 text[lenbefore] == L'&' ||
35b123a0 1930 (text[lenbefore] == L'"' && quote_quotes) ||
1931 (text[lenbefore] == L' ' && nbsp))
78c73085 1932 break;
1933 lenafter = lenbefore;
1934 bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf),
1935 ho->charset, &ho->cstate, &err);
1936 textlen -= (lenbefore - lenafter);
1937 if (bytes > 0)
1938 fwrite(outbuf, 1, bytes, ho->fp);
1939 if (err) {
1940 /*
1941 * We have encountered a character that cannot be
1942 * displayed in the selected output charset. Therefore,
1943 * we use an HTML numeric entity reference.
1944 */
1945 assert(textlen > 0);
1946 fprintf(ho->fp, "&#%ld;", (long int)*text);
1947 text++, textlen--;
1948 } else if (lenafter == 0 && textlen > 0) {
1949 /*
1950 * We have encountered a character which is special to
1951 * HTML.
1952 */
1953 if (*text == L'<')
1954 fprintf(ho->fp, "&lt;");
1955 else if (*text == L'>')
1956 fprintf(ho->fp, "&gt;");
1957 else if (*text == L'&')
1958 fprintf(ho->fp, "&amp;");
1959 else if (*text == L'"')
1960 fprintf(ho->fp, "&quot;");
35b123a0 1961 else if (*text == L' ') {
1962 assert(nbsp);
1963 fprintf(ho->fp, "&nbsp;");
1964 } else
78c73085 1965 assert(!"Can't happen");
1966 text++, textlen--;
1967 }
1968 }
1969}
1970
1971static void cleanup(htmloutput *ho)
1972{
1973 return_to_neutral(ho);
1974 fclose(ho->fp);
1975}
1976
1977static void html_href(htmloutput *ho, htmlfile *thisfile,
1978 htmlfile *targetfile, char *targetfrag)
1979{
1980 rdstringc rs = { 0, 0, NULL };
1981 char *url;
1982
1983 if (targetfile != thisfile)
1984 rdaddsc(&rs, targetfile->filename);
1985 if (targetfrag) {
1986 rdaddc(&rs, '#');
1987 rdaddsc(&rs, targetfrag);
1988 }
1989 url = rs.text;
1990
1991 element_open(ho, "a");
1992 element_attr(ho, "href", url);
1993 sfree(url);
1994}
1995
27bdc5ab 1996static void html_fragment(htmloutput *ho, char const *fragment)
1997{
1998 element_open(ho, "a");
1999 element_attr(ho, "name", fragment);
2000 if (is_xhtml(ho->ver))
2001 element_attr(ho, "id", fragment);
2002 element_close(ho, "a");
2003}
2004
78c73085 2005static char *html_format(paragraph *p, char *template_string)
2006{
2007 char *c, *t;
2008 word *w;
2009 wchar_t *ws, wsbuf[2];
2010 rdstringc rs = { 0, 0, NULL };
2011
2012 t = template_string;
2013 while (*t) {
2014 if (*t == '%' && t[1]) {
2015 int fmt;
2016
2017 t++;
2018 fmt = *t++;
2019
2020 if (fmt == '%') {
2021 rdaddc(&rs, fmt);
2022 continue;
2023 }
2024
2025 w = NULL;
2026 ws = NULL;
2027
2028 if (p->kwtext && fmt == 'n')
2029 w = p->kwtext;
2030 else if (p->kwtext2 && fmt == 'b') {
2031 /*
2032 * HTML fragment names must start with a letter, so
2033 * simply `1.2.3' is not adequate. In this case I'm
2034 * going to cheat slightly by prepending the first
2035 * character of the first word of kwtext, so that
2036 * we get `C1' for chapter 1, `S2.3' for section
2037 * 2.3 etc.
2038 */
2039 if (p->kwtext && p->kwtext->text[0]) {
2040 ws = wsbuf;
2041 wsbuf[1] = '\0';
2042 wsbuf[0] = p->kwtext->text[0];
2043 }
2044 w = p->kwtext2;
2045 } else if (p->keyword && *p->keyword && fmt == 'k')
2046 ws = p->keyword;
2047 else
46c7302f 2048 /* %N comes here; also failure cases of other fmts */
78c73085 2049 w = p->words;
2050
2051 if (ws) {
2052 c = utoa_dup(ws, CS_ASCII);
2053 rdaddsc(&rs,c);
2054 sfree(c);
2055 }
2056
2057 while (w) {
2058 if (removeattr(w->type) == word_Normal) {
2059 c = utoa_dup(w->text, CS_ASCII);
2060 rdaddsc(&rs,c);
2061 sfree(c);
2062 }
2063 w = w->next;
2064 }
2065 } else {
2066 rdaddc(&rs, *t++);
2067 }
2068 }
2069
2070 return rdtrimc(&rs);
2071}
2072
3e82de8f 2073static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
2074 char *text)
78c73085 2075{
2076 /*
2077 * The HTML 4 spec's strictest definition of fragment names (<a
2078 * name> and "id" attributes) says that they `must begin with a
2079 * letter and may be followed by any number of letters, digits,
2080 * hyphens, underscores, colons, and periods'.
2081 *
2082 * So here we unceremoniously rip out any characters not
2083 * conforming to this limitation.
2084 */
2085 char *p = text, *q = text;
2086
2087 while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z')))
2088 p++;
3e82de8f 2089 if ((*q++ = *p++) != '\0') {
2090 while (*p) {
2091 if ((*p>='A' && *p<='Z') ||
2092 (*p>='a' && *p<='z') ||
2093 (*p>='0' && *p<='9') ||
2094 *p=='-' || *p=='_' || *p==':' || *p=='.')
2095 *q++ = *p;
2096 p++;
2097 }
2098
2099 *q = '\0';
2100 }
2101
12f0ee84 2102 /* If there's nothing left, make something valid up */
2103 if (!*text) {
fbe8cf20 2104 static const char *const anonfrag = "anon";
12f0ee84 2105 text = sresize(text, lenof(anonfrag), char);
2106 strcpy(text, anonfrag);
2107 }
2108
3e82de8f 2109 /*
2110 * Now we check for clashes with other fragment names, and
2111 * adjust this one if necessary by appending a hyphen followed
2112 * by a number.
2113 */
2114 {
2115 htmlfragment *frag = snew(htmlfragment);
2116 int len = 0; /* >0 indicates we have resized */
2117 int suffix = 1;
2118
2119 frag->file = file;
2120 frag->fragment = text;
2121
2122 while (add234(files->frags, frag) != frag) {
2123 if (!len) {
2124 len = strlen(text);
2125 frag->fragment = text = sresize(text, len+20, char);
2126 }
2127
2128 sprintf(text + len, "-%d", ++suffix);
2129 }
78c73085 2130 }
2131
3e82de8f 2132 return text;
78c73085 2133}
2134
2135static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
2136 htmlfile *thisfile, keywordlist *keywords,
2137 htmlconfig *cfg)
2138{
f5dee642 2139 if (ho->contents_level >= depth && ho->contents_level > 0) {
2140 element_close(ho, "li");
2141 html_nl(ho);
2142 }
2143
78c73085 2144 while (ho->contents_level > depth) {
2145 element_close(ho, "ul");
2146 ho->contents_level--;
f5dee642 2147 if (ho->contents_level > 0) {
2148 element_close(ho, "li");
2149 }
2150 html_nl(ho);
78c73085 2151 }
2152
2153 while (ho->contents_level < depth) {
f5dee642 2154 html_nl(ho);
78c73085 2155 element_open(ho, "ul");
f5dee642 2156 html_nl(ho);
78c73085 2157 ho->contents_level++;
2158 }
2159
2160 if (!s)
2161 return;
2162
2163 element_open(ho, "li");
12f0ee84 2164 html_href(ho, thisfile, s->file, s->fragments[0]);
23c9bbc2 2165 html_section_title(ho, s, thisfile, keywords, cfg, FALSE);
78c73085 2166 element_close(ho, "a");
f5dee642 2167 /* <li> will be closed by a later invocation */
78c73085 2168}
2169
2170static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile,
23c9bbc2 2171 keywordlist *keywords, htmlconfig *cfg,
2172 int real)
78c73085 2173{
2174 if (s->title) {
2175 sectlevel *sl;
2176 word *number;
2177 int depth = heading_depth(s->title);
2178
2179 if (depth < 0)
2180 sl = NULL;
2181 else if (depth == 0)
2182 sl = &cfg->achapter;
2183 else if (depth <= cfg->nasect)
2184 sl = &cfg->asect[depth-1];
2185 else
2186 sl = &cfg->asect[cfg->nasect-1];
2187
2188 if (!sl)
2189 number = NULL;
2190 else if (sl->just_numbers)
2191 number = s->title->kwtext2;
2192 else
2193 number = s->title->kwtext;
2194
2195 if (number) {
2196 html_words(ho, number, MARKUP,
2197 thisfile, keywords, cfg);
2198 html_text(ho, sl->number_suffix);
2199 }
2200
23c9bbc2 2201 html_words(ho, s->title->words, real ? ALL : MARKUP,
78c73085 2202 thisfile, keywords, cfg);
2203 } else {
2204 assert(s->type != NORMAL);
56a99eb6 2205 /*
2206 * If we're printing the full document title for _real_ and
2207 * there isn't one, we don't want to print `Preamble' at
2208 * the top of what ought to just be some text. If we need
2209 * it in any other context such as TOCs, we need to print
2210 * `Preamble'.
2211 */
2212 if (s->type == TOP && !real)
2213 html_text(ho, cfg->preamble_text);
78c73085 2214 else if (s->type == INDEX)
56a99eb6 2215 html_text(ho, cfg->index_text);
78c73085 2216 }
2217}