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