2 * HTML backend for Halibut
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.)
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.
28 #define is_heading_type(type) ( (type) == para_Title || \
29 (type) == para_Chapter || \
30 (type) == para_Appendix || \
31 (type) == para_UnnumberedChapter || \
32 (type) == para_Heading || \
33 (type) == para_Subsect)
35 #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
36 (p)->type == para_Heading ? 1 : \
37 (p)->type == para_Title ? -1 : 0 )
40 int number_at_all
, just_numbers
;
41 wchar_t *number_suffix
;
46 sectlevel achapter
, *asect
;
47 int *contents_depths
; /* 0=main, 1=chapter, 2=sect etc */
49 int address_section
, visible_version_id
;
50 int leaf_contains_contents
, leaf_smallest_contents
;
53 char *contents_filename
;
55 char *template_filename
;
56 char *single_filename
;
57 char *chm_filename
, *hhp_filename
, *hhc_filename
, *hhk_filename
;
58 char **template_fragments
;
60 char *head_end
, *body_start
, *body_end
, *addr_start
, *addr_end
;
61 char *body_tag
, *nav_attr
;
62 wchar_t *author
, *description
;
63 wchar_t *index_text
, *contents_text
, *preamble_text
, *title_separator
;
64 wchar_t *nav_prev_text
, *nav_next_text
, *nav_up_text
, *nav_separator
;
65 wchar_t *index_main_sep
, *index_multi_sep
;
66 wchar_t *pre_versionid
, *post_versionid
;
67 int restrict_charset
, output_charset
;
69 HTML_3_2
, HTML_4
, ISO_HTML
,
70 XHTML_1_0_TRANSITIONAL
, XHTML_1_0_STRICT
72 wchar_t *lquote
, *rquote
;
76 #define contents_depth(conf, level) \
77 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
79 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
81 typedef struct htmlfile htmlfile
;
82 typedef struct htmlsect htmlsect
;
87 int last_fragment_number
;
88 int min_heading_depth
;
89 htmlsect
*first
, *last
; /* first/last highest-level sections */
91 * The `temp' field is available for use in individual passes
92 * over the file list. For example, the HHK index generation
93 * uses it to ensure no index term references the same file
100 htmlsect
*next
, *parent
;
102 paragraph
*title
, *text
;
103 enum { NORMAL
, TOP
, INDEX
} type
;
109 htmlfile
*head
, *tail
;
110 htmlfile
*single
, *index
;
116 htmlsect
*head
, *tail
;
132 int generated
, referenced
;
137 * This level deals with charset conversion, starting and
138 * ending tags, and writing to the file. It's the lexical
142 int charset
, restrict_charset
;
143 charset_state cstate
;
146 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
148 int hackflags
; /* used for icky .HH* stuff */
149 int hacklimit
; /* text size limit, again for .HH* */
151 * Stuff beyond here deals with the higher syntactic level: it
152 * tracks how many levels of <ul> are currently open when
153 * producing a contents list, for example.
159 * Nasty hacks that modify the behaviour of htmloutput files. All
160 * of these are flag bits set in ho.hackflags. HO_HACK_QUOTEQUOTES
161 * has the same effect as the `quote_quotes' parameter to
162 * html_text_limit_internal, except that it's set globally on an
163 * entire htmloutput structure; HO_HACK_QUOTENOTHING suppresses
164 * quoting of any HTML special characters (for .HHP files);
165 * HO_HACK_OMITQUOTES completely suppresses the generation of
166 * double quotes at all (turning them into single quotes, for want
169 #define HO_HACK_QUOTEQUOTES 1
170 #define HO_HACK_QUOTENOTHING 2
171 #define HO_HACK_OMITQUOTES 4
173 static int html_fragment_compare(void *av
, void *bv
)
175 htmlfragment
*a
= (htmlfragment
*)av
;
176 htmlfragment
*b
= (htmlfragment
*)bv
;
179 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
182 return strcmp(a
->fragment
, b
->fragment
);
185 static int html_filename_compare(void *av
, void *bv
)
187 char *a
= (char *)av
;
188 char *b
= (char *)bv
;
193 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
194 htmlsect
*sect
, int depth
);
196 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
197 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
200 /* Flags for html_words() flags parameter */
204 #define INDEXENTS 0x04
206 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
207 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
208 static void html_codepara(htmloutput
*ho
, word
*words
);
210 static void element_open(htmloutput
*ho
, char const *name
);
211 static void element_close(htmloutput
*ho
, char const *name
);
212 static void element_empty(htmloutput
*ho
, char const *name
);
213 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
214 static void element_attr_w(htmloutput
*ho
, char const *name
,
215 wchar_t const *value
);
216 static void html_text(htmloutput
*ho
, wchar_t const *str
);
217 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *str
);
218 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
219 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
220 int maxlen
, int quote_quotes
, int nbsp
);
221 static void html_nl(htmloutput
*ho
);
222 static void html_raw(htmloutput
*ho
, char *text
);
223 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
224 static void cleanup(htmloutput
*ho
);
226 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
227 htmlfile
*targetfile
, char *targetfrag
);
228 static void html_fragment(htmloutput
*ho
, char const *fragment
);
230 static char *html_format(paragraph
*p
, char *template_string
);
231 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
233 static char *html_sanitise_filename(htmlfilelist
*files
, char *text
);
235 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
236 htmlfile
*thisfile
, keywordlist
*keywords
,
238 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
239 htmlfile
*thisfile
, keywordlist
*keywords
,
240 htmlconfig
*cfg
, int real
);
242 static htmlconfig
html_configure(paragraph
*source
) {
250 ret
.achapter
.just_numbers
= FALSE
;
251 ret
.achapter
.number_at_all
= TRUE
;
252 ret
.achapter
.number_suffix
= L
": ";
254 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
255 ret
.asect
[0].just_numbers
= TRUE
;
256 ret
.asect
[0].number_at_all
= TRUE
;
257 ret
.asect
[0].number_suffix
= L
" ";
259 ret
.contents_depths
= 0;
260 ret
.visible_version_id
= TRUE
;
261 ret
.address_section
= TRUE
;
262 ret
.leaf_contains_contents
= FALSE
;
263 ret
.leaf_smallest_contents
= 4;
266 ret
.single_filename
= dupstr("Manual.html");
267 ret
.contents_filename
= dupstr("Contents.html");
268 ret
.index_filename
= dupstr("IndexPage.html");
269 ret
.template_filename
= dupstr("%n.html");
270 ret
.chm_filename
= ret
.hhp_filename
= NULL
;
271 ret
.hhc_filename
= ret
.hhk_filename
= NULL
;
273 ret
.template_fragments
= snewn(ret
.ntfragments
, char *);
274 ret
.template_fragments
[0] = dupstr("%b");
275 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
276 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
277 ret
.author
= ret
.description
= NULL
;
278 ret
.restrict_charset
= CS_UTF8
;
279 ret
.output_charset
= CS_ASCII
;
280 ret
.htmlver
= HTML_4
;
281 ret
.index_text
= L
"Index";
282 ret
.contents_text
= L
"Contents";
283 ret
.preamble_text
= L
"Preamble";
284 ret
.title_separator
= L
" - ";
285 ret
.nav_prev_text
= L
"Previous";
286 ret
.nav_next_text
= L
"Next";
287 ret
.nav_up_text
= L
"Up";
288 ret
.nav_separator
= L
" | ";
289 ret
.index_main_sep
= L
": ";
290 ret
.index_multi_sep
= L
", ";
291 ret
.pre_versionid
= L
"[";
292 ret
.post_versionid
= L
"]";
294 * Default quote characters are Unicode matched single quotes,
295 * falling back to ordinary ASCII ".
297 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
298 ret
.rquote
= uadv(ret
.lquote
);
301 * Two-pass configuration so that we can pick up global config
302 * (e.g. `quotes') before having it overridden by specific
303 * config (`html-quotes'), irrespective of the order in which
306 for (p
= source
; p
; p
= p
->next
) {
307 if (p
->type
== para_Config
) {
308 if (!ustricmp(p
->keyword
, L
"quotes")) {
309 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
310 ret
.lquote
= uadv(p
->keyword
);
311 ret
.rquote
= uadv(ret
.lquote
);
313 } else if (!ustricmp(p
->keyword
, L
"index")) {
314 ret
.index_text
= uadv(p
->keyword
);
315 } else if (!ustricmp(p
->keyword
, L
"contents")) {
316 ret
.contents_text
= uadv(p
->keyword
);
321 for (p
= source
; p
; p
= p
->next
) {
322 if (p
->type
== para_Config
) {
323 wchar_t *k
= p
->keyword
;
325 if (!ustrnicmp(k
, L
"xhtml-", 6))
326 k
++; /* treat `xhtml-' and `html-' the same */
328 if (!ustricmp(k
, L
"html-restrict-charset")) {
329 ret
.restrict_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
330 } else if (!ustricmp(k
, L
"html-output-charset")) {
331 ret
.output_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
332 } else if (!ustricmp(k
, L
"html-version")) {
333 wchar_t *vername
= uadv(k
);
334 static const struct {
338 {L
"html3.2", HTML_3_2
},
340 {L
"iso-html", ISO_HTML
},
341 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
342 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
346 for (i
= 0; i
< (int)lenof(versions
); i
++)
347 if (!ustricmp(versions
[i
].name
, vername
))
350 if (i
== lenof(versions
))
351 error(err_htmlver
, &p
->fpos
, vername
);
353 ret
.htmlver
= versions
[i
].ver
;
354 } else if (!ustricmp(k
, L
"html-single-filename")) {
355 sfree(ret
.single_filename
);
356 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
357 } else if (!ustricmp(k
, L
"html-contents-filename")) {
358 sfree(ret
.contents_filename
);
359 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
360 } else if (!ustricmp(k
, L
"html-index-filename")) {
361 sfree(ret
.index_filename
);
362 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
363 } else if (!ustricmp(k
, L
"html-template-filename")) {
364 sfree(ret
.template_filename
);
365 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
366 } else if (!ustricmp(k
, L
"html-template-fragment")) {
367 char *frag
= adv(p
->origkeyword
);
369 while (ret
.ntfragments
--)
370 sfree(ret
.template_fragments
[ret
.ntfragments
]);
371 sfree(ret
.template_fragments
);
372 ret
.template_fragments
= NULL
;
376 ret
.template_fragments
=
377 sresize(ret
.template_fragments
,
378 ret
.ntfragments
, char *);
379 ret
.template_fragments
[ret
.ntfragments
-1] =
384 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 1);
385 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
386 ret
.achapter
.just_numbers
= utob(uadv(k
));
387 } else if (!ustricmp(k
, L
"html-chapter-shownumber")) {
388 ret
.achapter
.number_at_all
= utob(uadv(k
));
389 } else if (!ustricmp(k
, L
"html-suppress-navlinks")) {
390 ret
.navlinks
= !utob(uadv(k
));
391 } else if (!ustricmp(k
, L
"html-rellinks")) {
392 ret
.rellinks
= utob(uadv(k
));
393 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
394 ret
.achapter
.number_suffix
= uadv(k
);
395 } else if (!ustricmp(k
, L
"html-leaf-level")) {
396 wchar_t *u
= uadv(k
);
397 if (!ustricmp(u
, L
"infinite") ||
398 !ustricmp(u
, L
"infinity") ||
399 !ustricmp(u
, L
"inf"))
400 ret
.leaf_level
= -1; /* represents infinity */
402 ret
.leaf_level
= utoi(u
);
403 } else if (!ustricmp(k
, L
"html-section-numeric")) {
404 wchar_t *q
= uadv(k
);
410 if (n
>= ret
.nasect
) {
412 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
413 for (i
= ret
.nasect
; i
<= n
; i
++)
414 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
417 ret
.asect
[n
].just_numbers
= utob(q
);
418 } else if (!ustricmp(k
, L
"html-section-shownumber")) {
419 wchar_t *q
= uadv(k
);
425 if (n
>= ret
.nasect
) {
427 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
428 for (i
= ret
.nasect
; i
<= n
; i
++)
429 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
432 ret
.asect
[n
].number_at_all
= utob(q
);
433 } else if (!ustricmp(k
, L
"html-section-suffix")) {
434 wchar_t *q
= uadv(k
);
440 if (n
>= ret
.nasect
) {
442 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
443 for (i
= ret
.nasect
; i
<= n
; i
++) {
444 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
448 ret
.asect
[n
].number_suffix
= q
;
449 } else if (!ustricmp(k
, L
"html-contents-depth") ||
450 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
452 * Relic of old implementation: this directive used
453 * to be written as \cfg{html-contents-depth-3}{2}
454 * rather than the usual Halibut convention of
455 * \cfg{html-contents-depth}{3}{2}. We therefore
458 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
464 if (n
>= ret
.ncdepths
) {
466 ret
.contents_depths
=
467 sresize(ret
.contents_depths
, n
+1, int);
468 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
469 ret
.contents_depths
[i
] = i
+2;
473 ret
.contents_depths
[n
] = utoi(q
);
474 } else if (!ustricmp(k
, L
"html-head-end")) {
475 ret
.head_end
= adv(p
->origkeyword
);
476 } else if (!ustricmp(k
, L
"html-body-tag")) {
477 ret
.body_tag
= adv(p
->origkeyword
);
478 } else if (!ustricmp(k
, L
"html-body-start")) {
479 ret
.body_start
= adv(p
->origkeyword
);
480 } else if (!ustricmp(k
, L
"html-body-end")) {
481 ret
.body_end
= adv(p
->origkeyword
);
482 } else if (!ustricmp(k
, L
"html-address-start")) {
483 ret
.addr_start
= adv(p
->origkeyword
);
484 } else if (!ustricmp(k
, L
"html-address-end")) {
485 ret
.addr_end
= adv(p
->origkeyword
);
486 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
487 ret
.nav_attr
= adv(p
->origkeyword
);
488 } else if (!ustricmp(k
, L
"html-author")) {
489 ret
.author
= uadv(k
);
490 } else if (!ustricmp(k
, L
"html-description")) {
491 ret
.description
= uadv(k
);
492 } else if (!ustricmp(k
, L
"html-suppress-address")) {
493 ret
.address_section
= !utob(uadv(k
));
494 } else if (!ustricmp(k
, L
"html-versionid")) {
495 ret
.visible_version_id
= utob(uadv(k
));
496 } else if (!ustricmp(k
, L
"html-quotes")) {
497 if (*uadv(k
) && *uadv(uadv(k
))) {
498 ret
.lquote
= uadv(k
);
499 ret
.rquote
= uadv(ret
.lquote
);
501 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
502 ret
.leaf_contains_contents
= utob(uadv(k
));
503 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
504 ret
.leaf_smallest_contents
= utoi(uadv(k
));
505 } else if (!ustricmp(k
, L
"html-index-text")) {
506 ret
.index_text
= uadv(k
);
507 } else if (!ustricmp(k
, L
"html-contents-text")) {
508 ret
.contents_text
= uadv(k
);
509 } else if (!ustricmp(k
, L
"html-preamble-text")) {
510 ret
.preamble_text
= uadv(k
);
511 } else if (!ustricmp(k
, L
"html-title-separator")) {
512 ret
.title_separator
= uadv(k
);
513 } else if (!ustricmp(k
, L
"html-nav-prev-text")) {
514 ret
.nav_prev_text
= uadv(k
);
515 } else if (!ustricmp(k
, L
"html-nav-next-text")) {
516 ret
.nav_next_text
= uadv(k
);
517 } else if (!ustricmp(k
, L
"html-nav-up-text")) {
518 ret
.nav_up_text
= uadv(k
);
519 } else if (!ustricmp(k
, L
"html-nav-separator")) {
520 ret
.nav_separator
= uadv(k
);
521 } else if (!ustricmp(k
, L
"html-index-main-separator")) {
522 ret
.index_main_sep
= uadv(k
);
523 } else if (!ustricmp(k
, L
"html-index-multiple-separator")) {
524 ret
.index_multi_sep
= uadv(k
);
525 } else if (!ustricmp(k
, L
"html-pre-versionid")) {
526 ret
.pre_versionid
= uadv(k
);
527 } else if (!ustricmp(k
, L
"html-post-versionid")) {
528 ret
.post_versionid
= uadv(k
);
529 } else if (!ustricmp(k
, L
"html-mshtmlhelp-chm")) {
530 sfree(ret
.chm_filename
);
531 ret
.chm_filename
= dupstr(adv(p
->origkeyword
));
532 } else if (!ustricmp(k
, L
"html-mshtmlhelp-project")) {
533 sfree(ret
.hhp_filename
);
534 ret
.hhp_filename
= dupstr(adv(p
->origkeyword
));
535 } else if (!ustricmp(k
, L
"html-mshtmlhelp-contents")) {
536 sfree(ret
.hhc_filename
);
537 ret
.hhc_filename
= dupstr(adv(p
->origkeyword
));
538 } else if (!ustricmp(k
, L
"html-mshtmlhelp-index")) {
539 sfree(ret
.hhk_filename
);
540 ret
.hhk_filename
= dupstr(adv(p
->origkeyword
));
546 * Enforce that the CHM and HHP filenames must either be both
547 * present or both absent. If one is present but not the other,
550 if (!ret
.chm_filename
^ !ret
.hhp_filename
) {
552 sfree(ret
.chm_filename
); ret
.chm_filename
= NULL
;
553 sfree(ret
.hhp_filename
); ret
.hhp_filename
= NULL
;
556 * And if we're not generating an HHP, there's no need for HHC
559 if (!ret
.hhp_filename
) {
560 sfree(ret
.hhc_filename
); ret
.hhc_filename
= NULL
;
561 sfree(ret
.hhk_filename
); ret
.hhk_filename
= NULL
;
565 * Now process fallbacks on quote characters.
567 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
568 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
569 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
570 ret
.lquote
= uadv(ret
.rquote
);
571 ret
.rquote
= uadv(ret
.lquote
);
577 paragraph
*html_config_filename(char *filename
)
580 * If the user passes in a single filename as a parameter to
581 * the `--html' command-line option, then we should assume it
582 * to imply _two_ config directives:
583 * \cfg{html-single-filename}{whatever} and
584 * \cfg{html-leaf-level}{0}; the rationale being that the user
585 * wants their output _in that file_.
589 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
590 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
595 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
596 indexdata
*idx
, void *unused
)
601 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
602 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
608 conf
= html_configure(sourceform
);
611 * We're going to make heavy use of paragraphs' private data
612 * fields in the forthcoming code. Clear them first, so we can
613 * reliably tell whether we have auxiliary data for a
614 * particular paragraph.
616 for (p
= sourceform
; p
; p
= p
->next
)
617 p
->private_data
= NULL
;
619 files
.frags
= newtree234(html_fragment_compare
);
620 files
.files
= newtree234(html_filename_compare
);
623 * Start by figuring out into which file each piece of the
624 * document should be put. We'll do this by inventing an
625 * `htmlsect' structure and stashing it in the private_data
626 * field of each section paragraph; we also need one additional
627 * htmlsect for the document index, which won't show up in the
628 * source form but needs to be consistently mentioned in
631 * While we're here, we'll also invent the HTML fragment name(s)
638 topsect
= html_new_sect(§s
, NULL
, &conf
);
640 topsect
->title
= NULL
;
641 topsect
->text
= sourceform
;
642 topsect
->contents_depth
= contents_depth(conf
, 0);
643 html_file_section(&conf
, &files
, topsect
, -1);
645 for (p
= sourceform
; p
; p
= p
->next
)
646 if (is_heading_type(p
->type
)) {
647 d
= heading_depth(p
);
649 if (p
->type
== para_Title
) {
654 sect
= html_new_sect(§s
, p
, &conf
);
655 sect
->text
= p
->next
;
657 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
660 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
661 assert(sect
->parent
!= NULL
);
663 sect
->parent
= topsect
;
664 p
->private_data
= sect
;
666 html_file_section(&conf
, &files
, sect
, d
);
670 for (i
=0; i
< conf
.ntfragments
; i
++) {
672 html_format(p
, conf
.template_fragments
[i
]);
674 html_sanitise_fragment(&files
, sect
->file
,
681 * And the index, if we have one. Note that we don't output
682 * an index as an HTML file if we're outputting one as a
685 has_index
= (count234(idx
->entries
) > 0);
686 if (has_index
&& !conf
.hhk_filename
) {
687 sect
= html_new_sect(§s
, NULL
, &conf
);
690 sect
->parent
= topsect
;
691 sect
->contents_depth
= 0;
692 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
693 sect
->fragments
[0] = utoa_dup(conf
.index_text
, CS_ASCII
);
694 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
696 files
.index
= sect
->file
;
701 * Go through the keyword list and sort out fragment IDs for
702 * all the potentially referenced paragraphs which _aren't_
710 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
711 paragraph
*q
, *p
= kw
->para
;
713 if (!is_heading_type(p
->type
)) {
717 * Find the paragraph's parent htmlsect, to
718 * determine which file it will end up in.
723 * Preamble paragraphs have no parent. So if we
724 * have a non-heading with no parent, it must
725 * be preamble, and therefore its parent
726 * htmlsect must be the preamble one.
729 sects
.head
->type
== TOP
);
732 parent
= (htmlsect
*)q
->private_data
;
735 * Now we can construct an htmlsect for this
736 * paragraph itself, taking care to put it in the
737 * list of non-sections rather than the list of
738 * sections (so that traverses of the `sects' list
739 * won't attempt to add it to the contents or
740 * anything weird like that).
742 sect
= html_new_sect(&nonsects
, p
, &conf
);
743 sect
->file
= parent
->file
;
744 sect
->parent
= parent
;
745 p
->private_data
= sect
;
748 * Fragment IDs for these paragraphs will simply be
749 * `p' followed by an integer.
751 sect
->fragments
[0] = snewn(40, char);
752 sprintf(sect
->fragments
[0], "p%d",
753 sect
->file
->last_fragment_number
++);
754 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
761 * Reset the fragment numbers in each file. I've just used them
762 * to generate `p' fragment IDs for non-section paragraphs
763 * (numbered list elements, bibliocited), and now I want to use
764 * them for `i' fragment IDs for index entries.
768 for (file
= files
.head
; file
; file
= file
->next
)
769 file
->last_fragment_number
= 0;
773 * Now sort out the index. This involves:
775 * - For each index term, we set up an htmlindex structure to
776 * store all the references to that term.
778 * - Then we make a pass over the actual document, finding
779 * every word_IndexRef; for each one, we actually figure out
780 * the HTML filename/fragment pair we will use to reference
781 * it, store that information in the private data field of
782 * the word_IndexRef itself (so we can recreate it when the
783 * time comes to output our HTML), and add a reference to it
784 * to the index term in question.
793 * Set up the htmlindex structures.
796 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
797 htmlindex
*hi
= snew(htmlindex
);
799 hi
->nrefs
= hi
->refsize
= 0;
802 entry
->backend_data
= hi
;
806 * Run over the document inventing fragments. Each fragment
807 * is of the form `i' followed by an integer.
809 lastsect
= sects
.head
; /* this is always the top section */
810 for (p
= sourceform
; p
; p
= p
->next
) {
811 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
812 lastsect
= (htmlsect
*)p
->private_data
;
814 for (w
= p
->words
; w
; w
= w
->next
)
815 if (w
->type
== word_IndexRef
) {
816 htmlindexref
*hr
= snew(htmlindexref
);
820 hr
->referenced
= hr
->generated
= FALSE
;
821 hr
->section
= lastsect
;
825 lastsect
->file
->last_fragment_number
++);
826 hr
->fragment
= dupstr(buf
);
828 html_sanitise_fragment(&files
, hr
->section
->file
,
831 w
->private_data
= hr
;
833 tag
= index_findtag(idx
, w
->text
);
837 for (i
= 0; i
< tag
->nrefs
; i
++) {
838 indexentry
*entry
= tag
->refs
[i
];
839 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
841 if (hi
->nrefs
>= hi
->refsize
) {
843 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
846 hi
->refs
[hi
->nrefs
++] = w
;
853 * Now we're ready to write out the actual HTML files.
857 * - we open that file and write its header
858 * - we run down the list of sections
859 * - for each section directly contained within that file, we
860 * output the section text
861 * - for each section which is not in the file but which has a
862 * parent that is, we output a contents entry for the
863 * section if appropriate
864 * - finally, we output the file trailer and close the file.
873 for (f
= files
.head
; f
; f
= f
->next
) {
876 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
877 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
878 struct stackelement
{
879 struct stackelement
*next
;
880 enum LISTTYPE listtype
;
881 enum ITEMTYPE itemtype
;
884 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
885 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
887 if (!strcmp(f
->filename
, "-"))
890 ho
.fp
= fopen(f
->filename
, "w");
892 error(err_cantopenw
, f
->filename
);
894 ho
.charset
= conf
.output_charset
;
895 ho
.restrict_charset
= conf
.restrict_charset
;
896 ho
.cstate
= charset_init_state
;
897 ho
.ver
= conf
.htmlver
;
898 ho
.state
= HO_NEUTRAL
;
899 ho
.contents_level
= 0;
900 ho
.hackflags
= 0; /* none of these thankyouverymuch */
904 switch (conf
.htmlver
) {
907 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
908 "HTML 3.2 Final//EN\">\n");
912 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
913 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
918 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
919 "15445:2000//DTD HTML//EN\">\n");
921 case XHTML_1_0_TRANSITIONAL
:
923 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
924 charset_to_mimeenc(conf
.output_charset
));
925 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
926 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
927 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
930 case XHTML_1_0_STRICT
:
932 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
933 charset_to_mimeenc(conf
.output_charset
));
934 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
935 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
936 "DTD/xhtml1-strict.dtd\">\n");
941 element_open(&ho
, "html");
942 if (is_xhtml(conf
.htmlver
)) {
943 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
947 element_open(&ho
, "head");
950 element_empty(&ho
, "meta");
951 element_attr(&ho
, "http-equiv", "content-type");
954 sprintf(buf
, "text/html; charset=%.150s",
955 charset_to_mimeenc(conf
.output_charset
));
956 element_attr(&ho
, "content", buf
);
961 element_empty(&ho
, "meta");
962 element_attr(&ho
, "name", "author");
963 element_attr_w(&ho
, "content", conf
.author
);
967 if (conf
.description
) {
968 element_empty(&ho
, "meta");
969 element_attr(&ho
, "name", "description");
970 element_attr_w(&ho
, "content", conf
.description
);
974 element_open(&ho
, "title");
975 if (f
->first
&& f
->first
->title
) {
976 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
980 if (f
->last
!= f
->first
&& f
->last
->title
) {
981 html_text(&ho
, conf
.title_separator
);
982 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
986 element_close(&ho
, "title");
992 element_empty(&ho
, "link");
993 element_attr(&ho
, "rel", "previous");
994 element_attr(&ho
, "href", prevf
->filename
);
998 if (f
!= files
.head
) {
999 element_empty(&ho
, "link");
1000 element_attr(&ho
, "rel", "ToC");
1001 element_attr(&ho
, "href", files
.head
->filename
);
1005 if (conf
.leaf_level
> 0) {
1006 htmlsect
*p
= f
->first
->parent
;
1007 assert(p
== f
->last
->parent
);
1009 element_empty(&ho
, "link");
1010 element_attr(&ho
, "rel", "up");
1011 element_attr(&ho
, "href", p
->file
->filename
);
1016 if (has_index
&& files
.index
&& f
!= files
.index
) {
1017 element_empty(&ho
, "link");
1018 element_attr(&ho
, "rel", "index");
1019 element_attr(&ho
, "href", files
.index
->filename
);
1024 element_empty(&ho
, "link");
1025 element_attr(&ho
, "rel", "next");
1026 element_attr(&ho
, "href", f
->next
->filename
);
1033 html_raw(&ho
, conf
.head_end
);
1036 * Add any <head> data defined in specific sections
1037 * that go in this file. (This is mostly to allow <meta
1038 * name="AppleTitle"> tags for Mac online help.)
1040 for (s
= sects
.head
; s
; s
= s
->next
) {
1041 if (s
->file
== f
&& s
->text
) {
1043 p
&& (p
== s
->text
|| p
->type
== para_Title
||
1044 !is_heading_type(p
->type
));
1046 if (p
->type
== para_Config
) {
1047 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
1048 html_raw(&ho
, adv(p
->origkeyword
));
1055 element_close(&ho
, "head");
1059 html_raw(&ho
, conf
.body_tag
);
1061 element_open(&ho
, "body");
1064 if (conf
.body_start
)
1065 html_raw(&ho
, conf
.body_start
);
1068 * Write out a nav bar. Special case: we don't do this
1069 * if there is only one file.
1071 if (conf
.navlinks
&& files
.head
!= files
.tail
) {
1072 element_open(&ho
, "p");
1074 html_raw_as_attr(&ho
, conf
.nav_attr
);
1077 element_open(&ho
, "a");
1078 element_attr(&ho
, "href", prevf
->filename
);
1080 html_text(&ho
, conf
.nav_prev_text
);
1082 element_close(&ho
, "a");
1084 html_text(&ho
, conf
.nav_separator
);
1086 if (f
!= files
.head
) {
1087 element_open(&ho
, "a");
1088 element_attr(&ho
, "href", files
.head
->filename
);
1090 html_text(&ho
, conf
.contents_text
);
1091 if (f
!= files
.head
)
1092 element_close(&ho
, "a");
1094 /* We don't bother with "Up" links for leaf-level 1,
1095 * as they would be identical to the "Contents" links. */
1096 if (conf
.leaf_level
>= 2) {
1097 htmlsect
*p
= f
->first
->parent
;
1098 assert(p
== f
->last
->parent
);
1099 html_text(&ho
, conf
.nav_separator
);
1101 element_open(&ho
, "a");
1102 element_attr(&ho
, "href", p
->file
->filename
);
1104 html_text(&ho
, conf
.nav_up_text
);
1106 element_close(&ho
, "a");
1110 if (has_index
&& files
.index
) {
1111 html_text(&ho
, conf
.nav_separator
);
1112 if (f
!= files
.index
) {
1113 element_open(&ho
, "a");
1114 element_attr(&ho
, "href", files
.index
->filename
);
1116 html_text(&ho
, conf
.index_text
);
1117 if (f
!= files
.index
)
1118 element_close(&ho
, "a");
1121 html_text(&ho
, conf
.nav_separator
);
1124 element_open(&ho
, "a");
1125 element_attr(&ho
, "href", f
->next
->filename
);
1127 html_text(&ho
, conf
.nav_next_text
);
1129 element_close(&ho
, "a");
1131 element_close(&ho
, "p");
1137 * Write out a prefix TOC for the file (if a leaf file).
1139 * We start by going through the section list and
1140 * collecting the sections which need to be added to
1141 * the contents. On the way, we also test to see if
1142 * this file is a leaf file (defined as one which
1143 * contains all descendants of any section it
1144 * contains), because this will play a part in our
1145 * decision on whether or not to _output_ the TOC.
1147 * Special case: we absolutely do not do this if we're
1148 * in single-file mode.
1150 if (files
.head
!= files
.tail
) {
1151 int ntoc
= 0, tocsize
= 0;
1152 htmlsect
**toc
= NULL
;
1155 for (s
= sects
.head
; s
; s
= s
->next
) {
1160 * Search up from this section until we find
1161 * the highest-level one which belongs in this
1166 for (ac
= s
; ac
; ac
= ac
->parent
) {
1167 if (ac
->file
== f
) {
1174 if (s
->file
!= f
&& a
!= NULL
)
1178 if (adepth
<= a
->contents_depth
) {
1179 if (ntoc
>= tocsize
) {
1181 toc
= sresize(toc
, tocsize
, htmlsect
*);
1188 if (leaf
&& conf
.leaf_contains_contents
&&
1189 ntoc
>= conf
.leaf_smallest_contents
) {
1192 for (i
= 0; i
< ntoc
; i
++) {
1193 htmlsect
*s
= toc
[i
];
1194 int hlevel
= (s
->type
== TOP ?
-1 :
1195 s
->type
== INDEX ?
0 :
1196 heading_depth(s
->title
))
1197 - f
->min_heading_depth
+ 1;
1199 assert(hlevel
>= 1);
1200 html_contents_entry(&ho
, hlevel
, s
,
1201 f
, keywords
, &conf
);
1203 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1208 * Now go through the document and output some real
1212 for (s
= sects
.head
; s
; s
= s
->next
) {
1215 * This section belongs in this file.
1221 * Doesn't belong in this file, but it may be
1222 * a descendant of a section which does, in
1223 * which case we should consider it for the
1224 * main TOC of this file (for non-leaf files).
1232 * Search up from this section until we find
1233 * the highest-level one which belongs in this
1238 for (ac
= s
; ac
; ac
= ac
->parent
) {
1239 if (ac
->file
== f
) {
1248 * This section does not belong in this
1249 * file, but an ancestor of it does. Write
1250 * out a contents table entry, if the depth
1251 * doesn't exceed the maximum contents
1252 * depth for the ancestor section.
1254 if (adepth
<= a
->contents_depth
) {
1255 html_contents_entry(&ho
, adepth
, s
,
1256 f
, keywords
, &conf
);
1265 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1268 * Display the section heading.
1271 hlevel
= (s
->type
== TOP ?
-1 :
1272 s
->type
== INDEX ?
0 :
1273 heading_depth(s
->title
))
1274 - f
->min_heading_depth
+ 1;
1275 assert(hlevel
>= 1);
1276 /* HTML headings only go up to <h6> */
1280 htag
[1] = '0' + hlevel
;
1282 element_open(&ho
, htag
);
1285 * Provide anchor(s) for cross-links to target.
1287 * (Also we'll have to do this separately in
1288 * other paragraph types - NumberedList and
1293 for (i
=0; i
< conf
.ntfragments
; i
++)
1294 if (s
->fragments
[i
])
1295 html_fragment(&ho
, s
->fragments
[i
]);
1298 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1300 element_close(&ho
, htag
);
1303 * Now display the section text.
1306 stackhead
= snew(struct stackelement
);
1307 stackhead
->next
= NULL
;
1308 stackhead
->listtype
= NOLIST
;
1309 stackhead
->itemtype
= NOITEM
;
1311 for (p
= s
->text
;; p
= p
->next
) {
1312 enum LISTTYPE listtype
;
1313 struct stackelement
*se
;
1316 * Preliminary switch to figure out what
1317 * sort of list we expect to be inside at
1320 * Since p may still be NULL at this point,
1321 * I invent a harmless paragraph type for
1324 switch (p ? p
->type
: para_Normal
) {
1327 case para_Copyright
:
1328 case para_BiblioCited
:
1330 case para_QuotePush
:
1334 case para_UnnumberedChapter
:
1345 case para_NumberedList
:
1349 case para_DescribedThing
:
1350 case para_Description
:
1354 case para_LcontPush
:
1355 se
= snew(struct stackelement
);
1356 se
->next
= stackhead
;
1357 se
->listtype
= NOLIST
;
1358 se
->itemtype
= NOITEM
;
1362 default: /* some totally non-printing para */
1369 * Terminate the most recent list item, if
1370 * any. (We left this until after
1371 * processing LcontPush, since in that case
1372 * the list item won't want to be
1373 * terminated until after the corresponding
1376 if (stackhead
->itemtype
!= NOITEM
) {
1377 element_close(&ho
, itemname(stackhead
->itemtype
));
1380 stackhead
->itemtype
= NOITEM
;
1383 * Terminate the current list, if it's not
1384 * the one we want to be in.
1386 if (listtype
!= stackhead
->listtype
&&
1387 stackhead
->listtype
!= NOLIST
) {
1388 element_close(&ho
, listname(stackhead
->listtype
));
1393 * Leave the loop if our time has come.
1395 if (!p
|| (is_heading_type(p
->type
) &&
1396 p
->type
!= para_Title
))
1397 break; /* end of section text */
1400 * Start a fresh list if necessary.
1402 if (listtype
!= stackhead
->listtype
&&
1404 element_open(&ho
, listname(listtype
));
1406 stackhead
->listtype
= listtype
;
1410 element_empty(&ho
, "hr");
1413 html_codepara(&ho
, p
->words
);
1416 case para_Copyright
:
1417 element_open(&ho
, "p");
1419 html_words(&ho
, p
->words
, ALL
,
1420 f
, keywords
, &conf
);
1422 element_close(&ho
, "p");
1424 case para_BiblioCited
:
1425 element_open(&ho
, "p");
1426 if (p
->private_data
) {
1427 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1429 for (i
=0; i
< conf
.ntfragments
; i
++)
1430 if (s
->fragments
[i
])
1431 html_fragment(&ho
, s
->fragments
[i
]);
1434 html_words(&ho
, p
->kwtext
, ALL
,
1435 f
, keywords
, &conf
);
1436 html_text(&ho
, L
" ");
1437 html_words(&ho
, p
->words
, ALL
,
1438 f
, keywords
, &conf
);
1440 element_close(&ho
, "p");
1443 case para_NumberedList
:
1444 element_open(&ho
, "li");
1445 if (p
->private_data
) {
1446 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1448 for (i
=0; i
< conf
.ntfragments
; i
++)
1449 if (s
->fragments
[i
])
1450 html_fragment(&ho
, s
->fragments
[i
]);
1453 stackhead
->itemtype
= LI
;
1454 html_words(&ho
, p
->words
, ALL
,
1455 f
, keywords
, &conf
);
1457 case para_DescribedThing
:
1458 element_open(&ho
, "dt");
1460 stackhead
->itemtype
= DT
;
1461 html_words(&ho
, p
->words
, ALL
,
1462 f
, keywords
, &conf
);
1464 case para_Description
:
1465 element_open(&ho
, "dd");
1467 stackhead
->itemtype
= DD
;
1468 html_words(&ho
, p
->words
, ALL
,
1469 f
, keywords
, &conf
);
1472 case para_QuotePush
:
1473 element_open(&ho
, "blockquote");
1476 element_close(&ho
, "blockquote");
1481 stackhead
= stackhead
->next
;
1488 assert(stackhead
&& !stackhead
->next
);
1492 if (s
->type
== INDEX
) {
1497 * This section is the index. I'll just
1498 * render it as a single paragraph, with a
1499 * colon between the index term and the
1500 * references, and <br> in between each
1503 element_open(&ho
, "p");
1505 for (i
= 0; (entry
=
1506 index234(idx
->entries
, i
)) != NULL
; i
++) {
1507 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1511 element_empty(&ho
, "br");
1514 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1515 f
, keywords
, &conf
);
1517 html_text(&ho
, conf
.index_main_sep
);
1519 for (j
= 0; j
< hi
->nrefs
; j
++) {
1521 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1522 paragraph
*p
= hr
->section
->title
;
1525 html_text(&ho
, conf
.index_multi_sep
);
1527 html_href(&ho
, f
, hr
->section
->file
,
1529 hr
->referenced
= TRUE
;
1531 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1532 f
, keywords
, &conf
);
1533 else if (p
&& p
->words
)
1534 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1535 f
, keywords
, &conf
);
1538 * If there is no title at all,
1539 * this must be because our
1540 * target section is the
1541 * preamble section and there
1542 * is no title. So we use the
1545 html_text(&ho
, conf
.preamble_text
);
1547 element_close(&ho
, "a");
1550 element_close(&ho
, "p");
1555 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1562 int done_version_ids
= FALSE
;
1564 if (conf
.address_section
)
1565 element_empty(&ho
, "hr");
1568 html_raw(&ho
, conf
.body_end
);
1570 if (conf
.address_section
) {
1571 int started
= FALSE
;
1572 if (conf
.htmlver
== ISO_HTML
) {
1574 * The ISO-HTML validator complains if
1575 * there isn't a <div> tag surrounding the
1576 * <address> tag. I'm uncertain of why this
1577 * should be - there appears to be no
1578 * mention of this in the ISO-HTML spec,
1579 * suggesting that it doesn't represent a
1580 * change from HTML 4, but nonetheless the
1581 * HTML 4 validator doesn't seem to mind.
1583 element_open(&ho
, "div");
1585 element_open(&ho
, "address");
1586 if (conf
.addr_start
) {
1587 html_raw(&ho
, conf
.addr_start
);
1591 if (conf
.visible_version_id
) {
1592 for (p
= sourceform
; p
; p
= p
->next
)
1593 if (p
->type
== para_VersionID
) {
1595 element_empty(&ho
, "br");
1597 html_text(&ho
, conf
.pre_versionid
);
1598 html_words(&ho
, p
->words
, NOTHING
,
1599 f
, keywords
, &conf
);
1600 html_text(&ho
, conf
.post_versionid
);
1603 done_version_ids
= TRUE
;
1605 if (conf
.addr_end
) {
1607 element_empty(&ho
, "br");
1608 html_raw(&ho
, conf
.addr_end
);
1610 element_close(&ho
, "address");
1611 if (conf
.htmlver
== ISO_HTML
)
1612 element_close(&ho
, "div");
1615 if (!done_version_ids
) {
1617 * If the user didn't want the version IDs
1618 * visible, I think we still have a duty to put
1619 * them in an HTML comment.
1621 int started
= FALSE
;
1622 for (p
= sourceform
; p
; p
= p
->next
)
1623 if (p
->type
== para_VersionID
) {
1625 html_raw(&ho
, "<!-- version IDs:\n");
1628 html_words(&ho
, p
->words
, NOTHING
,
1629 f
, keywords
, &conf
);
1633 html_raw(&ho
, "-->\n");
1637 element_close(&ho
, "body");
1639 element_close(&ho
, "html");
1646 * Before we start outputting the HTML Help files, check
1647 * whether there's even going to _be_ an index file: we omit it
1648 * if the index contains nothing.
1650 hhk_filename
= conf
.hhk_filename
;
1656 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1657 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1659 if (hi
->nrefs
> 0) {
1660 ok
= TRUE
; /* found an index entry */
1666 hhk_filename
= NULL
;
1670 * Output the MS HTML Help supporting files, if requested.
1672 * A good unofficial reference for these is <http://chmspec.nongnu.org/>.
1674 if (conf
.hhp_filename
) {
1678 ho
.charset
= CS_CP1252
; /* as far as I know, HHP files are */
1679 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1680 ho
.cstate
= charset_init_state
;
1681 ho
.ver
= HTML_4
; /* *shrug* */
1682 ho
.state
= HO_NEUTRAL
;
1683 ho
.contents_level
= 0;
1684 ho
.hackflags
= HO_HACK_QUOTENOTHING
;
1686 ho
.fp
= fopen(conf
.hhp_filename
, "w");
1688 error(err_cantopenw
, conf
.hhp_filename
);
1692 /* Binary TOC required for Next/Previous nav to work */
1694 "Compatibility=1.1 or later\n"
1695 "Compiled file=%s\n"
1696 "Default Window=main\n"
1697 "Default topic=%s\n"
1698 "Display compile progress=Yes\n"
1699 "Full-text search=Yes\n"
1700 "Title=", conf
.chm_filename
, files
.head
->filename
);
1703 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1704 NULL
, keywords
, &conf
);
1706 fprintf(ho
.fp
, "\n");
1709 * These two entries don't seem to be remotely necessary
1710 * for a successful run of the help _compiler_, but
1711 * omitting them causes the GUI Help Workshop to behave
1712 * rather strangely if you try to load the help project
1713 * into that and edit it.
1715 if (conf
.hhc_filename
)
1716 fprintf(ho
.fp
, "Contents file=%s\n", conf
.hhc_filename
);
1718 fprintf(ho
.fp
, "Index file=%s\n", hhk_filename
);
1720 fprintf(ho
.fp
, "\n[WINDOWS]\nmain=\"");
1722 ho
.hackflags
|= HO_HACK_OMITQUOTES
;
1724 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1725 NULL
, keywords
, &conf
);
1727 fprintf(ho
.fp
, "\",\"%s\",\"%s\",\"%s\",,,,,,"
1728 /* This first magic number is fsWinProperties, controlling
1729 * Navigation Pane options and the like.
1730 * Constants HHWIN_PROP_* in htmlhelp.h. */
1732 /* This second number is fsToolBarFlags, mainly controlling
1733 * toolbar buttons. Constants HHWIN_BUTTON_*.
1734 * NOTE: there are two pairs of bits for Next/Previous
1735 * buttons: 7/8 (which do nothing useful), and 21/22
1736 * (which work). (Neither of these are exposed in the HHW
1737 * UI, but they work fine in HH.) We use the latter. */
1738 "0x60304e,,,,,,,,0\n",
1739 conf
.hhc_filename ? conf
.hhc_filename
: "",
1740 hhk_filename ? hhk_filename
: "",
1741 files
.head
->filename
);
1744 * The [FILES] section is also not necessary for
1745 * compilation (hhc appears to build up a list of needed
1746 * files just by following links from the given starting
1747 * points), but useful for loading the project into HHW.
1749 fprintf(ho
.fp
, "\n[FILES]\n");
1750 for (f
= files
.head
; f
; f
= f
->next
)
1751 fprintf(ho
.fp
, "%s\n", f
->filename
);
1755 if (conf
.hhc_filename
) {
1761 ho
.fp
= fopen(conf
.hhc_filename
, "w");
1763 error(err_cantopenw
, conf
.hhc_filename
);
1765 ho
.charset
= CS_CP1252
; /* as far as I know, HHC files are */
1766 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1767 ho
.cstate
= charset_init_state
;
1768 ho
.ver
= HTML_4
; /* *shrug* */
1769 ho
.state
= HO_NEUTRAL
;
1770 ho
.contents_level
= 0;
1771 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1774 * Magic DOCTYPE which seems to work for .HHC files. I'm
1775 * wary of trying to change it!
1777 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1779 "<META HTTP-EQUIV=\"Content-Type\" "
1780 "CONTENT=\"text/html; charset=%s\">\n"
1781 "</HEAD><BODY><UL>\n",
1782 charset_to_mimeenc(conf
.output_charset
));
1784 for (f
= files
.head
; f
; f
= f
->next
) {
1786 * For each HTML file, write out a contents entry.
1788 int depth
, leaf
= TRUE
;
1791 * Determine the depth of this file in the contents
1794 * If the file contains no sections, it is assumed to
1799 for (a
= f
->first
->parent
; a
&& a
->type
!= TOP
; a
= a
->parent
)
1803 * Determine if this file is a leaf file, by
1804 * trawling the section list to see if there's any
1805 * section with an ancestor in this file but which
1806 * is not itself in this file.
1808 * Special case: for contents purposes, the TOP
1809 * file is not considered to be the parent of the
1810 * chapter files, so it's always a leaf.
1812 * A file with no sections in it is also a leaf.
1814 if (f
->first
&& f
->first
->type
!= TOP
) {
1815 for (s
= f
->first
; s
; s
= s
->next
) {
1818 if (leaf
&& s
->file
!= f
) {
1819 for (a
= s
; a
; a
= a
->parent
)
1829 * Now write out our contents entry.
1831 while (currdepth
< depth
) {
1832 fprintf(ho
.fp
, "<UL>\n");
1835 while (currdepth
> depth
) {
1836 fprintf(ho
.fp
, "</UL>\n");
1839 /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */
1840 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">"
1841 "<PARAM NAME=\"Name\" VALUE=\"");
1843 if (f
->first
->title
)
1844 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
1845 NULL
, keywords
, &conf
);
1846 else if (f
->first
->type
== INDEX
)
1847 html_text(&ho
, conf
.index_text
);
1848 fprintf(ho
.fp
, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">"
1849 "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n",
1850 f
->filename
, leaf ?
11 : 1);
1853 while (currdepth
> 0) {
1854 fprintf(ho
.fp
, "</UL>\n");
1858 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1869 * First make a pass over all HTML files and set their
1870 * `temp' fields to zero, because we're about to use them.
1872 for (f
= files
.head
; f
; f
= f
->next
)
1875 ho
.fp
= fopen(hhk_filename
, "w");
1877 error(err_cantopenw
, hhk_filename
);
1879 ho
.charset
= CS_CP1252
; /* as far as I know, HHK files are */
1880 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1881 ho
.cstate
= charset_init_state
;
1882 ho
.ver
= HTML_4
; /* *shrug* */
1883 ho
.state
= HO_NEUTRAL
;
1884 ho
.contents_level
= 0;
1885 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1888 * Magic DOCTYPE which seems to work for .HHK files. I'm
1889 * wary of trying to change it!
1891 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1893 "<META HTTP-EQUIV=\"Content-Type\" "
1894 "CONTENT=\"text/html; charset=%s\">\n"
1895 "</HEAD><BODY><UL>\n",
1896 charset_to_mimeenc(conf
.output_charset
));
1899 * Go through the index terms and output each one.
1901 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1902 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1905 if (hi
->nrefs
> 0) {
1906 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
1907 "<PARAM NAME=\"Name\" VALUE=\"");
1909 html_words(&ho
, entry
->text
, NOTHING
,
1910 NULL
, keywords
, &conf
);
1911 fprintf(ho
.fp
, "\">\n");
1913 for (j
= 0; j
< hi
->nrefs
; j
++) {
1915 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1918 * Use the temp field to ensure we don't
1919 * reference the same file more than once.
1921 if (!hr
->section
->file
->temp
) {
1922 fprintf(ho
.fp
, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n",
1923 hr
->section
->file
->filename
);
1924 hr
->section
->file
->temp
= 1;
1927 hr
->referenced
= TRUE
;
1930 fprintf(ho
.fp
, "</OBJECT>\n");
1933 * Now go through those files and re-clear the temp
1934 * fields ready for the _next_ index term.
1936 for (j
= 0; j
< hi
->nrefs
; j
++) {
1938 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1939 hr
->section
->file
->temp
= 0;
1944 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1949 * Go through and check that no index fragments were referenced
1950 * without being generated, or indeed vice versa.
1952 * (When I actually get round to freeing everything, this can
1953 * probably be the freeing loop as well.)
1955 for (p
= sourceform
; p
; p
= p
->next
) {
1957 for (w
= p
->words
; w
; w
= w
->next
)
1958 if (w
->type
== word_IndexRef
) {
1959 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1961 assert(!hr
->referenced
== !hr
->generated
);
1966 * Free all the working data.
1970 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1972 * frag->fragment is dynamically allocated, but will be
1973 * freed when we process the htmlsect structure which
1974 * it is attached to.
1978 freetree234(files
.frags
);
1981 * The strings in files.files are all owned by their containing
1982 * htmlfile structures, so there's no need to free them here.
1984 freetree234(files
.files
);
1986 htmlsect
*sect
, *tmp
;
1991 for (i
=0; i
< conf
.ntfragments
; i
++)
1992 sfree(sect
->fragments
[i
]);
1993 sfree(sect
->fragments
);
1997 sect
= nonsects
.head
;
2001 for (i
=0; i
< conf
.ntfragments
; i
++)
2002 sfree(sect
->fragments
[i
]);
2003 sfree(sect
->fragments
);
2009 htmlfile
*file
, *tmp
;
2013 sfree(file
->filename
);
2021 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
2022 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
2029 for (p
= sourceform
; p
; p
= p
->next
)
2030 for (w
= p
->words
; w
; w
= w
->next
)
2031 if (w
->type
== word_IndexRef
) {
2032 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2034 sfree(hr
->fragment
);
2039 sfree(conf
.single_filename
);
2040 sfree(conf
.contents_filename
);
2041 sfree(conf
.index_filename
);
2042 sfree(conf
.template_filename
);
2043 while (conf
.ntfragments
--)
2044 sfree(conf
.template_fragments
[conf
.ntfragments
]);
2045 sfree(conf
.template_fragments
);
2048 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
2049 htmlsect
*sect
, int depth
)
2055 * `depth' is derived from the heading_depth() macro at the top
2056 * of this file, which counts title as -1, chapter as 0,
2057 * heading as 1 and subsection as 2. However, the semantics of
2058 * cfg->leaf_level are defined to count chapter as 1, heading
2059 * as 2 etc. So first I increment depth :-(
2063 if (cfg
->leaf_level
== 0) {
2065 * leaf_level==0 is a special case, in which everything is
2066 * put into a single file.
2069 files
->single
= html_new_file(files
, cfg
->single_filename
);
2071 file
= files
->single
;
2074 * If the depth of this section is at or above leaf_level,
2075 * we invent a fresh file and put this section at its head.
2076 * Otherwise, we put it in the same file as its parent
2079 * Another special value of cfg->leaf_level is -1, which
2080 * means infinity (i.e. it's considered to always be
2081 * greater than depth).
2083 if (cfg
->leaf_level
> 0 && ldepth
> cfg
->leaf_level
) {
2085 * We know that sect->parent cannot be NULL. The only
2086 * circumstance in which it can be is if sect is at
2087 * chapter or appendix level, i.e. ldepth==1; and if
2088 * that's the case, then we cannot have entered this
2089 * branch unless cfg->leaf_level==0, in which case we
2090 * would be in the single-file case above and not here
2093 assert(sect
->parent
);
2095 file
= sect
->parent
->file
;
2097 if (sect
->type
== TOP
) {
2098 file
= html_new_file(files
, cfg
->contents_filename
);
2099 } else if (sect
->type
== INDEX
) {
2100 file
= html_new_file(files
, cfg
->index_filename
);
2104 assert(ldepth
> 0 && sect
->title
);
2105 title
= html_format(sect
->title
, cfg
->template_filename
);
2106 file
= html_new_file(files
, title
);
2114 if (file
->min_heading_depth
> depth
) {
2116 * This heading is at a higher level than any heading we
2117 * have so far placed in this file; so we set the `first'
2120 file
->min_heading_depth
= depth
;
2124 if (file
->min_heading_depth
== depth
)
2128 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
2130 htmlfile
*ret
= snew(htmlfile
);
2134 list
->tail
->next
= ret
;
2139 ret
->filename
= html_sanitise_filename(list
, dupstr(filename
));
2140 add234(list
->files
, ret
->filename
);
2141 ret
->last_fragment_number
= 0;
2142 ret
->min_heading_depth
= INT_MAX
;
2143 ret
->first
= ret
->last
= NULL
;
2148 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
2151 htmlsect
*ret
= snew(htmlsect
);
2155 list
->tail
->next
= ret
;
2165 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
2168 for (i
=0; i
< cfg
->ntfragments
; i
++)
2169 ret
->fragments
[i
] = NULL
;
2175 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
2176 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
2179 char *c
, *c2
, *p
, *q
;
2182 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
2183 case word_HyperLink
:
2184 if (flags
& LINKS
) {
2185 element_open(ho
, "a");
2186 c
= utoa_dup(w
->text
, CS_ASCII
);
2187 c2
= snewn(1 + 10*strlen(c
), char);
2188 for (p
= c
, q
= c2
; *p
; p
++) {
2190 q
+= sprintf(q
, "&");
2192 q
+= sprintf(q
, "<");
2194 q
+= sprintf(q
, ">");
2199 element_attr(ho
, "href", c2
);
2204 case word_UpperXref
:
2205 case word_LowerXref
:
2206 if (flags
& LINKS
) {
2207 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
2213 s
= (htmlsect
*)p
->private_data
;
2217 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
2223 element_close(ho
, "a");
2226 if (flags
& INDEXENTS
) {
2227 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2228 html_fragment(ho
, hr
->fragment
);
2229 hr
->generated
= TRUE
;
2236 case word_WhiteSpace
:
2237 case word_EmphSpace
:
2238 case word_CodeSpace
:
2239 case word_WkCodeSpace
:
2241 case word_EmphQuote
:
2242 case word_CodeQuote
:
2243 case word_WkCodeQuote
:
2244 style
= towordstyle(w
->type
);
2245 type
= removeattr(w
->type
);
2246 if (style
== word_Emph
&&
2247 (attraux(w
->aux
) == attr_First
||
2248 attraux(w
->aux
) == attr_Only
) &&
2250 element_open(ho
, "em");
2251 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2252 (attraux(w
->aux
) == attr_First
||
2253 attraux(w
->aux
) == attr_Only
) &&
2255 element_open(ho
, "code");
2257 if (type
== word_WhiteSpace
)
2258 html_text(ho
, L
" ");
2259 else if (type
== word_Quote
) {
2260 if (quoteaux(w
->aux
) == quote_Open
)
2261 html_text(ho
, cfg
->lquote
);
2263 html_text(ho
, cfg
->rquote
);
2265 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
2266 html_text_nbsp(ho
, w
->text
);
2268 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
2271 if (style
== word_Emph
&&
2272 (attraux(w
->aux
) == attr_Last
||
2273 attraux(w
->aux
) == attr_Only
) &&
2275 element_close(ho
, "em");
2276 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2277 (attraux(w
->aux
) == attr_Last
||
2278 attraux(w
->aux
) == attr_Only
) &&
2280 element_close(ho
, "code");
2286 static void html_codepara(htmloutput
*ho
, word
*words
)
2288 element_open(ho
, "pre");
2289 element_open(ho
, "code");
2290 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
2295 if (words
->next
&& words
->next
->type
== word_Emph
) {
2296 e
= words
->next
->text
;
2297 words
= words
->next
;
2301 while (e
&& *e
&& *t
) {
2305 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
2313 element_open(ho
, open_tag
);
2315 html_text_limit(ho
, t
, n
);
2318 element_close(ho
, open_tag
);
2326 element_close(ho
, "code");
2327 element_close(ho
, "pre");
2330 static void html_charset_cleanup(htmloutput
*ho
)
2335 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
2336 ho
->charset
, &ho
->cstate
, NULL
);
2337 if (ho
->fp
&& bytes
> 0)
2338 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2341 static void return_mostly_to_neutral(htmloutput
*ho
)
2344 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
2345 fprintf(ho
->fp
, " />");
2346 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
2347 fprintf(ho
->fp
, ">");
2351 ho
->state
= HO_NEUTRAL
;
2354 static void return_to_neutral(htmloutput
*ho
)
2356 if (ho
->state
== HO_IN_TEXT
) {
2357 html_charset_cleanup(ho
);
2360 return_mostly_to_neutral(ho
);
2363 static void element_open(htmloutput
*ho
, char const *name
)
2365 return_to_neutral(ho
);
2367 fprintf(ho
->fp
, "<%s", name
);
2368 ho
->state
= HO_IN_TAG
;
2371 static void element_close(htmloutput
*ho
, char const *name
)
2373 return_to_neutral(ho
);
2375 fprintf(ho
->fp
, "</%s>", name
);
2376 ho
->state
= HO_NEUTRAL
;
2379 static void element_empty(htmloutput
*ho
, char const *name
)
2381 return_to_neutral(ho
);
2383 fprintf(ho
->fp
, "<%s", name
);
2384 ho
->state
= HO_IN_EMPTY_TAG
;
2387 static void html_nl(htmloutput
*ho
)
2389 return_to_neutral(ho
);
2391 fputc('\n', ho
->fp
);
2394 static void html_raw(htmloutput
*ho
, char *text
)
2396 return_to_neutral(ho
);
2398 fputs(text
, ho
->fp
);
2401 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
2403 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2406 fputs(text
, ho
->fp
);
2410 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
2412 html_charset_cleanup(ho
);
2413 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2415 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
2418 static void element_attr_w(htmloutput
*ho
, char const *name
,
2419 wchar_t const *value
)
2421 html_charset_cleanup(ho
);
2423 fprintf(ho
->fp
, " %s=\"", name
);
2424 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
2425 html_charset_cleanup(ho
);
2430 static void html_text(htmloutput
*ho
, wchar_t const *text
)
2432 return_mostly_to_neutral(ho
);
2433 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
2436 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
2438 return_mostly_to_neutral(ho
);
2439 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
2442 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
2444 return_mostly_to_neutral(ho
);
2445 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
2448 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
2449 int maxlen
, int quote_quotes
, int nbsp
)
2451 int textlen
= ustrlen(text
);
2455 if (ho
->hackflags
& (HO_HACK_QUOTEQUOTES
| HO_HACK_OMITQUOTES
))
2456 quote_quotes
= TRUE
; /* override the input value */
2458 if (maxlen
> 0 && textlen
> maxlen
)
2460 if (ho
->hacklimit
>= 0) {
2461 if (textlen
> ho
->hacklimit
)
2462 textlen
= ho
->hacklimit
;
2463 ho
->hacklimit
-= textlen
;
2466 while (textlen
> 0) {
2467 /* Scan ahead for characters we really can't display in HTML. */
2468 int lenbefore
, lenafter
;
2469 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
2470 if (text
[lenbefore
] == L
'<' ||
2471 text
[lenbefore
] == L
'>' ||
2472 text
[lenbefore
] == L
'&' ||
2473 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
2474 (text
[lenbefore
] == L
' ' && nbsp
))
2476 lenafter
= lenbefore
;
2477 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
2478 ho
->charset
, &ho
->cstate
, &err
);
2479 textlen
-= (lenbefore
- lenafter
);
2480 if (bytes
> 0 && ho
->fp
)
2481 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2484 * We have encountered a character that cannot be
2485 * displayed in the selected output charset. Therefore,
2486 * we use an HTML numeric entity reference.
2488 assert(textlen
> 0);
2490 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
2492 } else if (lenafter
== 0 && textlen
> 0) {
2494 * We have encountered a character which is special to
2498 if (*text
== L
'"' && (ho
->hackflags
& HO_HACK_OMITQUOTES
)) {
2499 fputc('\'', ho
->fp
);
2500 } else if (ho
->hackflags
& HO_HACK_QUOTENOTHING
) {
2501 fputc(*text
, ho
->fp
);
2504 fprintf(ho
->fp
, "<");
2505 else if (*text
== L
'>')
2506 fprintf(ho
->fp
, ">");
2507 else if (*text
== L
'&')
2508 fprintf(ho
->fp
, "&");
2509 else if (*text
== L
'"')
2510 fprintf(ho
->fp
, """);
2511 else if (*text
== L
' ') {
2513 fprintf(ho
->fp
, " ");
2515 assert(!"Can't happen");
2523 static void cleanup(htmloutput
*ho
)
2525 return_to_neutral(ho
);
2526 if (ho
->fp
&& ho
->fp
!= stdout
)
2530 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
2531 htmlfile
*targetfile
, char *targetfrag
)
2533 rdstringc rs
= { 0, 0, NULL
};
2536 if (targetfile
!= thisfile
)
2537 rdaddsc(&rs
, targetfile
->filename
);
2540 rdaddsc(&rs
, targetfrag
);
2544 element_open(ho
, "a");
2545 element_attr(ho
, "href", url
);
2549 static void html_fragment(htmloutput
*ho
, char const *fragment
)
2551 element_open(ho
, "a");
2552 element_attr(ho
, "name", fragment
);
2553 if (is_xhtml(ho
->ver
))
2554 element_attr(ho
, "id", fragment
);
2555 element_close(ho
, "a");
2558 static char *html_format(paragraph
*p
, char *template_string
)
2562 wchar_t *ws
, wsbuf
[2];
2563 rdstringc rs
= { 0, 0, NULL
};
2565 t
= template_string
;
2567 if (*t
== '%' && t
[1]) {
2581 if (p
->kwtext
&& fmt
== 'n')
2583 else if (p
->kwtext2
&& fmt
== 'b') {
2585 * HTML fragment names must start with a letter, so
2586 * simply `1.2.3' is not adequate. In this case I'm
2587 * going to cheat slightly by prepending the first
2588 * character of the first word of kwtext, so that
2589 * we get `C1' for chapter 1, `S2.3' for section
2592 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2595 wsbuf
[0] = p
->kwtext
->text
[0];
2598 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2601 /* %N comes here; also failure cases of other fmts */
2605 c
= utoa_dup(ws
, CS_ASCII
);
2611 if (removeattr(w
->type
) == word_Normal
) {
2612 c
= utoa_dup(w
->text
, CS_ASCII
);
2623 return rdtrimc(&rs
);
2626 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2630 * The HTML 4 spec's strictest definition of fragment names (<a
2631 * name> and "id" attributes) says that they `must begin with a
2632 * letter and may be followed by any number of letters, digits,
2633 * hyphens, underscores, colons, and periods'.
2635 * So here we unceremoniously rip out any characters not
2636 * conforming to this limitation.
2638 char *p
= text
, *q
= text
;
2640 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2642 if ((*q
++ = *p
++) != '\0') {
2644 if ((*p
>='A' && *p
<='Z') ||
2645 (*p
>='a' && *p
<='z') ||
2646 (*p
>='0' && *p
<='9') ||
2647 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2655 /* If there's nothing left, make something valid up */
2657 static const char anonfrag
[] = "anon";
2658 text
= sresize(text
, lenof(anonfrag
), char);
2659 strcpy(text
, anonfrag
);
2663 * Now we check for clashes with other fragment names, and
2664 * adjust this one if necessary by appending a hyphen followed
2668 htmlfragment
*frag
= snew(htmlfragment
);
2669 int len
= 0; /* >0 indicates we have resized */
2673 frag
->fragment
= text
;
2675 while (add234(files
->frags
, frag
) != frag
) {
2678 frag
->fragment
= text
= sresize(text
, len
+20, char);
2681 sprintf(text
+ len
, "-%d", ++suffix
);
2688 static char *html_sanitise_filename(htmlfilelist
*files
, char *text
)
2691 * Unceremoniously rip out any character that might cause
2692 * difficulty in some filesystem or another, or be otherwise
2695 * That doesn't leave much punctuation. I permit alphanumerics
2698 char *p
= text
, *q
= text
;
2701 if ((*p
>='A' && *p
<='Z') ||
2702 (*p
>='a' && *p
<='z') ||
2703 (*p
>='0' && *p
<='9') ||
2704 *p
=='-' || *p
=='_' || *p
=='+' || *p
=='.' || *p
=='=')
2710 /* If there's nothing left, make something valid up */
2712 static const char anonfrag
[] = "anon.html";
2713 text
= sresize(text
, lenof(anonfrag
), char);
2714 strcpy(text
, anonfrag
);
2718 * Now we check for clashes with other filenames, and adjust
2719 * this one if necessary by appending a hyphen followed by a
2720 * number just before the file extension (if any).
2728 while (find234(files
->files
, text
, NULL
)) {
2732 text
= snewn(len
+20, char);
2734 for (extpos
= len
; extpos
> 0 && p
[extpos
-1] != '.'; extpos
--);
2741 sprintf(text
, "%.*s-%d%s", extpos
, p
, ++suffix
, p
+extpos
);
2751 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2752 htmlfile
*thisfile
, keywordlist
*keywords
,
2755 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2756 element_close(ho
, "li");
2760 while (ho
->contents_level
> depth
) {
2761 element_close(ho
, "ul");
2762 ho
->contents_level
--;
2763 if (ho
->contents_level
> 0) {
2764 element_close(ho
, "li");
2769 while (ho
->contents_level
< depth
) {
2771 element_open(ho
, "ul");
2773 ho
->contents_level
++;
2779 element_open(ho
, "li");
2780 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2781 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2782 element_close(ho
, "a");
2783 /* <li> will be closed by a later invocation */
2786 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2787 keywordlist
*keywords
, htmlconfig
*cfg
,
2793 int depth
= heading_depth(s
->title
);
2797 else if (depth
== 0)
2798 sl
= &cfg
->achapter
;
2799 else if (depth
<= cfg
->nasect
)
2800 sl
= &cfg
->asect
[depth
-1];
2802 sl
= &cfg
->asect
[cfg
->nasect
-1];
2804 if (!sl
|| !sl
->number_at_all
)
2806 else if (sl
->just_numbers
)
2807 number
= s
->title
->kwtext2
;
2809 number
= s
->title
->kwtext
;
2812 html_words(ho
, number
, MARKUP
,
2813 thisfile
, keywords
, cfg
);
2814 html_text(ho
, sl
->number_suffix
);
2817 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2818 thisfile
, keywords
, cfg
);
2820 assert(s
->type
!= NORMAL
);
2822 * If we're printing the full document title for _real_ and
2823 * there isn't one, we don't want to print `Preamble' at
2824 * the top of what ought to just be some text. If we need
2825 * it in any other context such as TOCs, we need to print
2828 if (s
->type
== TOP
&& !real
)
2829 html_text(ho
, cfg
->preamble_text
);
2830 else if (s
->type
== INDEX
)
2831 html_text(ho
, cfg
->index_text
);