About time I fixed this: allow for some people's md5sum utilities
[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");
3ae196b4 766 if (!ho.fp)
767 error(err_cantopenw, f->filename);
768
78c73085 769 ho.charset = conf.output_charset;
b7309494 770 ho.restrict_charset = conf.restrict_charset;
78c73085 771 ho.cstate = charset_init_state;
772 ho.ver = conf.htmlver;
773 ho.state = HO_NEUTRAL;
774 ho.contents_level = 0;
775
776 /* <!DOCTYPE>. */
777 switch (conf.htmlver) {
778 case HTML_3_2:
3ae196b4 779 if (ho.fp)
780 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
781 "HTML 3.2 Final//EN\">\n");
78c73085 782 break;
783 case HTML_4:
3ae196b4 784 if (ho.fp)
785 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
786 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
787 "strict.dtd\">\n");
78c73085 788 break;
27bdc5ab 789 case ISO_HTML:
3ae196b4 790 if (ho.fp)
791 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
792 "15445:2000//DTD HTML//EN\">\n");
27bdc5ab 793 break;
78c73085 794 case XHTML_1_0_TRANSITIONAL:
3ae196b4 795 if (ho.fp) {
796 fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
797 charset_to_mimeenc(conf.output_charset));
798 fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
799 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
800 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
801 }
78c73085 802 break;
803 case XHTML_1_0_STRICT:
3ae196b4 804 if (ho.fp) {
805 fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
806 charset_to_mimeenc(conf.output_charset));
807 fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
808 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
809 "DTD/xhtml1-strict.dtd\">\n");
810 }
78c73085 811 break;
812 }
813
814 element_open(&ho, "html");
815 if (is_xhtml(conf.htmlver)) {
816 element_attr(&ho, "xmlns", "http://www.w3.org/1999/xhtml");
817 }
818 html_nl(&ho);
819
820 element_open(&ho, "head");
821 html_nl(&ho);
822
823 element_empty(&ho, "meta");
824 element_attr(&ho, "http-equiv", "content-type");
825 {
826 char buf[200];
827 sprintf(buf, "text/html; charset=%.150s",
828 charset_to_mimeenc(conf.output_charset));
829 element_attr(&ho, "content", buf);
830 }
831 html_nl(&ho);
832
833 if (conf.author) {
834 element_empty(&ho, "meta");
835 element_attr(&ho, "name", "author");
836 element_attr_w(&ho, "content", conf.author);
837 html_nl(&ho);
838 }
839
840 if (conf.description) {
841 element_empty(&ho, "meta");
842 element_attr(&ho, "name", "description");
843 element_attr_w(&ho, "content", conf.description);
844 html_nl(&ho);
845 }
846
847 element_open(&ho, "title");
848 if (f->first && f->first->title) {
849 html_words(&ho, f->first->title->words, NOTHING,
850 f, keywords, &conf);
851
852 assert(f->last);
853 if (f->last != f->first && f->last->title) {
56a99eb6 854 html_text(&ho, conf.title_separator);
78c73085 855 html_words(&ho, f->last->title->words, NOTHING,
856 f, keywords, &conf);
857 }
858 }
859 element_close(&ho, "title");
860 html_nl(&ho);
861
862 if (conf.head_end)
863 html_raw(&ho, conf.head_end);
864
9acfce4f 865 /*
866 * Add any <head> data defined in specific sections
867 * that go in this file. (This is mostly to allow <meta
868 * name="AppleTitle"> tags for Mac online help.)
869 */
870 for (s = sects.head; s; s = s->next) {
871 if (s->file == f && s->text) {
872 for (p = s->text;
6f0bdcde 873 p && (p == s->text || p->type == para_Title ||
874 !is_heading_type(p->type));
9acfce4f 875 p = p->next) {
876 if (p->type == para_Config) {
877 if (!ustricmp(p->keyword, L"html-local-head")) {
878 html_raw(&ho, adv(p->origkeyword));
879 }
880 }
881 }
882 }
883 }
884
78c73085 885 element_close(&ho, "head");
886 html_nl(&ho);
887
78c73085 888 if (conf.body_tag)
889 html_raw(&ho, conf.body_tag);
890 else
891 element_open(&ho, "body");
892 html_nl(&ho);
893
894 if (conf.body_start)
895 html_raw(&ho, conf.body_start);
896
897 /*
898 * Write out a nav bar. Special case: we don't do this
899 * if there is only one file.
900 */
901 if (files.head != files.tail) {
902 element_open(&ho, "p");
903 if (conf.nav_attr)
904 html_raw_as_attr(&ho, conf.nav_attr);
905
906 if (prevf) {
907 element_open(&ho, "a");
908 element_attr(&ho, "href", prevf->filename);
909 }
56a99eb6 910 html_text(&ho, conf.nav_prev_text);
78c73085 911 if (prevf)
912 element_close(&ho, "a");
913
56a99eb6 914 html_text(&ho, conf.nav_separator);
78c73085 915
916 if (f != files.head) {
917 element_open(&ho, "a");
918 element_attr(&ho, "href", files.head->filename);
919 }
56a99eb6 920 html_text(&ho, conf.contents_text);
78c73085 921 if (f != files.head)
922 element_close(&ho, "a");
923
03fcb340 924 if (has_index) {
771d3da3 925 html_text(&ho, conf.nav_separator);
03fcb340 926 if (f != files.index) {
927 element_open(&ho, "a");
928 element_attr(&ho, "href", files.index->filename);
929 }
930 html_text(&ho, conf.index_text);
931 if (f != files.index)
932 element_close(&ho, "a");
78c73085 933 }
78c73085 934
56a99eb6 935 html_text(&ho, conf.nav_separator);
78c73085 936
937 if (f->next) {
938 element_open(&ho, "a");
939 element_attr(&ho, "href", f->next->filename);
940 }
56a99eb6 941 html_text(&ho, conf.nav_next_text);
78c73085 942 if (f->next)
943 element_close(&ho, "a");
944
945 element_close(&ho, "p");
946 html_nl(&ho);
947 }
948 prevf = f;
949
950 /*
f5dee642 951 * Write out a prefix TOC for the file (if a leaf file).
78c73085 952 *
953 * We start by going through the section list and
954 * collecting the sections which need to be added to
955 * the contents. On the way, we also test to see if
956 * this file is a leaf file (defined as one which
957 * contains all descendants of any section it
958 * contains), because this will play a part in our
959 * decision on whether or not to _output_ the TOC.
960 *
961 * Special case: we absolutely do not do this if we're
962 * in single-file mode.
963 */
964 if (files.head != files.tail) {
965 int ntoc = 0, tocsize = 0;
966 htmlsect **toc = NULL;
967 int leaf = TRUE;
968
969 for (s = sects.head; s; s = s->next) {
970 htmlsect *a, *ac;
971 int depth, adepth;
972
973 /*
974 * Search up from this section until we find
975 * the highest-level one which belongs in this
976 * file.
977 */
978 depth = adepth = 0;
979 a = NULL;
980 for (ac = s; ac; ac = ac->parent) {
981 if (ac->file == f) {
982 a = ac;
983 adepth = depth;
984 }
985 depth++;
986 }
987
988 if (s->file != f && a != NULL)
989 leaf = FALSE;
990
991 if (a) {
992 if (adepth <= a->contents_depth) {
993 if (ntoc >= tocsize) {
994 tocsize += 64;
f1530049 995 toc = sresize(toc, tocsize, htmlsect *);
78c73085 996 }
997 toc[ntoc++] = s;
998 }
999 }
1000 }
1001
1002 if (leaf && conf.leaf_contains_contents &&
1003 ntoc >= conf.leaf_smallest_contents) {
1004 int i;
1005
1006 for (i = 0; i < ntoc; i++) {
1007 htmlsect *s = toc[i];
1008 int hlevel = (s->type == TOP ? -1 :
1009 s->type == INDEX ? 0 :
1010 heading_depth(s->title))
1011 - f->min_heading_depth + 1;
1012
1013 assert(hlevel >= 1);
1014 html_contents_entry(&ho, hlevel, s,
1015 f, keywords, &conf);
1016 }
1017 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1018 }
1019 }
1020
1021 /*
1022 * Now go through the document and output some real
1023 * text.
1024 */
1025 displaying = FALSE;
1026 for (s = sects.head; s; s = s->next) {
1027 if (s->file == f) {
1028 /*
1029 * This section belongs in this file.
1030 * Display it.
1031 */
1032 displaying = TRUE;
1033 } else {
f5dee642 1034 /*
1035 * Doesn't belong in this file, but it may be
1036 * a descendant of a section which does, in
1037 * which case we should consider it for the
1038 * main TOC of this file (for non-leaf files).
1039 */
78c73085 1040 htmlsect *a, *ac;
1041 int depth, adepth;
1042
1043 displaying = FALSE;
1044
1045 /*
1046 * Search up from this section until we find
1047 * the highest-level one which belongs in this
1048 * file.
1049 */
1050 depth = adepth = 0;
1051 a = NULL;
1052 for (ac = s; ac; ac = ac->parent) {
1053 if (ac->file == f) {
1054 a = ac;
1055 adepth = depth;
1056 }
1057 depth++;
1058 }
1059
1060 if (a != NULL) {
1061 /*
1062 * This section does not belong in this
1063 * file, but an ancestor of it does. Write
1064 * out a contents table entry, if the depth
1065 * doesn't exceed the maximum contents
1066 * depth for the ancestor section.
1067 */
1068 if (adepth <= a->contents_depth) {
1069 html_contents_entry(&ho, adepth, s,
1070 f, keywords, &conf);
1071 }
1072 }
1073 }
1074
1075 if (displaying) {
1076 int hlevel;
1077 char htag[3];
1078
1079 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1080
1081 /*
1082 * Display the section heading.
1083 */
1084
1085 hlevel = (s->type == TOP ? -1 :
1086 s->type == INDEX ? 0 :
1087 heading_depth(s->title))
1088 - f->min_heading_depth + 1;
1089 assert(hlevel >= 1);
1090 /* HTML headings only go up to <h6> */
1091 if (hlevel > 6)
1092 hlevel = 6;
1093 htag[0] = 'h';
1094 htag[1] = '0' + hlevel;
1095 htag[2] = '\0';
1096 element_open(&ho, htag);
1097
1098 /*
12f0ee84 1099 * Provide anchor(s) for cross-links to target.
78c73085 1100 *
78c73085 1101 * (Also we'll have to do this separately in
1102 * other paragraph types - NumberedList and
1103 * BiblioCited.)
1104 */
12f0ee84 1105 {
1106 int i;
1107 for (i=0; i < conf.ntfragments; i++)
1108 if (s->fragments[i])
1109 html_fragment(&ho, s->fragments[i]);
1110 }
78c73085 1111
23c9bbc2 1112 html_section_title(&ho, s, f, keywords, &conf, TRUE);
78c73085 1113
1114 element_close(&ho, htag);
1115
1116 /*
1117 * Now display the section text.
1118 */
1119 if (s->text) {
f1530049 1120 stackhead = snew(struct stackelement);
78c73085 1121 stackhead->next = NULL;
1122 stackhead->listtype = NOLIST;
1123 stackhead->itemtype = NOITEM;
1124
1125 for (p = s->text;; p = p->next) {
1126 enum LISTTYPE listtype;
1127 struct stackelement *se;
1128
1129 /*
1130 * Preliminary switch to figure out what
1131 * sort of list we expect to be inside at
1132 * this stage.
1133 *
1134 * Since p may still be NULL at this point,
1135 * I invent a harmless paragraph type for
1136 * it if it is.
1137 */
1138 switch (p ? p->type : para_Normal) {
1139 case para_Rule:
1140 case para_Normal:
1141 case para_Copyright:
1142 case para_BiblioCited:
1143 case para_Code:
1144 case para_QuotePush:
1145 case para_QuotePop:
1146 case para_Chapter:
1147 case para_Appendix:
1148 case para_UnnumberedChapter:
1149 case para_Heading:
1150 case para_Subsect:
1151 case para_LcontPop:
1152 listtype = NOLIST;
1153 break;
1154
1155 case para_Bullet:
1156 listtype = UL;
1157 break;
1158
1159 case para_NumberedList:
1160 listtype = OL;
1161 break;
1162
1163 case para_DescribedThing:
1164 case para_Description:
1165 listtype = DL;
1166 break;
1167
1168 case para_LcontPush:
f1530049 1169 se = snew(struct stackelement);
78c73085 1170 se->next = stackhead;
1171 se->listtype = NOLIST;
1172 se->itemtype = NOITEM;
1173 stackhead = se;
1174 continue;
1175
1176 default: /* some totally non-printing para */
1177 continue;
1178 }
1179
1180 html_nl(&ho);
1181
1182 /*
1183 * Terminate the most recent list item, if
1184 * any. (We left this until after
1185 * processing LcontPush, since in that case
1186 * the list item won't want to be
1187 * terminated until after the corresponding
1188 * LcontPop.)
1189 */
1190 if (stackhead->itemtype != NOITEM) {
1191 element_close(&ho, itemname(stackhead->itemtype));
1192 html_nl(&ho);
1193 }
1194 stackhead->itemtype = NOITEM;
1195
1196 /*
1197 * Terminate the current list, if it's not
1198 * the one we want to be in.
1199 */
1200 if (listtype != stackhead->listtype &&
1201 stackhead->listtype != NOLIST) {
1202 element_close(&ho, listname(stackhead->listtype));
1203 html_nl(&ho);
1204 }
1205
1206 /*
1207 * Leave the loop if our time has come.
1208 */
1209 if (!p || (is_heading_type(p->type) &&
1210 p->type != para_Title))
1211 break; /* end of section text */
1212
1213 /*
1214 * Start a fresh list if necessary.
1215 */
1216 if (listtype != stackhead->listtype &&
1217 listtype != NOLIST)
1218 element_open(&ho, listname(listtype));
1219
1220 stackhead->listtype = listtype;
1221
1222 switch (p->type) {
1223 case para_Rule:
1224 element_empty(&ho, "hr");
1225 break;
1226 case para_Code:
1227 html_codepara(&ho, p->words);
1228 break;
1229 case para_Normal:
1230 case para_Copyright:
1231 element_open(&ho, "p");
1232 html_nl(&ho);
1233 html_words(&ho, p->words, ALL,
1234 f, keywords, &conf);
1235 html_nl(&ho);
1236 element_close(&ho, "p");
1237 break;
1238 case para_BiblioCited:
1239 element_open(&ho, "p");
1240 if (p->private_data) {
1241 htmlsect *s = (htmlsect *)p->private_data;
12f0ee84 1242 int i;
1243 for (i=0; i < conf.ntfragments; i++)
1244 if (s->fragments[i])
1245 html_fragment(&ho, s->fragments[i]);
78c73085 1246 }
1247 html_nl(&ho);
1248 html_words(&ho, p->kwtext, ALL,
1249 f, keywords, &conf);
1250 html_text(&ho, L" ");
1251 html_words(&ho, p->words, ALL,
1252 f, keywords, &conf);
1253 html_nl(&ho);
1254 element_close(&ho, "p");
1255 break;
1256 case para_Bullet:
1257 case para_NumberedList:
1258 element_open(&ho, "li");
1259 if (p->private_data) {
1260 htmlsect *s = (htmlsect *)p->private_data;
12f0ee84 1261 int i;
1262 for (i=0; i < conf.ntfragments; i++)
1263 if (s->fragments[i])
1264 html_fragment(&ho, s->fragments[i]);
78c73085 1265 }
1266 html_nl(&ho);
1267 stackhead->itemtype = LI;
1268 html_words(&ho, p->words, ALL,
1269 f, keywords, &conf);
1270 break;
1271 case para_DescribedThing:
1272 element_open(&ho, "dt");
1273 html_nl(&ho);
1274 stackhead->itemtype = DT;
1275 html_words(&ho, p->words, ALL,
1276 f, keywords, &conf);
1277 break;
1278 case para_Description:
1279 element_open(&ho, "dd");
1280 html_nl(&ho);
1281 stackhead->itemtype = DD;
1282 html_words(&ho, p->words, ALL,
1283 f, keywords, &conf);
1284 break;
1285
1286 case para_QuotePush:
1287 element_open(&ho, "blockquote");
1288 break;
1289 case para_QuotePop:
1290 element_close(&ho, "blockquote");
1291 break;
1292
1293 case para_LcontPop:
1294 se = stackhead;
1295 stackhead = stackhead->next;
1296 assert(stackhead);
1297 sfree(se);
1298 break;
1299 }
1300 }
1301
1302 assert(stackhead && !stackhead->next);
1303 sfree(stackhead);
1304 }
1305
1306 if (s->type == INDEX) {
1307 indexentry *entry;
1308 int i;
1309
1310 /*
1311 * This section is the index. I'll just
1312 * render it as a single paragraph, with a
1313 * colon between the index term and the
1314 * references, and <br> in between each
1315 * entry.
1316 */
1317 element_open(&ho, "p");
1318
1319 for (i = 0; (entry =
1320 index234(idx->entries, i)) != NULL; i++) {
1321 htmlindex *hi = (htmlindex *)entry->backend_data;
1322 int j;
1323
1324 if (i > 0)
1325 element_empty(&ho, "br");
1326 html_nl(&ho);
1327
1328 html_words(&ho, entry->text, MARKUP|LINKS,
1329 f, keywords, &conf);
1330
56a99eb6 1331 html_text(&ho, conf.index_main_sep);
78c73085 1332
1333 for (j = 0; j < hi->nrefs; j++) {
1334 htmlindexref *hr =
1335 (htmlindexref *)hi->refs[j]->private_data;
1336 paragraph *p = hr->section->title;
1337
1338 if (j > 0)
56a99eb6 1339 html_text(&ho, conf.index_multi_sep);
78c73085 1340
1341 html_href(&ho, f, hr->section->file,
1342 hr->fragment);
1b7bf715 1343 hr->referenced = TRUE;
78c73085 1344 if (p && p->kwtext)
1345 html_words(&ho, p->kwtext, MARKUP|LINKS,
1346 f, keywords, &conf);
1347 else if (p && p->words)
1348 html_words(&ho, p->words, MARKUP|LINKS,
1349 f, keywords, &conf);
56a99eb6 1350 else {
1351 /*
1352 * If there is no title at all,
1353 * this must be because our
1354 * target section is the
1355 * preamble section and there
1356 * is no title. So we use the
1357 * preamble_text.
1358 */
1359 html_text(&ho, conf.preamble_text);
1360 }
78c73085 1361 element_close(&ho, "a");
1362 }
1363 }
1364 element_close(&ho, "p");
1365 }
1366 }
1367 }
1368
1369 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1370 html_nl(&ho);
1371
1372 {
1373 /*
1374 * Footer.
1375 */
1376 int done_version_ids = FALSE;
1377
1378 element_empty(&ho, "hr");
1379
1380 if (conf.body_end)
1381 html_raw(&ho, conf.body_end);
1382
1383 if (conf.address_section) {
27bdc5ab 1384 int started = FALSE;
1385 if (conf.htmlver == ISO_HTML) {
1386 /*
1387 * The ISO-HTML validator complains if
1388 * there isn't a <div> tag surrounding the
1389 * <address> tag. I'm uncertain of why this
1390 * should be - there appears to be no
1391 * mention of this in the ISO-HTML spec,
1392 * suggesting that it doesn't represent a
1393 * change from HTML 4, but nonetheless the
1394 * HTML 4 validator doesn't seem to mind.
1395 */
1396 element_open(&ho, "div");
1397 }
78c73085 1398 element_open(&ho, "address");
1399 if (conf.addr_start) {
1400 html_raw(&ho, conf.addr_start);
1401 html_nl(&ho);
27bdc5ab 1402 started = TRUE;
78c73085 1403 }
1404 if (conf.visible_version_id) {
78c73085 1405 for (p = sourceform; p; p = p->next)
1406 if (p->type == para_VersionID) {
27bdc5ab 1407 if (started)
78c73085 1408 element_empty(&ho, "br");
1409 html_nl(&ho);
56a99eb6 1410 html_text(&ho, conf.pre_versionid);
78c73085 1411 html_words(&ho, p->words, NOTHING,
1412 f, keywords, &conf);
56a99eb6 1413 html_text(&ho, conf.post_versionid);
78c73085 1414 started = TRUE;
1415 }
78c73085 1416 done_version_ids = TRUE;
1417 }
27bdc5ab 1418 if (conf.addr_end) {
1419 if (started)
1420 element_empty(&ho, "br");
78c73085 1421 html_raw(&ho, conf.addr_end);
27bdc5ab 1422 }
78c73085 1423 element_close(&ho, "address");
27bdc5ab 1424 if (conf.htmlver == ISO_HTML)
1425 element_close(&ho, "div");
78c73085 1426 }
1427
1428 if (!done_version_ids) {
1429 /*
1430 * If the user didn't want the version IDs
1431 * visible, I think we still have a duty to put
1432 * them in an HTML comment.
1433 */
1434 int started = FALSE;
1435 for (p = sourceform; p; p = p->next)
1436 if (p->type == para_VersionID) {
1437 if (!started) {
1438 html_raw(&ho, "<!-- version IDs:\n");
1439 started = TRUE;
1440 }
1441 html_words(&ho, p->words, NOTHING,
1442 f, keywords, &conf);
1443 html_nl(&ho);
1444 }
1445 if (started)
1446 html_raw(&ho, "-->\n");
1447 }
1448 }
1449
1450 element_close(&ho, "body");
1451 html_nl(&ho);
1452 element_close(&ho, "html");
1453 html_nl(&ho);
1454 cleanup(&ho);
1455 }
1456 }
1457
1458 /*
1b7bf715 1459 * Go through and check that no index fragments were referenced
1460 * without being generated, or indeed vice versa.
1461 *
1462 * (When I actually get round to freeing everything, this can
1463 * probably be the freeing loop as well.)
1464 */
1465 for (p = sourceform; p; p = p->next) {
1466 word *w;
1467 for (w = p->words; w; w = w->next)
1468 if (w->type == word_IndexRef) {
1469 htmlindexref *hr = (htmlindexref *)w->private_data;
1470
1471 assert(!hr->referenced == !hr->generated);
1472 }
1473 }
1474
1475 /*
529a6c83 1476 * Free all the working data.
78c73085 1477 */
529a6c83 1478 {
1479 htmlfragment *frag;
1480 while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) {
1481 /*
1482 * frag->fragment is dynamically allocated, but will be
1483 * freed when we process the htmlsect structure which
1484 * it is attached to.
1485 */
1486 sfree(frag);
1487 }
1488 freetree234(files.frags);
1489 }
1490 {
1491 htmlsect *sect, *tmp;
1492 sect = sects.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 sect = nonsects.head;
1503 while (sect) {
12f0ee84 1504 int i;
529a6c83 1505 tmp = sect->next;
12f0ee84 1506 for (i=0; i < conf.ntfragments; i++)
1507 sfree(sect->fragments[i]);
1508 sfree(sect->fragments);
529a6c83 1509 sfree(sect);
1510 sect = tmp;
1511 }
1512 }
1513 {
1514 htmlfile *file, *tmp;
1515 file = files.head;
1516 while (file) {
1517 tmp = file->next;
1518 sfree(file->filename);
1519 sfree(file);
1520 file = tmp;
1521 }
1522 }
1523 {
1524 int i;
1525 indexentry *entry;
1526 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
1527 htmlindex *hi = (htmlindex *)entry->backend_data;
1528 sfree(hi);
1529 }
1530 }
1531 {
1532 paragraph *p;
1533 word *w;
1534 for (p = sourceform; p; p = p->next)
1535 for (w = p->words; w; w = w->next)
1536 if (w->type == word_IndexRef) {
1537 htmlindexref *hr = (htmlindexref *)w->private_data;
1538 assert(hr != NULL);
1539 sfree(hr->fragment);
1540 sfree(hr);
1541 }
1542 }
12f0ee84 1543 sfree(conf.asect);
1544 sfree(conf.single_filename);
1545 sfree(conf.contents_filename);
1546 sfree(conf.index_filename);
1547 sfree(conf.template_filename);
1548 while (conf.ntfragments--)
1549 sfree(conf.template_fragments[conf.ntfragments]);
1550 sfree(conf.template_fragments);
78c73085 1551}
1552
1553static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
1554 htmlsect *sect, int depth)
1555{
1556 htmlfile *file;
1557 int ldepth;
1558
1559 /*
1560 * `depth' is derived from the heading_depth() macro at the top
1561 * of this file, which counts title as -1, chapter as 0,
1562 * heading as 1 and subsection as 2. However, the semantics of
1563 * cfg->leaf_level are defined to count chapter as 1, heading
1564 * as 2 etc. So first I increment depth :-(
1565 */
1566 ldepth = depth + 1;
1567
1568 if (cfg->leaf_level == 0) {
1569 /*
1570 * leaf_level==0 is a special case, in which everything is
1571 * put into a single file.
1572 */
1573 if (!files->single)
1574 files->single = html_new_file(files, cfg->single_filename);
1575
1576 file = files->single;
1577 } else {
1578 /*
1579 * If the depth of this section is at or above leaf_level,
1580 * we invent a fresh file and put this section at its head.
1581 * Otherwise, we put it in the same file as its parent
1582 * section.
1583 */
1584 if (ldepth > cfg->leaf_level) {
1585 /*
1586 * We know that sect->parent cannot be NULL. The only
1587 * circumstance in which it can be is if sect is at
1588 * chapter or appendix level, i.e. ldepth==1; and if
1589 * that's the case, then we cannot have entered this
1590 * branch unless cfg->leaf_level==0, in which case we
1591 * would be in the single-file case above and not here
1592 * at all.
1593 */
1594 assert(sect->parent);
1595
1596 file = sect->parent->file;
1597 } else {
1598 if (sect->type == TOP) {
1599 file = html_new_file(files, cfg->contents_filename);
1600 } else if (sect->type == INDEX) {
1601 file = html_new_file(files, cfg->index_filename);
1602 } else {
1603 char *title;
1604
1605 assert(ldepth > 0 && sect->title);
1606 title = html_format(sect->title, cfg->template_filename);
1607 file = html_new_file(files, title);
1608 sfree(title);
1609 }
1610 }
1611 }
1612
1613 sect->file = file;
1614
1615 if (file->min_heading_depth > depth) {
1616 /*
1617 * This heading is at a higher level than any heading we
1618 * have so far placed in this file; so we set the `first'
1619 * pointer.
1620 */
1621 file->min_heading_depth = depth;
1622 file->first = sect;
1623 }
1624
1625 if (file->min_heading_depth == depth)
1626 file->last = sect;
1627}
1628
1629static htmlfile *html_new_file(htmlfilelist *list, char *filename)
1630{
f1530049 1631 htmlfile *ret = snew(htmlfile);
78c73085 1632
1633 ret->next = NULL;
1634 if (list->tail)
1635 list->tail->next = ret;
1636 else
1637 list->head = ret;
1638 list->tail = ret;
1639
1640 ret->filename = dupstr(filename);
1641 ret->last_fragment_number = 0;
1642 ret->min_heading_depth = INT_MAX;
1643 ret->first = ret->last = NULL;
1644
1645 return ret;
1646}
1647
12f0ee84 1648static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
1649 htmlconfig *cfg)
78c73085 1650{
f1530049 1651 htmlsect *ret = snew(htmlsect);
78c73085 1652
1653 ret->next = NULL;
1654 if (list->tail)
1655 list->tail->next = ret;
1656 else
1657 list->head = ret;
1658 list->tail = ret;
1659
1660 ret->title = title;
1661 ret->file = NULL;
1662 ret->parent = NULL;
1663 ret->type = NORMAL;
1664
12f0ee84 1665 ret->fragments = snewn(cfg->ntfragments, char *);
1666 {
1667 int i;
1668 for (i=0; i < cfg->ntfragments; i++)
1669 ret->fragments[i] = NULL;
1670 }
1671
78c73085 1672 return ret;
1673}
1674
1675static void html_words(htmloutput *ho, word *words, int flags,
1676 htmlfile *file, keywordlist *keywords, htmlconfig *cfg)
1677{
1678 word *w;
1679 char *c;
1680 int style, type;
1681
1682 for (w = words; w; w = w->next) switch (w->type) {
1683 case word_HyperLink:
1684 if (flags & LINKS) {
1685 element_open(ho, "a");
1686 c = utoa_dup(w->text, CS_ASCII);
1687 element_attr(ho, "href", c);
1688 sfree(c);
1689 }
1690 break;
1691 case word_UpperXref:
1692 case word_LowerXref:
1693 if (flags & LINKS) {
1694 keyword *kwl = kw_lookup(keywords, w->text);
2b0986a3 1695 paragraph *p;
1696 htmlsect *s;
1697
1698 assert(kwl);
1699 p = kwl->para;
1700 s = (htmlsect *)p->private_data;
78c73085 1701
1702 assert(s);
1703
12f0ee84 1704 html_href(ho, file, s->file, s->fragments[0]);
78c73085 1705 }
1706 break;
1707 case word_HyperEnd:
1708 case word_XrefEnd:
1709 if (flags & LINKS)
1710 element_close(ho, "a");
1711 break;
1712 case word_IndexRef:
1713 if (flags & INDEXENTS) {
1714 htmlindexref *hr = (htmlindexref *)w->private_data;
27bdc5ab 1715 html_fragment(ho, hr->fragment);
1b7bf715 1716 hr->generated = TRUE;
78c73085 1717 }
1718 break;
1719 case word_Normal:
1720 case word_Emph:
1721 case word_Code:
1722 case word_WeakCode:
1723 case word_WhiteSpace:
1724 case word_EmphSpace:
1725 case word_CodeSpace:
1726 case word_WkCodeSpace:
1727 case word_Quote:
1728 case word_EmphQuote:
1729 case word_CodeQuote:
1730 case word_WkCodeQuote:
1731 style = towordstyle(w->type);
1732 type = removeattr(w->type);
1733 if (style == word_Emph &&
1734 (attraux(w->aux) == attr_First ||
1735 attraux(w->aux) == attr_Only) &&
1736 (flags & MARKUP))
1737 element_open(ho, "em");
1738 else if ((style == word_Code || style == word_WeakCode) &&
1739 (attraux(w->aux) == attr_First ||
1740 attraux(w->aux) == attr_Only) &&
1741 (flags & MARKUP))
1742 element_open(ho, "code");
1743
1744 if (type == word_WhiteSpace)
1745 html_text(ho, L" ");
1746 else if (type == word_Quote) {
1747 if (quoteaux(w->aux) == quote_Open)
1748 html_text(ho, cfg->lquote);
1749 else
1750 html_text(ho, cfg->rquote);
1751 } else {
35b123a0 1752 if (!w->alt || cvt_ok(ho->restrict_charset, w->text))
1753 html_text_nbsp(ho, w->text);
78c73085 1754 else
1755 html_words(ho, w->alt, flags, file, keywords, cfg);
1756 }
1757
1758 if (style == word_Emph &&
1759 (attraux(w->aux) == attr_Last ||
1760 attraux(w->aux) == attr_Only) &&
1761 (flags & MARKUP))
1762 element_close(ho, "em");
1763 else if ((style == word_Code || style == word_WeakCode) &&
1764 (attraux(w->aux) == attr_Last ||
1765 attraux(w->aux) == attr_Only) &&
1766 (flags & MARKUP))
1767 element_close(ho, "code");
1768
1769 break;
1770 }
1771}
1772
1773static void html_codepara(htmloutput *ho, word *words)
1774{
1775 element_open(ho, "pre");
1776 element_open(ho, "code");
1777 for (; words; words = words->next) if (words->type == word_WeakCode) {
1778 char *open_tag;
1779 wchar_t *t, *e;
1780
1781 t = words->text;
1782 if (words->next && words->next->type == word_Emph) {
1783 e = words->next->text;
1784 words = words->next;
1785 } else
1786 e = NULL;
1787
1788 while (e && *e && *t) {
1789 int n;
1790 int ec = *e;
1791
1792 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
1793
1794 open_tag = NULL;
1795 if (ec == 'i')
1796 open_tag = "em";
1797 else if (ec == 'b')
1798 open_tag = "b";
1799 if (open_tag)
1800 element_open(ho, open_tag);
1801
1802 html_text_limit(ho, t, n);
1803
1804 if (open_tag)
1805 element_close(ho, open_tag);
1806
1807 t += n;
1808 e += n;
1809 }
1810 html_text(ho, t);
1811 html_nl(ho);
1812 }
1813 element_close(ho, "code");
1814 element_close(ho, "pre");
1815}
1816
1817static void html_charset_cleanup(htmloutput *ho)
1818{
1819 char outbuf[256];
1820 int bytes;
1821
1822 bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf),
1823 ho->charset, &ho->cstate, NULL);
3ae196b4 1824 if (ho->fp && bytes > 0)
78c73085 1825 fwrite(outbuf, 1, bytes, ho->fp);
1826}
1827
35b123a0 1828static void return_mostly_to_neutral(htmloutput *ho)
78c73085 1829{
3ae196b4 1830 if (ho->fp) {
1831 if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) {
1832 fprintf(ho->fp, " />");
1833 } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) {
1834 fprintf(ho->fp, ">");
1835 }
78c73085 1836 }
1837
1838 ho->state = HO_NEUTRAL;
1839}
1840
35b123a0 1841static void return_to_neutral(htmloutput *ho)
1842{
1843 if (ho->state == HO_IN_TEXT) {
1844 html_charset_cleanup(ho);
1845 }
1846
1847 return_mostly_to_neutral(ho);
1848}
1849
78c73085 1850static void element_open(htmloutput *ho, char const *name)
1851{
1852 return_to_neutral(ho);
3ae196b4 1853 if (ho->fp)
1854 fprintf(ho->fp, "<%s", name);
78c73085 1855 ho->state = HO_IN_TAG;
1856}
1857
1858static void element_close(htmloutput *ho, char const *name)
1859{
1860 return_to_neutral(ho);
3ae196b4 1861 if (ho->fp)
1862 fprintf(ho->fp, "</%s>", name);
78c73085 1863 ho->state = HO_NEUTRAL;
1864}
1865
1866static void element_empty(htmloutput *ho, char const *name)
1867{
1868 return_to_neutral(ho);
3ae196b4 1869 if (ho->fp)
1870 fprintf(ho->fp, "<%s", name);
78c73085 1871 ho->state = HO_IN_EMPTY_TAG;
1872}
1873
1874static void html_nl(htmloutput *ho)
1875{
1876 return_to_neutral(ho);
3ae196b4 1877 if (ho->fp)
1878 fputc('\n', ho->fp);
78c73085 1879}
1880
1881static void html_raw(htmloutput *ho, char *text)
1882{
1883 return_to_neutral(ho);
3ae196b4 1884 if (ho->fp)
1885 fputs(text, ho->fp);
78c73085 1886}
1887
1888static void html_raw_as_attr(htmloutput *ho, char *text)
1889{
1890 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
3ae196b4 1891 if (ho->fp) {
1892 fputc(' ', ho->fp);
1893 fputs(text, ho->fp);
1894 }
78c73085 1895}
1896
1897static void element_attr(htmloutput *ho, char const *name, char const *value)
1898{
1899 html_charset_cleanup(ho);
1900 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
3ae196b4 1901 if (ho->fp)
1902 fprintf(ho->fp, " %s=\"%s\"", name, value);
78c73085 1903}
1904
1905static void element_attr_w(htmloutput *ho, char const *name,
1906 wchar_t const *value)
1907{
1908 html_charset_cleanup(ho);
3ae196b4 1909 if (ho->fp)
1910 fprintf(ho->fp, " %s=\"", name);
35b123a0 1911 html_text_limit_internal(ho, value, 0, TRUE, FALSE);
78c73085 1912 html_charset_cleanup(ho);
3ae196b4 1913 if (ho->fp)
1914 fputc('"', ho->fp);
78c73085 1915}
1916
1917static void html_text(htmloutput *ho, wchar_t const *text)
1918{
35b123a0 1919 return_mostly_to_neutral(ho);
1920 html_text_limit_internal(ho, text, 0, FALSE, FALSE);
1921}
1922
1923static void html_text_nbsp(htmloutput *ho, wchar_t const *text)
1924{
1925 return_mostly_to_neutral(ho);
1926 html_text_limit_internal(ho, text, 0, FALSE, TRUE);
78c73085 1927}
1928
1929static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen)
1930{
35b123a0 1931 return_mostly_to_neutral(ho);
1932 html_text_limit_internal(ho, text, maxlen, FALSE, FALSE);
78c73085 1933}
1934
1935static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
35b123a0 1936 int maxlen, int quote_quotes, int nbsp)
78c73085 1937{
1938 int textlen = ustrlen(text);
1939 char outbuf[256];
1940 int bytes, err;
1941
1942 if (maxlen > 0 && textlen > maxlen)
1943 textlen = maxlen;
1944
1945 while (textlen > 0) {
1946 /* Scan ahead for characters we really can't display in HTML. */
1947 int lenbefore, lenafter;
1948 for (lenbefore = 0; lenbefore < textlen; lenbefore++)
1949 if (text[lenbefore] == L'<' ||
1950 text[lenbefore] == L'>' ||
1951 text[lenbefore] == L'&' ||
35b123a0 1952 (text[lenbefore] == L'"' && quote_quotes) ||
1953 (text[lenbefore] == L' ' && nbsp))
78c73085 1954 break;
1955 lenafter = lenbefore;
1956 bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf),
1957 ho->charset, &ho->cstate, &err);
1958 textlen -= (lenbefore - lenafter);
3ae196b4 1959 if (bytes > 0 && ho->fp)
78c73085 1960 fwrite(outbuf, 1, bytes, ho->fp);
1961 if (err) {
1962 /*
1963 * We have encountered a character that cannot be
1964 * displayed in the selected output charset. Therefore,
1965 * we use an HTML numeric entity reference.
1966 */
1967 assert(textlen > 0);
3ae196b4 1968 if (ho->fp)
1969 fprintf(ho->fp, "&#%ld;", (long int)*text);
78c73085 1970 text++, textlen--;
1971 } else if (lenafter == 0 && textlen > 0) {
1972 /*
1973 * We have encountered a character which is special to
1974 * HTML.
1975 */
3ae196b4 1976 if (ho->fp) {
1977 if (*text == L'<')
1978 fprintf(ho->fp, "&lt;");
1979 else if (*text == L'>')
1980 fprintf(ho->fp, "&gt;");
1981 else if (*text == L'&')
1982 fprintf(ho->fp, "&amp;");
1983 else if (*text == L'"')
1984 fprintf(ho->fp, "&quot;");
1985 else if (*text == L' ') {
1986 assert(nbsp);
1987 fprintf(ho->fp, "&nbsp;");
1988 } else
1989 assert(!"Can't happen");
1990 }
78c73085 1991 text++, textlen--;
1992 }
1993 }
1994}
1995
1996static void cleanup(htmloutput *ho)
1997{
1998 return_to_neutral(ho);
3ae196b4 1999 if (ho->fp)
2000 fclose(ho->fp);
78c73085 2001}
2002
2003static void html_href(htmloutput *ho, htmlfile *thisfile,
2004 htmlfile *targetfile, char *targetfrag)
2005{
2006 rdstringc rs = { 0, 0, NULL };
2007 char *url;
2008
2009 if (targetfile != thisfile)
2010 rdaddsc(&rs, targetfile->filename);
2011 if (targetfrag) {
2012 rdaddc(&rs, '#');
2013 rdaddsc(&rs, targetfrag);
2014 }
2015 url = rs.text;
2016
2017 element_open(ho, "a");
2018 element_attr(ho, "href", url);
2019 sfree(url);
2020}
2021
27bdc5ab 2022static void html_fragment(htmloutput *ho, char const *fragment)
2023{
2024 element_open(ho, "a");
2025 element_attr(ho, "name", fragment);
2026 if (is_xhtml(ho->ver))
2027 element_attr(ho, "id", fragment);
2028 element_close(ho, "a");
2029}
2030
78c73085 2031static char *html_format(paragraph *p, char *template_string)
2032{
2033 char *c, *t;
2034 word *w;
2035 wchar_t *ws, wsbuf[2];
2036 rdstringc rs = { 0, 0, NULL };
2037
2038 t = template_string;
2039 while (*t) {
2040 if (*t == '%' && t[1]) {
2041 int fmt;
2042
2043 t++;
2044 fmt = *t++;
2045
2046 if (fmt == '%') {
2047 rdaddc(&rs, fmt);
2048 continue;
2049 }
2050
2051 w = NULL;
2052 ws = NULL;
2053
2054 if (p->kwtext && fmt == 'n')
2055 w = p->kwtext;
2056 else if (p->kwtext2 && fmt == 'b') {
2057 /*
2058 * HTML fragment names must start with a letter, so
2059 * simply `1.2.3' is not adequate. In this case I'm
2060 * going to cheat slightly by prepending the first
2061 * character of the first word of kwtext, so that
2062 * we get `C1' for chapter 1, `S2.3' for section
2063 * 2.3 etc.
2064 */
2065 if (p->kwtext && p->kwtext->text[0]) {
2066 ws = wsbuf;
2067 wsbuf[1] = '\0';
2068 wsbuf[0] = p->kwtext->text[0];
2069 }
2070 w = p->kwtext2;
2071 } else if (p->keyword && *p->keyword && fmt == 'k')
2072 ws = p->keyword;
2073 else
46c7302f 2074 /* %N comes here; also failure cases of other fmts */
78c73085 2075 w = p->words;
2076
2077 if (ws) {
2078 c = utoa_dup(ws, CS_ASCII);
2079 rdaddsc(&rs,c);
2080 sfree(c);
2081 }
2082
2083 while (w) {
2084 if (removeattr(w->type) == word_Normal) {
2085 c = utoa_dup(w->text, CS_ASCII);
2086 rdaddsc(&rs,c);
2087 sfree(c);
2088 }
2089 w = w->next;
2090 }
2091 } else {
2092 rdaddc(&rs, *t++);
2093 }
2094 }
2095
2096 return rdtrimc(&rs);
2097}
2098
3e82de8f 2099static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
2100 char *text)
78c73085 2101{
2102 /*
2103 * The HTML 4 spec's strictest definition of fragment names (<a
2104 * name> and "id" attributes) says that they `must begin with a
2105 * letter and may be followed by any number of letters, digits,
2106 * hyphens, underscores, colons, and periods'.
2107 *
2108 * So here we unceremoniously rip out any characters not
2109 * conforming to this limitation.
2110 */
2111 char *p = text, *q = text;
2112
2113 while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z')))
2114 p++;
3e82de8f 2115 if ((*q++ = *p++) != '\0') {
2116 while (*p) {
2117 if ((*p>='A' && *p<='Z') ||
2118 (*p>='a' && *p<='z') ||
2119 (*p>='0' && *p<='9') ||
2120 *p=='-' || *p=='_' || *p==':' || *p=='.')
2121 *q++ = *p;
2122 p++;
2123 }
2124
2125 *q = '\0';
2126 }
2127
12f0ee84 2128 /* If there's nothing left, make something valid up */
2129 if (!*text) {
0bac815b 2130 static const char anonfrag[] = "anon";
12f0ee84 2131 text = sresize(text, lenof(anonfrag), char);
2132 strcpy(text, anonfrag);
2133 }
2134
3e82de8f 2135 /*
2136 * Now we check for clashes with other fragment names, and
2137 * adjust this one if necessary by appending a hyphen followed
2138 * by a number.
2139 */
2140 {
2141 htmlfragment *frag = snew(htmlfragment);
2142 int len = 0; /* >0 indicates we have resized */
2143 int suffix = 1;
2144
2145 frag->file = file;
2146 frag->fragment = text;
2147
2148 while (add234(files->frags, frag) != frag) {
2149 if (!len) {
2150 len = strlen(text);
2151 frag->fragment = text = sresize(text, len+20, char);
2152 }
2153
2154 sprintf(text + len, "-%d", ++suffix);
2155 }
78c73085 2156 }
2157
3e82de8f 2158 return text;
78c73085 2159}
2160
2161static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
2162 htmlfile *thisfile, keywordlist *keywords,
2163 htmlconfig *cfg)
2164{
f5dee642 2165 if (ho->contents_level >= depth && ho->contents_level > 0) {
2166 element_close(ho, "li");
2167 html_nl(ho);
2168 }
2169
78c73085 2170 while (ho->contents_level > depth) {
2171 element_close(ho, "ul");
2172 ho->contents_level--;
f5dee642 2173 if (ho->contents_level > 0) {
2174 element_close(ho, "li");
2175 }
2176 html_nl(ho);
78c73085 2177 }
2178
2179 while (ho->contents_level < depth) {
f5dee642 2180 html_nl(ho);
78c73085 2181 element_open(ho, "ul");
f5dee642 2182 html_nl(ho);
78c73085 2183 ho->contents_level++;
2184 }
2185
2186 if (!s)
2187 return;
2188
2189 element_open(ho, "li");
12f0ee84 2190 html_href(ho, thisfile, s->file, s->fragments[0]);
23c9bbc2 2191 html_section_title(ho, s, thisfile, keywords, cfg, FALSE);
78c73085 2192 element_close(ho, "a");
f5dee642 2193 /* <li> will be closed by a later invocation */
78c73085 2194}
2195
2196static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile,
23c9bbc2 2197 keywordlist *keywords, htmlconfig *cfg,
2198 int real)
78c73085 2199{
2200 if (s->title) {
2201 sectlevel *sl;
2202 word *number;
2203 int depth = heading_depth(s->title);
2204
2205 if (depth < 0)
2206 sl = NULL;
2207 else if (depth == 0)
2208 sl = &cfg->achapter;
2209 else if (depth <= cfg->nasect)
2210 sl = &cfg->asect[depth-1];
2211 else
2212 sl = &cfg->asect[cfg->nasect-1];
2213
2214 if (!sl)
2215 number = NULL;
2216 else if (sl->just_numbers)
2217 number = s->title->kwtext2;
2218 else
2219 number = s->title->kwtext;
2220
2221 if (number) {
2222 html_words(ho, number, MARKUP,
2223 thisfile, keywords, cfg);
2224 html_text(ho, sl->number_suffix);
2225 }
2226
23c9bbc2 2227 html_words(ho, s->title->words, real ? ALL : MARKUP,
78c73085 2228 thisfile, keywords, cfg);
2229 } else {
2230 assert(s->type != NORMAL);
56a99eb6 2231 /*
2232 * If we're printing the full document title for _real_ and
2233 * there isn't one, we don't want to print `Preamble' at
2234 * the top of what ought to just be some text. If we need
2235 * it in any other context such as TOCs, we need to print
2236 * `Preamble'.
2237 */
2238 if (s->type == TOP && !real)
2239 html_text(ho, cfg->preamble_text);
78c73085 2240 else if (s->type == INDEX)
56a99eb6 2241 html_text(ho, cfg->index_text);
78c73085 2242 }
2243}