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 ho
.fp
= fopen(f
->filename
, "w");
889 error(err_cantopenw
, f
->filename
);
891 ho
.charset
= conf
.output_charset
;
892 ho
.restrict_charset
= conf
.restrict_charset
;
893 ho
.cstate
= charset_init_state
;
894 ho
.ver
= conf
.htmlver
;
895 ho
.state
= HO_NEUTRAL
;
896 ho
.contents_level
= 0;
897 ho
.hackflags
= 0; /* none of these thankyouverymuch */
901 switch (conf
.htmlver
) {
904 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
905 "HTML 3.2 Final//EN\">\n");
909 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
910 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
915 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
916 "15445:2000//DTD HTML//EN\">\n");
918 case XHTML_1_0_TRANSITIONAL
:
920 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
921 charset_to_mimeenc(conf
.output_charset
));
922 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
923 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
924 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
927 case XHTML_1_0_STRICT
:
929 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
930 charset_to_mimeenc(conf
.output_charset
));
931 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
932 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
933 "DTD/xhtml1-strict.dtd\">\n");
938 element_open(&ho
, "html");
939 if (is_xhtml(conf
.htmlver
)) {
940 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
944 element_open(&ho
, "head");
947 element_empty(&ho
, "meta");
948 element_attr(&ho
, "http-equiv", "content-type");
951 sprintf(buf
, "text/html; charset=%.150s",
952 charset_to_mimeenc(conf
.output_charset
));
953 element_attr(&ho
, "content", buf
);
958 element_empty(&ho
, "meta");
959 element_attr(&ho
, "name", "author");
960 element_attr_w(&ho
, "content", conf
.author
);
964 if (conf
.description
) {
965 element_empty(&ho
, "meta");
966 element_attr(&ho
, "name", "description");
967 element_attr_w(&ho
, "content", conf
.description
);
971 element_open(&ho
, "title");
972 if (f
->first
&& f
->first
->title
) {
973 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
977 if (f
->last
!= f
->first
&& f
->last
->title
) {
978 html_text(&ho
, conf
.title_separator
);
979 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
983 element_close(&ho
, "title");
989 element_empty(&ho
, "link");
990 element_attr(&ho
, "rel", "previous");
991 element_attr(&ho
, "href", prevf
->filename
);
995 if (f
!= files
.head
) {
996 element_empty(&ho
, "link");
997 element_attr(&ho
, "rel", "ToC");
998 element_attr(&ho
, "href", files
.head
->filename
);
1002 if (conf
.leaf_level
> 0) {
1003 htmlsect
*p
= f
->first
->parent
;
1004 assert(p
== f
->last
->parent
);
1006 element_empty(&ho
, "link");
1007 element_attr(&ho
, "rel", "up");
1008 element_attr(&ho
, "href", p
->file
->filename
);
1013 if (has_index
&& files
.index
&& f
!= files
.index
) {
1014 element_empty(&ho
, "link");
1015 element_attr(&ho
, "rel", "index");
1016 element_attr(&ho
, "href", files
.index
->filename
);
1021 element_empty(&ho
, "link");
1022 element_attr(&ho
, "rel", "next");
1023 element_attr(&ho
, "href", f
->next
->filename
);
1030 html_raw(&ho
, conf
.head_end
);
1033 * Add any <head> data defined in specific sections
1034 * that go in this file. (This is mostly to allow <meta
1035 * name="AppleTitle"> tags for Mac online help.)
1037 for (s
= sects
.head
; s
; s
= s
->next
) {
1038 if (s
->file
== f
&& s
->text
) {
1040 p
&& (p
== s
->text
|| p
->type
== para_Title
||
1041 !is_heading_type(p
->type
));
1043 if (p
->type
== para_Config
) {
1044 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
1045 html_raw(&ho
, adv(p
->origkeyword
));
1052 element_close(&ho
, "head");
1056 html_raw(&ho
, conf
.body_tag
);
1058 element_open(&ho
, "body");
1061 if (conf
.body_start
)
1062 html_raw(&ho
, conf
.body_start
);
1065 * Write out a nav bar. Special case: we don't do this
1066 * if there is only one file.
1068 if (conf
.navlinks
&& files
.head
!= files
.tail
) {
1069 element_open(&ho
, "p");
1071 html_raw_as_attr(&ho
, conf
.nav_attr
);
1074 element_open(&ho
, "a");
1075 element_attr(&ho
, "href", prevf
->filename
);
1077 html_text(&ho
, conf
.nav_prev_text
);
1079 element_close(&ho
, "a");
1081 html_text(&ho
, conf
.nav_separator
);
1083 if (f
!= files
.head
) {
1084 element_open(&ho
, "a");
1085 element_attr(&ho
, "href", files
.head
->filename
);
1087 html_text(&ho
, conf
.contents_text
);
1088 if (f
!= files
.head
)
1089 element_close(&ho
, "a");
1091 /* We don't bother with "Up" links for leaf-level 1,
1092 * as they would be identical to the "Contents" links. */
1093 if (conf
.leaf_level
>= 2) {
1094 htmlsect
*p
= f
->first
->parent
;
1095 assert(p
== f
->last
->parent
);
1096 html_text(&ho
, conf
.nav_separator
);
1098 element_open(&ho
, "a");
1099 element_attr(&ho
, "href", p
->file
->filename
);
1101 html_text(&ho
, conf
.nav_up_text
);
1103 element_close(&ho
, "a");
1107 if (has_index
&& files
.index
) {
1108 html_text(&ho
, conf
.nav_separator
);
1109 if (f
!= files
.index
) {
1110 element_open(&ho
, "a");
1111 element_attr(&ho
, "href", files
.index
->filename
);
1113 html_text(&ho
, conf
.index_text
);
1114 if (f
!= files
.index
)
1115 element_close(&ho
, "a");
1118 html_text(&ho
, conf
.nav_separator
);
1121 element_open(&ho
, "a");
1122 element_attr(&ho
, "href", f
->next
->filename
);
1124 html_text(&ho
, conf
.nav_next_text
);
1126 element_close(&ho
, "a");
1128 element_close(&ho
, "p");
1134 * Write out a prefix TOC for the file (if a leaf file).
1136 * We start by going through the section list and
1137 * collecting the sections which need to be added to
1138 * the contents. On the way, we also test to see if
1139 * this file is a leaf file (defined as one which
1140 * contains all descendants of any section it
1141 * contains), because this will play a part in our
1142 * decision on whether or not to _output_ the TOC.
1144 * Special case: we absolutely do not do this if we're
1145 * in single-file mode.
1147 if (files
.head
!= files
.tail
) {
1148 int ntoc
= 0, tocsize
= 0;
1149 htmlsect
**toc
= NULL
;
1152 for (s
= sects
.head
; s
; s
= s
->next
) {
1157 * Search up from this section until we find
1158 * the highest-level one which belongs in this
1163 for (ac
= s
; ac
; ac
= ac
->parent
) {
1164 if (ac
->file
== f
) {
1171 if (s
->file
!= f
&& a
!= NULL
)
1175 if (adepth
<= a
->contents_depth
) {
1176 if (ntoc
>= tocsize
) {
1178 toc
= sresize(toc
, tocsize
, htmlsect
*);
1185 if (leaf
&& conf
.leaf_contains_contents
&&
1186 ntoc
>= conf
.leaf_smallest_contents
) {
1189 for (i
= 0; i
< ntoc
; i
++) {
1190 htmlsect
*s
= toc
[i
];
1191 int hlevel
= (s
->type
== TOP ?
-1 :
1192 s
->type
== INDEX ?
0 :
1193 heading_depth(s
->title
))
1194 - f
->min_heading_depth
+ 1;
1196 assert(hlevel
>= 1);
1197 html_contents_entry(&ho
, hlevel
, s
,
1198 f
, keywords
, &conf
);
1200 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1205 * Now go through the document and output some real
1209 for (s
= sects
.head
; s
; s
= s
->next
) {
1212 * This section belongs in this file.
1218 * Doesn't belong in this file, but it may be
1219 * a descendant of a section which does, in
1220 * which case we should consider it for the
1221 * main TOC of this file (for non-leaf files).
1229 * Search up from this section until we find
1230 * the highest-level one which belongs in this
1235 for (ac
= s
; ac
; ac
= ac
->parent
) {
1236 if (ac
->file
== f
) {
1245 * This section does not belong in this
1246 * file, but an ancestor of it does. Write
1247 * out a contents table entry, if the depth
1248 * doesn't exceed the maximum contents
1249 * depth for the ancestor section.
1251 if (adepth
<= a
->contents_depth
) {
1252 html_contents_entry(&ho
, adepth
, s
,
1253 f
, keywords
, &conf
);
1262 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1265 * Display the section heading.
1268 hlevel
= (s
->type
== TOP ?
-1 :
1269 s
->type
== INDEX ?
0 :
1270 heading_depth(s
->title
))
1271 - f
->min_heading_depth
+ 1;
1272 assert(hlevel
>= 1);
1273 /* HTML headings only go up to <h6> */
1277 htag
[1] = '0' + hlevel
;
1279 element_open(&ho
, htag
);
1282 * Provide anchor(s) for cross-links to target.
1284 * (Also we'll have to do this separately in
1285 * other paragraph types - NumberedList and
1290 for (i
=0; i
< conf
.ntfragments
; i
++)
1291 if (s
->fragments
[i
])
1292 html_fragment(&ho
, s
->fragments
[i
]);
1295 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1297 element_close(&ho
, htag
);
1300 * Now display the section text.
1303 stackhead
= snew(struct stackelement
);
1304 stackhead
->next
= NULL
;
1305 stackhead
->listtype
= NOLIST
;
1306 stackhead
->itemtype
= NOITEM
;
1308 for (p
= s
->text
;; p
= p
->next
) {
1309 enum LISTTYPE listtype
;
1310 struct stackelement
*se
;
1313 * Preliminary switch to figure out what
1314 * sort of list we expect to be inside at
1317 * Since p may still be NULL at this point,
1318 * I invent a harmless paragraph type for
1321 switch (p ? p
->type
: para_Normal
) {
1324 case para_Copyright
:
1325 case para_BiblioCited
:
1327 case para_QuotePush
:
1331 case para_UnnumberedChapter
:
1342 case para_NumberedList
:
1346 case para_DescribedThing
:
1347 case para_Description
:
1351 case para_LcontPush
:
1352 se
= snew(struct stackelement
);
1353 se
->next
= stackhead
;
1354 se
->listtype
= NOLIST
;
1355 se
->itemtype
= NOITEM
;
1359 default: /* some totally non-printing para */
1366 * Terminate the most recent list item, if
1367 * any. (We left this until after
1368 * processing LcontPush, since in that case
1369 * the list item won't want to be
1370 * terminated until after the corresponding
1373 if (stackhead
->itemtype
!= NOITEM
) {
1374 element_close(&ho
, itemname(stackhead
->itemtype
));
1377 stackhead
->itemtype
= NOITEM
;
1380 * Terminate the current list, if it's not
1381 * the one we want to be in.
1383 if (listtype
!= stackhead
->listtype
&&
1384 stackhead
->listtype
!= NOLIST
) {
1385 element_close(&ho
, listname(stackhead
->listtype
));
1390 * Leave the loop if our time has come.
1392 if (!p
|| (is_heading_type(p
->type
) &&
1393 p
->type
!= para_Title
))
1394 break; /* end of section text */
1397 * Start a fresh list if necessary.
1399 if (listtype
!= stackhead
->listtype
&&
1401 element_open(&ho
, listname(listtype
));
1403 stackhead
->listtype
= listtype
;
1407 element_empty(&ho
, "hr");
1410 html_codepara(&ho
, p
->words
);
1413 case para_Copyright
:
1414 element_open(&ho
, "p");
1416 html_words(&ho
, p
->words
, ALL
,
1417 f
, keywords
, &conf
);
1419 element_close(&ho
, "p");
1421 case para_BiblioCited
:
1422 element_open(&ho
, "p");
1423 if (p
->private_data
) {
1424 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1426 for (i
=0; i
< conf
.ntfragments
; i
++)
1427 if (s
->fragments
[i
])
1428 html_fragment(&ho
, s
->fragments
[i
]);
1431 html_words(&ho
, p
->kwtext
, ALL
,
1432 f
, keywords
, &conf
);
1433 html_text(&ho
, L
" ");
1434 html_words(&ho
, p
->words
, ALL
,
1435 f
, keywords
, &conf
);
1437 element_close(&ho
, "p");
1440 case para_NumberedList
:
1441 element_open(&ho
, "li");
1442 if (p
->private_data
) {
1443 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1445 for (i
=0; i
< conf
.ntfragments
; i
++)
1446 if (s
->fragments
[i
])
1447 html_fragment(&ho
, s
->fragments
[i
]);
1450 stackhead
->itemtype
= LI
;
1451 html_words(&ho
, p
->words
, ALL
,
1452 f
, keywords
, &conf
);
1454 case para_DescribedThing
:
1455 element_open(&ho
, "dt");
1457 stackhead
->itemtype
= DT
;
1458 html_words(&ho
, p
->words
, ALL
,
1459 f
, keywords
, &conf
);
1461 case para_Description
:
1462 element_open(&ho
, "dd");
1464 stackhead
->itemtype
= DD
;
1465 html_words(&ho
, p
->words
, ALL
,
1466 f
, keywords
, &conf
);
1469 case para_QuotePush
:
1470 element_open(&ho
, "blockquote");
1473 element_close(&ho
, "blockquote");
1478 stackhead
= stackhead
->next
;
1485 assert(stackhead
&& !stackhead
->next
);
1489 if (s
->type
== INDEX
) {
1494 * This section is the index. I'll just
1495 * render it as a single paragraph, with a
1496 * colon between the index term and the
1497 * references, and <br> in between each
1500 element_open(&ho
, "p");
1502 for (i
= 0; (entry
=
1503 index234(idx
->entries
, i
)) != NULL
; i
++) {
1504 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1508 element_empty(&ho
, "br");
1511 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1512 f
, keywords
, &conf
);
1514 html_text(&ho
, conf
.index_main_sep
);
1516 for (j
= 0; j
< hi
->nrefs
; j
++) {
1518 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1519 paragraph
*p
= hr
->section
->title
;
1522 html_text(&ho
, conf
.index_multi_sep
);
1524 html_href(&ho
, f
, hr
->section
->file
,
1526 hr
->referenced
= TRUE
;
1528 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1529 f
, keywords
, &conf
);
1530 else if (p
&& p
->words
)
1531 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1532 f
, keywords
, &conf
);
1535 * If there is no title at all,
1536 * this must be because our
1537 * target section is the
1538 * preamble section and there
1539 * is no title. So we use the
1542 html_text(&ho
, conf
.preamble_text
);
1544 element_close(&ho
, "a");
1547 element_close(&ho
, "p");
1552 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1559 int done_version_ids
= FALSE
;
1561 if (conf
.address_section
)
1562 element_empty(&ho
, "hr");
1565 html_raw(&ho
, conf
.body_end
);
1567 if (conf
.address_section
) {
1568 int started
= FALSE
;
1569 if (conf
.htmlver
== ISO_HTML
) {
1571 * The ISO-HTML validator complains if
1572 * there isn't a <div> tag surrounding the
1573 * <address> tag. I'm uncertain of why this
1574 * should be - there appears to be no
1575 * mention of this in the ISO-HTML spec,
1576 * suggesting that it doesn't represent a
1577 * change from HTML 4, but nonetheless the
1578 * HTML 4 validator doesn't seem to mind.
1580 element_open(&ho
, "div");
1582 element_open(&ho
, "address");
1583 if (conf
.addr_start
) {
1584 html_raw(&ho
, conf
.addr_start
);
1588 if (conf
.visible_version_id
) {
1589 for (p
= sourceform
; p
; p
= p
->next
)
1590 if (p
->type
== para_VersionID
) {
1592 element_empty(&ho
, "br");
1594 html_text(&ho
, conf
.pre_versionid
);
1595 html_words(&ho
, p
->words
, NOTHING
,
1596 f
, keywords
, &conf
);
1597 html_text(&ho
, conf
.post_versionid
);
1600 done_version_ids
= TRUE
;
1602 if (conf
.addr_end
) {
1604 element_empty(&ho
, "br");
1605 html_raw(&ho
, conf
.addr_end
);
1607 element_close(&ho
, "address");
1608 if (conf
.htmlver
== ISO_HTML
)
1609 element_close(&ho
, "div");
1612 if (!done_version_ids
) {
1614 * If the user didn't want the version IDs
1615 * visible, I think we still have a duty to put
1616 * them in an HTML comment.
1618 int started
= FALSE
;
1619 for (p
= sourceform
; p
; p
= p
->next
)
1620 if (p
->type
== para_VersionID
) {
1622 html_raw(&ho
, "<!-- version IDs:\n");
1625 html_words(&ho
, p
->words
, NOTHING
,
1626 f
, keywords
, &conf
);
1630 html_raw(&ho
, "-->\n");
1634 element_close(&ho
, "body");
1636 element_close(&ho
, "html");
1643 * Before we start outputting the HTML Help files, check
1644 * whether there's even going to _be_ an index file: we omit it
1645 * if the index contains nothing.
1647 hhk_filename
= conf
.hhk_filename
;
1653 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1654 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1656 if (hi
->nrefs
> 0) {
1657 ok
= TRUE
; /* found an index entry */
1663 hhk_filename
= NULL
;
1667 * Output the MS HTML Help supporting files, if requested.
1669 * A good unofficial reference for these is <http://chmspec.nongnu.org/>.
1671 if (conf
.hhp_filename
) {
1675 ho
.charset
= CS_CP1252
; /* as far as I know, HHP files are */
1676 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1677 ho
.cstate
= charset_init_state
;
1678 ho
.ver
= HTML_4
; /* *shrug* */
1679 ho
.state
= HO_NEUTRAL
;
1680 ho
.contents_level
= 0;
1681 ho
.hackflags
= HO_HACK_QUOTENOTHING
;
1683 ho
.fp
= fopen(conf
.hhp_filename
, "w");
1685 error(err_cantopenw
, conf
.hhp_filename
);
1689 /* Binary TOC required for Next/Previous nav to work */
1691 "Compatibility=1.1 or later\n"
1692 "Compiled file=%s\n"
1693 "Default Window=main\n"
1694 "Default topic=%s\n"
1695 "Display compile progress=Yes\n"
1696 "Full-text search=Yes\n"
1697 "Title=", conf
.chm_filename
, files
.head
->filename
);
1700 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1701 NULL
, keywords
, &conf
);
1703 fprintf(ho
.fp
, "\n");
1706 * These two entries don't seem to be remotely necessary
1707 * for a successful run of the help _compiler_, but
1708 * omitting them causes the GUI Help Workshop to behave
1709 * rather strangely if you try to load the help project
1710 * into that and edit it.
1712 if (conf
.hhc_filename
)
1713 fprintf(ho
.fp
, "Contents file=%s\n", conf
.hhc_filename
);
1715 fprintf(ho
.fp
, "Index file=%s\n", hhk_filename
);
1717 fprintf(ho
.fp
, "\n[WINDOWS]\nmain=\"");
1719 ho
.hackflags
|= HO_HACK_OMITQUOTES
;
1721 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1722 NULL
, keywords
, &conf
);
1724 fprintf(ho
.fp
, "\",\"%s\",\"%s\",\"%s\",,,,,,"
1725 /* This first magic number is fsWinProperties, controlling
1726 * Navigation Pane options and the like.
1727 * Constants HHWIN_PROP_* in htmlhelp.h. */
1729 /* This second number is fsToolBarFlags, mainly controlling
1730 * toolbar buttons. Constants HHWIN_BUTTON_*.
1731 * NOTE: there are two pairs of bits for Next/Previous
1732 * buttons: 7/8 (which do nothing useful), and 21/22
1733 * (which work). (Neither of these are exposed in the HHW
1734 * UI, but they work fine in HH.) We use the latter. */
1735 "0x60304e,,,,,,,,0\n",
1736 conf
.hhc_filename ? conf
.hhc_filename
: "",
1737 hhk_filename ? hhk_filename
: "",
1738 files
.head
->filename
);
1741 * The [FILES] section is also not necessary for
1742 * compilation (hhc appears to build up a list of needed
1743 * files just by following links from the given starting
1744 * points), but useful for loading the project into HHW.
1746 fprintf(ho
.fp
, "\n[FILES]\n");
1747 for (f
= files
.head
; f
; f
= f
->next
)
1748 fprintf(ho
.fp
, "%s\n", f
->filename
);
1752 if (conf
.hhc_filename
) {
1758 ho
.fp
= fopen(conf
.hhc_filename
, "w");
1760 error(err_cantopenw
, conf
.hhc_filename
);
1762 ho
.charset
= CS_CP1252
; /* as far as I know, HHC files are */
1763 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1764 ho
.cstate
= charset_init_state
;
1765 ho
.ver
= HTML_4
; /* *shrug* */
1766 ho
.state
= HO_NEUTRAL
;
1767 ho
.contents_level
= 0;
1768 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1771 * Magic DOCTYPE which seems to work for .HHC files. I'm
1772 * wary of trying to change it!
1774 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1776 "<META HTTP-EQUIV=\"Content-Type\" "
1777 "CONTENT=\"text/html; charset=%s\">\n"
1778 "</HEAD><BODY><UL>\n",
1779 charset_to_mimeenc(conf
.output_charset
));
1781 for (f
= files
.head
; f
; f
= f
->next
) {
1783 * For each HTML file, write out a contents entry.
1785 int depth
, leaf
= TRUE
;
1788 * Determine the depth of this file in the contents
1791 * If the file contains no sections, it is assumed to
1796 for (a
= f
->first
->parent
; a
&& a
->type
!= TOP
; a
= a
->parent
)
1800 * Determine if this file is a leaf file, by
1801 * trawling the section list to see if there's any
1802 * section with an ancestor in this file but which
1803 * is not itself in this file.
1805 * Special case: for contents purposes, the TOP
1806 * file is not considered to be the parent of the
1807 * chapter files, so it's always a leaf.
1809 * A file with no sections in it is also a leaf.
1811 if (f
->first
&& f
->first
->type
!= TOP
) {
1812 for (s
= f
->first
; s
; s
= s
->next
) {
1815 if (leaf
&& s
->file
!= f
) {
1816 for (a
= s
; a
; a
= a
->parent
)
1826 * Now write out our contents entry.
1828 while (currdepth
< depth
) {
1829 fprintf(ho
.fp
, "<UL>\n");
1832 while (currdepth
> depth
) {
1833 fprintf(ho
.fp
, "</UL>\n");
1836 /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */
1837 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">"
1838 "<PARAM NAME=\"Name\" VALUE=\"");
1840 if (f
->first
->title
)
1841 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
1842 NULL
, keywords
, &conf
);
1843 else if (f
->first
->type
== INDEX
)
1844 html_text(&ho
, conf
.index_text
);
1845 fprintf(ho
.fp
, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">"
1846 "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n",
1847 f
->filename
, leaf ?
11 : 1);
1850 while (currdepth
> 0) {
1851 fprintf(ho
.fp
, "</UL>\n");
1855 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1866 * First make a pass over all HTML files and set their
1867 * `temp' fields to zero, because we're about to use them.
1869 for (f
= files
.head
; f
; f
= f
->next
)
1872 ho
.fp
= fopen(hhk_filename
, "w");
1874 error(err_cantopenw
, hhk_filename
);
1876 ho
.charset
= CS_CP1252
; /* as far as I know, HHK files are */
1877 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1878 ho
.cstate
= charset_init_state
;
1879 ho
.ver
= HTML_4
; /* *shrug* */
1880 ho
.state
= HO_NEUTRAL
;
1881 ho
.contents_level
= 0;
1882 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1885 * Magic DOCTYPE which seems to work for .HHK files. I'm
1886 * wary of trying to change it!
1888 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1890 "<META HTTP-EQUIV=\"Content-Type\" "
1891 "CONTENT=\"text/html; charset=%s\">\n"
1892 "</HEAD><BODY><UL>\n",
1893 charset_to_mimeenc(conf
.output_charset
));
1896 * Go through the index terms and output each one.
1898 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1899 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1902 if (hi
->nrefs
> 0) {
1903 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
1904 "<PARAM NAME=\"Name\" VALUE=\"");
1906 html_words(&ho
, entry
->text
, NOTHING
,
1907 NULL
, keywords
, &conf
);
1908 fprintf(ho
.fp
, "\">\n");
1910 for (j
= 0; j
< hi
->nrefs
; j
++) {
1912 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1915 * Use the temp field to ensure we don't
1916 * reference the same file more than once.
1918 if (!hr
->section
->file
->temp
) {
1919 fprintf(ho
.fp
, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n",
1920 hr
->section
->file
->filename
);
1921 hr
->section
->file
->temp
= 1;
1924 hr
->referenced
= TRUE
;
1927 fprintf(ho
.fp
, "</OBJECT>\n");
1930 * Now go through those files and re-clear the temp
1931 * fields ready for the _next_ index term.
1933 for (j
= 0; j
< hi
->nrefs
; j
++) {
1935 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1936 hr
->section
->file
->temp
= 0;
1941 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1946 * Go through and check that no index fragments were referenced
1947 * without being generated, or indeed vice versa.
1949 * (When I actually get round to freeing everything, this can
1950 * probably be the freeing loop as well.)
1952 for (p
= sourceform
; p
; p
= p
->next
) {
1954 for (w
= p
->words
; w
; w
= w
->next
)
1955 if (w
->type
== word_IndexRef
) {
1956 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1958 assert(!hr
->referenced
== !hr
->generated
);
1963 * Free all the working data.
1967 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1969 * frag->fragment is dynamically allocated, but will be
1970 * freed when we process the htmlsect structure which
1971 * it is attached to.
1975 freetree234(files
.frags
);
1978 * The strings in files.files are all owned by their containing
1979 * htmlfile structures, so there's no need to free them here.
1981 freetree234(files
.files
);
1983 htmlsect
*sect
, *tmp
;
1988 for (i
=0; i
< conf
.ntfragments
; i
++)
1989 sfree(sect
->fragments
[i
]);
1990 sfree(sect
->fragments
);
1994 sect
= nonsects
.head
;
1998 for (i
=0; i
< conf
.ntfragments
; i
++)
1999 sfree(sect
->fragments
[i
]);
2000 sfree(sect
->fragments
);
2006 htmlfile
*file
, *tmp
;
2010 sfree(file
->filename
);
2018 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
2019 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
2026 for (p
= sourceform
; p
; p
= p
->next
)
2027 for (w
= p
->words
; w
; w
= w
->next
)
2028 if (w
->type
== word_IndexRef
) {
2029 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2031 sfree(hr
->fragment
);
2036 sfree(conf
.single_filename
);
2037 sfree(conf
.contents_filename
);
2038 sfree(conf
.index_filename
);
2039 sfree(conf
.template_filename
);
2040 while (conf
.ntfragments
--)
2041 sfree(conf
.template_fragments
[conf
.ntfragments
]);
2042 sfree(conf
.template_fragments
);
2045 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
2046 htmlsect
*sect
, int depth
)
2052 * `depth' is derived from the heading_depth() macro at the top
2053 * of this file, which counts title as -1, chapter as 0,
2054 * heading as 1 and subsection as 2. However, the semantics of
2055 * cfg->leaf_level are defined to count chapter as 1, heading
2056 * as 2 etc. So first I increment depth :-(
2060 if (cfg
->leaf_level
== 0) {
2062 * leaf_level==0 is a special case, in which everything is
2063 * put into a single file.
2066 files
->single
= html_new_file(files
, cfg
->single_filename
);
2068 file
= files
->single
;
2071 * If the depth of this section is at or above leaf_level,
2072 * we invent a fresh file and put this section at its head.
2073 * Otherwise, we put it in the same file as its parent
2076 * Another special value of cfg->leaf_level is -1, which
2077 * means infinity (i.e. it's considered to always be
2078 * greater than depth).
2080 if (cfg
->leaf_level
> 0 && ldepth
> cfg
->leaf_level
) {
2082 * We know that sect->parent cannot be NULL. The only
2083 * circumstance in which it can be is if sect is at
2084 * chapter or appendix level, i.e. ldepth==1; and if
2085 * that's the case, then we cannot have entered this
2086 * branch unless cfg->leaf_level==0, in which case we
2087 * would be in the single-file case above and not here
2090 assert(sect
->parent
);
2092 file
= sect
->parent
->file
;
2094 if (sect
->type
== TOP
) {
2095 file
= html_new_file(files
, cfg
->contents_filename
);
2096 } else if (sect
->type
== INDEX
) {
2097 file
= html_new_file(files
, cfg
->index_filename
);
2101 assert(ldepth
> 0 && sect
->title
);
2102 title
= html_format(sect
->title
, cfg
->template_filename
);
2103 file
= html_new_file(files
, title
);
2111 if (file
->min_heading_depth
> depth
) {
2113 * This heading is at a higher level than any heading we
2114 * have so far placed in this file; so we set the `first'
2117 file
->min_heading_depth
= depth
;
2121 if (file
->min_heading_depth
== depth
)
2125 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
2127 htmlfile
*ret
= snew(htmlfile
);
2131 list
->tail
->next
= ret
;
2136 ret
->filename
= html_sanitise_filename(list
, dupstr(filename
));
2137 add234(list
->files
, ret
->filename
);
2138 ret
->last_fragment_number
= 0;
2139 ret
->min_heading_depth
= INT_MAX
;
2140 ret
->first
= ret
->last
= NULL
;
2145 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
2148 htmlsect
*ret
= snew(htmlsect
);
2152 list
->tail
->next
= ret
;
2162 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
2165 for (i
=0; i
< cfg
->ntfragments
; i
++)
2166 ret
->fragments
[i
] = NULL
;
2172 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
2173 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
2179 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
2180 case word_HyperLink
:
2181 if (flags
& LINKS
) {
2182 element_open(ho
, "a");
2183 c
= utoa_dup(w
->text
, CS_ASCII
);
2184 element_attr(ho
, "href", c
);
2188 case word_UpperXref
:
2189 case word_LowerXref
:
2190 if (flags
& LINKS
) {
2191 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
2197 s
= (htmlsect
*)p
->private_data
;
2201 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
2207 element_close(ho
, "a");
2210 if (flags
& INDEXENTS
) {
2211 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2212 html_fragment(ho
, hr
->fragment
);
2213 hr
->generated
= TRUE
;
2220 case word_WhiteSpace
:
2221 case word_EmphSpace
:
2222 case word_CodeSpace
:
2223 case word_WkCodeSpace
:
2225 case word_EmphQuote
:
2226 case word_CodeQuote
:
2227 case word_WkCodeQuote
:
2228 style
= towordstyle(w
->type
);
2229 type
= removeattr(w
->type
);
2230 if (style
== word_Emph
&&
2231 (attraux(w
->aux
) == attr_First
||
2232 attraux(w
->aux
) == attr_Only
) &&
2234 element_open(ho
, "em");
2235 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2236 (attraux(w
->aux
) == attr_First
||
2237 attraux(w
->aux
) == attr_Only
) &&
2239 element_open(ho
, "code");
2241 if (type
== word_WhiteSpace
)
2242 html_text(ho
, L
" ");
2243 else if (type
== word_Quote
) {
2244 if (quoteaux(w
->aux
) == quote_Open
)
2245 html_text(ho
, cfg
->lquote
);
2247 html_text(ho
, cfg
->rquote
);
2249 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
2250 html_text_nbsp(ho
, w
->text
);
2252 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
2255 if (style
== word_Emph
&&
2256 (attraux(w
->aux
) == attr_Last
||
2257 attraux(w
->aux
) == attr_Only
) &&
2259 element_close(ho
, "em");
2260 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2261 (attraux(w
->aux
) == attr_Last
||
2262 attraux(w
->aux
) == attr_Only
) &&
2264 element_close(ho
, "code");
2270 static void html_codepara(htmloutput
*ho
, word
*words
)
2272 element_open(ho
, "pre");
2273 element_open(ho
, "code");
2274 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
2279 if (words
->next
&& words
->next
->type
== word_Emph
) {
2280 e
= words
->next
->text
;
2281 words
= words
->next
;
2285 while (e
&& *e
&& *t
) {
2289 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
2297 element_open(ho
, open_tag
);
2299 html_text_limit(ho
, t
, n
);
2302 element_close(ho
, open_tag
);
2310 element_close(ho
, "code");
2311 element_close(ho
, "pre");
2314 static void html_charset_cleanup(htmloutput
*ho
)
2319 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
2320 ho
->charset
, &ho
->cstate
, NULL
);
2321 if (ho
->fp
&& bytes
> 0)
2322 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2325 static void return_mostly_to_neutral(htmloutput
*ho
)
2328 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
2329 fprintf(ho
->fp
, " />");
2330 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
2331 fprintf(ho
->fp
, ">");
2335 ho
->state
= HO_NEUTRAL
;
2338 static void return_to_neutral(htmloutput
*ho
)
2340 if (ho
->state
== HO_IN_TEXT
) {
2341 html_charset_cleanup(ho
);
2344 return_mostly_to_neutral(ho
);
2347 static void element_open(htmloutput
*ho
, char const *name
)
2349 return_to_neutral(ho
);
2351 fprintf(ho
->fp
, "<%s", name
);
2352 ho
->state
= HO_IN_TAG
;
2355 static void element_close(htmloutput
*ho
, char const *name
)
2357 return_to_neutral(ho
);
2359 fprintf(ho
->fp
, "</%s>", name
);
2360 ho
->state
= HO_NEUTRAL
;
2363 static void element_empty(htmloutput
*ho
, char const *name
)
2365 return_to_neutral(ho
);
2367 fprintf(ho
->fp
, "<%s", name
);
2368 ho
->state
= HO_IN_EMPTY_TAG
;
2371 static void html_nl(htmloutput
*ho
)
2373 return_to_neutral(ho
);
2375 fputc('\n', ho
->fp
);
2378 static void html_raw(htmloutput
*ho
, char *text
)
2380 return_to_neutral(ho
);
2382 fputs(text
, ho
->fp
);
2385 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
2387 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2390 fputs(text
, ho
->fp
);
2394 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
2396 html_charset_cleanup(ho
);
2397 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2399 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
2402 static void element_attr_w(htmloutput
*ho
, char const *name
,
2403 wchar_t const *value
)
2405 html_charset_cleanup(ho
);
2407 fprintf(ho
->fp
, " %s=\"", name
);
2408 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
2409 html_charset_cleanup(ho
);
2414 static void html_text(htmloutput
*ho
, wchar_t const *text
)
2416 return_mostly_to_neutral(ho
);
2417 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
2420 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
2422 return_mostly_to_neutral(ho
);
2423 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
2426 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
2428 return_mostly_to_neutral(ho
);
2429 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
2432 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
2433 int maxlen
, int quote_quotes
, int nbsp
)
2435 int textlen
= ustrlen(text
);
2439 if (ho
->hackflags
& (HO_HACK_QUOTEQUOTES
| HO_HACK_OMITQUOTES
))
2440 quote_quotes
= TRUE
; /* override the input value */
2442 if (maxlen
> 0 && textlen
> maxlen
)
2444 if (ho
->hacklimit
>= 0) {
2445 if (textlen
> ho
->hacklimit
)
2446 textlen
= ho
->hacklimit
;
2447 ho
->hacklimit
-= textlen
;
2450 while (textlen
> 0) {
2451 /* Scan ahead for characters we really can't display in HTML. */
2452 int lenbefore
, lenafter
;
2453 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
2454 if (text
[lenbefore
] == L
'<' ||
2455 text
[lenbefore
] == L
'>' ||
2456 text
[lenbefore
] == L
'&' ||
2457 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
2458 (text
[lenbefore
] == L
' ' && nbsp
))
2460 lenafter
= lenbefore
;
2461 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
2462 ho
->charset
, &ho
->cstate
, &err
);
2463 textlen
-= (lenbefore
- lenafter
);
2464 if (bytes
> 0 && ho
->fp
)
2465 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2468 * We have encountered a character that cannot be
2469 * displayed in the selected output charset. Therefore,
2470 * we use an HTML numeric entity reference.
2472 assert(textlen
> 0);
2474 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
2476 } else if (lenafter
== 0 && textlen
> 0) {
2478 * We have encountered a character which is special to
2482 if (*text
== L
'"' && (ho
->hackflags
& HO_HACK_OMITQUOTES
)) {
2483 fputc('\'', ho
->fp
);
2484 } else if (ho
->hackflags
& HO_HACK_QUOTENOTHING
) {
2485 fputc(*text
, ho
->fp
);
2488 fprintf(ho
->fp
, "<");
2489 else if (*text
== L
'>')
2490 fprintf(ho
->fp
, ">");
2491 else if (*text
== L
'&')
2492 fprintf(ho
->fp
, "&");
2493 else if (*text
== L
'"')
2494 fprintf(ho
->fp
, """);
2495 else if (*text
== L
' ') {
2497 fprintf(ho
->fp
, " ");
2499 assert(!"Can't happen");
2507 static void cleanup(htmloutput
*ho
)
2509 return_to_neutral(ho
);
2514 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
2515 htmlfile
*targetfile
, char *targetfrag
)
2517 rdstringc rs
= { 0, 0, NULL
};
2520 if (targetfile
!= thisfile
)
2521 rdaddsc(&rs
, targetfile
->filename
);
2524 rdaddsc(&rs
, targetfrag
);
2528 element_open(ho
, "a");
2529 element_attr(ho
, "href", url
);
2533 static void html_fragment(htmloutput
*ho
, char const *fragment
)
2535 element_open(ho
, "a");
2536 element_attr(ho
, "name", fragment
);
2537 if (is_xhtml(ho
->ver
))
2538 element_attr(ho
, "id", fragment
);
2539 element_close(ho
, "a");
2542 static char *html_format(paragraph
*p
, char *template_string
)
2546 wchar_t *ws
, wsbuf
[2];
2547 rdstringc rs
= { 0, 0, NULL
};
2549 t
= template_string
;
2551 if (*t
== '%' && t
[1]) {
2565 if (p
->kwtext
&& fmt
== 'n')
2567 else if (p
->kwtext2
&& fmt
== 'b') {
2569 * HTML fragment names must start with a letter, so
2570 * simply `1.2.3' is not adequate. In this case I'm
2571 * going to cheat slightly by prepending the first
2572 * character of the first word of kwtext, so that
2573 * we get `C1' for chapter 1, `S2.3' for section
2576 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2579 wsbuf
[0] = p
->kwtext
->text
[0];
2582 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2585 /* %N comes here; also failure cases of other fmts */
2589 c
= utoa_dup(ws
, CS_ASCII
);
2595 if (removeattr(w
->type
) == word_Normal
) {
2596 c
= utoa_dup(w
->text
, CS_ASCII
);
2607 return rdtrimc(&rs
);
2610 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2614 * The HTML 4 spec's strictest definition of fragment names (<a
2615 * name> and "id" attributes) says that they `must begin with a
2616 * letter and may be followed by any number of letters, digits,
2617 * hyphens, underscores, colons, and periods'.
2619 * So here we unceremoniously rip out any characters not
2620 * conforming to this limitation.
2622 char *p
= text
, *q
= text
;
2624 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2626 if ((*q
++ = *p
++) != '\0') {
2628 if ((*p
>='A' && *p
<='Z') ||
2629 (*p
>='a' && *p
<='z') ||
2630 (*p
>='0' && *p
<='9') ||
2631 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2639 /* If there's nothing left, make something valid up */
2641 static const char anonfrag
[] = "anon";
2642 text
= sresize(text
, lenof(anonfrag
), char);
2643 strcpy(text
, anonfrag
);
2647 * Now we check for clashes with other fragment names, and
2648 * adjust this one if necessary by appending a hyphen followed
2652 htmlfragment
*frag
= snew(htmlfragment
);
2653 int len
= 0; /* >0 indicates we have resized */
2657 frag
->fragment
= text
;
2659 while (add234(files
->frags
, frag
) != frag
) {
2662 frag
->fragment
= text
= sresize(text
, len
+20, char);
2665 sprintf(text
+ len
, "-%d", ++suffix
);
2672 static char *html_sanitise_filename(htmlfilelist
*files
, char *text
)
2675 * Unceremoniously rip out any character that might cause
2676 * difficulty in some filesystem or another, or be otherwise
2679 * That doesn't leave much punctuation. I permit alphanumerics
2682 char *p
= text
, *q
= text
;
2685 if ((*p
>='A' && *p
<='Z') ||
2686 (*p
>='a' && *p
<='z') ||
2687 (*p
>='0' && *p
<='9') ||
2688 *p
=='-' || *p
=='_' || *p
=='+' || *p
=='.' || *p
=='=')
2694 /* If there's nothing left, make something valid up */
2696 static const char anonfrag
[] = "anon.html";
2697 text
= sresize(text
, lenof(anonfrag
), char);
2698 strcpy(text
, anonfrag
);
2702 * Now we check for clashes with other filenames, and adjust
2703 * this one if necessary by appending a hyphen followed by a
2704 * number just before the file extension (if any).
2712 while (find234(files
->files
, text
, NULL
)) {
2716 text
= snewn(len
+20, char);
2718 for (extpos
= len
; extpos
> 0 && p
[extpos
-1] != '.'; extpos
--);
2725 sprintf(text
, "%.*s-%d%s", extpos
, p
, ++suffix
, p
+extpos
);
2735 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2736 htmlfile
*thisfile
, keywordlist
*keywords
,
2739 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2740 element_close(ho
, "li");
2744 while (ho
->contents_level
> depth
) {
2745 element_close(ho
, "ul");
2746 ho
->contents_level
--;
2747 if (ho
->contents_level
> 0) {
2748 element_close(ho
, "li");
2753 while (ho
->contents_level
< depth
) {
2755 element_open(ho
, "ul");
2757 ho
->contents_level
++;
2763 element_open(ho
, "li");
2764 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2765 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2766 element_close(ho
, "a");
2767 /* <li> will be closed by a later invocation */
2770 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2771 keywordlist
*keywords
, htmlconfig
*cfg
,
2777 int depth
= heading_depth(s
->title
);
2781 else if (depth
== 0)
2782 sl
= &cfg
->achapter
;
2783 else if (depth
<= cfg
->nasect
)
2784 sl
= &cfg
->asect
[depth
-1];
2786 sl
= &cfg
->asect
[cfg
->nasect
-1];
2788 if (!sl
|| !sl
->number_at_all
)
2790 else if (sl
->just_numbers
)
2791 number
= s
->title
->kwtext2
;
2793 number
= s
->title
->kwtext
;
2796 html_words(ho
, number
, MARKUP
,
2797 thisfile
, keywords
, cfg
);
2798 html_text(ho
, sl
->number_suffix
);
2801 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2802 thisfile
, keywords
, cfg
);
2804 assert(s
->type
!= NORMAL
);
2806 * If we're printing the full document title for _real_ and
2807 * there isn't one, we don't want to print `Preamble' at
2808 * the top of what ought to just be some text. If we need
2809 * it in any other context such as TOCs, we need to print
2812 if (s
->type
== TOP
&& !real
)
2813 html_text(ho
, cfg
->preamble_text
);
2814 else if (s
->type
== INDEX
)
2815 html_text(ho
, cfg
->index_text
);