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