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