Free up all the data we allocated during the HTML backend.
[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")) {
78c73085 270 char *csname = utoa_dup(uadv(k), CS_ASCII);
b7309494 271 ret.restrict_charset = charset_from_localenc(csname);
272 sfree(csname);
273 } else if (!ustricmp(k, L"html-output-charset")) {
274 char *csname = utoa_dup(uadv(k), CS_ASCII);
275 ret.output_charset = charset_from_localenc(csname);
78c73085 276 sfree(csname);
27bdc5ab 277 } else if (!ustricmp(k, L"html-version")) {
278 wchar_t *vername = uadv(k);
279 static const struct {
280 const wchar_t *name;
281 int ver;
282 } versions[] = {
283 {L"html3.2", HTML_3_2},
284 {L"html4", HTML_4},
285 {L"iso-html", ISO_HTML},
286 {L"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL},
287 {L"xhtml1.0strict", XHTML_1_0_STRICT}
288 };
289 int i;
290
291 for (i = 0; i < (int)lenof(versions); i++)
292 if (!ustricmp(versions[i].name, vername))
293 break;
294
295 if (i == lenof(versions))
296 error(err_htmlver, &p->fpos, vername);
297 else
298 ret.htmlver = versions[i].ver;
78c73085 299 } else if (!ustricmp(k, L"html-single-filename")) {
300 sfree(ret.single_filename);
301 ret.single_filename = dupstr(adv(p->origkeyword));
302 } else if (!ustricmp(k, L"html-contents-filename")) {
303 sfree(ret.contents_filename);
304 ret.contents_filename = dupstr(adv(p->origkeyword));
305 } else if (!ustricmp(k, L"html-index-filename")) {
306 sfree(ret.index_filename);
307 ret.index_filename = dupstr(adv(p->origkeyword));
308 } else if (!ustricmp(k, L"html-template-filename")) {
309 sfree(ret.template_filename);
310 ret.template_filename = dupstr(adv(p->origkeyword));
311 } else if (!ustricmp(k, L"html-template-fragment")) {
312 sfree(ret.template_fragment);
313 ret.template_fragment = dupstr(adv(p->origkeyword));
314 } else if (!ustricmp(k, L"html-chapter-numeric")) {
315 ret.achapter.just_numbers = utob(uadv(k));
316 } else if (!ustricmp(k, L"html-chapter-suffix")) {
317 ret.achapter.number_suffix = uadv(k);
318 } else if (!ustricmp(k, L"html-leaf-level")) {
319 ret.leaf_level = utoi(uadv(k));
320 } else if (!ustricmp(k, L"html-section-numeric")) {
321 wchar_t *q = uadv(k);
322 int n = 0;
323 if (uisdigit(*q)) {
324 n = utoi(q);
325 q = uadv(q);
326 }
327 if (n >= ret.nasect) {
328 int i;
f1530049 329 ret.asect = sresize(ret.asect, n+1, sectlevel);
78c73085 330 for (i = ret.nasect; i <= n; i++)
331 ret.asect[i] = ret.asect[ret.nasect-1];
332 ret.nasect = n+1;
333 }
334 ret.asect[n].just_numbers = utob(q);
335 } else if (!ustricmp(k, L"html-section-suffix")) {
336 wchar_t *q = uadv(k);
337 int n = 0;
338 if (uisdigit(*q)) {
339 n = utoi(q);
340 q = uadv(q);
341 }
342 if (n >= ret.nasect) {
343 int i;
f1530049 344 ret.asect = sresize(ret.asect, n+1, sectlevel);
78c73085 345 for (i = ret.nasect; i <= n; i++) {
346 ret.asect[i] = ret.asect[ret.nasect-1];
347 }
348 ret.nasect = n+1;
349 }
350 ret.asect[n].number_suffix = q;
351 } else if (!ustricmp(k, L"html-contents-depth") ||
352 !ustrnicmp(k, L"html-contents-depth-", 20)) {
353 /*
354 * Relic of old implementation: this directive used
355 * to be written as \cfg{html-contents-depth-3}{2}
356 * rather than the usual Halibut convention of
357 * \cfg{html-contents-depth}{3}{2}. We therefore
358 * support both.
359 */
360 wchar_t *q = k[19] ? k+20 : uadv(k);
361 int n = 0;
362 if (uisdigit(*q)) {
363 n = utoi(q);
364 q = uadv(q);
365 }
366 if (n >= ret.ncdepths) {
367 int i;
f1530049 368 ret.contents_depths =
369 sresize(ret.contents_depths, n+1, int);
78c73085 370 for (i = ret.ncdepths; i <= n; i++) {
371 ret.contents_depths[i] = i+2;
372 }
373 ret.ncdepths = n+1;
374 }
375 ret.contents_depths[n] = utoi(q);
376 } else if (!ustricmp(k, L"html-head-end")) {
377 ret.head_end = adv(p->origkeyword);
378 } else if (!ustricmp(k, L"html-body-tag")) {
379 ret.body_tag = adv(p->origkeyword);
380 } else if (!ustricmp(k, L"html-body-start")) {
381 ret.body_start = adv(p->origkeyword);
382 } else if (!ustricmp(k, L"html-body-end")) {
383 ret.body_end = adv(p->origkeyword);
384 } else if (!ustricmp(k, L"html-address-start")) {
385 ret.addr_start = adv(p->origkeyword);
386 } else if (!ustricmp(k, L"html-address-end")) {
387 ret.addr_end = adv(p->origkeyword);
388 } else if (!ustricmp(k, L"html-navigation-attributes")) {
389 ret.nav_attr = adv(p->origkeyword);
390 } else if (!ustricmp(k, L"html-author")) {
391 ret.author = uadv(k);
392 } else if (!ustricmp(k, L"html-description")) {
393 ret.description = uadv(k);
394 } else if (!ustricmp(k, L"html-suppress-address")) {
395 ret.address_section = !utob(uadv(k));
396 } else if (!ustricmp(k, L"html-versionid")) {
397 ret.visible_version_id = utob(uadv(k));
398 } else if (!ustricmp(k, L"html-quotes")) {
399 if (*uadv(k) && *uadv(uadv(k))) {
400 ret.lquote = uadv(k);
401 ret.rquote = uadv(ret.lquote);
402 }
403 } else if (!ustricmp(k, L"html-leaf-contains-contents")) {
404 ret.leaf_contains_contents = utob(uadv(k));
405 } else if (!ustricmp(k, L"html-leaf-smallest-contents")) {
406 ret.leaf_smallest_contents = utoi(uadv(k));
75a96e91 407 } else if (!ustricmp(k, L"html-index-text")) {
408 ret.index_text = uadv(k);
409 } else if (!ustricmp(k, L"html-contents-text")) {
410 ret.contents_text = uadv(k);
411 } else if (!ustricmp(k, L"html-preamble-text")) {
412 ret.preamble_text = uadv(k);
413 } else if (!ustricmp(k, L"html-title-separator")) {
414 ret.title_separator = uadv(k);
415 } else if (!ustricmp(k, L"html-nav-prev-text")) {
416 ret.nav_prev_text = uadv(k);
417 } else if (!ustricmp(k, L"html-nav-next-text")) {
418 ret.nav_next_text = uadv(k);
419 } else if (!ustricmp(k, L"html-nav-separator")) {
420 ret.nav_separator = uadv(k);
421 } else if (!ustricmp(k, L"html-index-main-separator")) {
422 ret.index_main_sep = uadv(k);
423 } else if (!ustricmp(k, L"html-index-multiple-separator")) {
424 ret.index_multi_sep = uadv(k);
425 } else if (!ustricmp(k, L"html-pre-versionid")) {
426 ret.pre_versionid = uadv(k);
427 } else if (!ustricmp(k, L"html-post-versionid")) {
428 ret.post_versionid = uadv(k);
78c73085 429 }
430 }
431 }
432
433 /*
434 * Now process fallbacks on quote characters.
435 */
436 while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) &&
437 (!cvt_ok(ret.restrict_charset, ret.lquote) ||
438 !cvt_ok(ret.restrict_charset, ret.rquote))) {
439 ret.lquote = uadv(ret.rquote);
440 ret.rquote = uadv(ret.lquote);
441 }
442
443 return ret;
444}
445
446paragraph *html_config_filename(char *filename)
447{
448 /*
449 * If the user passes in a single filename as a parameter to
450 * the `--html' command-line option, then we should assume it
451 * to imply _two_ config directives:
452 * \cfg{html-single-filename}{whatever} and
453 * \cfg{html-leaf-level}{0}; the rationale being that the user
454 * wants their output _in that file_.
455 */
456 paragraph *p, *q;
457
458 p = cmdline_cfg_simple("html-single-filename", filename, NULL);
459 q = cmdline_cfg_simple("html-leaf-level", "0", NULL);
460 p->next = q;
461 return p;
462}
463
464void html_backend(paragraph *sourceform, keywordlist *keywords,
529a6c83 465 indexdata *idx, void *unused)
466{
78c73085 467 paragraph *p;
468 htmlconfig conf;
3e82de8f 469 htmlfilelist files = { NULL, NULL, NULL, NULL, NULL };
78c73085 470 htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL };
471
472 IGNORE(unused);
473
474 conf = html_configure(sourceform);
475
476 /*
477 * We're going to make heavy use of paragraphs' private data
478 * fields in the forthcoming code. Clear them first, so we can
479 * reliably tell whether we have auxiliary data for a
480 * particular paragraph.
481 */
482 for (p = sourceform; p; p = p->next)
483 p->private_data = NULL;
484
3e82de8f 485 files.frags = newtree234(html_fragment_compare);
486
78c73085 487 /*
488 * Start by figuring out into which file each piece of the
489 * document should be put. We'll do this by inventing an
490 * `htmlsect' structure and stashing it in the private_data
491 * field of each section paragraph; we also need one additional
492 * htmlsect for the document index, which won't show up in the
493 * source form but needs to be consistently mentioned in
494 * contents links.
495 *
496 * While we're here, we'll also invent the HTML fragment name
497 * for each section.
498 */
499 {
500 htmlsect *topsect, *sect;
501 int d;
502
56a99eb6 503 topsect = html_new_sect(&sects, NULL);
78c73085 504 topsect->type = TOP;
505 topsect->title = NULL;
506 topsect->text = sourceform;
507 topsect->contents_depth = contents_depth(conf, 0);
508 html_file_section(&conf, &files, topsect, -1);
509 topsect->fragment = NULL;
510
511 for (p = sourceform; p; p = p->next)
512 if (is_heading_type(p->type)) {
513 d = heading_depth(p);
514
515 if (p->type == para_Title) {
516 topsect->title = p;
517 continue;
518 }
519
520 sect = html_new_sect(&sects, p);
521 sect->text = p->next;
522
523 sect->contents_depth = contents_depth(conf, d+1) - (d+1);
524
525 if (p->parent) {
526 sect->parent = (htmlsect *)p->parent->private_data;
527 assert(sect->parent != NULL);
528 } else
529 sect->parent = topsect;
530 p->private_data = sect;
531
532 html_file_section(&conf, &files, sect, d);
533
534 sect->fragment = html_format(p, conf.template_fragment);
3e82de8f 535 sect->fragment = html_sanitise_fragment(&files, sect->file,
536 sect->fragment);
78c73085 537 }
538
539 /* And the index. */
540 sect = html_new_sect(&sects, NULL);
78c73085 541 sect->text = NULL;
542 sect->type = INDEX;
543 sect->parent = topsect;
544 html_file_section(&conf, &files, sect, 0); /* peer of chapters */
56a99eb6 545 sect->fragment = utoa_dup(conf.index_text, CS_ASCII);
3e82de8f 546 sect->fragment = html_sanitise_fragment(&files, sect->file,
547 sect->fragment);
78c73085 548 files.index = sect->file;
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
869 if (f != files.index) {
870 element_open(&ho, "a");
871 element_attr(&ho, "href", files.index->filename);
872 }
56a99eb6 873 html_text(&ho, conf.index_text);
78c73085 874 if (f != files.index)
875 element_close(&ho, "a");
876
56a99eb6 877 html_text(&ho, conf.nav_separator);
78c73085 878
879 if (f->next) {
880 element_open(&ho, "a");
881 element_attr(&ho, "href", f->next->filename);
882 }
56a99eb6 883 html_text(&ho, conf.nav_next_text);
78c73085 884 if (f->next)
885 element_close(&ho, "a");
886
887 element_close(&ho, "p");
888 html_nl(&ho);
889 }
890 prevf = f;
891
892 /*
893 * Write out a prefix TOC for the file.
894 *
895 * We start by going through the section list and
896 * collecting the sections which need to be added to
897 * the contents. On the way, we also test to see if
898 * this file is a leaf file (defined as one which
899 * contains all descendants of any section it
900 * contains), because this will play a part in our
901 * decision on whether or not to _output_ the TOC.
902 *
903 * Special case: we absolutely do not do this if we're
904 * in single-file mode.
905 */
906 if (files.head != files.tail) {
907 int ntoc = 0, tocsize = 0;
908 htmlsect **toc = NULL;
909 int leaf = TRUE;
910
911 for (s = sects.head; s; s = s->next) {
912 htmlsect *a, *ac;
913 int depth, adepth;
914
915 /*
916 * Search up from this section until we find
917 * the highest-level one which belongs in this
918 * file.
919 */
920 depth = adepth = 0;
921 a = NULL;
922 for (ac = s; ac; ac = ac->parent) {
923 if (ac->file == f) {
924 a = ac;
925 adepth = depth;
926 }
927 depth++;
928 }
929
930 if (s->file != f && a != NULL)
931 leaf = FALSE;
932
933 if (a) {
934 if (adepth <= a->contents_depth) {
935 if (ntoc >= tocsize) {
936 tocsize += 64;
f1530049 937 toc = sresize(toc, tocsize, htmlsect *);
78c73085 938 }
939 toc[ntoc++] = s;
940 }
941 }
942 }
943
944 if (leaf && conf.leaf_contains_contents &&
945 ntoc >= conf.leaf_smallest_contents) {
946 int i;
947
948 for (i = 0; i < ntoc; i++) {
949 htmlsect *s = toc[i];
950 int hlevel = (s->type == TOP ? -1 :
951 s->type == INDEX ? 0 :
952 heading_depth(s->title))
953 - f->min_heading_depth + 1;
954
955 assert(hlevel >= 1);
956 html_contents_entry(&ho, hlevel, s,
957 f, keywords, &conf);
958 }
959 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
960 }
961 }
962
963 /*
964 * Now go through the document and output some real
965 * text.
966 */
967 displaying = FALSE;
968 for (s = sects.head; s; s = s->next) {
969 if (s->file == f) {
970 /*
971 * This section belongs in this file.
972 * Display it.
973 */
974 displaying = TRUE;
975 } else {
976 htmlsect *a, *ac;
977 int depth, adepth;
978
979 displaying = FALSE;
980
981 /*
982 * Search up from this section until we find
983 * the highest-level one which belongs in this
984 * file.
985 */
986 depth = adepth = 0;
987 a = NULL;
988 for (ac = s; ac; ac = ac->parent) {
989 if (ac->file == f) {
990 a = ac;
991 adepth = depth;
992 }
993 depth++;
994 }
995
996 if (a != NULL) {
997 /*
998 * This section does not belong in this
999 * file, but an ancestor of it does. Write
1000 * out a contents table entry, if the depth
1001 * doesn't exceed the maximum contents
1002 * depth for the ancestor section.
1003 */
1004 if (adepth <= a->contents_depth) {
1005 html_contents_entry(&ho, adepth, s,
1006 f, keywords, &conf);
1007 }
1008 }
1009 }
1010
1011 if (displaying) {
1012 int hlevel;
1013 char htag[3];
1014
1015 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1016
1017 /*
1018 * Display the section heading.
1019 */
1020
1021 hlevel = (s->type == TOP ? -1 :
1022 s->type == INDEX ? 0 :
1023 heading_depth(s->title))
1024 - f->min_heading_depth + 1;
1025 assert(hlevel >= 1);
1026 /* HTML headings only go up to <h6> */
1027 if (hlevel > 6)
1028 hlevel = 6;
1029 htag[0] = 'h';
1030 htag[1] = '0' + hlevel;
1031 htag[2] = '\0';
1032 element_open(&ho, htag);
1033
1034 /*
1035 * Provide anchor for cross-links to target.
1036 *
78c73085 1037 * (Also we'll have to do this separately in
1038 * other paragraph types - NumberedList and
1039 * BiblioCited.)
1040 */
27bdc5ab 1041 if (s->fragment)
1042 html_fragment(&ho, s->fragment);
78c73085 1043
23c9bbc2 1044 html_section_title(&ho, s, f, keywords, &conf, TRUE);
78c73085 1045
1046 element_close(&ho, htag);
1047
1048 /*
1049 * Now display the section text.
1050 */
1051 if (s->text) {
f1530049 1052 stackhead = snew(struct stackelement);
78c73085 1053 stackhead->next = NULL;
1054 stackhead->listtype = NOLIST;
1055 stackhead->itemtype = NOITEM;
1056
1057 for (p = s->text;; p = p->next) {
1058 enum LISTTYPE listtype;
1059 struct stackelement *se;
1060
1061 /*
1062 * Preliminary switch to figure out what
1063 * sort of list we expect to be inside at
1064 * this stage.
1065 *
1066 * Since p may still be NULL at this point,
1067 * I invent a harmless paragraph type for
1068 * it if it is.
1069 */
1070 switch (p ? p->type : para_Normal) {
1071 case para_Rule:
1072 case para_Normal:
1073 case para_Copyright:
1074 case para_BiblioCited:
1075 case para_Code:
1076 case para_QuotePush:
1077 case para_QuotePop:
1078 case para_Chapter:
1079 case para_Appendix:
1080 case para_UnnumberedChapter:
1081 case para_Heading:
1082 case para_Subsect:
1083 case para_LcontPop:
1084 listtype = NOLIST;
1085 break;
1086
1087 case para_Bullet:
1088 listtype = UL;
1089 break;
1090
1091 case para_NumberedList:
1092 listtype = OL;
1093 break;
1094
1095 case para_DescribedThing:
1096 case para_Description:
1097 listtype = DL;
1098 break;
1099
1100 case para_LcontPush:
f1530049 1101 se = snew(struct stackelement);
78c73085 1102 se->next = stackhead;
1103 se->listtype = NOLIST;
1104 se->itemtype = NOITEM;
1105 stackhead = se;
1106 continue;
1107
1108 default: /* some totally non-printing para */
1109 continue;
1110 }
1111
1112 html_nl(&ho);
1113
1114 /*
1115 * Terminate the most recent list item, if
1116 * any. (We left this until after
1117 * processing LcontPush, since in that case
1118 * the list item won't want to be
1119 * terminated until after the corresponding
1120 * LcontPop.)
1121 */
1122 if (stackhead->itemtype != NOITEM) {
1123 element_close(&ho, itemname(stackhead->itemtype));
1124 html_nl(&ho);
1125 }
1126 stackhead->itemtype = NOITEM;
1127
1128 /*
1129 * Terminate the current list, if it's not
1130 * the one we want to be in.
1131 */
1132 if (listtype != stackhead->listtype &&
1133 stackhead->listtype != NOLIST) {
1134 element_close(&ho, listname(stackhead->listtype));
1135 html_nl(&ho);
1136 }
1137
1138 /*
1139 * Leave the loop if our time has come.
1140 */
1141 if (!p || (is_heading_type(p->type) &&
1142 p->type != para_Title))
1143 break; /* end of section text */
1144
1145 /*
1146 * Start a fresh list if necessary.
1147 */
1148 if (listtype != stackhead->listtype &&
1149 listtype != NOLIST)
1150 element_open(&ho, listname(listtype));
1151
1152 stackhead->listtype = listtype;
1153
1154 switch (p->type) {
1155 case para_Rule:
1156 element_empty(&ho, "hr");
1157 break;
1158 case para_Code:
1159 html_codepara(&ho, p->words);
1160 break;
1161 case para_Normal:
1162 case para_Copyright:
1163 element_open(&ho, "p");
1164 html_nl(&ho);
1165 html_words(&ho, p->words, ALL,
1166 f, keywords, &conf);
1167 html_nl(&ho);
1168 element_close(&ho, "p");
1169 break;
1170 case para_BiblioCited:
1171 element_open(&ho, "p");
1172 if (p->private_data) {
1173 htmlsect *s = (htmlsect *)p->private_data;
27bdc5ab 1174 html_fragment(&ho, s->fragment);
78c73085 1175 }
1176 html_nl(&ho);
1177 html_words(&ho, p->kwtext, ALL,
1178 f, keywords, &conf);
1179 html_text(&ho, L" ");
1180 html_words(&ho, p->words, ALL,
1181 f, keywords, &conf);
1182 html_nl(&ho);
1183 element_close(&ho, "p");
1184 break;
1185 case para_Bullet:
1186 case para_NumberedList:
1187 element_open(&ho, "li");
1188 if (p->private_data) {
1189 htmlsect *s = (htmlsect *)p->private_data;
27bdc5ab 1190 html_fragment(&ho, s->fragment);
78c73085 1191 }
1192 html_nl(&ho);
1193 stackhead->itemtype = LI;
1194 html_words(&ho, p->words, ALL,
1195 f, keywords, &conf);
1196 break;
1197 case para_DescribedThing:
1198 element_open(&ho, "dt");
1199 html_nl(&ho);
1200 stackhead->itemtype = DT;
1201 html_words(&ho, p->words, ALL,
1202 f, keywords, &conf);
1203 break;
1204 case para_Description:
1205 element_open(&ho, "dd");
1206 html_nl(&ho);
1207 stackhead->itemtype = DD;
1208 html_words(&ho, p->words, ALL,
1209 f, keywords, &conf);
1210 break;
1211
1212 case para_QuotePush:
1213 element_open(&ho, "blockquote");
1214 break;
1215 case para_QuotePop:
1216 element_close(&ho, "blockquote");
1217 break;
1218
1219 case para_LcontPop:
1220 se = stackhead;
1221 stackhead = stackhead->next;
1222 assert(stackhead);
1223 sfree(se);
1224 break;
1225 }
1226 }
1227
1228 assert(stackhead && !stackhead->next);
1229 sfree(stackhead);
1230 }
1231
1232 if (s->type == INDEX) {
1233 indexentry *entry;
1234 int i;
1235
1236 /*
1237 * This section is the index. I'll just
1238 * render it as a single paragraph, with a
1239 * colon between the index term and the
1240 * references, and <br> in between each
1241 * entry.
1242 */
1243 element_open(&ho, "p");
1244
1245 for (i = 0; (entry =
1246 index234(idx->entries, i)) != NULL; i++) {
1247 htmlindex *hi = (htmlindex *)entry->backend_data;
1248 int j;
1249
1250 if (i > 0)
1251 element_empty(&ho, "br");
1252 html_nl(&ho);
1253
1254 html_words(&ho, entry->text, MARKUP|LINKS,
1255 f, keywords, &conf);
1256
56a99eb6 1257 html_text(&ho, conf.index_main_sep);
78c73085 1258
1259 for (j = 0; j < hi->nrefs; j++) {
1260 htmlindexref *hr =
1261 (htmlindexref *)hi->refs[j]->private_data;
1262 paragraph *p = hr->section->title;
1263
1264 if (j > 0)
56a99eb6 1265 html_text(&ho, conf.index_multi_sep);
78c73085 1266
1267 html_href(&ho, f, hr->section->file,
1268 hr->fragment);
1b7bf715 1269 hr->referenced = TRUE;
78c73085 1270 if (p && p->kwtext)
1271 html_words(&ho, p->kwtext, MARKUP|LINKS,
1272 f, keywords, &conf);
1273 else if (p && p->words)
1274 html_words(&ho, p->words, MARKUP|LINKS,
1275 f, keywords, &conf);
56a99eb6 1276 else {
1277 /*
1278 * If there is no title at all,
1279 * this must be because our
1280 * target section is the
1281 * preamble section and there
1282 * is no title. So we use the
1283 * preamble_text.
1284 */
1285 html_text(&ho, conf.preamble_text);
1286 }
78c73085 1287 element_close(&ho, "a");
1288 }
1289 }
1290 element_close(&ho, "p");
1291 }
1292 }
1293 }
1294
1295 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1296 html_nl(&ho);
1297
1298 {
1299 /*
1300 * Footer.
1301 */
1302 int done_version_ids = FALSE;
1303
1304 element_empty(&ho, "hr");
1305
1306 if (conf.body_end)
1307 html_raw(&ho, conf.body_end);
1308
1309 if (conf.address_section) {
27bdc5ab 1310 int started = FALSE;
1311 if (conf.htmlver == ISO_HTML) {
1312 /*
1313 * The ISO-HTML validator complains if
1314 * there isn't a <div> tag surrounding the
1315 * <address> tag. I'm uncertain of why this
1316 * should be - there appears to be no
1317 * mention of this in the ISO-HTML spec,
1318 * suggesting that it doesn't represent a
1319 * change from HTML 4, but nonetheless the
1320 * HTML 4 validator doesn't seem to mind.
1321 */
1322 element_open(&ho, "div");
1323 }
78c73085 1324 element_open(&ho, "address");
1325 if (conf.addr_start) {
1326 html_raw(&ho, conf.addr_start);
1327 html_nl(&ho);
27bdc5ab 1328 started = TRUE;
78c73085 1329 }
1330 if (conf.visible_version_id) {
78c73085 1331 for (p = sourceform; p; p = p->next)
1332 if (p->type == para_VersionID) {
27bdc5ab 1333 if (started)
78c73085 1334 element_empty(&ho, "br");
1335 html_nl(&ho);
56a99eb6 1336 html_text(&ho, conf.pre_versionid);
78c73085 1337 html_words(&ho, p->words, NOTHING,
1338 f, keywords, &conf);
56a99eb6 1339 html_text(&ho, conf.post_versionid);
78c73085 1340 started = TRUE;
1341 }
78c73085 1342 done_version_ids = TRUE;
1343 }
27bdc5ab 1344 if (conf.addr_end) {
1345 if (started)
1346 element_empty(&ho, "br");
78c73085 1347 html_raw(&ho, conf.addr_end);
27bdc5ab 1348 }
78c73085 1349 element_close(&ho, "address");
27bdc5ab 1350 if (conf.htmlver == ISO_HTML)
1351 element_close(&ho, "div");
78c73085 1352 }
1353
1354 if (!done_version_ids) {
1355 /*
1356 * If the user didn't want the version IDs
1357 * visible, I think we still have a duty to put
1358 * them in an HTML comment.
1359 */
1360 int started = FALSE;
1361 for (p = sourceform; p; p = p->next)
1362 if (p->type == para_VersionID) {
1363 if (!started) {
1364 html_raw(&ho, "<!-- version IDs:\n");
1365 started = TRUE;
1366 }
1367 html_words(&ho, p->words, NOTHING,
1368 f, keywords, &conf);
1369 html_nl(&ho);
1370 }
1371 if (started)
1372 html_raw(&ho, "-->\n");
1373 }
1374 }
1375
1376 element_close(&ho, "body");
1377 html_nl(&ho);
1378 element_close(&ho, "html");
1379 html_nl(&ho);
1380 cleanup(&ho);
1381 }
1382 }
1383
1384 /*
1b7bf715 1385 * Go through and check that no index fragments were referenced
1386 * without being generated, or indeed vice versa.
1387 *
1388 * (When I actually get round to freeing everything, this can
1389 * probably be the freeing loop as well.)
1390 */
1391 for (p = sourceform; p; p = p->next) {
1392 word *w;
1393 for (w = p->words; w; w = w->next)
1394 if (w->type == word_IndexRef) {
1395 htmlindexref *hr = (htmlindexref *)w->private_data;
1396
1397 assert(!hr->referenced == !hr->generated);
1398 }
1399 }
1400
1401 /*
529a6c83 1402 * Free all the working data.
78c73085 1403 */
529a6c83 1404 sfree(conf.asect);
1405 sfree(conf.single_filename);
1406 sfree(conf.contents_filename);
1407 sfree(conf.index_filename);
1408 sfree(conf.template_filename);
1409 sfree(conf.template_fragment);
1410 {
1411 htmlfragment *frag;
1412 while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) {
1413 /*
1414 * frag->fragment is dynamically allocated, but will be
1415 * freed when we process the htmlsect structure which
1416 * it is attached to.
1417 */
1418 sfree(frag);
1419 }
1420 freetree234(files.frags);
1421 }
1422 {
1423 htmlsect *sect, *tmp;
1424 sect = sects.head;
1425 while (sect) {
1426 tmp = sect->next;
1427 sfree(sect->fragment);
1428 sfree(sect);
1429 sect = tmp;
1430 }
1431 sect = nonsects.head;
1432 while (sect) {
1433 tmp = sect->next;
1434 sfree(sect->fragment);
1435 sfree(sect);
1436 sect = tmp;
1437 }
1438 }
1439 {
1440 htmlfile *file, *tmp;
1441 file = files.head;
1442 while (file) {
1443 tmp = file->next;
1444 sfree(file->filename);
1445 sfree(file);
1446 file = tmp;
1447 }
1448 }
1449 {
1450 int i;
1451 indexentry *entry;
1452 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
1453 htmlindex *hi = (htmlindex *)entry->backend_data;
1454 sfree(hi);
1455 }
1456 }
1457 {
1458 paragraph *p;
1459 word *w;
1460 for (p = sourceform; p; p = p->next)
1461 for (w = p->words; w; w = w->next)
1462 if (w->type == word_IndexRef) {
1463 htmlindexref *hr = (htmlindexref *)w->private_data;
1464 assert(hr != NULL);
1465 sfree(hr->fragment);
1466 sfree(hr);
1467 }
1468 }
78c73085 1469}
1470
1471static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
1472 htmlsect *sect, int depth)
1473{
1474 htmlfile *file;
1475 int ldepth;
1476
1477 /*
1478 * `depth' is derived from the heading_depth() macro at the top
1479 * of this file, which counts title as -1, chapter as 0,
1480 * heading as 1 and subsection as 2. However, the semantics of
1481 * cfg->leaf_level are defined to count chapter as 1, heading
1482 * as 2 etc. So first I increment depth :-(
1483 */
1484 ldepth = depth + 1;
1485
1486 if (cfg->leaf_level == 0) {
1487 /*
1488 * leaf_level==0 is a special case, in which everything is
1489 * put into a single file.
1490 */
1491 if (!files->single)
1492 files->single = html_new_file(files, cfg->single_filename);
1493
1494 file = files->single;
1495 } else {
1496 /*
1497 * If the depth of this section is at or above leaf_level,
1498 * we invent a fresh file and put this section at its head.
1499 * Otherwise, we put it in the same file as its parent
1500 * section.
1501 */
1502 if (ldepth > cfg->leaf_level) {
1503 /*
1504 * We know that sect->parent cannot be NULL. The only
1505 * circumstance in which it can be is if sect is at
1506 * chapter or appendix level, i.e. ldepth==1; and if
1507 * that's the case, then we cannot have entered this
1508 * branch unless cfg->leaf_level==0, in which case we
1509 * would be in the single-file case above and not here
1510 * at all.
1511 */
1512 assert(sect->parent);
1513
1514 file = sect->parent->file;
1515 } else {
1516 if (sect->type == TOP) {
1517 file = html_new_file(files, cfg->contents_filename);
1518 } else if (sect->type == INDEX) {
1519 file = html_new_file(files, cfg->index_filename);
1520 } else {
1521 char *title;
1522
1523 assert(ldepth > 0 && sect->title);
1524 title = html_format(sect->title, cfg->template_filename);
1525 file = html_new_file(files, title);
1526 sfree(title);
1527 }
1528 }
1529 }
1530
1531 sect->file = file;
1532
1533 if (file->min_heading_depth > depth) {
1534 /*
1535 * This heading is at a higher level than any heading we
1536 * have so far placed in this file; so we set the `first'
1537 * pointer.
1538 */
1539 file->min_heading_depth = depth;
1540 file->first = sect;
1541 }
1542
1543 if (file->min_heading_depth == depth)
1544 file->last = sect;
1545}
1546
1547static htmlfile *html_new_file(htmlfilelist *list, char *filename)
1548{
f1530049 1549 htmlfile *ret = snew(htmlfile);
78c73085 1550
1551 ret->next = NULL;
1552 if (list->tail)
1553 list->tail->next = ret;
1554 else
1555 list->head = ret;
1556 list->tail = ret;
1557
1558 ret->filename = dupstr(filename);
1559 ret->last_fragment_number = 0;
1560 ret->min_heading_depth = INT_MAX;
1561 ret->first = ret->last = NULL;
1562
1563 return ret;
1564}
1565
1566static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title)
1567{
f1530049 1568 htmlsect *ret = snew(htmlsect);
78c73085 1569
1570 ret->next = NULL;
1571 if (list->tail)
1572 list->tail->next = ret;
1573 else
1574 list->head = ret;
1575 list->tail = ret;
1576
1577 ret->title = title;
1578 ret->file = NULL;
1579 ret->parent = NULL;
1580 ret->type = NORMAL;
1581
1582 return ret;
1583}
1584
1585static void html_words(htmloutput *ho, word *words, int flags,
1586 htmlfile *file, keywordlist *keywords, htmlconfig *cfg)
1587{
1588 word *w;
1589 char *c;
1590 int style, type;
1591
1592 for (w = words; w; w = w->next) switch (w->type) {
1593 case word_HyperLink:
1594 if (flags & LINKS) {
1595 element_open(ho, "a");
1596 c = utoa_dup(w->text, CS_ASCII);
1597 element_attr(ho, "href", c);
1598 sfree(c);
1599 }
1600 break;
1601 case word_UpperXref:
1602 case word_LowerXref:
1603 if (flags & LINKS) {
1604 keyword *kwl = kw_lookup(keywords, w->text);
1605 paragraph *p = kwl->para;
1606 htmlsect *s = (htmlsect *)p->private_data;
1607
1608 assert(s);
1609
1610 html_href(ho, file, s->file, s->fragment);
1611 }
1612 break;
1613 case word_HyperEnd:
1614 case word_XrefEnd:
1615 if (flags & LINKS)
1616 element_close(ho, "a");
1617 break;
1618 case word_IndexRef:
1619 if (flags & INDEXENTS) {
1620 htmlindexref *hr = (htmlindexref *)w->private_data;
27bdc5ab 1621 html_fragment(ho, hr->fragment);
1b7bf715 1622 hr->generated = TRUE;
78c73085 1623 }
1624 break;
1625 case word_Normal:
1626 case word_Emph:
1627 case word_Code:
1628 case word_WeakCode:
1629 case word_WhiteSpace:
1630 case word_EmphSpace:
1631 case word_CodeSpace:
1632 case word_WkCodeSpace:
1633 case word_Quote:
1634 case word_EmphQuote:
1635 case word_CodeQuote:
1636 case word_WkCodeQuote:
1637 style = towordstyle(w->type);
1638 type = removeattr(w->type);
1639 if (style == word_Emph &&
1640 (attraux(w->aux) == attr_First ||
1641 attraux(w->aux) == attr_Only) &&
1642 (flags & MARKUP))
1643 element_open(ho, "em");
1644 else if ((style == word_Code || style == word_WeakCode) &&
1645 (attraux(w->aux) == attr_First ||
1646 attraux(w->aux) == attr_Only) &&
1647 (flags & MARKUP))
1648 element_open(ho, "code");
1649
1650 if (type == word_WhiteSpace)
1651 html_text(ho, L" ");
1652 else if (type == word_Quote) {
1653 if (quoteaux(w->aux) == quote_Open)
1654 html_text(ho, cfg->lquote);
1655 else
1656 html_text(ho, cfg->rquote);
1657 } else {
35b123a0 1658 if (!w->alt || cvt_ok(ho->restrict_charset, w->text))
1659 html_text_nbsp(ho, w->text);
78c73085 1660 else
1661 html_words(ho, w->alt, flags, file, keywords, cfg);
1662 }
1663
1664 if (style == word_Emph &&
1665 (attraux(w->aux) == attr_Last ||
1666 attraux(w->aux) == attr_Only) &&
1667 (flags & MARKUP))
1668 element_close(ho, "em");
1669 else if ((style == word_Code || style == word_WeakCode) &&
1670 (attraux(w->aux) == attr_Last ||
1671 attraux(w->aux) == attr_Only) &&
1672 (flags & MARKUP))
1673 element_close(ho, "code");
1674
1675 break;
1676 }
1677}
1678
1679static void html_codepara(htmloutput *ho, word *words)
1680{
1681 element_open(ho, "pre");
1682 element_open(ho, "code");
1683 for (; words; words = words->next) if (words->type == word_WeakCode) {
1684 char *open_tag;
1685 wchar_t *t, *e;
1686
1687 t = words->text;
1688 if (words->next && words->next->type == word_Emph) {
1689 e = words->next->text;
1690 words = words->next;
1691 } else
1692 e = NULL;
1693
1694 while (e && *e && *t) {
1695 int n;
1696 int ec = *e;
1697
1698 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
1699
1700 open_tag = NULL;
1701 if (ec == 'i')
1702 open_tag = "em";
1703 else if (ec == 'b')
1704 open_tag = "b";
1705 if (open_tag)
1706 element_open(ho, open_tag);
1707
1708 html_text_limit(ho, t, n);
1709
1710 if (open_tag)
1711 element_close(ho, open_tag);
1712
1713 t += n;
1714 e += n;
1715 }
1716 html_text(ho, t);
1717 html_nl(ho);
1718 }
1719 element_close(ho, "code");
1720 element_close(ho, "pre");
1721}
1722
1723static void html_charset_cleanup(htmloutput *ho)
1724{
1725 char outbuf[256];
1726 int bytes;
1727
1728 bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf),
1729 ho->charset, &ho->cstate, NULL);
1730 if (bytes > 0)
1731 fwrite(outbuf, 1, bytes, ho->fp);
1732}
1733
35b123a0 1734static void return_mostly_to_neutral(htmloutput *ho)
78c73085 1735{
35b123a0 1736 if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) {
78c73085 1737 fprintf(ho->fp, " />");
1738 } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) {
1739 fprintf(ho->fp, ">");
1740 }
1741
1742 ho->state = HO_NEUTRAL;
1743}
1744
35b123a0 1745static void return_to_neutral(htmloutput *ho)
1746{
1747 if (ho->state == HO_IN_TEXT) {
1748 html_charset_cleanup(ho);
1749 }
1750
1751 return_mostly_to_neutral(ho);
1752}
1753
78c73085 1754static void element_open(htmloutput *ho, char const *name)
1755{
1756 return_to_neutral(ho);
1757 fprintf(ho->fp, "<%s", name);
1758 ho->state = HO_IN_TAG;
1759}
1760
1761static void element_close(htmloutput *ho, char const *name)
1762{
1763 return_to_neutral(ho);
1764 fprintf(ho->fp, "</%s>", name);
1765 ho->state = HO_NEUTRAL;
1766}
1767
1768static void element_empty(htmloutput *ho, char const *name)
1769{
1770 return_to_neutral(ho);
1771 fprintf(ho->fp, "<%s", name);
1772 ho->state = HO_IN_EMPTY_TAG;
1773}
1774
1775static void html_nl(htmloutput *ho)
1776{
1777 return_to_neutral(ho);
1778 fputc('\n', ho->fp);
1779}
1780
1781static void html_raw(htmloutput *ho, char *text)
1782{
1783 return_to_neutral(ho);
1784 fputs(text, ho->fp);
1785}
1786
1787static void html_raw_as_attr(htmloutput *ho, char *text)
1788{
1789 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
1790 fputc(' ', ho->fp);
1791 fputs(text, ho->fp);
1792}
1793
1794static void element_attr(htmloutput *ho, char const *name, char const *value)
1795{
1796 html_charset_cleanup(ho);
1797 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
1798 fprintf(ho->fp, " %s=\"%s\"", name, value);
1799}
1800
1801static void element_attr_w(htmloutput *ho, char const *name,
1802 wchar_t const *value)
1803{
1804 html_charset_cleanup(ho);
1805 fprintf(ho->fp, " %s=\"", name);
35b123a0 1806 html_text_limit_internal(ho, value, 0, TRUE, FALSE);
78c73085 1807 html_charset_cleanup(ho);
1808 fputc('"', ho->fp);
1809}
1810
1811static void html_text(htmloutput *ho, wchar_t const *text)
1812{
35b123a0 1813 return_mostly_to_neutral(ho);
1814 html_text_limit_internal(ho, text, 0, FALSE, FALSE);
1815}
1816
1817static void html_text_nbsp(htmloutput *ho, wchar_t const *text)
1818{
1819 return_mostly_to_neutral(ho);
1820 html_text_limit_internal(ho, text, 0, FALSE, TRUE);
78c73085 1821}
1822
1823static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen)
1824{
35b123a0 1825 return_mostly_to_neutral(ho);
1826 html_text_limit_internal(ho, text, maxlen, FALSE, FALSE);
78c73085 1827}
1828
1829static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
35b123a0 1830 int maxlen, int quote_quotes, int nbsp)
78c73085 1831{
1832 int textlen = ustrlen(text);
1833 char outbuf[256];
1834 int bytes, err;
1835
1836 if (maxlen > 0 && textlen > maxlen)
1837 textlen = maxlen;
1838
1839 while (textlen > 0) {
1840 /* Scan ahead for characters we really can't display in HTML. */
1841 int lenbefore, lenafter;
1842 for (lenbefore = 0; lenbefore < textlen; lenbefore++)
1843 if (text[lenbefore] == L'<' ||
1844 text[lenbefore] == L'>' ||
1845 text[lenbefore] == L'&' ||
35b123a0 1846 (text[lenbefore] == L'"' && quote_quotes) ||
1847 (text[lenbefore] == L' ' && nbsp))
78c73085 1848 break;
1849 lenafter = lenbefore;
1850 bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf),
1851 ho->charset, &ho->cstate, &err);
1852 textlen -= (lenbefore - lenafter);
1853 if (bytes > 0)
1854 fwrite(outbuf, 1, bytes, ho->fp);
1855 if (err) {
1856 /*
1857 * We have encountered a character that cannot be
1858 * displayed in the selected output charset. Therefore,
1859 * we use an HTML numeric entity reference.
1860 */
1861 assert(textlen > 0);
1862 fprintf(ho->fp, "&#%ld;", (long int)*text);
1863 text++, textlen--;
1864 } else if (lenafter == 0 && textlen > 0) {
1865 /*
1866 * We have encountered a character which is special to
1867 * HTML.
1868 */
1869 if (*text == L'<')
1870 fprintf(ho->fp, "&lt;");
1871 else if (*text == L'>')
1872 fprintf(ho->fp, "&gt;");
1873 else if (*text == L'&')
1874 fprintf(ho->fp, "&amp;");
1875 else if (*text == L'"')
1876 fprintf(ho->fp, "&quot;");
35b123a0 1877 else if (*text == L' ') {
1878 assert(nbsp);
1879 fprintf(ho->fp, "&nbsp;");
1880 } else
78c73085 1881 assert(!"Can't happen");
1882 text++, textlen--;
1883 }
1884 }
1885}
1886
1887static void cleanup(htmloutput *ho)
1888{
1889 return_to_neutral(ho);
1890 fclose(ho->fp);
1891}
1892
1893static void html_href(htmloutput *ho, htmlfile *thisfile,
1894 htmlfile *targetfile, char *targetfrag)
1895{
1896 rdstringc rs = { 0, 0, NULL };
1897 char *url;
1898
1899 if (targetfile != thisfile)
1900 rdaddsc(&rs, targetfile->filename);
1901 if (targetfrag) {
1902 rdaddc(&rs, '#');
1903 rdaddsc(&rs, targetfrag);
1904 }
1905 url = rs.text;
1906
1907 element_open(ho, "a");
1908 element_attr(ho, "href", url);
1909 sfree(url);
1910}
1911
27bdc5ab 1912static void html_fragment(htmloutput *ho, char const *fragment)
1913{
1914 element_open(ho, "a");
1915 element_attr(ho, "name", fragment);
1916 if (is_xhtml(ho->ver))
1917 element_attr(ho, "id", fragment);
1918 element_close(ho, "a");
1919}
1920
78c73085 1921static char *html_format(paragraph *p, char *template_string)
1922{
1923 char *c, *t;
1924 word *w;
1925 wchar_t *ws, wsbuf[2];
1926 rdstringc rs = { 0, 0, NULL };
1927
1928 t = template_string;
1929 while (*t) {
1930 if (*t == '%' && t[1]) {
1931 int fmt;
1932
1933 t++;
1934 fmt = *t++;
1935
1936 if (fmt == '%') {
1937 rdaddc(&rs, fmt);
1938 continue;
1939 }
1940
1941 w = NULL;
1942 ws = NULL;
1943
1944 if (p->kwtext && fmt == 'n')
1945 w = p->kwtext;
1946 else if (p->kwtext2 && fmt == 'b') {
1947 /*
1948 * HTML fragment names must start with a letter, so
1949 * simply `1.2.3' is not adequate. In this case I'm
1950 * going to cheat slightly by prepending the first
1951 * character of the first word of kwtext, so that
1952 * we get `C1' for chapter 1, `S2.3' for section
1953 * 2.3 etc.
1954 */
1955 if (p->kwtext && p->kwtext->text[0]) {
1956 ws = wsbuf;
1957 wsbuf[1] = '\0';
1958 wsbuf[0] = p->kwtext->text[0];
1959 }
1960 w = p->kwtext2;
1961 } else if (p->keyword && *p->keyword && fmt == 'k')
1962 ws = p->keyword;
1963 else
1964 w = p->words;
1965
1966 if (ws) {
1967 c = utoa_dup(ws, CS_ASCII);
1968 rdaddsc(&rs,c);
1969 sfree(c);
1970 }
1971
1972 while (w) {
1973 if (removeattr(w->type) == word_Normal) {
1974 c = utoa_dup(w->text, CS_ASCII);
1975 rdaddsc(&rs,c);
1976 sfree(c);
1977 }
1978 w = w->next;
1979 }
1980 } else {
1981 rdaddc(&rs, *t++);
1982 }
1983 }
1984
1985 return rdtrimc(&rs);
1986}
1987
3e82de8f 1988static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
1989 char *text)
78c73085 1990{
1991 /*
1992 * The HTML 4 spec's strictest definition of fragment names (<a
1993 * name> and "id" attributes) says that they `must begin with a
1994 * letter and may be followed by any number of letters, digits,
1995 * hyphens, underscores, colons, and periods'.
1996 *
1997 * So here we unceremoniously rip out any characters not
1998 * conforming to this limitation.
1999 */
2000 char *p = text, *q = text;
2001
2002 while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z')))
2003 p++;
3e82de8f 2004 if ((*q++ = *p++) != '\0') {
2005 while (*p) {
2006 if ((*p>='A' && *p<='Z') ||
2007 (*p>='a' && *p<='z') ||
2008 (*p>='0' && *p<='9') ||
2009 *p=='-' || *p=='_' || *p==':' || *p=='.')
2010 *q++ = *p;
2011 p++;
2012 }
2013
2014 *q = '\0';
2015 }
2016
2017 /*
2018 * Now we check for clashes with other fragment names, and
2019 * adjust this one if necessary by appending a hyphen followed
2020 * by a number.
2021 */
2022 {
2023 htmlfragment *frag = snew(htmlfragment);
2024 int len = 0; /* >0 indicates we have resized */
2025 int suffix = 1;
2026
2027 frag->file = file;
2028 frag->fragment = text;
2029
2030 while (add234(files->frags, frag) != frag) {
2031 if (!len) {
2032 len = strlen(text);
2033 frag->fragment = text = sresize(text, len+20, char);
2034 }
2035
2036 sprintf(text + len, "-%d", ++suffix);
2037 }
78c73085 2038 }
2039
3e82de8f 2040 return text;
78c73085 2041}
2042
2043static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
2044 htmlfile *thisfile, keywordlist *keywords,
2045 htmlconfig *cfg)
2046{
2047 while (ho->contents_level > depth) {
2048 element_close(ho, "ul");
2049 ho->contents_level--;
2050 }
2051
2052 while (ho->contents_level < depth) {
2053 element_open(ho, "ul");
2054 ho->contents_level++;
2055 }
2056
2057 if (!s)
2058 return;
2059
2060 element_open(ho, "li");
2061 html_href(ho, thisfile, s->file, s->fragment);
23c9bbc2 2062 html_section_title(ho, s, thisfile, keywords, cfg, FALSE);
78c73085 2063 element_close(ho, "a");
2064 element_close(ho, "li");
2065}
2066
2067static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile,
23c9bbc2 2068 keywordlist *keywords, htmlconfig *cfg,
2069 int real)
78c73085 2070{
2071 if (s->title) {
2072 sectlevel *sl;
2073 word *number;
2074 int depth = heading_depth(s->title);
2075
2076 if (depth < 0)
2077 sl = NULL;
2078 else if (depth == 0)
2079 sl = &cfg->achapter;
2080 else if (depth <= cfg->nasect)
2081 sl = &cfg->asect[depth-1];
2082 else
2083 sl = &cfg->asect[cfg->nasect-1];
2084
2085 if (!sl)
2086 number = NULL;
2087 else if (sl->just_numbers)
2088 number = s->title->kwtext2;
2089 else
2090 number = s->title->kwtext;
2091
2092 if (number) {
2093 html_words(ho, number, MARKUP,
2094 thisfile, keywords, cfg);
2095 html_text(ho, sl->number_suffix);
2096 }
2097
23c9bbc2 2098 html_words(ho, s->title->words, real ? ALL : MARKUP,
78c73085 2099 thisfile, keywords, cfg);
2100 } else {
2101 assert(s->type != NORMAL);
56a99eb6 2102 /*
2103 * If we're printing the full document title for _real_ and
2104 * there isn't one, we don't want to print `Preamble' at
2105 * the top of what ought to just be some text. If we need
2106 * it in any other context such as TOCs, we need to print
2107 * `Preamble'.
2108 */
2109 if (s->type == TOP && !real)
2110 html_text(ho, cfg->preamble_text);
78c73085 2111 else if (s->type == INDEX)
56a99eb6 2112 html_text(ho, cfg->index_text);
78c73085 2113 }
2114}