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