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