fad891c3e321c467eb5e72e09789432310e593e3
[sgt/halibut] / bk_html.c
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 * - In HHK index mode: subsidiary hhk entries (as in replacing
15 * `foo, bar' with `foo\n\tbar') can be done by embedding
16 * sub-<UL>s in the hhk file. This requires me getting round to
17 * supporting that idiom in the rest of Halibut, but I thought
18 * I'd record how it's done here in case I turn out to have
19 * forgotten when I get there.
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <limits.h>
26 #include "halibut.h"
27
28 #define is_heading_type(type) ( (type) == para_Title || \
29 (type) == para_Chapter || \
30 (type) == para_Appendix || \
31 (type) == para_UnnumberedChapter || \
32 (type) == para_Heading || \
33 (type) == para_Subsect)
34
35 #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
36 (p)->type == para_Heading ? 1 : \
37 (p)->type == para_Title ? -1 : 0 )
38
39 typedef struct {
40 int number_at_all, just_numbers;
41 wchar_t *number_suffix;
42 } sectlevel;
43
44 typedef struct {
45 int nasect;
46 sectlevel achapter, *asect;
47 int *contents_depths; /* 0=main, 1=chapter, 2=sect etc */
48 int ncdepths;
49 int address_section, visible_version_id;
50 int leaf_contains_contents, leaf_smallest_contents;
51 int navlinks;
52 int rellinks;
53 char *contents_filename;
54 char *index_filename;
55 char *template_filename;
56 char *single_filename;
57 char *chm_filename, *hhp_filename, *hhc_filename, *hhk_filename;
58 char **template_fragments;
59 int ntfragments;
60 char *head_end, *body_start, *body_end, *addr_start, *addr_end;
61 char *body_tag, *nav_attr;
62 wchar_t *author, *description;
63 wchar_t *index_text, *contents_text, *preamble_text, *title_separator;
64 wchar_t *nav_prev_text, *nav_next_text, *nav_up_text, *nav_separator;
65 wchar_t *index_main_sep, *index_multi_sep;
66 wchar_t *pre_versionid, *post_versionid;
67 int restrict_charset, output_charset;
68 enum {
69 HTML_3_2, HTML_4, ISO_HTML,
70 XHTML_1_0_TRANSITIONAL, XHTML_1_0_STRICT
71 } htmlver;
72 wchar_t *lquote, *rquote;
73 int leaf_level;
74 } htmlconfig;
75
76 #define contents_depth(conf, level) \
77 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
78
79 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
80
81 typedef struct htmlfile htmlfile;
82 typedef struct htmlsect htmlsect;
83
84 struct htmlfile {
85 htmlfile *next;
86 char *filename;
87 int last_fragment_number;
88 int min_heading_depth;
89 htmlsect *first, *last; /* first/last highest-level sections */
90 /*
91 * The `temp' field is available for use in individual passes
92 * over the file list. For example, the HHK index generation
93 * uses it to ensure no index term references the same file
94 * more than once.
95 */
96 int temp;
97 };
98
99 struct htmlsect {
100 htmlsect *next, *parent;
101 htmlfile *file;
102 paragraph *title, *text;
103 enum { NORMAL, TOP, INDEX } type;
104 int contents_depth;
105 char **fragments;
106 };
107
108 typedef struct {
109 htmlfile *head, *tail;
110 htmlfile *single, *index;
111 tree234 *frags;
112 tree234 *files;
113 } htmlfilelist;
114
115 typedef struct {
116 htmlsect *head, *tail;
117 } htmlsectlist;
118
119 typedef struct {
120 htmlfile *file;
121 char *fragment;
122 } htmlfragment;
123
124 typedef struct {
125 int nrefs, refsize;
126 word **refs;
127 } htmlindex;
128
129 typedef struct {
130 htmlsect *section;
131 char *fragment;
132 int generated, referenced;
133 } htmlindexref;
134
135 typedef struct {
136 /*
137 * This level deals with charset conversion, starting and
138 * ending tags, and writing to the file. It's the lexical
139 * level.
140 */
141 FILE *fp;
142 int charset, restrict_charset;
143 charset_state cstate;
144 int ver;
145 enum {
146 HO_NEUTRAL, HO_IN_TAG, HO_IN_EMPTY_TAG, HO_IN_TEXT
147 } state;
148 int hackflags; /* used for icky .HH* stuff */
149 int hacklimit; /* text size limit, again for .HH* */
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
158 /*
159 * Nasty hacks that modify the behaviour of htmloutput files. All
160 * of these are flag bits set in ho.hackflags. HO_HACK_QUOTEQUOTES
161 * has the same effect as the `quote_quotes' parameter to
162 * html_text_limit_internal, except that it's set globally on an
163 * entire htmloutput structure; HO_HACK_QUOTENOTHING suppresses
164 * quoting of any HTML special characters (for .HHP files);
165 * HO_HACK_OMITQUOTES completely suppresses the generation of
166 * double quotes at all (turning them into single quotes, for want
167 * of a better idea).
168 */
169 #define HO_HACK_QUOTEQUOTES 1
170 #define HO_HACK_QUOTENOTHING 2
171 #define HO_HACK_OMITQUOTES 4
172
173 static int html_fragment_compare(void *av, void *bv)
174 {
175 htmlfragment *a = (htmlfragment *)av;
176 htmlfragment *b = (htmlfragment *)bv;
177 int cmp;
178
179 if ((cmp = strcmp(a->file->filename, b->file->filename)) != 0)
180 return cmp;
181 else
182 return strcmp(a->fragment, b->fragment);
183 }
184
185 static int html_filename_compare(void *av, void *bv)
186 {
187 char *a = (char *)av;
188 char *b = (char *)bv;
189
190 return strcmp(a, b);
191 }
192
193 static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
194 htmlsect *sect, int depth);
195
196 static htmlfile *html_new_file(htmlfilelist *list, char *filename);
197 static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
198 htmlconfig *cfg);
199
200 /* Flags for html_words() flags parameter */
201 #define NOTHING 0x00
202 #define MARKUP 0x01
203 #define LINKS 0x02
204 #define INDEXENTS 0x04
205 #define ALL 0x07
206 static void html_words(htmloutput *ho, word *words, int flags,
207 htmlfile *file, keywordlist *keywords, htmlconfig *cfg);
208 static void html_codepara(htmloutput *ho, word *words);
209
210 static void element_open(htmloutput *ho, char const *name);
211 static void element_close(htmloutput *ho, char const *name);
212 static void element_empty(htmloutput *ho, char const *name);
213 static void element_attr(htmloutput *ho, char const *name, char const *value);
214 static void element_attr_w(htmloutput *ho, char const *name,
215 wchar_t const *value);
216 static void html_text(htmloutput *ho, wchar_t const *str);
217 static void html_text_nbsp(htmloutput *ho, wchar_t const *str);
218 static void html_text_limit(htmloutput *ho, wchar_t const *str, int maxlen);
219 static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
220 int maxlen, int quote_quotes, int nbsp);
221 static void html_nl(htmloutput *ho);
222 static void html_raw(htmloutput *ho, char *text);
223 static void html_raw_as_attr(htmloutput *ho, char *text);
224 static void cleanup(htmloutput *ho);
225
226 static void html_href(htmloutput *ho, htmlfile *thisfile,
227 htmlfile *targetfile, char *targetfrag);
228 static void html_fragment(htmloutput *ho, char const *fragment);
229
230 static char *html_format(paragraph *p, char *template_string);
231 static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
232 char *text);
233 static char *html_sanitise_filename(htmlfilelist *files, char *text);
234
235 static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
236 htmlfile *thisfile, keywordlist *keywords,
237 htmlconfig *cfg);
238 static void html_section_title(htmloutput *ho, htmlsect *s,
239 htmlfile *thisfile, keywordlist *keywords,
240 htmlconfig *cfg, int real);
241
242 static htmlconfig html_configure(paragraph *source) {
243 htmlconfig ret;
244 paragraph *p;
245
246 /*
247 * Defaults.
248 */
249 ret.leaf_level = 2;
250 ret.achapter.just_numbers = FALSE;
251 ret.achapter.number_at_all = TRUE;
252 ret.achapter.number_suffix = L": ";
253 ret.nasect = 1;
254 ret.asect = snewn(ret.nasect, sectlevel);
255 ret.asect[0].just_numbers = TRUE;
256 ret.asect[0].number_at_all = TRUE;
257 ret.asect[0].number_suffix = L" ";
258 ret.ncdepths = 0;
259 ret.contents_depths = 0;
260 ret.visible_version_id = TRUE;
261 ret.address_section = TRUE;
262 ret.leaf_contains_contents = FALSE;
263 ret.leaf_smallest_contents = 4;
264 ret.navlinks = TRUE;
265 ret.rellinks = TRUE;
266 ret.single_filename = dupstr("Manual.html");
267 ret.contents_filename = dupstr("Contents.html");
268 ret.index_filename = dupstr("IndexPage.html");
269 ret.template_filename = dupstr("%n.html");
270 ret.chm_filename = ret.hhp_filename = NULL;
271 ret.hhc_filename = ret.hhk_filename = NULL;
272 ret.ntfragments = 1;
273 ret.template_fragments = snewn(ret.ntfragments, char *);
274 ret.template_fragments[0] = dupstr("%b");
275 ret.head_end = ret.body_tag = ret.body_start = ret.body_end =
276 ret.addr_start = ret.addr_end = ret.nav_attr = NULL;
277 ret.author = ret.description = NULL;
278 ret.restrict_charset = CS_UTF8;
279 ret.output_charset = CS_ASCII;
280 ret.htmlver = HTML_4;
281 ret.index_text = L"Index";
282 ret.contents_text = L"Contents";
283 ret.preamble_text = L"Preamble";
284 ret.title_separator = L" - ";
285 ret.nav_prev_text = L"Previous";
286 ret.nav_next_text = L"Next";
287 ret.nav_up_text = L"Up";
288 ret.nav_separator = L" | ";
289 ret.index_main_sep = L": ";
290 ret.index_multi_sep = L", ";
291 ret.pre_versionid = L"[";
292 ret.post_versionid = L"]";
293 /*
294 * Default quote characters are Unicode matched single quotes,
295 * falling back to ordinary ASCII ".
296 */
297 ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0";
298 ret.rquote = uadv(ret.lquote);
299
300 /*
301 * Two-pass configuration so that we can pick up global config
302 * (e.g. `quotes') before having it overridden by specific
303 * config (`html-quotes'), irrespective of the order in which
304 * they occur.
305 */
306 for (p = source; p; p = p->next) {
307 if (p->type == para_Config) {
308 if (!ustricmp(p->keyword, L"quotes")) {
309 if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
310 ret.lquote = uadv(p->keyword);
311 ret.rquote = uadv(ret.lquote);
312 }
313 } else if (!ustricmp(p->keyword, L"index")) {
314 ret.index_text = uadv(p->keyword);
315 } else if (!ustricmp(p->keyword, L"contents")) {
316 ret.contents_text = uadv(p->keyword);
317 }
318 }
319 }
320
321 for (p = source; p; p = p->next) {
322 if (p->type == para_Config) {
323 wchar_t *k = p->keyword;
324
325 if (!ustrnicmp(k, L"xhtml-", 6))
326 k++; /* treat `xhtml-' and `html-' the same */
327
328 if (!ustricmp(k, L"html-restrict-charset")) {
329 ret.restrict_charset = charset_from_ustr(&p->fpos, uadv(k));
330 } else if (!ustricmp(k, L"html-output-charset")) {
331 ret.output_charset = charset_from_ustr(&p->fpos, uadv(k));
332 } else if (!ustricmp(k, L"html-version")) {
333 wchar_t *vername = uadv(k);
334 static const struct {
335 const wchar_t *name;
336 int ver;
337 } versions[] = {
338 {L"html3.2", HTML_3_2},
339 {L"html4", HTML_4},
340 {L"iso-html", ISO_HTML},
341 {L"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL},
342 {L"xhtml1.0strict", XHTML_1_0_STRICT}
343 };
344 int i;
345
346 for (i = 0; i < (int)lenof(versions); i++)
347 if (!ustricmp(versions[i].name, vername))
348 break;
349
350 if (i == lenof(versions))
351 error(err_htmlver, &p->fpos, vername);
352 else
353 ret.htmlver = versions[i].ver;
354 } else if (!ustricmp(k, L"html-single-filename")) {
355 sfree(ret.single_filename);
356 ret.single_filename = dupstr(adv(p->origkeyword));
357 } else if (!ustricmp(k, L"html-contents-filename")) {
358 sfree(ret.contents_filename);
359 ret.contents_filename = dupstr(adv(p->origkeyword));
360 } else if (!ustricmp(k, L"html-index-filename")) {
361 sfree(ret.index_filename);
362 ret.index_filename = dupstr(adv(p->origkeyword));
363 } else if (!ustricmp(k, L"html-template-filename")) {
364 sfree(ret.template_filename);
365 ret.template_filename = dupstr(adv(p->origkeyword));
366 } else if (!ustricmp(k, L"html-template-fragment")) {
367 char *frag = adv(p->origkeyword);
368 if (*frag) {
369 while (ret.ntfragments--)
370 sfree(ret.template_fragments[ret.ntfragments]);
371 sfree(ret.template_fragments);
372 ret.template_fragments = NULL;
373 ret.ntfragments = 0;
374 while (*frag) {
375 ret.ntfragments++;
376 ret.template_fragments =
377 sresize(ret.template_fragments,
378 ret.ntfragments, char *);
379 ret.template_fragments[ret.ntfragments-1] =
380 dupstr(frag);
381 frag = adv(frag);
382 }
383 } else
384 error(err_cfginsufarg, &p->fpos, p->origkeyword, 1);
385 } else if (!ustricmp(k, L"html-chapter-numeric")) {
386 ret.achapter.just_numbers = utob(uadv(k));
387 } else if (!ustricmp(k, L"html-chapter-shownumber")) {
388 ret.achapter.number_at_all = utob(uadv(k));
389 } else if (!ustricmp(k, L"html-suppress-navlinks")) {
390 ret.navlinks = !utob(uadv(k));
391 } else if (!ustricmp(k, L"html-rellinks")) {
392 ret.rellinks = utob(uadv(k));
393 } else if (!ustricmp(k, L"html-chapter-suffix")) {
394 ret.achapter.number_suffix = uadv(k);
395 } else if (!ustricmp(k, L"html-leaf-level")) {
396 wchar_t *u = uadv(k);
397 if (!ustricmp(u, L"infinite") ||
398 !ustricmp(u, L"infinity") ||
399 !ustricmp(u, L"inf"))
400 ret.leaf_level = -1; /* represents infinity */
401 else
402 ret.leaf_level = utoi(u);
403 } else if (!ustricmp(k, L"html-section-numeric")) {
404 wchar_t *q = uadv(k);
405 int n = 0;
406 if (uisdigit(*q)) {
407 n = utoi(q);
408 q = uadv(q);
409 }
410 if (n >= ret.nasect) {
411 int i;
412 ret.asect = sresize(ret.asect, n+1, sectlevel);
413 for (i = ret.nasect; i <= n; i++)
414 ret.asect[i] = ret.asect[ret.nasect-1];
415 ret.nasect = n+1;
416 }
417 ret.asect[n].just_numbers = utob(q);
418 } else if (!ustricmp(k, L"html-section-shownumber")) {
419 wchar_t *q = uadv(k);
420 int n = 0;
421 if (uisdigit(*q)) {
422 n = utoi(q);
423 q = uadv(q);
424 }
425 if (n >= ret.nasect) {
426 int i;
427 ret.asect = sresize(ret.asect, n+1, sectlevel);
428 for (i = ret.nasect; i <= n; i++)
429 ret.asect[i] = ret.asect[ret.nasect-1];
430 ret.nasect = n+1;
431 }
432 ret.asect[n].number_at_all = utob(q);
433 } else if (!ustricmp(k, L"html-section-suffix")) {
434 wchar_t *q = uadv(k);
435 int n = 0;
436 if (uisdigit(*q)) {
437 n = utoi(q);
438 q = uadv(q);
439 }
440 if (n >= ret.nasect) {
441 int i;
442 ret.asect = sresize(ret.asect, n+1, sectlevel);
443 for (i = ret.nasect; i <= n; i++) {
444 ret.asect[i] = ret.asect[ret.nasect-1];
445 }
446 ret.nasect = n+1;
447 }
448 ret.asect[n].number_suffix = q;
449 } else if (!ustricmp(k, L"html-contents-depth") ||
450 !ustrnicmp(k, L"html-contents-depth-", 20)) {
451 /*
452 * Relic of old implementation: this directive used
453 * to be written as \cfg{html-contents-depth-3}{2}
454 * rather than the usual Halibut convention of
455 * \cfg{html-contents-depth}{3}{2}. We therefore
456 * support both.
457 */
458 wchar_t *q = k[19] ? k+20 : uadv(k);
459 int n = 0;
460 if (uisdigit(*q)) {
461 n = utoi(q);
462 q = uadv(q);
463 }
464 if (n >= ret.ncdepths) {
465 int i;
466 ret.contents_depths =
467 sresize(ret.contents_depths, n+1, int);
468 for (i = ret.ncdepths; i <= n; i++) {
469 ret.contents_depths[i] = i+2;
470 }
471 ret.ncdepths = n+1;
472 }
473 ret.contents_depths[n] = utoi(q);
474 } else if (!ustricmp(k, L"html-head-end")) {
475 ret.head_end = adv(p->origkeyword);
476 } else if (!ustricmp(k, L"html-body-tag")) {
477 ret.body_tag = adv(p->origkeyword);
478 } else if (!ustricmp(k, L"html-body-start")) {
479 ret.body_start = adv(p->origkeyword);
480 } else if (!ustricmp(k, L"html-body-end")) {
481 ret.body_end = adv(p->origkeyword);
482 } else if (!ustricmp(k, L"html-address-start")) {
483 ret.addr_start = adv(p->origkeyword);
484 } else if (!ustricmp(k, L"html-address-end")) {
485 ret.addr_end = adv(p->origkeyword);
486 } else if (!ustricmp(k, L"html-navigation-attributes")) {
487 ret.nav_attr = adv(p->origkeyword);
488 } else if (!ustricmp(k, L"html-author")) {
489 ret.author = uadv(k);
490 } else if (!ustricmp(k, L"html-description")) {
491 ret.description = uadv(k);
492 } else if (!ustricmp(k, L"html-suppress-address")) {
493 ret.address_section = !utob(uadv(k));
494 } else if (!ustricmp(k, L"html-versionid")) {
495 ret.visible_version_id = utob(uadv(k));
496 } else if (!ustricmp(k, L"html-quotes")) {
497 if (*uadv(k) && *uadv(uadv(k))) {
498 ret.lquote = uadv(k);
499 ret.rquote = uadv(ret.lquote);
500 }
501 } else if (!ustricmp(k, L"html-leaf-contains-contents")) {
502 ret.leaf_contains_contents = utob(uadv(k));
503 } else if (!ustricmp(k, L"html-leaf-smallest-contents")) {
504 ret.leaf_smallest_contents = utoi(uadv(k));
505 } else if (!ustricmp(k, L"html-index-text")) {
506 ret.index_text = uadv(k);
507 } else if (!ustricmp(k, L"html-contents-text")) {
508 ret.contents_text = uadv(k);
509 } else if (!ustricmp(k, L"html-preamble-text")) {
510 ret.preamble_text = uadv(k);
511 } else if (!ustricmp(k, L"html-title-separator")) {
512 ret.title_separator = uadv(k);
513 } else if (!ustricmp(k, L"html-nav-prev-text")) {
514 ret.nav_prev_text = uadv(k);
515 } else if (!ustricmp(k, L"html-nav-next-text")) {
516 ret.nav_next_text = uadv(k);
517 } else if (!ustricmp(k, L"html-nav-up-text")) {
518 ret.nav_up_text = uadv(k);
519 } else if (!ustricmp(k, L"html-nav-separator")) {
520 ret.nav_separator = uadv(k);
521 } else if (!ustricmp(k, L"html-index-main-separator")) {
522 ret.index_main_sep = uadv(k);
523 } else if (!ustricmp(k, L"html-index-multiple-separator")) {
524 ret.index_multi_sep = uadv(k);
525 } else if (!ustricmp(k, L"html-pre-versionid")) {
526 ret.pre_versionid = uadv(k);
527 } else if (!ustricmp(k, L"html-post-versionid")) {
528 ret.post_versionid = uadv(k);
529 } else if (!ustricmp(k, L"html-mshtmlhelp-chm")) {
530 sfree(ret.chm_filename);
531 ret.chm_filename = dupstr(adv(p->origkeyword));
532 } else if (!ustricmp(k, L"html-mshtmlhelp-project")) {
533 sfree(ret.hhp_filename);
534 ret.hhp_filename = dupstr(adv(p->origkeyword));
535 } else if (!ustricmp(k, L"html-mshtmlhelp-contents")) {
536 sfree(ret.hhc_filename);
537 ret.hhc_filename = dupstr(adv(p->origkeyword));
538 } else if (!ustricmp(k, L"html-mshtmlhelp-index")) {
539 sfree(ret.hhk_filename);
540 ret.hhk_filename = dupstr(adv(p->origkeyword));
541 }
542 }
543 }
544
545 /*
546 * Enforce that the CHM and HHP filenames must either be both
547 * present or both absent. If one is present but not the other,
548 * turn both off.
549 */
550 if (!ret.chm_filename ^ !ret.hhp_filename) {
551 error(err_chmnames);
552 sfree(ret.chm_filename); ret.chm_filename = NULL;
553 sfree(ret.hhp_filename); ret.hhp_filename = NULL;
554 }
555 /*
556 * And if we're not generating an HHP, there's no need for HHC
557 * or HHK.
558 */
559 if (!ret.hhp_filename) {
560 sfree(ret.hhc_filename); ret.hhc_filename = NULL;
561 sfree(ret.hhk_filename); ret.hhk_filename = NULL;
562 }
563
564 /*
565 * Now process fallbacks on quote characters.
566 */
567 while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) &&
568 (!cvt_ok(ret.restrict_charset, ret.lquote) ||
569 !cvt_ok(ret.restrict_charset, ret.rquote))) {
570 ret.lquote = uadv(ret.rquote);
571 ret.rquote = uadv(ret.lquote);
572 }
573
574 return ret;
575 }
576
577 paragraph *html_config_filename(char *filename)
578 {
579 /*
580 * If the user passes in a single filename as a parameter to
581 * the `--html' command-line option, then we should assume it
582 * to imply _two_ config directives:
583 * \cfg{html-single-filename}{whatever} and
584 * \cfg{html-leaf-level}{0}; the rationale being that the user
585 * wants their output _in that file_.
586 */
587 paragraph *p, *q;
588
589 p = cmdline_cfg_simple("html-single-filename", filename, NULL);
590 q = cmdline_cfg_simple("html-leaf-level", "0", NULL);
591 p->next = q;
592 return p;
593 }
594
595 void html_backend(paragraph *sourceform, keywordlist *keywords,
596 indexdata *idx, void *unused)
597 {
598 paragraph *p;
599 htmlsect *topsect;
600 htmlconfig conf;
601 htmlfilelist files = { NULL, NULL, NULL, NULL, NULL, NULL };
602 htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL };
603 char *hhk_filename;
604 int has_index;
605
606 IGNORE(unused);
607
608 conf = html_configure(sourceform);
609
610 /*
611 * We're going to make heavy use of paragraphs' private data
612 * fields in the forthcoming code. Clear them first, so we can
613 * reliably tell whether we have auxiliary data for a
614 * particular paragraph.
615 */
616 for (p = sourceform; p; p = p->next)
617 p->private_data = NULL;
618
619 files.frags = newtree234(html_fragment_compare);
620 files.files = newtree234(html_filename_compare);
621
622 /*
623 * Start by figuring out into which file each piece of the
624 * document should be put. We'll do this by inventing an
625 * `htmlsect' structure and stashing it in the private_data
626 * field of each section paragraph; we also need one additional
627 * htmlsect for the document index, which won't show up in the
628 * source form but needs to be consistently mentioned in
629 * contents links.
630 *
631 * While we're here, we'll also invent the HTML fragment name(s)
632 * for each section.
633 */
634 {
635 htmlsect *sect;
636 int d;
637
638 topsect = html_new_sect(&sects, NULL, &conf);
639 topsect->type = TOP;
640 topsect->title = NULL;
641 topsect->text = sourceform;
642 topsect->contents_depth = contents_depth(conf, 0);
643 html_file_section(&conf, &files, topsect, -1);
644
645 for (p = sourceform; p; p = p->next)
646 if (is_heading_type(p->type)) {
647 d = heading_depth(p);
648
649 if (p->type == para_Title) {
650 topsect->title = p;
651 continue;
652 }
653
654 sect = html_new_sect(&sects, p, &conf);
655 sect->text = p->next;
656
657 sect->contents_depth = contents_depth(conf, d+1) - (d+1);
658
659 if (p->parent) {
660 sect->parent = (htmlsect *)p->parent->private_data;
661 assert(sect->parent != NULL);
662 } else
663 sect->parent = topsect;
664 p->private_data = sect;
665
666 html_file_section(&conf, &files, sect, d);
667
668 {
669 int i;
670 for (i=0; i < conf.ntfragments; i++) {
671 sect->fragments[i] =
672 html_format(p, conf.template_fragments[i]);
673 sect->fragments[i] =
674 html_sanitise_fragment(&files, sect->file,
675 sect->fragments[i]);
676 }
677 }
678 }
679
680 /*
681 * And the index, if we have one. Note that we don't output
682 * an index as an HTML file if we're outputting one as a
683 * .HHK.
684 */
685 has_index = (count234(idx->entries) > 0);
686 if (has_index && !conf.hhk_filename) {
687 sect = html_new_sect(&sects, NULL, &conf);
688 sect->text = NULL;
689 sect->type = INDEX;
690 sect->parent = topsect;
691 sect->contents_depth = 0;
692 html_file_section(&conf, &files, sect, 0); /* peer of chapters */
693 sect->fragments[0] = utoa_dup(conf.index_text, CS_ASCII);
694 sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
695 sect->fragments[0]);
696 files.index = sect->file;
697 }
698 }
699
700 /*
701 * Go through the keyword list and sort out fragment IDs for
702 * all the potentially referenced paragraphs which _aren't_
703 * headings.
704 */
705 {
706 int i;
707 keyword *kw;
708 htmlsect *sect;
709
710 for (i = 0; (kw = index234(keywords->keys, i)) != NULL; i++) {
711 paragraph *q, *p = kw->para;
712
713 if (!is_heading_type(p->type)) {
714 htmlsect *parent;
715
716 /*
717 * Find the paragraph's parent htmlsect, to
718 * determine which file it will end up in.
719 */
720 q = p->parent;
721 if (!q) {
722 /*
723 * Preamble paragraphs have no parent. So if we
724 * have a non-heading with no parent, it must
725 * be preamble, and therefore its parent
726 * htmlsect must be the preamble one.
727 */
728 assert(sects.head &&
729 sects.head->type == TOP);
730 parent = sects.head;
731 } else
732 parent = (htmlsect *)q->private_data;
733
734 /*
735 * Now we can construct an htmlsect for this
736 * paragraph itself, taking care to put it in the
737 * list of non-sections rather than the list of
738 * sections (so that traverses of the `sects' list
739 * won't attempt to add it to the contents or
740 * anything weird like that).
741 */
742 sect = html_new_sect(&nonsects, p, &conf);
743 sect->file = parent->file;
744 sect->parent = parent;
745 p->private_data = sect;
746
747 /*
748 * Fragment IDs for these paragraphs will simply be
749 * `p' followed by an integer.
750 */
751 sect->fragments[0] = snewn(40, char);
752 sprintf(sect->fragments[0], "p%d",
753 sect->file->last_fragment_number++);
754 sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
755 sect->fragments[0]);
756 }
757 }
758 }
759
760 /*
761 * Reset the fragment numbers in each file. I've just used them
762 * to generate `p' fragment IDs for non-section paragraphs
763 * (numbered list elements, bibliocited), and now I want to use
764 * them for `i' fragment IDs for index entries.
765 */
766 {
767 htmlfile *file;
768 for (file = files.head; file; file = file->next)
769 file->last_fragment_number = 0;
770 }
771
772 /*
773 * Now sort out the index. This involves:
774 *
775 * - For each index term, we set up an htmlindex structure to
776 * store all the references to that term.
777 *
778 * - Then we make a pass over the actual document, finding
779 * every word_IndexRef; for each one, we actually figure out
780 * the HTML filename/fragment pair we will use to reference
781 * it, store that information in the private data field of
782 * the word_IndexRef itself (so we can recreate it when the
783 * time comes to output our HTML), and add a reference to it
784 * to the index term in question.
785 */
786 {
787 int i;
788 indexentry *entry;
789 htmlsect *lastsect;
790 word *w;
791
792 /*
793 * Set up the htmlindex structures.
794 */
795
796 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
797 htmlindex *hi = snew(htmlindex);
798
799 hi->nrefs = hi->refsize = 0;
800 hi->refs = NULL;
801
802 entry->backend_data = hi;
803 }
804
805 /*
806 * Run over the document inventing fragments. Each fragment
807 * is of the form `i' followed by an integer.
808 */
809 lastsect = sects.head; /* this is always the top section */
810 for (p = sourceform; p; p = p->next) {
811 if (is_heading_type(p->type) && p->type != para_Title)
812 lastsect = (htmlsect *)p->private_data;
813
814 for (w = p->words; w; w = w->next)
815 if (w->type == word_IndexRef) {
816 htmlindexref *hr = snew(htmlindexref);
817 indextag *tag;
818 int i;
819
820 hr->referenced = hr->generated = FALSE;
821 hr->section = lastsect;
822 {
823 char buf[40];
824 sprintf(buf, "i%d",
825 lastsect->file->last_fragment_number++);
826 hr->fragment = dupstr(buf);
827 hr->fragment =
828 html_sanitise_fragment(&files, hr->section->file,
829 hr->fragment);
830 }
831 w->private_data = hr;
832
833 tag = index_findtag(idx, w->text);
834 if (!tag)
835 break;
836
837 for (i = 0; i < tag->nrefs; i++) {
838 indexentry *entry = tag->refs[i];
839 htmlindex *hi = (htmlindex *)entry->backend_data;
840
841 if (hi->nrefs >= hi->refsize) {
842 hi->refsize += 32;
843 hi->refs = sresize(hi->refs, hi->refsize, word *);
844 }
845
846 hi->refs[hi->nrefs++] = w;
847 }
848 }
849 }
850 }
851
852 /*
853 * Now we're ready to write out the actual HTML files.
854 *
855 * For each file:
856 *
857 * - we open that file and write its header
858 * - we run down the list of sections
859 * - for each section directly contained within that file, we
860 * output the section text
861 * - for each section which is not in the file but which has a
862 * parent that is, we output a contents entry for the
863 * section if appropriate
864 * - finally, we output the file trailer and close the file.
865 */
866 {
867 htmlfile *f, *prevf;
868 htmlsect *s;
869 paragraph *p;
870
871 prevf = NULL;
872
873 for (f = files.head; f; f = f->next) {
874 htmloutput ho;
875 int displaying;
876 enum LISTTYPE { NOLIST, UL, OL, DL };
877 enum ITEMTYPE { NOITEM, LI, DT, DD };
878 struct stackelement {
879 struct stackelement *next;
880 enum LISTTYPE listtype;
881 enum ITEMTYPE itemtype;
882 } *stackhead;
883
884 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
885 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
886
887 ho.fp = fopen(f->filename, "w");
888 if (!ho.fp)
889 error(err_cantopenw, f->filename);
890
891 ho.charset = conf.output_charset;
892 ho.restrict_charset = conf.restrict_charset;
893 ho.cstate = charset_init_state;
894 ho.ver = conf.htmlver;
895 ho.state = HO_NEUTRAL;
896 ho.contents_level = 0;
897 ho.hackflags = 0; /* none of these thankyouverymuch */
898 ho.hacklimit = -1;
899
900 /* <!DOCTYPE>. */
901 switch (conf.htmlver) {
902 case HTML_3_2:
903 if (ho.fp)
904 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
905 "HTML 3.2 Final//EN\">\n");
906 break;
907 case HTML_4:
908 if (ho.fp)
909 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
910 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
911 "strict.dtd\">\n");
912 break;
913 case ISO_HTML:
914 if (ho.fp)
915 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
916 "15445:2000//DTD HTML//EN\">\n");
917 break;
918 case XHTML_1_0_TRANSITIONAL:
919 if (ho.fp) {
920 fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
921 charset_to_mimeenc(conf.output_charset));
922 fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
923 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
924 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
925 }
926 break;
927 case XHTML_1_0_STRICT:
928 if (ho.fp) {
929 fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
930 charset_to_mimeenc(conf.output_charset));
931 fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
932 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
933 "DTD/xhtml1-strict.dtd\">\n");
934 }
935 break;
936 }
937
938 element_open(&ho, "html");
939 if (is_xhtml(conf.htmlver)) {
940 element_attr(&ho, "xmlns", "http://www.w3.org/1999/xhtml");
941 }
942 html_nl(&ho);
943
944 element_open(&ho, "head");
945 html_nl(&ho);
946
947 element_empty(&ho, "meta");
948 element_attr(&ho, "http-equiv", "content-type");
949 {
950 char buf[200];
951 sprintf(buf, "text/html; charset=%.150s",
952 charset_to_mimeenc(conf.output_charset));
953 element_attr(&ho, "content", buf);
954 }
955 html_nl(&ho);
956
957 if (conf.author) {
958 element_empty(&ho, "meta");
959 element_attr(&ho, "name", "author");
960 element_attr_w(&ho, "content", conf.author);
961 html_nl(&ho);
962 }
963
964 if (conf.description) {
965 element_empty(&ho, "meta");
966 element_attr(&ho, "name", "description");
967 element_attr_w(&ho, "content", conf.description);
968 html_nl(&ho);
969 }
970
971 element_open(&ho, "title");
972 if (f->first && f->first->title) {
973 html_words(&ho, f->first->title->words, NOTHING,
974 f, keywords, &conf);
975
976 assert(f->last);
977 if (f->last != f->first && f->last->title) {
978 html_text(&ho, conf.title_separator);
979 html_words(&ho, f->last->title->words, NOTHING,
980 f, keywords, &conf);
981 }
982 }
983 element_close(&ho, "title");
984 html_nl(&ho);
985
986 if (conf.rellinks) {
987
988 if (prevf) {
989 element_empty(&ho, "link");
990 element_attr(&ho, "rel", "previous");
991 element_attr(&ho, "href", prevf->filename);
992 html_nl(&ho);
993 }
994
995 if (f != files.head) {
996 element_empty(&ho, "link");
997 element_attr(&ho, "rel", "ToC");
998 element_attr(&ho, "href", files.head->filename);
999 html_nl(&ho);
1000 }
1001
1002 if (conf.leaf_level > 0) {
1003 htmlsect *p = f->first->parent;
1004 assert(p == f->last->parent);
1005 if (p) {
1006 element_empty(&ho, "link");
1007 element_attr(&ho, "rel", "up");
1008 element_attr(&ho, "href", p->file->filename);
1009 html_nl(&ho);
1010 }
1011 }
1012
1013 if (has_index && files.index && f != files.index) {
1014 element_empty(&ho, "link");
1015 element_attr(&ho, "rel", "index");
1016 element_attr(&ho, "href", files.index->filename);
1017 html_nl(&ho);
1018 }
1019
1020 if (f->next) {
1021 element_empty(&ho, "link");
1022 element_attr(&ho, "rel", "next");
1023 element_attr(&ho, "href", f->next->filename);
1024 html_nl(&ho);
1025 }
1026
1027 }
1028
1029 if (conf.head_end)
1030 html_raw(&ho, conf.head_end);
1031
1032 /*
1033 * Add any <head> data defined in specific sections
1034 * that go in this file. (This is mostly to allow <meta
1035 * name="AppleTitle"> tags for Mac online help.)
1036 */
1037 for (s = sects.head; s; s = s->next) {
1038 if (s->file == f && s->text) {
1039 for (p = s->text;
1040 p && (p == s->text || p->type == para_Title ||
1041 !is_heading_type(p->type));
1042 p = p->next) {
1043 if (p->type == para_Config) {
1044 if (!ustricmp(p->keyword, L"html-local-head")) {
1045 html_raw(&ho, adv(p->origkeyword));
1046 }
1047 }
1048 }
1049 }
1050 }
1051
1052 element_close(&ho, "head");
1053 html_nl(&ho);
1054
1055 if (conf.body_tag)
1056 html_raw(&ho, conf.body_tag);
1057 else
1058 element_open(&ho, "body");
1059 html_nl(&ho);
1060
1061 if (conf.body_start)
1062 html_raw(&ho, conf.body_start);
1063
1064 /*
1065 * Write out a nav bar. Special case: we don't do this
1066 * if there is only one file.
1067 */
1068 if (conf.navlinks && files.head != files.tail) {
1069 element_open(&ho, "p");
1070 if (conf.nav_attr)
1071 html_raw_as_attr(&ho, conf.nav_attr);
1072
1073 if (prevf) {
1074 element_open(&ho, "a");
1075 element_attr(&ho, "href", prevf->filename);
1076 }
1077 html_text(&ho, conf.nav_prev_text);
1078 if (prevf)
1079 element_close(&ho, "a");
1080
1081 html_text(&ho, conf.nav_separator);
1082
1083 if (f != files.head) {
1084 element_open(&ho, "a");
1085 element_attr(&ho, "href", files.head->filename);
1086 }
1087 html_text(&ho, conf.contents_text);
1088 if (f != files.head)
1089 element_close(&ho, "a");
1090
1091 /* We don't bother with "Up" links for leaf-level 1,
1092 * as they would be identical to the "Contents" links. */
1093 if (conf.leaf_level >= 2) {
1094 htmlsect *p = f->first->parent;
1095 assert(p == f->last->parent);
1096 html_text(&ho, conf.nav_separator);
1097 if (p) {
1098 element_open(&ho, "a");
1099 element_attr(&ho, "href", p->file->filename);
1100 }
1101 html_text(&ho, conf.nav_up_text);
1102 if (p) {
1103 element_close(&ho, "a");
1104 }
1105 }
1106
1107 if (has_index && files.index) {
1108 html_text(&ho, conf.nav_separator);
1109 if (f != files.index) {
1110 element_open(&ho, "a");
1111 element_attr(&ho, "href", files.index->filename);
1112 }
1113 html_text(&ho, conf.index_text);
1114 if (f != files.index)
1115 element_close(&ho, "a");
1116 }
1117
1118 html_text(&ho, conf.nav_separator);
1119
1120 if (f->next) {
1121 element_open(&ho, "a");
1122 element_attr(&ho, "href", f->next->filename);
1123 }
1124 html_text(&ho, conf.nav_next_text);
1125 if (f->next)
1126 element_close(&ho, "a");
1127
1128 element_close(&ho, "p");
1129 html_nl(&ho);
1130 }
1131 prevf = f;
1132
1133 /*
1134 * Write out a prefix TOC for the file (if a leaf file).
1135 *
1136 * We start by going through the section list and
1137 * collecting the sections which need to be added to
1138 * the contents. On the way, we also test to see if
1139 * this file is a leaf file (defined as one which
1140 * contains all descendants of any section it
1141 * contains), because this will play a part in our
1142 * decision on whether or not to _output_ the TOC.
1143 *
1144 * Special case: we absolutely do not do this if we're
1145 * in single-file mode.
1146 */
1147 if (files.head != files.tail) {
1148 int ntoc = 0, tocsize = 0;
1149 htmlsect **toc = NULL;
1150 int leaf = TRUE;
1151
1152 for (s = sects.head; s; s = s->next) {
1153 htmlsect *a, *ac;
1154 int depth, adepth;
1155
1156 /*
1157 * Search up from this section until we find
1158 * the highest-level one which belongs in this
1159 * file.
1160 */
1161 depth = adepth = 0;
1162 a = NULL;
1163 for (ac = s; ac; ac = ac->parent) {
1164 if (ac->file == f) {
1165 a = ac;
1166 adepth = depth;
1167 }
1168 depth++;
1169 }
1170
1171 if (s->file != f && a != NULL)
1172 leaf = FALSE;
1173
1174 if (a) {
1175 if (adepth <= a->contents_depth) {
1176 if (ntoc >= tocsize) {
1177 tocsize += 64;
1178 toc = sresize(toc, tocsize, htmlsect *);
1179 }
1180 toc[ntoc++] = s;
1181 }
1182 }
1183 }
1184
1185 if (leaf && conf.leaf_contains_contents &&
1186 ntoc >= conf.leaf_smallest_contents) {
1187 int i;
1188
1189 for (i = 0; i < ntoc; i++) {
1190 htmlsect *s = toc[i];
1191 int hlevel = (s->type == TOP ? -1 :
1192 s->type == INDEX ? 0 :
1193 heading_depth(s->title))
1194 - f->min_heading_depth + 1;
1195
1196 assert(hlevel >= 1);
1197 html_contents_entry(&ho, hlevel, s,
1198 f, keywords, &conf);
1199 }
1200 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1201 }
1202 }
1203
1204 /*
1205 * Now go through the document and output some real
1206 * text.
1207 */
1208 displaying = FALSE;
1209 for (s = sects.head; s; s = s->next) {
1210 if (s->file == f) {
1211 /*
1212 * This section belongs in this file.
1213 * Display it.
1214 */
1215 displaying = TRUE;
1216 } else {
1217 /*
1218 * Doesn't belong in this file, but it may be
1219 * a descendant of a section which does, in
1220 * which case we should consider it for the
1221 * main TOC of this file (for non-leaf files).
1222 */
1223 htmlsect *a, *ac;
1224 int depth, adepth;
1225
1226 displaying = FALSE;
1227
1228 /*
1229 * Search up from this section until we find
1230 * the highest-level one which belongs in this
1231 * file.
1232 */
1233 depth = adepth = 0;
1234 a = NULL;
1235 for (ac = s; ac; ac = ac->parent) {
1236 if (ac->file == f) {
1237 a = ac;
1238 adepth = depth;
1239 }
1240 depth++;
1241 }
1242
1243 if (a != NULL) {
1244 /*
1245 * This section does not belong in this
1246 * file, but an ancestor of it does. Write
1247 * out a contents table entry, if the depth
1248 * doesn't exceed the maximum contents
1249 * depth for the ancestor section.
1250 */
1251 if (adepth <= a->contents_depth) {
1252 html_contents_entry(&ho, adepth, s,
1253 f, keywords, &conf);
1254 }
1255 }
1256 }
1257
1258 if (displaying) {
1259 int hlevel;
1260 char htag[3];
1261
1262 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1263
1264 /*
1265 * Display the section heading.
1266 */
1267
1268 hlevel = (s->type == TOP ? -1 :
1269 s->type == INDEX ? 0 :
1270 heading_depth(s->title))
1271 - f->min_heading_depth + 1;
1272 assert(hlevel >= 1);
1273 /* HTML headings only go up to <h6> */
1274 if (hlevel > 6)
1275 hlevel = 6;
1276 htag[0] = 'h';
1277 htag[1] = '0' + hlevel;
1278 htag[2] = '\0';
1279 element_open(&ho, htag);
1280
1281 /*
1282 * Provide anchor(s) for cross-links to target.
1283 *
1284 * (Also we'll have to do this separately in
1285 * other paragraph types - NumberedList and
1286 * BiblioCited.)
1287 */
1288 {
1289 int i;
1290 for (i=0; i < conf.ntfragments; i++)
1291 if (s->fragments[i])
1292 html_fragment(&ho, s->fragments[i]);
1293 }
1294
1295 html_section_title(&ho, s, f, keywords, &conf, TRUE);
1296
1297 element_close(&ho, htag);
1298
1299 /*
1300 * Now display the section text.
1301 */
1302 if (s->text) {
1303 stackhead = snew(struct stackelement);
1304 stackhead->next = NULL;
1305 stackhead->listtype = NOLIST;
1306 stackhead->itemtype = NOITEM;
1307
1308 for (p = s->text;; p = p->next) {
1309 enum LISTTYPE listtype;
1310 struct stackelement *se;
1311
1312 /*
1313 * Preliminary switch to figure out what
1314 * sort of list we expect to be inside at
1315 * this stage.
1316 *
1317 * Since p may still be NULL at this point,
1318 * I invent a harmless paragraph type for
1319 * it if it is.
1320 */
1321 switch (p ? p->type : para_Normal) {
1322 case para_Rule:
1323 case para_Normal:
1324 case para_Copyright:
1325 case para_BiblioCited:
1326 case para_Code:
1327 case para_QuotePush:
1328 case para_QuotePop:
1329 case para_Chapter:
1330 case para_Appendix:
1331 case para_UnnumberedChapter:
1332 case para_Heading:
1333 case para_Subsect:
1334 case para_LcontPop:
1335 listtype = NOLIST;
1336 break;
1337
1338 case para_Bullet:
1339 listtype = UL;
1340 break;
1341
1342 case para_NumberedList:
1343 listtype = OL;
1344 break;
1345
1346 case para_DescribedThing:
1347 case para_Description:
1348 listtype = DL;
1349 break;
1350
1351 case para_LcontPush:
1352 se = snew(struct stackelement);
1353 se->next = stackhead;
1354 se->listtype = NOLIST;
1355 se->itemtype = NOITEM;
1356 stackhead = se;
1357 continue;
1358
1359 default: /* some totally non-printing para */
1360 continue;
1361 }
1362
1363 html_nl(&ho);
1364
1365 /*
1366 * Terminate the most recent list item, if
1367 * any. (We left this until after
1368 * processing LcontPush, since in that case
1369 * the list item won't want to be
1370 * terminated until after the corresponding
1371 * LcontPop.)
1372 */
1373 if (stackhead->itemtype != NOITEM) {
1374 element_close(&ho, itemname(stackhead->itemtype));
1375 html_nl(&ho);
1376 }
1377 stackhead->itemtype = NOITEM;
1378
1379 /*
1380 * Terminate the current list, if it's not
1381 * the one we want to be in.
1382 */
1383 if (listtype != stackhead->listtype &&
1384 stackhead->listtype != NOLIST) {
1385 element_close(&ho, listname(stackhead->listtype));
1386 html_nl(&ho);
1387 }
1388
1389 /*
1390 * Leave the loop if our time has come.
1391 */
1392 if (!p || (is_heading_type(p->type) &&
1393 p->type != para_Title))
1394 break; /* end of section text */
1395
1396 /*
1397 * Start a fresh list if necessary.
1398 */
1399 if (listtype != stackhead->listtype &&
1400 listtype != NOLIST)
1401 element_open(&ho, listname(listtype));
1402
1403 stackhead->listtype = listtype;
1404
1405 switch (p->type) {
1406 case para_Rule:
1407 element_empty(&ho, "hr");
1408 break;
1409 case para_Code:
1410 html_codepara(&ho, p->words);
1411 break;
1412 case para_Normal:
1413 case para_Copyright:
1414 element_open(&ho, "p");
1415 html_nl(&ho);
1416 html_words(&ho, p->words, ALL,
1417 f, keywords, &conf);
1418 html_nl(&ho);
1419 element_close(&ho, "p");
1420 break;
1421 case para_BiblioCited:
1422 element_open(&ho, "p");
1423 if (p->private_data) {
1424 htmlsect *s = (htmlsect *)p->private_data;
1425 int i;
1426 for (i=0; i < conf.ntfragments; i++)
1427 if (s->fragments[i])
1428 html_fragment(&ho, s->fragments[i]);
1429 }
1430 html_nl(&ho);
1431 html_words(&ho, p->kwtext, ALL,
1432 f, keywords, &conf);
1433 html_text(&ho, L" ");
1434 html_words(&ho, p->words, ALL,
1435 f, keywords, &conf);
1436 html_nl(&ho);
1437 element_close(&ho, "p");
1438 break;
1439 case para_Bullet:
1440 case para_NumberedList:
1441 element_open(&ho, "li");
1442 if (p->private_data) {
1443 htmlsect *s = (htmlsect *)p->private_data;
1444 int i;
1445 for (i=0; i < conf.ntfragments; i++)
1446 if (s->fragments[i])
1447 html_fragment(&ho, s->fragments[i]);
1448 }
1449 html_nl(&ho);
1450 stackhead->itemtype = LI;
1451 html_words(&ho, p->words, ALL,
1452 f, keywords, &conf);
1453 break;
1454 case para_DescribedThing:
1455 element_open(&ho, "dt");
1456 html_nl(&ho);
1457 stackhead->itemtype = DT;
1458 html_words(&ho, p->words, ALL,
1459 f, keywords, &conf);
1460 break;
1461 case para_Description:
1462 element_open(&ho, "dd");
1463 html_nl(&ho);
1464 stackhead->itemtype = DD;
1465 html_words(&ho, p->words, ALL,
1466 f, keywords, &conf);
1467 break;
1468
1469 case para_QuotePush:
1470 element_open(&ho, "blockquote");
1471 break;
1472 case para_QuotePop:
1473 element_close(&ho, "blockquote");
1474 break;
1475
1476 case para_LcontPop:
1477 se = stackhead;
1478 stackhead = stackhead->next;
1479 assert(stackhead);
1480 sfree(se);
1481 break;
1482 }
1483 }
1484
1485 assert(stackhead && !stackhead->next);
1486 sfree(stackhead);
1487 }
1488
1489 if (s->type == INDEX) {
1490 indexentry *entry;
1491 int i;
1492
1493 /*
1494 * This section is the index. I'll just
1495 * render it as a single paragraph, with a
1496 * colon between the index term and the
1497 * references, and <br> in between each
1498 * entry.
1499 */
1500 element_open(&ho, "p");
1501
1502 for (i = 0; (entry =
1503 index234(idx->entries, i)) != NULL; i++) {
1504 htmlindex *hi = (htmlindex *)entry->backend_data;
1505 int j;
1506
1507 if (i > 0)
1508 element_empty(&ho, "br");
1509 html_nl(&ho);
1510
1511 html_words(&ho, entry->text, MARKUP|LINKS,
1512 f, keywords, &conf);
1513
1514 html_text(&ho, conf.index_main_sep);
1515
1516 for (j = 0; j < hi->nrefs; j++) {
1517 htmlindexref *hr =
1518 (htmlindexref *)hi->refs[j]->private_data;
1519 paragraph *p = hr->section->title;
1520
1521 if (j > 0)
1522 html_text(&ho, conf.index_multi_sep);
1523
1524 html_href(&ho, f, hr->section->file,
1525 hr->fragment);
1526 hr->referenced = TRUE;
1527 if (p && p->kwtext)
1528 html_words(&ho, p->kwtext, MARKUP|LINKS,
1529 f, keywords, &conf);
1530 else if (p && p->words)
1531 html_words(&ho, p->words, MARKUP|LINKS,
1532 f, keywords, &conf);
1533 else {
1534 /*
1535 * If there is no title at all,
1536 * this must be because our
1537 * target section is the
1538 * preamble section and there
1539 * is no title. So we use the
1540 * preamble_text.
1541 */
1542 html_text(&ho, conf.preamble_text);
1543 }
1544 element_close(&ho, "a");
1545 }
1546 }
1547 element_close(&ho, "p");
1548 }
1549 }
1550 }
1551
1552 html_contents_entry(&ho, 0, NULL, f, keywords, &conf);
1553 html_nl(&ho);
1554
1555 {
1556 /*
1557 * Footer.
1558 */
1559 int done_version_ids = FALSE;
1560
1561 if (conf.address_section)
1562 element_empty(&ho, "hr");
1563
1564 if (conf.body_end)
1565 html_raw(&ho, conf.body_end);
1566
1567 if (conf.address_section) {
1568 int started = FALSE;
1569 if (conf.htmlver == ISO_HTML) {
1570 /*
1571 * The ISO-HTML validator complains if
1572 * there isn't a <div> tag surrounding the
1573 * <address> tag. I'm uncertain of why this
1574 * should be - there appears to be no
1575 * mention of this in the ISO-HTML spec,
1576 * suggesting that it doesn't represent a
1577 * change from HTML 4, but nonetheless the
1578 * HTML 4 validator doesn't seem to mind.
1579 */
1580 element_open(&ho, "div");
1581 }
1582 element_open(&ho, "address");
1583 if (conf.addr_start) {
1584 html_raw(&ho, conf.addr_start);
1585 html_nl(&ho);
1586 started = TRUE;
1587 }
1588 if (conf.visible_version_id) {
1589 for (p = sourceform; p; p = p->next)
1590 if (p->type == para_VersionID) {
1591 if (started)
1592 element_empty(&ho, "br");
1593 html_nl(&ho);
1594 html_text(&ho, conf.pre_versionid);
1595 html_words(&ho, p->words, NOTHING,
1596 f, keywords, &conf);
1597 html_text(&ho, conf.post_versionid);
1598 started = TRUE;
1599 }
1600 done_version_ids = TRUE;
1601 }
1602 if (conf.addr_end) {
1603 if (started)
1604 element_empty(&ho, "br");
1605 html_raw(&ho, conf.addr_end);
1606 }
1607 element_close(&ho, "address");
1608 if (conf.htmlver == ISO_HTML)
1609 element_close(&ho, "div");
1610 }
1611
1612 if (!done_version_ids) {
1613 /*
1614 * If the user didn't want the version IDs
1615 * visible, I think we still have a duty to put
1616 * them in an HTML comment.
1617 */
1618 int started = FALSE;
1619 for (p = sourceform; p; p = p->next)
1620 if (p->type == para_VersionID) {
1621 if (!started) {
1622 html_raw(&ho, "<!-- version IDs:\n");
1623 started = TRUE;
1624 }
1625 html_words(&ho, p->words, NOTHING,
1626 f, keywords, &conf);
1627 html_nl(&ho);
1628 }
1629 if (started)
1630 html_raw(&ho, "-->\n");
1631 }
1632 }
1633
1634 element_close(&ho, "body");
1635 html_nl(&ho);
1636 element_close(&ho, "html");
1637 html_nl(&ho);
1638 cleanup(&ho);
1639 }
1640 }
1641
1642 /*
1643 * Before we start outputting the HTML Help files, check
1644 * whether there's even going to _be_ an index file: we omit it
1645 * if the index contains nothing.
1646 */
1647 hhk_filename = conf.hhk_filename;
1648 if (hhk_filename) {
1649 int ok = FALSE;
1650 int i;
1651 indexentry *entry;
1652
1653 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
1654 htmlindex *hi = (htmlindex *)entry->backend_data;
1655
1656 if (hi->nrefs > 0) {
1657 ok = TRUE; /* found an index entry */
1658 break;
1659 }
1660 }
1661
1662 if (!ok)
1663 hhk_filename = NULL;
1664 }
1665
1666 /*
1667 * Output the MS HTML Help supporting files, if requested.
1668 *
1669 * A good unofficial reference for these is <http://chmspec.nongnu.org/>.
1670 */
1671 if (conf.hhp_filename) {
1672 htmlfile *f;
1673 htmloutput ho;
1674
1675 ho.charset = CS_CP1252; /* as far as I know, HHP files are */
1676 ho.restrict_charset = CS_CP1252; /* hardwired to this charset */
1677 ho.cstate = charset_init_state;
1678 ho.ver = HTML_4; /* *shrug* */
1679 ho.state = HO_NEUTRAL;
1680 ho.contents_level = 0;
1681 ho.hackflags = HO_HACK_QUOTENOTHING;
1682
1683 ho.fp = fopen(conf.hhp_filename, "w");
1684 if (!ho.fp)
1685 error(err_cantopenw, conf.hhp_filename);
1686
1687 fprintf(ho.fp,
1688 "[OPTIONS]\n"
1689 /* Binary TOC required for Next/Previous nav to work */
1690 "Binary TOC=Yes\n"
1691 "Compatibility=1.1 or later\n"
1692 "Compiled file=%s\n"
1693 "Default Window=main\n"
1694 "Default topic=%s\n"
1695 "Display compile progress=Yes\n"
1696 "Full-text search=Yes\n"
1697 "Title=", conf.chm_filename, files.head->filename);
1698
1699 ho.hacklimit = 255;
1700 html_words(&ho, topsect->title->words, NOTHING,
1701 NULL, keywords, &conf);
1702
1703 fprintf(ho.fp, "\n");
1704
1705 /*
1706 * These two entries don't seem to be remotely necessary
1707 * for a successful run of the help _compiler_, but
1708 * omitting them causes the GUI Help Workshop to behave
1709 * rather strangely if you try to load the help project
1710 * into that and edit it.
1711 */
1712 if (conf.hhc_filename)
1713 fprintf(ho.fp, "Contents file=%s\n", conf.hhc_filename);
1714 if (hhk_filename)
1715 fprintf(ho.fp, "Index file=%s\n", hhk_filename);
1716
1717 fprintf(ho.fp, "\n[WINDOWS]\nmain=\"");
1718
1719 ho.hackflags |= HO_HACK_OMITQUOTES;
1720 ho.hacklimit = 255;
1721 html_words(&ho, topsect->title->words, NOTHING,
1722 NULL, keywords, &conf);
1723
1724 fprintf(ho.fp, "\",\"%s\",\"%s\",\"%s\",,,,,,"
1725 /* This first magic number is fsWinProperties, controlling
1726 * Navigation Pane options and the like.
1727 * Constants HHWIN_PROP_* in htmlhelp.h. */
1728 "0x62520,,"
1729 /* This second number is fsToolBarFlags, mainly controlling
1730 * toolbar buttons. Constants HHWIN_BUTTON_*.
1731 * NOTE: there are two pairs of bits for Next/Previous
1732 * buttons: 7/8 (which do nothing useful), and 21/22
1733 * (which work). (Neither of these are exposed in the HHW
1734 * UI, but they work fine in HH.) We use the latter. */
1735 "0x60304e,,,,,,,,0\n",
1736 conf.hhc_filename ? conf.hhc_filename : "",
1737 hhk_filename ? hhk_filename : "",
1738 files.head->filename);
1739
1740 /*
1741 * The [FILES] section is also not necessary for
1742 * compilation (hhc appears to build up a list of needed
1743 * files just by following links from the given starting
1744 * points), but useful for loading the project into HHW.
1745 */
1746 fprintf(ho.fp, "\n[FILES]\n");
1747 for (f = files.head; f; f = f->next)
1748 fprintf(ho.fp, "%s\n", f->filename);
1749
1750 fclose(ho.fp);
1751 }
1752 if (conf.hhc_filename) {
1753 htmlfile *f;
1754 htmlsect *s, *a;
1755 htmloutput ho;
1756 int currdepth = 0;
1757
1758 ho.fp = fopen(conf.hhc_filename, "w");
1759 if (!ho.fp)
1760 error(err_cantopenw, conf.hhc_filename);
1761
1762 ho.charset = CS_CP1252; /* as far as I know, HHC files are */
1763 ho.restrict_charset = CS_CP1252; /* hardwired to this charset */
1764 ho.cstate = charset_init_state;
1765 ho.ver = HTML_4; /* *shrug* */
1766 ho.state = HO_NEUTRAL;
1767 ho.contents_level = 0;
1768 ho.hackflags = HO_HACK_QUOTEQUOTES;
1769
1770 /*
1771 * Magic DOCTYPE which seems to work for .HHC files. I'm
1772 * wary of trying to change it!
1773 */
1774 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1775 "<HTML><HEAD>\n"
1776 "<META HTTP-EQUIV=\"Content-Type\" "
1777 "CONTENT=\"text/html; charset=%s\">\n"
1778 "</HEAD><BODY><UL>\n",
1779 charset_to_mimeenc(conf.output_charset));
1780
1781 for (f = files.head; f; f = f->next) {
1782 /*
1783 * For each HTML file, write out a contents entry.
1784 */
1785 int depth, leaf = TRUE;
1786
1787 /*
1788 * Determine the depth of this file in the contents
1789 * tree.
1790 *
1791 * If the file contains no sections, it is assumed to
1792 * have depth zero.
1793 */
1794 depth = 0;
1795 if (f->first)
1796 for (a = f->first->parent; a && a->type != TOP; a = a->parent)
1797 depth++;
1798
1799 /*
1800 * Determine if this file is a leaf file, by
1801 * trawling the section list to see if there's any
1802 * section with an ancestor in this file but which
1803 * is not itself in this file.
1804 *
1805 * Special case: for contents purposes, the TOP
1806 * file is not considered to be the parent of the
1807 * chapter files, so it's always a leaf.
1808 *
1809 * A file with no sections in it is also a leaf.
1810 */
1811 if (f->first && f->first->type != TOP) {
1812 for (s = f->first; s; s = s->next) {
1813 htmlsect *a;
1814
1815 if (leaf && s->file != f) {
1816 for (a = s; a; a = a->parent)
1817 if (a->file == f) {
1818 leaf = FALSE;
1819 break;
1820 }
1821 }
1822 }
1823 }
1824
1825 /*
1826 * Now write out our contents entry.
1827 */
1828 while (currdepth < depth) {
1829 fprintf(ho.fp, "<UL>\n");
1830 currdepth++;
1831 }
1832 while (currdepth > depth) {
1833 fprintf(ho.fp, "</UL>\n");
1834 currdepth--;
1835 }
1836 /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */
1837 fprintf(ho.fp, "<LI><OBJECT TYPE=\"text/sitemap\">"
1838 "<PARAM NAME=\"Name\" VALUE=\"");
1839 ho.hacklimit = 255;
1840 if (f->first->title)
1841 html_words(&ho, f->first->title->words, NOTHING,
1842 NULL, keywords, &conf);
1843 else if (f->first->type == INDEX)
1844 html_text(&ho, conf.index_text);
1845 fprintf(ho.fp, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">"
1846 "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n",
1847 f->filename, leaf ? 11 : 1);
1848 }
1849
1850 while (currdepth > 0) {
1851 fprintf(ho.fp, "</UL>\n");
1852 currdepth--;
1853 }
1854
1855 fprintf(ho.fp, "</UL></BODY></HTML>\n");
1856
1857 cleanup(&ho);
1858 }
1859 if (hhk_filename) {
1860 htmlfile *f;
1861 htmloutput ho;
1862 indexentry *entry;
1863 int i;
1864
1865 /*
1866 * First make a pass over all HTML files and set their
1867 * `temp' fields to zero, because we're about to use them.
1868 */
1869 for (f = files.head; f; f = f->next)
1870 f->temp = 0;
1871
1872 ho.fp = fopen(hhk_filename, "w");
1873 if (!ho.fp)
1874 error(err_cantopenw, hhk_filename);
1875
1876 ho.charset = CS_CP1252; /* as far as I know, HHK files are */
1877 ho.restrict_charset = CS_CP1252; /* hardwired to this charset */
1878 ho.cstate = charset_init_state;
1879 ho.ver = HTML_4; /* *shrug* */
1880 ho.state = HO_NEUTRAL;
1881 ho.contents_level = 0;
1882 ho.hackflags = HO_HACK_QUOTEQUOTES;
1883
1884 /*
1885 * Magic DOCTYPE which seems to work for .HHK files. I'm
1886 * wary of trying to change it!
1887 */
1888 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1889 "<HTML><HEAD>\n"
1890 "<META HTTP-EQUIV=\"Content-Type\" "
1891 "CONTENT=\"text/html; charset=%s\">\n"
1892 "</HEAD><BODY><UL>\n",
1893 charset_to_mimeenc(conf.output_charset));
1894
1895 /*
1896 * Go through the index terms and output each one.
1897 */
1898 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
1899 htmlindex *hi = (htmlindex *)entry->backend_data;
1900 int j;
1901
1902 if (hi->nrefs > 0) {
1903 fprintf(ho.fp, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
1904 "<PARAM NAME=\"Name\" VALUE=\"");
1905 ho.hacklimit = 255;
1906 html_words(&ho, entry->text, NOTHING,
1907 NULL, keywords, &conf);
1908 fprintf(ho.fp, "\">\n");
1909
1910 for (j = 0; j < hi->nrefs; j++) {
1911 htmlindexref *hr =
1912 (htmlindexref *)hi->refs[j]->private_data;
1913
1914 /*
1915 * Use the temp field to ensure we don't
1916 * reference the same file more than once.
1917 */
1918 if (!hr->section->file->temp) {
1919 fprintf(ho.fp, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n",
1920 hr->section->file->filename);
1921 hr->section->file->temp = 1;
1922 }
1923
1924 hr->referenced = TRUE;
1925 }
1926
1927 fprintf(ho.fp, "</OBJECT>\n");
1928
1929 /*
1930 * Now go through those files and re-clear the temp
1931 * fields ready for the _next_ index term.
1932 */
1933 for (j = 0; j < hi->nrefs; j++) {
1934 htmlindexref *hr =
1935 (htmlindexref *)hi->refs[j]->private_data;
1936 hr->section->file->temp = 0;
1937 }
1938 }
1939 }
1940
1941 fprintf(ho.fp, "</UL></BODY></HTML>\n");
1942 cleanup(&ho);
1943 }
1944
1945 /*
1946 * Go through and check that no index fragments were referenced
1947 * without being generated, or indeed vice versa.
1948 *
1949 * (When I actually get round to freeing everything, this can
1950 * probably be the freeing loop as well.)
1951 */
1952 for (p = sourceform; p; p = p->next) {
1953 word *w;
1954 for (w = p->words; w; w = w->next)
1955 if (w->type == word_IndexRef) {
1956 htmlindexref *hr = (htmlindexref *)w->private_data;
1957
1958 assert(!hr->referenced == !hr->generated);
1959 }
1960 }
1961
1962 /*
1963 * Free all the working data.
1964 */
1965 {
1966 htmlfragment *frag;
1967 while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) {
1968 /*
1969 * frag->fragment is dynamically allocated, but will be
1970 * freed when we process the htmlsect structure which
1971 * it is attached to.
1972 */
1973 sfree(frag);
1974 }
1975 freetree234(files.frags);
1976 }
1977 /*
1978 * The strings in files.files are all owned by their containing
1979 * htmlfile structures, so there's no need to free them here.
1980 */
1981 freetree234(files.files);
1982 {
1983 htmlsect *sect, *tmp;
1984 sect = sects.head;
1985 while (sect) {
1986 int i;
1987 tmp = sect->next;
1988 for (i=0; i < conf.ntfragments; i++)
1989 sfree(sect->fragments[i]);
1990 sfree(sect->fragments);
1991 sfree(sect);
1992 sect = tmp;
1993 }
1994 sect = nonsects.head;
1995 while (sect) {
1996 int i;
1997 tmp = sect->next;
1998 for (i=0; i < conf.ntfragments; i++)
1999 sfree(sect->fragments[i]);
2000 sfree(sect->fragments);
2001 sfree(sect);
2002 sect = tmp;
2003 }
2004 }
2005 {
2006 htmlfile *file, *tmp;
2007 file = files.head;
2008 while (file) {
2009 tmp = file->next;
2010 sfree(file->filename);
2011 sfree(file);
2012 file = tmp;
2013 }
2014 }
2015 {
2016 int i;
2017 indexentry *entry;
2018 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
2019 htmlindex *hi = (htmlindex *)entry->backend_data;
2020 sfree(hi);
2021 }
2022 }
2023 {
2024 paragraph *p;
2025 word *w;
2026 for (p = sourceform; p; p = p->next)
2027 for (w = p->words; w; w = w->next)
2028 if (w->type == word_IndexRef) {
2029 htmlindexref *hr = (htmlindexref *)w->private_data;
2030 assert(hr != NULL);
2031 sfree(hr->fragment);
2032 sfree(hr);
2033 }
2034 }
2035 sfree(conf.asect);
2036 sfree(conf.single_filename);
2037 sfree(conf.contents_filename);
2038 sfree(conf.index_filename);
2039 sfree(conf.template_filename);
2040 while (conf.ntfragments--)
2041 sfree(conf.template_fragments[conf.ntfragments]);
2042 sfree(conf.template_fragments);
2043 }
2044
2045 static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
2046 htmlsect *sect, int depth)
2047 {
2048 htmlfile *file;
2049 int ldepth;
2050
2051 /*
2052 * `depth' is derived from the heading_depth() macro at the top
2053 * of this file, which counts title as -1, chapter as 0,
2054 * heading as 1 and subsection as 2. However, the semantics of
2055 * cfg->leaf_level are defined to count chapter as 1, heading
2056 * as 2 etc. So first I increment depth :-(
2057 */
2058 ldepth = depth + 1;
2059
2060 if (cfg->leaf_level == 0) {
2061 /*
2062 * leaf_level==0 is a special case, in which everything is
2063 * put into a single file.
2064 */
2065 if (!files->single)
2066 files->single = html_new_file(files, cfg->single_filename);
2067
2068 file = files->single;
2069 } else {
2070 /*
2071 * If the depth of this section is at or above leaf_level,
2072 * we invent a fresh file and put this section at its head.
2073 * Otherwise, we put it in the same file as its parent
2074 * section.
2075 *
2076 * Another special value of cfg->leaf_level is -1, which
2077 * means infinity (i.e. it's considered to always be
2078 * greater than depth).
2079 */
2080 if (cfg->leaf_level > 0 && ldepth > cfg->leaf_level) {
2081 /*
2082 * We know that sect->parent cannot be NULL. The only
2083 * circumstance in which it can be is if sect is at
2084 * chapter or appendix level, i.e. ldepth==1; and if
2085 * that's the case, then we cannot have entered this
2086 * branch unless cfg->leaf_level==0, in which case we
2087 * would be in the single-file case above and not here
2088 * at all.
2089 */
2090 assert(sect->parent);
2091
2092 file = sect->parent->file;
2093 } else {
2094 if (sect->type == TOP) {
2095 file = html_new_file(files, cfg->contents_filename);
2096 } else if (sect->type == INDEX) {
2097 file = html_new_file(files, cfg->index_filename);
2098 } else {
2099 char *title;
2100
2101 assert(ldepth > 0 && sect->title);
2102 title = html_format(sect->title, cfg->template_filename);
2103 file = html_new_file(files, title);
2104 sfree(title);
2105 }
2106 }
2107 }
2108
2109 sect->file = file;
2110
2111 if (file->min_heading_depth > depth) {
2112 /*
2113 * This heading is at a higher level than any heading we
2114 * have so far placed in this file; so we set the `first'
2115 * pointer.
2116 */
2117 file->min_heading_depth = depth;
2118 file->first = sect;
2119 }
2120
2121 if (file->min_heading_depth == depth)
2122 file->last = sect;
2123 }
2124
2125 static htmlfile *html_new_file(htmlfilelist *list, char *filename)
2126 {
2127 htmlfile *ret = snew(htmlfile);
2128
2129 ret->next = NULL;
2130 if (list->tail)
2131 list->tail->next = ret;
2132 else
2133 list->head = ret;
2134 list->tail = ret;
2135
2136 ret->filename = html_sanitise_filename(list, dupstr(filename));
2137 add234(list->files, ret->filename);
2138 ret->last_fragment_number = 0;
2139 ret->min_heading_depth = INT_MAX;
2140 ret->first = ret->last = NULL;
2141
2142 return ret;
2143 }
2144
2145 static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
2146 htmlconfig *cfg)
2147 {
2148 htmlsect *ret = snew(htmlsect);
2149
2150 ret->next = NULL;
2151 if (list->tail)
2152 list->tail->next = ret;
2153 else
2154 list->head = ret;
2155 list->tail = ret;
2156
2157 ret->title = title;
2158 ret->file = NULL;
2159 ret->parent = NULL;
2160 ret->type = NORMAL;
2161
2162 ret->fragments = snewn(cfg->ntfragments, char *);
2163 {
2164 int i;
2165 for (i=0; i < cfg->ntfragments; i++)
2166 ret->fragments[i] = NULL;
2167 }
2168
2169 return ret;
2170 }
2171
2172 static void html_words(htmloutput *ho, word *words, int flags,
2173 htmlfile *file, keywordlist *keywords, htmlconfig *cfg)
2174 {
2175 word *w;
2176 char *c;
2177 int style, type;
2178
2179 for (w = words; w; w = w->next) switch (w->type) {
2180 case word_HyperLink:
2181 if (flags & LINKS) {
2182 element_open(ho, "a");
2183 c = utoa_dup(w->text, CS_ASCII);
2184 element_attr(ho, "href", c);
2185 sfree(c);
2186 }
2187 break;
2188 case word_UpperXref:
2189 case word_LowerXref:
2190 if (flags & LINKS) {
2191 keyword *kwl = kw_lookup(keywords, w->text);
2192 paragraph *p;
2193 htmlsect *s;
2194
2195 assert(kwl);
2196 p = kwl->para;
2197 s = (htmlsect *)p->private_data;
2198
2199 assert(s);
2200
2201 html_href(ho, file, s->file, s->fragments[0]);
2202 }
2203 break;
2204 case word_HyperEnd:
2205 case word_XrefEnd:
2206 if (flags & LINKS)
2207 element_close(ho, "a");
2208 break;
2209 case word_IndexRef:
2210 if (flags & INDEXENTS) {
2211 htmlindexref *hr = (htmlindexref *)w->private_data;
2212 html_fragment(ho, hr->fragment);
2213 hr->generated = TRUE;
2214 }
2215 break;
2216 case word_Normal:
2217 case word_Emph:
2218 case word_Code:
2219 case word_WeakCode:
2220 case word_WhiteSpace:
2221 case word_EmphSpace:
2222 case word_CodeSpace:
2223 case word_WkCodeSpace:
2224 case word_Quote:
2225 case word_EmphQuote:
2226 case word_CodeQuote:
2227 case word_WkCodeQuote:
2228 style = towordstyle(w->type);
2229 type = removeattr(w->type);
2230 if (style == word_Emph &&
2231 (attraux(w->aux) == attr_First ||
2232 attraux(w->aux) == attr_Only) &&
2233 (flags & MARKUP))
2234 element_open(ho, "em");
2235 else if ((style == word_Code || style == word_WeakCode) &&
2236 (attraux(w->aux) == attr_First ||
2237 attraux(w->aux) == attr_Only) &&
2238 (flags & MARKUP))
2239 element_open(ho, "code");
2240
2241 if (type == word_WhiteSpace)
2242 html_text(ho, L" ");
2243 else if (type == word_Quote) {
2244 if (quoteaux(w->aux) == quote_Open)
2245 html_text(ho, cfg->lquote);
2246 else
2247 html_text(ho, cfg->rquote);
2248 } else {
2249 if (!w->alt || cvt_ok(ho->restrict_charset, w->text))
2250 html_text_nbsp(ho, w->text);
2251 else
2252 html_words(ho, w->alt, flags, file, keywords, cfg);
2253 }
2254
2255 if (style == word_Emph &&
2256 (attraux(w->aux) == attr_Last ||
2257 attraux(w->aux) == attr_Only) &&
2258 (flags & MARKUP))
2259 element_close(ho, "em");
2260 else if ((style == word_Code || style == word_WeakCode) &&
2261 (attraux(w->aux) == attr_Last ||
2262 attraux(w->aux) == attr_Only) &&
2263 (flags & MARKUP))
2264 element_close(ho, "code");
2265
2266 break;
2267 }
2268 }
2269
2270 static void html_codepara(htmloutput *ho, word *words)
2271 {
2272 element_open(ho, "pre");
2273 element_open(ho, "code");
2274 for (; words; words = words->next) if (words->type == word_WeakCode) {
2275 char *open_tag;
2276 wchar_t *t, *e;
2277
2278 t = words->text;
2279 if (words->next && words->next->type == word_Emph) {
2280 e = words->next->text;
2281 words = words->next;
2282 } else
2283 e = NULL;
2284
2285 while (e && *e && *t) {
2286 int n;
2287 int ec = *e;
2288
2289 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
2290
2291 open_tag = NULL;
2292 if (ec == 'i')
2293 open_tag = "em";
2294 else if (ec == 'b')
2295 open_tag = "b";
2296 if (open_tag)
2297 element_open(ho, open_tag);
2298
2299 html_text_limit(ho, t, n);
2300
2301 if (open_tag)
2302 element_close(ho, open_tag);
2303
2304 t += n;
2305 e += n;
2306 }
2307 html_text(ho, t);
2308 html_nl(ho);
2309 }
2310 element_close(ho, "code");
2311 element_close(ho, "pre");
2312 }
2313
2314 static void html_charset_cleanup(htmloutput *ho)
2315 {
2316 char outbuf[256];
2317 int bytes;
2318
2319 bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf),
2320 ho->charset, &ho->cstate, NULL);
2321 if (ho->fp && bytes > 0)
2322 fwrite(outbuf, 1, bytes, ho->fp);
2323 }
2324
2325 static void return_mostly_to_neutral(htmloutput *ho)
2326 {
2327 if (ho->fp) {
2328 if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) {
2329 fprintf(ho->fp, " />");
2330 } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) {
2331 fprintf(ho->fp, ">");
2332 }
2333 }
2334
2335 ho->state = HO_NEUTRAL;
2336 }
2337
2338 static void return_to_neutral(htmloutput *ho)
2339 {
2340 if (ho->state == HO_IN_TEXT) {
2341 html_charset_cleanup(ho);
2342 }
2343
2344 return_mostly_to_neutral(ho);
2345 }
2346
2347 static void element_open(htmloutput *ho, char const *name)
2348 {
2349 return_to_neutral(ho);
2350 if (ho->fp)
2351 fprintf(ho->fp, "<%s", name);
2352 ho->state = HO_IN_TAG;
2353 }
2354
2355 static void element_close(htmloutput *ho, char const *name)
2356 {
2357 return_to_neutral(ho);
2358 if (ho->fp)
2359 fprintf(ho->fp, "</%s>", name);
2360 ho->state = HO_NEUTRAL;
2361 }
2362
2363 static void element_empty(htmloutput *ho, char const *name)
2364 {
2365 return_to_neutral(ho);
2366 if (ho->fp)
2367 fprintf(ho->fp, "<%s", name);
2368 ho->state = HO_IN_EMPTY_TAG;
2369 }
2370
2371 static void html_nl(htmloutput *ho)
2372 {
2373 return_to_neutral(ho);
2374 if (ho->fp)
2375 fputc('\n', ho->fp);
2376 }
2377
2378 static void html_raw(htmloutput *ho, char *text)
2379 {
2380 return_to_neutral(ho);
2381 if (ho->fp)
2382 fputs(text, ho->fp);
2383 }
2384
2385 static void html_raw_as_attr(htmloutput *ho, char *text)
2386 {
2387 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
2388 if (ho->fp) {
2389 fputc(' ', ho->fp);
2390 fputs(text, ho->fp);
2391 }
2392 }
2393
2394 static void element_attr(htmloutput *ho, char const *name, char const *value)
2395 {
2396 html_charset_cleanup(ho);
2397 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
2398 if (ho->fp)
2399 fprintf(ho->fp, " %s=\"%s\"", name, value);
2400 }
2401
2402 static void element_attr_w(htmloutput *ho, char const *name,
2403 wchar_t const *value)
2404 {
2405 html_charset_cleanup(ho);
2406 if (ho->fp)
2407 fprintf(ho->fp, " %s=\"", name);
2408 html_text_limit_internal(ho, value, 0, TRUE, FALSE);
2409 html_charset_cleanup(ho);
2410 if (ho->fp)
2411 fputc('"', ho->fp);
2412 }
2413
2414 static void html_text(htmloutput *ho, wchar_t const *text)
2415 {
2416 return_mostly_to_neutral(ho);
2417 html_text_limit_internal(ho, text, 0, FALSE, FALSE);
2418 }
2419
2420 static void html_text_nbsp(htmloutput *ho, wchar_t const *text)
2421 {
2422 return_mostly_to_neutral(ho);
2423 html_text_limit_internal(ho, text, 0, FALSE, TRUE);
2424 }
2425
2426 static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen)
2427 {
2428 return_mostly_to_neutral(ho);
2429 html_text_limit_internal(ho, text, maxlen, FALSE, FALSE);
2430 }
2431
2432 static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
2433 int maxlen, int quote_quotes, int nbsp)
2434 {
2435 int textlen = ustrlen(text);
2436 char outbuf[256];
2437 int bytes, err;
2438
2439 if (ho->hackflags & (HO_HACK_QUOTEQUOTES | HO_HACK_OMITQUOTES))
2440 quote_quotes = TRUE; /* override the input value */
2441
2442 if (maxlen > 0 && textlen > maxlen)
2443 textlen = maxlen;
2444 if (ho->hacklimit >= 0) {
2445 if (textlen > ho->hacklimit)
2446 textlen = ho->hacklimit;
2447 ho->hacklimit -= textlen;
2448 }
2449
2450 while (textlen > 0) {
2451 /* Scan ahead for characters we really can't display in HTML. */
2452 int lenbefore, lenafter;
2453 for (lenbefore = 0; lenbefore < textlen; lenbefore++)
2454 if (text[lenbefore] == L'<' ||
2455 text[lenbefore] == L'>' ||
2456 text[lenbefore] == L'&' ||
2457 (text[lenbefore] == L'"' && quote_quotes) ||
2458 (text[lenbefore] == L' ' && nbsp))
2459 break;
2460 lenafter = lenbefore;
2461 bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf),
2462 ho->charset, &ho->cstate, &err);
2463 textlen -= (lenbefore - lenafter);
2464 if (bytes > 0 && ho->fp)
2465 fwrite(outbuf, 1, bytes, ho->fp);
2466 if (err) {
2467 /*
2468 * We have encountered a character that cannot be
2469 * displayed in the selected output charset. Therefore,
2470 * we use an HTML numeric entity reference.
2471 */
2472 assert(textlen > 0);
2473 if (ho->fp)
2474 fprintf(ho->fp, "&#%ld;", (long int)*text);
2475 text++, textlen--;
2476 } else if (lenafter == 0 && textlen > 0) {
2477 /*
2478 * We have encountered a character which is special to
2479 * HTML.
2480 */
2481 if (ho->fp) {
2482 if (*text == L'"' && (ho->hackflags & HO_HACK_OMITQUOTES)) {
2483 fputc('\'', ho->fp);
2484 } else if (ho->hackflags & HO_HACK_QUOTENOTHING) {
2485 fputc(*text, ho->fp);
2486 } else {
2487 if (*text == L'<')
2488 fprintf(ho->fp, "&lt;");
2489 else if (*text == L'>')
2490 fprintf(ho->fp, "&gt;");
2491 else if (*text == L'&')
2492 fprintf(ho->fp, "&amp;");
2493 else if (*text == L'"')
2494 fprintf(ho->fp, "&quot;");
2495 else if (*text == L' ') {
2496 assert(nbsp);
2497 fprintf(ho->fp, "&nbsp;");
2498 } else
2499 assert(!"Can't happen");
2500 }
2501 }
2502 text++, textlen--;
2503 }
2504 }
2505 }
2506
2507 static void cleanup(htmloutput *ho)
2508 {
2509 return_to_neutral(ho);
2510 if (ho->fp)
2511 fclose(ho->fp);
2512 }
2513
2514 static void html_href(htmloutput *ho, htmlfile *thisfile,
2515 htmlfile *targetfile, char *targetfrag)
2516 {
2517 rdstringc rs = { 0, 0, NULL };
2518 char *url;
2519
2520 if (targetfile != thisfile)
2521 rdaddsc(&rs, targetfile->filename);
2522 if (targetfrag) {
2523 rdaddc(&rs, '#');
2524 rdaddsc(&rs, targetfrag);
2525 }
2526 url = rs.text;
2527
2528 element_open(ho, "a");
2529 element_attr(ho, "href", url);
2530 sfree(url);
2531 }
2532
2533 static void html_fragment(htmloutput *ho, char const *fragment)
2534 {
2535 element_open(ho, "a");
2536 element_attr(ho, "name", fragment);
2537 if (is_xhtml(ho->ver))
2538 element_attr(ho, "id", fragment);
2539 element_close(ho, "a");
2540 }
2541
2542 static char *html_format(paragraph *p, char *template_string)
2543 {
2544 char *c, *t;
2545 word *w;
2546 wchar_t *ws, wsbuf[2];
2547 rdstringc rs = { 0, 0, NULL };
2548
2549 t = template_string;
2550 while (*t) {
2551 if (*t == '%' && t[1]) {
2552 int fmt;
2553
2554 t++;
2555 fmt = *t++;
2556
2557 if (fmt == '%') {
2558 rdaddc(&rs, fmt);
2559 continue;
2560 }
2561
2562 w = NULL;
2563 ws = NULL;
2564
2565 if (p->kwtext && fmt == 'n')
2566 w = p->kwtext;
2567 else if (p->kwtext2 && fmt == 'b') {
2568 /*
2569 * HTML fragment names must start with a letter, so
2570 * simply `1.2.3' is not adequate. In this case I'm
2571 * going to cheat slightly by prepending the first
2572 * character of the first word of kwtext, so that
2573 * we get `C1' for chapter 1, `S2.3' for section
2574 * 2.3 etc.
2575 */
2576 if (p->kwtext && p->kwtext->text[0]) {
2577 ws = wsbuf;
2578 wsbuf[1] = '\0';
2579 wsbuf[0] = p->kwtext->text[0];
2580 }
2581 w = p->kwtext2;
2582 } else if (p->keyword && *p->keyword && fmt == 'k')
2583 ws = p->keyword;
2584 else
2585 /* %N comes here; also failure cases of other fmts */
2586 w = p->words;
2587
2588 if (ws) {
2589 c = utoa_dup(ws, CS_ASCII);
2590 rdaddsc(&rs,c);
2591 sfree(c);
2592 }
2593
2594 while (w) {
2595 if (removeattr(w->type) == word_Normal) {
2596 c = utoa_dup(w->text, CS_ASCII);
2597 rdaddsc(&rs,c);
2598 sfree(c);
2599 }
2600 w = w->next;
2601 }
2602 } else {
2603 rdaddc(&rs, *t++);
2604 }
2605 }
2606
2607 return rdtrimc(&rs);
2608 }
2609
2610 static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
2611 char *text)
2612 {
2613 /*
2614 * The HTML 4 spec's strictest definition of fragment names (<a
2615 * name> and "id" attributes) says that they `must begin with a
2616 * letter and may be followed by any number of letters, digits,
2617 * hyphens, underscores, colons, and periods'.
2618 *
2619 * So here we unceremoniously rip out any characters not
2620 * conforming to this limitation.
2621 */
2622 char *p = text, *q = text;
2623
2624 while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z')))
2625 p++;
2626 if ((*q++ = *p++) != '\0') {
2627 while (*p) {
2628 if ((*p>='A' && *p<='Z') ||
2629 (*p>='a' && *p<='z') ||
2630 (*p>='0' && *p<='9') ||
2631 *p=='-' || *p=='_' || *p==':' || *p=='.')
2632 *q++ = *p;
2633 p++;
2634 }
2635
2636 *q = '\0';
2637 }
2638
2639 /* If there's nothing left, make something valid up */
2640 if (!*text) {
2641 static const char anonfrag[] = "anon";
2642 text = sresize(text, lenof(anonfrag), char);
2643 strcpy(text, anonfrag);
2644 }
2645
2646 /*
2647 * Now we check for clashes with other fragment names, and
2648 * adjust this one if necessary by appending a hyphen followed
2649 * by a number.
2650 */
2651 {
2652 htmlfragment *frag = snew(htmlfragment);
2653 int len = 0; /* >0 indicates we have resized */
2654 int suffix = 1;
2655
2656 frag->file = file;
2657 frag->fragment = text;
2658
2659 while (add234(files->frags, frag) != frag) {
2660 if (!len) {
2661 len = strlen(text);
2662 frag->fragment = text = sresize(text, len+20, char);
2663 }
2664
2665 sprintf(text + len, "-%d", ++suffix);
2666 }
2667 }
2668
2669 return text;
2670 }
2671
2672 static char *html_sanitise_filename(htmlfilelist *files, char *text)
2673 {
2674 /*
2675 * Unceremoniously rip out any character that might cause
2676 * difficulty in some filesystem or another, or be otherwise
2677 * inconvenient.
2678 *
2679 * That doesn't leave much punctuation. I permit alphanumerics
2680 * and +-.=_ only.
2681 */
2682 char *p = text, *q = text;
2683
2684 while (*p) {
2685 if ((*p>='A' && *p<='Z') ||
2686 (*p>='a' && *p<='z') ||
2687 (*p>='0' && *p<='9') ||
2688 *p=='-' || *p=='_' || *p=='+' || *p=='.' || *p=='=')
2689 *q++ = *p;
2690 p++;
2691 }
2692 *q = '\0';
2693
2694 /* If there's nothing left, make something valid up */
2695 if (!*text) {
2696 static const char anonfrag[] = "anon.html";
2697 text = sresize(text, lenof(anonfrag), char);
2698 strcpy(text, anonfrag);
2699 }
2700
2701 /*
2702 * Now we check for clashes with other filenames, and adjust
2703 * this one if necessary by appending a hyphen followed by a
2704 * number just before the file extension (if any).
2705 */
2706 {
2707 int len, extpos;
2708 int suffix = 1;
2709
2710 p = NULL;
2711
2712 while (find234(files->files, text, NULL)) {
2713 if (!p) {
2714 len = strlen(text);
2715 p = text;
2716 text = snewn(len+20, char);
2717
2718 for (extpos = len; extpos > 0 && p[extpos-1] != '.'; extpos--);
2719 if (extpos > 0)
2720 extpos--;
2721 else
2722 extpos = len;
2723 }
2724
2725 sprintf(text, "%.*s-%d%s", extpos, p, ++suffix, p+extpos);
2726 }
2727
2728 if (p)
2729 sfree(p);
2730 }
2731
2732 return text;
2733 }
2734
2735 static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
2736 htmlfile *thisfile, keywordlist *keywords,
2737 htmlconfig *cfg)
2738 {
2739 if (ho->contents_level >= depth && ho->contents_level > 0) {
2740 element_close(ho, "li");
2741 html_nl(ho);
2742 }
2743
2744 while (ho->contents_level > depth) {
2745 element_close(ho, "ul");
2746 ho->contents_level--;
2747 if (ho->contents_level > 0) {
2748 element_close(ho, "li");
2749 }
2750 html_nl(ho);
2751 }
2752
2753 while (ho->contents_level < depth) {
2754 html_nl(ho);
2755 element_open(ho, "ul");
2756 html_nl(ho);
2757 ho->contents_level++;
2758 }
2759
2760 if (!s)
2761 return;
2762
2763 element_open(ho, "li");
2764 html_href(ho, thisfile, s->file, s->fragments[0]);
2765 html_section_title(ho, s, thisfile, keywords, cfg, FALSE);
2766 element_close(ho, "a");
2767 /* <li> will be closed by a later invocation */
2768 }
2769
2770 static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile,
2771 keywordlist *keywords, htmlconfig *cfg,
2772 int real)
2773 {
2774 if (s->title) {
2775 sectlevel *sl;
2776 word *number;
2777 int depth = heading_depth(s->title);
2778
2779 if (depth < 0)
2780 sl = NULL;
2781 else if (depth == 0)
2782 sl = &cfg->achapter;
2783 else if (depth <= cfg->nasect)
2784 sl = &cfg->asect[depth-1];
2785 else
2786 sl = &cfg->asect[cfg->nasect-1];
2787
2788 if (!sl || !sl->number_at_all)
2789 number = NULL;
2790 else if (sl->just_numbers)
2791 number = s->title->kwtext2;
2792 else
2793 number = s->title->kwtext;
2794
2795 if (number) {
2796 html_words(ho, number, MARKUP,
2797 thisfile, keywords, cfg);
2798 html_text(ho, sl->number_suffix);
2799 }
2800
2801 html_words(ho, s->title->words, real ? ALL : MARKUP,
2802 thisfile, keywords, cfg);
2803 } else {
2804 assert(s->type != NORMAL);
2805 /*
2806 * If we're printing the full document title for _real_ and
2807 * there isn't one, we don't want to print `Preamble' at
2808 * the top of what ought to just be some text. If we need
2809 * it in any other context such as TOCs, we need to print
2810 * `Preamble'.
2811 */
2812 if (s->type == TOP && !real)
2813 html_text(ho, cfg->preamble_text);
2814 else if (s->type == INDEX)
2815 html_text(ho, cfg->index_text);
2816 }
2817 }