Tweak the HTML Help output to enable Previous/Next navigation in the generated
[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.
376db5ee 1583 *
1584 * A good unofficial reference for these is <http://chmspec.nongnu.org/>.
f2ef00b5 1585 */
1586 if (conf.hhp_filename) {
1587 htmlfile *f;
1588 htmloutput ho;
1589
1590 ho.charset = CS_CP1252; /* as far as I know, HHP files are */
1591 ho.restrict_charset = CS_CP1252; /* hardwired to this charset */
1592 ho.cstate = charset_init_state;
1593 ho.ver = HTML_4; /* *shrug* */
1594 ho.state = HO_NEUTRAL;
1595 ho.contents_level = 0;
1596 ho.hackflags = HO_HACK_QUOTENOTHING;
1597
1598 ho.fp = fopen(conf.hhp_filename, "w");
1599 if (!ho.fp)
1600 error(err_cantopenw, conf.hhp_filename);
1601
1602 fprintf(ho.fp,
1603 "[OPTIONS]\n"
376db5ee 1604 /* Binary TOC required for Next/Previous nav to work */
1605 "Binary TOC=Yes\n"
f2ef00b5 1606 "Compatibility=1.1 or later\n"
1607 "Compiled file=%s\n"
1608 "Default Window=main\n"
1609 "Default topic=%s\n"
1610 "Display compile progress=Yes\n"
1611 "Full-text search=Yes\n"
1612 "Title=", conf.chm_filename, files.head->filename);
1613
1614 ho.hacklimit = 255;
1615 html_words(&ho, topsect->title->words, NOTHING,
1616 NULL, keywords, &conf);
1617
1618 fprintf(ho.fp, "\n");
1619
1620 /*
1621 * These two entries don't seem to be remotely necessary
1622 * for a successful run of the help _compiler_, but
1623 * omitting them causes the GUI Help Workshop to behave
1624 * rather strangely if you try to load the help project
1625 * into that and edit it.
1626 */
1627 if (conf.hhc_filename)
1628 fprintf(ho.fp, "Contents file=%s\n", conf.hhc_filename);
1629 if (hhk_filename)
1630 fprintf(ho.fp, "Index file=%s\n", hhk_filename);
1631
1632 fprintf(ho.fp, "\n[WINDOWS]\nmain=\"");
1633
1634 ho.hackflags |= HO_HACK_OMITQUOTES;
1635 ho.hacklimit = 255;
1636 html_words(&ho, topsect->title->words, NOTHING,
1637 NULL, keywords, &conf);
1638
1639 fprintf(ho.fp, "\",\"%s\",\"%s\",\"%s\",,,,,,"
376db5ee 1640 /* This first magic number is fsWinProperties, controlling
1641 * Navigation Pane options and the like.
1642 * Constants HHWIN_PROP_* in htmlhelp.h. */
1643 "0x62520,,"
1644 /* This second number is fsToolBarFlags, mainly controlling
1645 * toolbar buttons. Constants HHWIN_BUTTON_*.
1646 * NOTE: there are two pairs of bits for Next/Previous
1647 * buttons: 7/8 (which do nothing useful), and 21/22
1648 * (which work). (Neither of these are exposed in the HHW
1649 * UI, but they work fine in HH.) We use the latter. */
1650 "0x60304e,,,,,,,,0\n",
f2ef00b5 1651 conf.hhc_filename ? conf.hhc_filename : "",
1652 hhk_filename ? hhk_filename : "",
1653 files.head->filename);
1654
1655 /*
1656 * The [FILES] section is also not necessary for
1657 * compilation (hhc appears to build up a list of needed
1658 * files just by following links from the given starting
1659 * points), but useful for loading the project into HHW.
1660 */
1661 fprintf(ho.fp, "\n[FILES]\n");
1662 for (f = files.head; f; f = f->next)
1663 fprintf(ho.fp, "%s\n", f->filename);
1664
1665 fclose(ho.fp);
1666 }
1667 if (conf.hhc_filename) {
1668 htmlfile *f;
1669 htmlsect *s, *a;
1670 htmloutput ho;
1671 int currdepth = 0;
1672
1673 ho.fp = fopen(conf.hhc_filename, "w");
1674 if (!ho.fp)
1675 error(err_cantopenw, conf.hhc_filename);
1676
1677 ho.charset = CS_CP1252; /* as far as I know, HHC files are */
1678 ho.restrict_charset = CS_CP1252; /* hardwired to this charset */
1679 ho.cstate = charset_init_state;
1680 ho.ver = HTML_4; /* *shrug* */
1681 ho.state = HO_NEUTRAL;
1682 ho.contents_level = 0;
1683 ho.hackflags = HO_HACK_QUOTEQUOTES;
1684
1685 /*
1686 * Magic DOCTYPE which seems to work for .HHC files. I'm
1687 * wary of trying to change it!
1688 */
1689 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1690 "<HTML><HEAD>\n"
1691 "<META HTTP-EQUIV=\"Content-Type\" "
1692 "CONTENT=\"text/html; charset=%s\">\n"
1693 "</HEAD><BODY><UL>\n",
1694 charset_to_mimeenc(conf.output_charset));
1695
1696 for (f = files.head; f; f = f->next) {
1697 /*
1698 * For each HTML file, write out a contents entry.
1699 */
1700 int depth, leaf = TRUE;
1701
1702 /*
1703 * Determine the depth of this file in the contents
1704 * tree.
1705 *
1706 * If the file contains no sections, it is assumed to
1707 * have depth zero.
1708 */
1709 depth = 0;
1710 if (f->first)
1711 for (a = f->first->parent; a && a->type != TOP; a = a->parent)
1712 depth++;
1713
1714 /*
1715 * Determine if this file is a leaf file, by
1716 * trawling the section list to see if there's any
1717 * section with an ancestor in this file but which
1718 * is not itself in this file.
1719 *
1720 * Special case: for contents purposes, the TOP
1721 * file is not considered to be the parent of the
1722 * chapter files, so it's always a leaf.
1723 *
1724 * A file with no sections in it is also a leaf.
1725 */
1726 if (f->first && f->first->type != TOP) {
1727 for (s = f->first; s; s = s->next) {
1728 htmlsect *a;
1729
1730 if (leaf && s->file != f) {
1731 for (a = s; a; a = a->parent)
1732 if (a->file == f) {
1733 leaf = FALSE;
1734 break;
1735 }
1736 }
1737 }
1738 }
1739
1740 /*
1741 * Now write out our contents entry.
1742 */
1743 while (currdepth < depth) {
1744 fprintf(ho.fp, "<UL>\n");
1745 currdepth++;
1746 }
1747 while (currdepth > depth) {
1748 fprintf(ho.fp, "</UL>\n");
1749 currdepth--;
1750 }
1751 /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */
1752 fprintf(ho.fp, "<LI><OBJECT TYPE=\"text/sitemap\">"
1753 "<PARAM NAME=\"Name\" VALUE=\"");
1754 ho.hacklimit = 255;
1755 if (f->first->title)
1756 html_words(&ho, f->first->title->words, NOTHING,
1757 NULL, keywords, &conf);
1758 else if (f->first->type == INDEX)
1759 html_text(&ho, conf.index_text);
1760 fprintf(ho.fp, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">"
1761 "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n",
1762 f->filename, leaf ? 11 : 1);
1763 }
1764
1765 while (currdepth > 0) {
1766 fprintf(ho.fp, "</UL>\n");
1767 currdepth--;
1768 }
1769
1770 fprintf(ho.fp, "</UL></BODY></HTML>\n");
1771
1772 cleanup(&ho);
1773 }
1774 if (hhk_filename) {
1775 htmlfile *f;
1776 htmloutput ho;
1777 indexentry *entry;
1778 int i;
1779
1780 /*
1781 * First make a pass over all HTML files and set their
1782 * `temp' fields to zero, because we're about to use them.
1783 */
1784 for (f = files.head; f; f = f->next)
1785 f->temp = 0;
1786
1787 ho.fp = fopen(hhk_filename, "w");
1788 if (!ho.fp)
1789 error(err_cantopenw, hhk_filename);
1790
1791 ho.charset = CS_CP1252; /* as far as I know, HHK files are */
1792 ho.restrict_charset = CS_CP1252; /* hardwired to this charset */
1793 ho.cstate = charset_init_state;
1794 ho.ver = HTML_4; /* *shrug* */
1795 ho.state = HO_NEUTRAL;
1796 ho.contents_level = 0;
1797 ho.hackflags = HO_HACK_QUOTEQUOTES;
1798
1799 /*
1800 * Magic DOCTYPE which seems to work for .HHK files. I'm
1801 * wary of trying to change it!
1802 */
1803 fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1804 "<HTML><HEAD>\n"
1805 "<META HTTP-EQUIV=\"Content-Type\" "
1806 "CONTENT=\"text/html; charset=%s\">\n"
1807 "</HEAD><BODY><UL>\n",
1808 charset_to_mimeenc(conf.output_charset));
1809
1810 /*
1811 * Go through the index terms and output each one.
1812 */
1813 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
1814 htmlindex *hi = (htmlindex *)entry->backend_data;
1815 int j;
1816
1817 if (hi->nrefs > 0) {
1818 fprintf(ho.fp, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
1819 "<PARAM NAME=\"Name\" VALUE=\"");
1820 ho.hacklimit = 255;
1821 html_words(&ho, entry->text, NOTHING,
1822 NULL, keywords, &conf);
1823 fprintf(ho.fp, "\">\n");
1824
1825 for (j = 0; j < hi->nrefs; j++) {
1826 htmlindexref *hr =
1827 (htmlindexref *)hi->refs[j]->private_data;
1828
1829 /*
1830 * Use the temp field to ensure we don't
1831 * reference the same file more than once.
1832 */
1833 if (!hr->section->file->temp) {
1834 fprintf(ho.fp, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n",
1835 hr->section->file->filename);
1836 hr->section->file->temp = 1;
1837 }
1838
1839 hr->referenced = TRUE;
1840 }
1841
1842 fprintf(ho.fp, "</OBJECT>\n");
1843
1844 /*
1845 * Now go through those files and re-clear the temp
1846 * fields ready for the _next_ index term.
1847 */
1848 for (j = 0; j < hi->nrefs; j++) {
1849 htmlindexref *hr =
1850 (htmlindexref *)hi->refs[j]->private_data;
1851 hr->section->file->temp = 0;
1852 }
1853 }
1854 }
1855
1856 fprintf(ho.fp, "</UL></BODY></HTML>\n");
1857 cleanup(&ho);
1858 }
1859
1860 /*
1b7bf715 1861 * Go through and check that no index fragments were referenced
1862 * without being generated, or indeed vice versa.
1863 *
1864 * (When I actually get round to freeing everything, this can
1865 * probably be the freeing loop as well.)
1866 */
1867 for (p = sourceform; p; p = p->next) {
1868 word *w;
1869 for (w = p->words; w; w = w->next)
1870 if (w->type == word_IndexRef) {
1871 htmlindexref *hr = (htmlindexref *)w->private_data;
1872
1873 assert(!hr->referenced == !hr->generated);
1874 }
1875 }
1876
1877 /*
529a6c83 1878 * Free all the working data.
78c73085 1879 */
529a6c83 1880 {
1881 htmlfragment *frag;
1882 while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) {
1883 /*
1884 * frag->fragment is dynamically allocated, but will be
1885 * freed when we process the htmlsect structure which
1886 * it is attached to.
1887 */
1888 sfree(frag);
1889 }
1890 freetree234(files.frags);
1891 }
f2ef00b5 1892 /*
1893 * The strings in files.files are all owned by their containing
1894 * htmlfile structures, so there's no need to free them here.
1895 */
1896 freetree234(files.files);
529a6c83 1897 {
1898 htmlsect *sect, *tmp;
1899 sect = sects.head;
1900 while (sect) {
12f0ee84 1901 int i;
529a6c83 1902 tmp = sect->next;
12f0ee84 1903 for (i=0; i < conf.ntfragments; i++)
1904 sfree(sect->fragments[i]);
1905 sfree(sect->fragments);
529a6c83 1906 sfree(sect);
1907 sect = tmp;
1908 }
1909 sect = nonsects.head;
1910 while (sect) {
12f0ee84 1911 int i;
529a6c83 1912 tmp = sect->next;
12f0ee84 1913 for (i=0; i < conf.ntfragments; i++)
1914 sfree(sect->fragments[i]);
1915 sfree(sect->fragments);
529a6c83 1916 sfree(sect);
1917 sect = tmp;
1918 }
1919 }
1920 {
1921 htmlfile *file, *tmp;
1922 file = files.head;
1923 while (file) {
1924 tmp = file->next;
1925 sfree(file->filename);
1926 sfree(file);
1927 file = tmp;
1928 }
1929 }
1930 {
1931 int i;
1932 indexentry *entry;
1933 for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
1934 htmlindex *hi = (htmlindex *)entry->backend_data;
1935 sfree(hi);
1936 }
1937 }
1938 {
1939 paragraph *p;
1940 word *w;
1941 for (p = sourceform; p; p = p->next)
1942 for (w = p->words; w; w = w->next)
1943 if (w->type == word_IndexRef) {
1944 htmlindexref *hr = (htmlindexref *)w->private_data;
1945 assert(hr != NULL);
1946 sfree(hr->fragment);
1947 sfree(hr);
1948 }
1949 }
12f0ee84 1950 sfree(conf.asect);
1951 sfree(conf.single_filename);
1952 sfree(conf.contents_filename);
1953 sfree(conf.index_filename);
1954 sfree(conf.template_filename);
1955 while (conf.ntfragments--)
1956 sfree(conf.template_fragments[conf.ntfragments]);
1957 sfree(conf.template_fragments);
78c73085 1958}
1959
1960static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
1961 htmlsect *sect, int depth)
1962{
1963 htmlfile *file;
1964 int ldepth;
1965
1966 /*
1967 * `depth' is derived from the heading_depth() macro at the top
1968 * of this file, which counts title as -1, chapter as 0,
1969 * heading as 1 and subsection as 2. However, the semantics of
1970 * cfg->leaf_level are defined to count chapter as 1, heading
1971 * as 2 etc. So first I increment depth :-(
1972 */
1973 ldepth = depth + 1;
1974
1975 if (cfg->leaf_level == 0) {
1976 /*
1977 * leaf_level==0 is a special case, in which everything is
1978 * put into a single file.
1979 */
1980 if (!files->single)
1981 files->single = html_new_file(files, cfg->single_filename);
1982
1983 file = files->single;
1984 } else {
1985 /*
1986 * If the depth of this section is at or above leaf_level,
1987 * we invent a fresh file and put this section at its head.
1988 * Otherwise, we put it in the same file as its parent
1989 * section.
f2ef00b5 1990 *
1991 * Another special value of cfg->leaf_level is -1, which
1992 * means infinity (i.e. it's considered to always be
1993 * greater than depth).
78c73085 1994 */
f2ef00b5 1995 if (cfg->leaf_level > 0 && ldepth > cfg->leaf_level) {
78c73085 1996 /*
1997 * We know that sect->parent cannot be NULL. The only
1998 * circumstance in which it can be is if sect is at
1999 * chapter or appendix level, i.e. ldepth==1; and if
2000 * that's the case, then we cannot have entered this
2001 * branch unless cfg->leaf_level==0, in which case we
2002 * would be in the single-file case above and not here
2003 * at all.
2004 */
2005 assert(sect->parent);
2006
2007 file = sect->parent->file;
2008 } else {
2009 if (sect->type == TOP) {
2010 file = html_new_file(files, cfg->contents_filename);
2011 } else if (sect->type == INDEX) {
2012 file = html_new_file(files, cfg->index_filename);
2013 } else {
2014 char *title;
2015
2016 assert(ldepth > 0 && sect->title);
2017 title = html_format(sect->title, cfg->template_filename);
2018 file = html_new_file(files, title);
2019 sfree(title);
2020 }
2021 }
2022 }
2023
2024 sect->file = file;
2025
2026 if (file->min_heading_depth > depth) {
2027 /*
2028 * This heading is at a higher level than any heading we
2029 * have so far placed in this file; so we set the `first'
2030 * pointer.
2031 */
2032 file->min_heading_depth = depth;
2033 file->first = sect;
2034 }
2035
2036 if (file->min_heading_depth == depth)
2037 file->last = sect;
2038}
2039
2040static htmlfile *html_new_file(htmlfilelist *list, char *filename)
2041{
f1530049 2042 htmlfile *ret = snew(htmlfile);
78c73085 2043
2044 ret->next = NULL;
2045 if (list->tail)
2046 list->tail->next = ret;
2047 else
2048 list->head = ret;
2049 list->tail = ret;
2050
f2ef00b5 2051 ret->filename = html_sanitise_filename(list, dupstr(filename));
2052 add234(list->files, ret->filename);
78c73085 2053 ret->last_fragment_number = 0;
2054 ret->min_heading_depth = INT_MAX;
2055 ret->first = ret->last = NULL;
2056
2057 return ret;
2058}
2059
12f0ee84 2060static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
2061 htmlconfig *cfg)
78c73085 2062{
f1530049 2063 htmlsect *ret = snew(htmlsect);
78c73085 2064
2065 ret->next = NULL;
2066 if (list->tail)
2067 list->tail->next = ret;
2068 else
2069 list->head = ret;
2070 list->tail = ret;
2071
2072 ret->title = title;
2073 ret->file = NULL;
2074 ret->parent = NULL;
2075 ret->type = NORMAL;
2076
12f0ee84 2077 ret->fragments = snewn(cfg->ntfragments, char *);
2078 {
2079 int i;
2080 for (i=0; i < cfg->ntfragments; i++)
2081 ret->fragments[i] = NULL;
2082 }
2083
78c73085 2084 return ret;
2085}
2086
2087static void html_words(htmloutput *ho, word *words, int flags,
2088 htmlfile *file, keywordlist *keywords, htmlconfig *cfg)
2089{
2090 word *w;
2091 char *c;
2092 int style, type;
2093
2094 for (w = words; w; w = w->next) switch (w->type) {
2095 case word_HyperLink:
2096 if (flags & LINKS) {
2097 element_open(ho, "a");
2098 c = utoa_dup(w->text, CS_ASCII);
2099 element_attr(ho, "href", c);
2100 sfree(c);
2101 }
2102 break;
2103 case word_UpperXref:
2104 case word_LowerXref:
2105 if (flags & LINKS) {
2106 keyword *kwl = kw_lookup(keywords, w->text);
2b0986a3 2107 paragraph *p;
2108 htmlsect *s;
2109
2110 assert(kwl);
2111 p = kwl->para;
2112 s = (htmlsect *)p->private_data;
78c73085 2113
2114 assert(s);
2115
12f0ee84 2116 html_href(ho, file, s->file, s->fragments[0]);
78c73085 2117 }
2118 break;
2119 case word_HyperEnd:
2120 case word_XrefEnd:
2121 if (flags & LINKS)
2122 element_close(ho, "a");
2123 break;
2124 case word_IndexRef:
2125 if (flags & INDEXENTS) {
2126 htmlindexref *hr = (htmlindexref *)w->private_data;
27bdc5ab 2127 html_fragment(ho, hr->fragment);
1b7bf715 2128 hr->generated = TRUE;
78c73085 2129 }
2130 break;
2131 case word_Normal:
2132 case word_Emph:
2133 case word_Code:
2134 case word_WeakCode:
2135 case word_WhiteSpace:
2136 case word_EmphSpace:
2137 case word_CodeSpace:
2138 case word_WkCodeSpace:
2139 case word_Quote:
2140 case word_EmphQuote:
2141 case word_CodeQuote:
2142 case word_WkCodeQuote:
2143 style = towordstyle(w->type);
2144 type = removeattr(w->type);
2145 if (style == word_Emph &&
2146 (attraux(w->aux) == attr_First ||
2147 attraux(w->aux) == attr_Only) &&
2148 (flags & MARKUP))
2149 element_open(ho, "em");
2150 else if ((style == word_Code || style == word_WeakCode) &&
2151 (attraux(w->aux) == attr_First ||
2152 attraux(w->aux) == attr_Only) &&
2153 (flags & MARKUP))
2154 element_open(ho, "code");
2155
2156 if (type == word_WhiteSpace)
2157 html_text(ho, L" ");
2158 else if (type == word_Quote) {
2159 if (quoteaux(w->aux) == quote_Open)
2160 html_text(ho, cfg->lquote);
2161 else
2162 html_text(ho, cfg->rquote);
2163 } else {
35b123a0 2164 if (!w->alt || cvt_ok(ho->restrict_charset, w->text))
2165 html_text_nbsp(ho, w->text);
78c73085 2166 else
2167 html_words(ho, w->alt, flags, file, keywords, cfg);
2168 }
2169
2170 if (style == word_Emph &&
2171 (attraux(w->aux) == attr_Last ||
2172 attraux(w->aux) == attr_Only) &&
2173 (flags & MARKUP))
2174 element_close(ho, "em");
2175 else if ((style == word_Code || style == word_WeakCode) &&
2176 (attraux(w->aux) == attr_Last ||
2177 attraux(w->aux) == attr_Only) &&
2178 (flags & MARKUP))
2179 element_close(ho, "code");
2180
2181 break;
2182 }
2183}
2184
2185static void html_codepara(htmloutput *ho, word *words)
2186{
2187 element_open(ho, "pre");
2188 element_open(ho, "code");
2189 for (; words; words = words->next) if (words->type == word_WeakCode) {
2190 char *open_tag;
2191 wchar_t *t, *e;
2192
2193 t = words->text;
2194 if (words->next && words->next->type == word_Emph) {
2195 e = words->next->text;
2196 words = words->next;
2197 } else
2198 e = NULL;
2199
2200 while (e && *e && *t) {
2201 int n;
2202 int ec = *e;
2203
2204 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
2205
2206 open_tag = NULL;
2207 if (ec == 'i')
2208 open_tag = "em";
2209 else if (ec == 'b')
2210 open_tag = "b";
2211 if (open_tag)
2212 element_open(ho, open_tag);
2213
2214 html_text_limit(ho, t, n);
2215
2216 if (open_tag)
2217 element_close(ho, open_tag);
2218
2219 t += n;
2220 e += n;
2221 }
2222 html_text(ho, t);
2223 html_nl(ho);
2224 }
2225 element_close(ho, "code");
2226 element_close(ho, "pre");
2227}
2228
2229static void html_charset_cleanup(htmloutput *ho)
2230{
2231 char outbuf[256];
2232 int bytes;
2233
2234 bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf),
2235 ho->charset, &ho->cstate, NULL);
3ae196b4 2236 if (ho->fp && bytes > 0)
78c73085 2237 fwrite(outbuf, 1, bytes, ho->fp);
2238}
2239
35b123a0 2240static void return_mostly_to_neutral(htmloutput *ho)
78c73085 2241{
3ae196b4 2242 if (ho->fp) {
2243 if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) {
2244 fprintf(ho->fp, " />");
2245 } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) {
2246 fprintf(ho->fp, ">");
2247 }
78c73085 2248 }
2249
2250 ho->state = HO_NEUTRAL;
2251}
2252
35b123a0 2253static void return_to_neutral(htmloutput *ho)
2254{
2255 if (ho->state == HO_IN_TEXT) {
2256 html_charset_cleanup(ho);
2257 }
2258
2259 return_mostly_to_neutral(ho);
2260}
2261
78c73085 2262static void element_open(htmloutput *ho, char const *name)
2263{
2264 return_to_neutral(ho);
3ae196b4 2265 if (ho->fp)
2266 fprintf(ho->fp, "<%s", name);
78c73085 2267 ho->state = HO_IN_TAG;
2268}
2269
2270static void element_close(htmloutput *ho, char const *name)
2271{
2272 return_to_neutral(ho);
3ae196b4 2273 if (ho->fp)
2274 fprintf(ho->fp, "</%s>", name);
78c73085 2275 ho->state = HO_NEUTRAL;
2276}
2277
2278static void element_empty(htmloutput *ho, char const *name)
2279{
2280 return_to_neutral(ho);
3ae196b4 2281 if (ho->fp)
2282 fprintf(ho->fp, "<%s", name);
78c73085 2283 ho->state = HO_IN_EMPTY_TAG;
2284}
2285
2286static void html_nl(htmloutput *ho)
2287{
2288 return_to_neutral(ho);
3ae196b4 2289 if (ho->fp)
2290 fputc('\n', ho->fp);
78c73085 2291}
2292
2293static void html_raw(htmloutput *ho, char *text)
2294{
2295 return_to_neutral(ho);
3ae196b4 2296 if (ho->fp)
2297 fputs(text, ho->fp);
78c73085 2298}
2299
2300static void html_raw_as_attr(htmloutput *ho, char *text)
2301{
2302 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
3ae196b4 2303 if (ho->fp) {
2304 fputc(' ', ho->fp);
2305 fputs(text, ho->fp);
2306 }
78c73085 2307}
2308
2309static void element_attr(htmloutput *ho, char const *name, char const *value)
2310{
2311 html_charset_cleanup(ho);
2312 assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG);
3ae196b4 2313 if (ho->fp)
2314 fprintf(ho->fp, " %s=\"%s\"", name, value);
78c73085 2315}
2316
2317static void element_attr_w(htmloutput *ho, char const *name,
2318 wchar_t const *value)
2319{
2320 html_charset_cleanup(ho);
3ae196b4 2321 if (ho->fp)
2322 fprintf(ho->fp, " %s=\"", name);
35b123a0 2323 html_text_limit_internal(ho, value, 0, TRUE, FALSE);
78c73085 2324 html_charset_cleanup(ho);
3ae196b4 2325 if (ho->fp)
2326 fputc('"', ho->fp);
78c73085 2327}
2328
2329static void html_text(htmloutput *ho, wchar_t const *text)
2330{
35b123a0 2331 return_mostly_to_neutral(ho);
2332 html_text_limit_internal(ho, text, 0, FALSE, FALSE);
2333}
2334
2335static void html_text_nbsp(htmloutput *ho, wchar_t const *text)
2336{
2337 return_mostly_to_neutral(ho);
2338 html_text_limit_internal(ho, text, 0, FALSE, TRUE);
78c73085 2339}
2340
2341static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen)
2342{
35b123a0 2343 return_mostly_to_neutral(ho);
2344 html_text_limit_internal(ho, text, maxlen, FALSE, FALSE);
78c73085 2345}
2346
2347static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
35b123a0 2348 int maxlen, int quote_quotes, int nbsp)
78c73085 2349{
2350 int textlen = ustrlen(text);
2351 char outbuf[256];
2352 int bytes, err;
2353
f2ef00b5 2354 if (ho->hackflags & (HO_HACK_QUOTEQUOTES | HO_HACK_OMITQUOTES))
2355 quote_quotes = TRUE; /* override the input value */
2356
78c73085 2357 if (maxlen > 0 && textlen > maxlen)
2358 textlen = maxlen;
f2ef00b5 2359 if (ho->hacklimit >= 0) {
2360 if (textlen > ho->hacklimit)
2361 textlen = ho->hacklimit;
2362 ho->hacklimit -= textlen;
2363 }
78c73085 2364
2365 while (textlen > 0) {
2366 /* Scan ahead for characters we really can't display in HTML. */
2367 int lenbefore, lenafter;
2368 for (lenbefore = 0; lenbefore < textlen; lenbefore++)
2369 if (text[lenbefore] == L'<' ||
2370 text[lenbefore] == L'>' ||
2371 text[lenbefore] == L'&' ||
35b123a0 2372 (text[lenbefore] == L'"' && quote_quotes) ||
2373 (text[lenbefore] == L' ' && nbsp))
78c73085 2374 break;
2375 lenafter = lenbefore;
2376 bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf),
2377 ho->charset, &ho->cstate, &err);
2378 textlen -= (lenbefore - lenafter);
3ae196b4 2379 if (bytes > 0 && ho->fp)
78c73085 2380 fwrite(outbuf, 1, bytes, ho->fp);
2381 if (err) {
2382 /*
2383 * We have encountered a character that cannot be
2384 * displayed in the selected output charset. Therefore,
2385 * we use an HTML numeric entity reference.
2386 */
2387 assert(textlen > 0);
3ae196b4 2388 if (ho->fp)
2389 fprintf(ho->fp, "&#%ld;", (long int)*text);
78c73085 2390 text++, textlen--;
2391 } else if (lenafter == 0 && textlen > 0) {
2392 /*
2393 * We have encountered a character which is special to
2394 * HTML.
2395 */
3ae196b4 2396 if (ho->fp) {
f2ef00b5 2397 if (*text == L'"' && (ho->hackflags & HO_HACK_OMITQUOTES)) {
2398 fputc('\'', ho->fp);
2399 } else if (ho->hackflags & HO_HACK_QUOTENOTHING) {
2400 fputc(*text, ho->fp);
2401 } else {
2402 if (*text == L'<')
2403 fprintf(ho->fp, "&lt;");
2404 else if (*text == L'>')
2405 fprintf(ho->fp, "&gt;");
2406 else if (*text == L'&')
2407 fprintf(ho->fp, "&amp;");
2408 else if (*text == L'"')
2409 fprintf(ho->fp, "&quot;");
2410 else if (*text == L' ') {
2411 assert(nbsp);
2412 fprintf(ho->fp, "&nbsp;");
2413 } else
2414 assert(!"Can't happen");
2415 }
3ae196b4 2416 }
78c73085 2417 text++, textlen--;
2418 }
2419 }
2420}
2421
2422static void cleanup(htmloutput *ho)
2423{
2424 return_to_neutral(ho);
3ae196b4 2425 if (ho->fp)
2426 fclose(ho->fp);
78c73085 2427}
2428
2429static void html_href(htmloutput *ho, htmlfile *thisfile,
2430 htmlfile *targetfile, char *targetfrag)
2431{
2432 rdstringc rs = { 0, 0, NULL };
2433 char *url;
2434
2435 if (targetfile != thisfile)
2436 rdaddsc(&rs, targetfile->filename);
2437 if (targetfrag) {
2438 rdaddc(&rs, '#');
2439 rdaddsc(&rs, targetfrag);
2440 }
2441 url = rs.text;
2442
2443 element_open(ho, "a");
2444 element_attr(ho, "href", url);
2445 sfree(url);
2446}
2447
27bdc5ab 2448static void html_fragment(htmloutput *ho, char const *fragment)
2449{
2450 element_open(ho, "a");
2451 element_attr(ho, "name", fragment);
2452 if (is_xhtml(ho->ver))
2453 element_attr(ho, "id", fragment);
2454 element_close(ho, "a");
2455}
2456
78c73085 2457static char *html_format(paragraph *p, char *template_string)
2458{
2459 char *c, *t;
2460 word *w;
2461 wchar_t *ws, wsbuf[2];
2462 rdstringc rs = { 0, 0, NULL };
2463
2464 t = template_string;
2465 while (*t) {
2466 if (*t == '%' && t[1]) {
2467 int fmt;
2468
2469 t++;
2470 fmt = *t++;
2471
2472 if (fmt == '%') {
2473 rdaddc(&rs, fmt);
2474 continue;
2475 }
2476
2477 w = NULL;
2478 ws = NULL;
2479
2480 if (p->kwtext && fmt == 'n')
2481 w = p->kwtext;
2482 else if (p->kwtext2 && fmt == 'b') {
2483 /*
2484 * HTML fragment names must start with a letter, so
2485 * simply `1.2.3' is not adequate. In this case I'm
2486 * going to cheat slightly by prepending the first
2487 * character of the first word of kwtext, so that
2488 * we get `C1' for chapter 1, `S2.3' for section
2489 * 2.3 etc.
2490 */
2491 if (p->kwtext && p->kwtext->text[0]) {
2492 ws = wsbuf;
2493 wsbuf[1] = '\0';
2494 wsbuf[0] = p->kwtext->text[0];
2495 }
2496 w = p->kwtext2;
2497 } else if (p->keyword && *p->keyword && fmt == 'k')
2498 ws = p->keyword;
2499 else
46c7302f 2500 /* %N comes here; also failure cases of other fmts */
78c73085 2501 w = p->words;
2502
2503 if (ws) {
2504 c = utoa_dup(ws, CS_ASCII);
2505 rdaddsc(&rs,c);
2506 sfree(c);
2507 }
2508
2509 while (w) {
2510 if (removeattr(w->type) == word_Normal) {
2511 c = utoa_dup(w->text, CS_ASCII);
2512 rdaddsc(&rs,c);
2513 sfree(c);
2514 }
2515 w = w->next;
2516 }
2517 } else {
2518 rdaddc(&rs, *t++);
2519 }
2520 }
2521
2522 return rdtrimc(&rs);
2523}
2524
3e82de8f 2525static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
2526 char *text)
78c73085 2527{
2528 /*
2529 * The HTML 4 spec's strictest definition of fragment names (<a
2530 * name> and "id" attributes) says that they `must begin with a
2531 * letter and may be followed by any number of letters, digits,
2532 * hyphens, underscores, colons, and periods'.
2533 *
2534 * So here we unceremoniously rip out any characters not
2535 * conforming to this limitation.
2536 */
2537 char *p = text, *q = text;
2538
2539 while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z')))
2540 p++;
3e82de8f 2541 if ((*q++ = *p++) != '\0') {
2542 while (*p) {
2543 if ((*p>='A' && *p<='Z') ||
2544 (*p>='a' && *p<='z') ||
2545 (*p>='0' && *p<='9') ||
2546 *p=='-' || *p=='_' || *p==':' || *p=='.')
2547 *q++ = *p;
2548 p++;
2549 }
2550
2551 *q = '\0';
2552 }
2553
12f0ee84 2554 /* If there's nothing left, make something valid up */
2555 if (!*text) {
0bac815b 2556 static const char anonfrag[] = "anon";
12f0ee84 2557 text = sresize(text, lenof(anonfrag), char);
2558 strcpy(text, anonfrag);
2559 }
2560
3e82de8f 2561 /*
2562 * Now we check for clashes with other fragment names, and
2563 * adjust this one if necessary by appending a hyphen followed
2564 * by a number.
2565 */
2566 {
2567 htmlfragment *frag = snew(htmlfragment);
2568 int len = 0; /* >0 indicates we have resized */
2569 int suffix = 1;
2570
2571 frag->file = file;
2572 frag->fragment = text;
2573
2574 while (add234(files->frags, frag) != frag) {
2575 if (!len) {
2576 len = strlen(text);
2577 frag->fragment = text = sresize(text, len+20, char);
2578 }
2579
2580 sprintf(text + len, "-%d", ++suffix);
2581 }
78c73085 2582 }
f2ef00b5 2583
2584 return text;
2585}
2586
2587static char *html_sanitise_filename(htmlfilelist *files, char *text)
2588{
2589 /*
2590 * Unceremoniously rip out any character that might cause
2591 * difficulty in some filesystem or another, or be otherwise
2592 * inconvenient.
2593 *
2594 * That doesn't leave much punctuation. I permit alphanumerics
2595 * and +-.=_ only.
2596 */
2597 char *p = text, *q = text;
2598
2599 while (*p) {
2600 if ((*p>='A' && *p<='Z') ||
2601 (*p>='a' && *p<='z') ||
2602 (*p>='0' && *p<='9') ||
2603 *p=='-' || *p=='_' || *p=='+' || *p=='.' || *p=='=')
2604 *q++ = *p;
2605 p++;
2606 }
2607 *q = '\0';
2608
2609 /* If there's nothing left, make something valid up */
2610 if (!*text) {
2611 static const char anonfrag[] = "anon.html";
2612 text = sresize(text, lenof(anonfrag), char);
2613 strcpy(text, anonfrag);
2614 }
2615
2616 /*
2617 * Now we check for clashes with other filenames, and adjust
2618 * this one if necessary by appending a hyphen followed by a
2619 * number just before the file extension (if any).
2620 */
2621 {
2622 int len, extpos;
2623 int suffix = 1;
2624
2625 p = NULL;
2626
2627 while (find234(files->files, text, NULL)) {
2628 if (!p) {
2629 len = strlen(text);
2630 p = text;
2631 text = snewn(len+20, char);
2632
2633 for (extpos = len; extpos > 0 && p[extpos-1] != '.'; extpos--);
2634 if (extpos > 0)
2635 extpos--;
2636 else
2637 extpos = len;
2638 }
2639
2640 sprintf(text, "%.*s-%d%s", extpos, p, ++suffix, p+extpos);
2641 }
2642
2643 if (p)
2644 sfree(p);
2645 }
78c73085 2646
3e82de8f 2647 return text;
78c73085 2648}
2649
2650static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
2651 htmlfile *thisfile, keywordlist *keywords,
2652 htmlconfig *cfg)
2653{
f5dee642 2654 if (ho->contents_level >= depth && ho->contents_level > 0) {
2655 element_close(ho, "li");
2656 html_nl(ho);
2657 }
2658
78c73085 2659 while (ho->contents_level > depth) {
2660 element_close(ho, "ul");
2661 ho->contents_level--;
f5dee642 2662 if (ho->contents_level > 0) {
2663 element_close(ho, "li");
2664 }
2665 html_nl(ho);
78c73085 2666 }
2667
2668 while (ho->contents_level < depth) {
f5dee642 2669 html_nl(ho);
78c73085 2670 element_open(ho, "ul");
f5dee642 2671 html_nl(ho);
78c73085 2672 ho->contents_level++;
2673 }
2674
2675 if (!s)
2676 return;
2677
2678 element_open(ho, "li");
12f0ee84 2679 html_href(ho, thisfile, s->file, s->fragments[0]);
23c9bbc2 2680 html_section_title(ho, s, thisfile, keywords, cfg, FALSE);
78c73085 2681 element_close(ho, "a");
f5dee642 2682 /* <li> will be closed by a later invocation */
78c73085 2683}
2684
2685static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile,
23c9bbc2 2686 keywordlist *keywords, htmlconfig *cfg,
2687 int real)
78c73085 2688{
2689 if (s->title) {
2690 sectlevel *sl;
2691 word *number;
2692 int depth = heading_depth(s->title);
2693
2694 if (depth < 0)
2695 sl = NULL;
2696 else if (depth == 0)
2697 sl = &cfg->achapter;
2698 else if (depth <= cfg->nasect)
2699 sl = &cfg->asect[depth-1];
2700 else
2701 sl = &cfg->asect[cfg->nasect-1];
2702
2703 if (!sl)
2704 number = NULL;
2705 else if (sl->just_numbers)
2706 number = s->title->kwtext2;
2707 else
2708 number = s->title->kwtext;
2709
2710 if (number) {
2711 html_words(ho, number, MARKUP,
2712 thisfile, keywords, cfg);
2713 html_text(ho, sl->number_suffix);
2714 }
2715
23c9bbc2 2716 html_words(ho, s->title->words, real ? ALL : MARKUP,
78c73085 2717 thisfile, keywords, cfg);
2718 } else {
2719 assert(s->type != NORMAL);
56a99eb6 2720 /*
2721 * If we're printing the full document title for _real_ and
2722 * there isn't one, we don't want to print `Preamble' at
2723 * the top of what ought to just be some text. If we need
2724 * it in any other context such as TOCs, we need to print
2725 * `Preamble'.
2726 */
2727 if (s->type == TOP && !real)
2728 html_text(ho, cfg->preamble_text);
78c73085 2729 else if (s->type == INDEX)
56a99eb6 2730 html_text(ho, cfg->index_text);
78c73085 2731 }
2732}