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