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