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 )
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
;
52 char *contents_filename
;
54 char *template_filename
;
55 char *single_filename
;
56 char *chm_filename
, *hhp_filename
, *hhc_filename
, *hhk_filename
;
57 char **template_fragments
;
59 char *head_end
, *body_start
, *body_end
, *addr_start
, *addr_end
;
60 char *body_tag
, *nav_attr
;
61 wchar_t *author
, *description
;
62 wchar_t *index_text
, *contents_text
, *preamble_text
, *title_separator
;
63 wchar_t *nav_prev_text
, *nav_next_text
, *nav_separator
;
64 wchar_t *index_main_sep
, *index_multi_sep
;
65 wchar_t *pre_versionid
, *post_versionid
;
66 int restrict_charset
, output_charset
;
68 HTML_3_2
, HTML_4
, ISO_HTML
,
69 XHTML_1_0_TRANSITIONAL
, XHTML_1_0_STRICT
71 wchar_t *lquote
, *rquote
;
75 #define contents_depth(conf, level) \
76 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
78 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
80 typedef struct htmlfile htmlfile
;
81 typedef struct htmlsect htmlsect
;
86 int last_fragment_number
;
87 int min_heading_depth
;
88 htmlsect
*first
, *last
; /* first/last highest-level sections */
90 * The `temp' field is available for use in individual passes
91 * over the file list. For example, the HHK index generation
92 * uses it to ensure no index term references the same file
99 htmlsect
*next
, *parent
;
101 paragraph
*title
, *text
;
102 enum { NORMAL
, TOP
, INDEX
} type
;
108 htmlfile
*head
, *tail
;
109 htmlfile
*single
, *index
;
115 htmlsect
*head
, *tail
;
131 int generated
, referenced
;
136 * This level deals with charset conversion, starting and
137 * ending tags, and writing to the file. It's the lexical
141 int charset
, restrict_charset
;
142 charset_state cstate
;
145 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
147 int hackflags
; /* used for icky .HH* stuff */
148 int hacklimit
; /* text size limit, again for .HH* */
150 * Stuff beyond here deals with the higher syntactic level: it
151 * tracks how many levels of <ul> are currently open when
152 * producing a contents list, for example.
158 * Nasty hacks that modify the behaviour of htmloutput files. All
159 * of these are flag bits set in ho.hackflags. HO_HACK_QUOTEQUOTES
160 * has the same effect as the `quote_quotes' parameter to
161 * html_text_limit_internal, except that it's set globally on an
162 * entire htmloutput structure; HO_HACK_QUOTENOTHING suppresses
163 * quoting of any HTML special characters (for .HHP files);
164 * HO_HACK_OMITQUOTES completely suppresses the generation of
165 * double quotes at all (turning them into single quotes, for want
168 #define HO_HACK_QUOTEQUOTES 1
169 #define HO_HACK_QUOTENOTHING 2
170 #define HO_HACK_OMITQUOTES 4
172 static int html_fragment_compare(void *av
, void *bv
)
174 htmlfragment
*a
= (htmlfragment
*)av
;
175 htmlfragment
*b
= (htmlfragment
*)bv
;
178 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
181 return strcmp(a
->fragment
, b
->fragment
);
184 static int html_filename_compare(void *av
, void *bv
)
186 char *a
= (char *)av
;
187 char *b
= (char *)bv
;
192 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
193 htmlsect
*sect
, int depth
);
195 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
196 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
199 /* Flags for html_words() flags parameter */
203 #define INDEXENTS 0x04
205 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
206 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
207 static void html_codepara(htmloutput
*ho
, word
*words
);
209 static void element_open(htmloutput
*ho
, char const *name
);
210 static void element_close(htmloutput
*ho
, char const *name
);
211 static void element_empty(htmloutput
*ho
, char const *name
);
212 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
213 static void element_attr_w(htmloutput
*ho
, char const *name
,
214 wchar_t const *value
);
215 static void html_text(htmloutput
*ho
, wchar_t const *str
);
216 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *str
);
217 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
218 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
219 int maxlen
, int quote_quotes
, int nbsp
);
220 static void html_nl(htmloutput
*ho
);
221 static void html_raw(htmloutput
*ho
, char *text
);
222 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
223 static void cleanup(htmloutput
*ho
);
225 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
226 htmlfile
*targetfile
, char *targetfrag
);
227 static void html_fragment(htmloutput
*ho
, char const *fragment
);
229 static char *html_format(paragraph
*p
, char *template_string
);
230 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
232 static char *html_sanitise_filename(htmlfilelist
*files
, char *text
);
234 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
235 htmlfile
*thisfile
, keywordlist
*keywords
,
237 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
238 htmlfile
*thisfile
, keywordlist
*keywords
,
239 htmlconfig
*cfg
, int real
);
241 static htmlconfig
html_configure(paragraph
*source
) {
249 ret
.achapter
.just_numbers
= FALSE
;
250 ret
.achapter
.number_suffix
= L
": ";
252 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
253 ret
.asect
[0].just_numbers
= TRUE
;
254 ret
.asect
[0].number_suffix
= L
" ";
256 ret
.contents_depths
= 0;
257 ret
.visible_version_id
= TRUE
;
258 ret
.address_section
= TRUE
;
259 ret
.leaf_contains_contents
= FALSE
;
260 ret
.leaf_smallest_contents
= 4;
262 ret
.single_filename
= dupstr("Manual.html");
263 ret
.contents_filename
= dupstr("Contents.html");
264 ret
.index_filename
= dupstr("IndexPage.html");
265 ret
.template_filename
= dupstr("%n.html");
266 ret
.chm_filename
= ret
.hhp_filename
= NULL
;
267 ret
.hhc_filename
= ret
.hhk_filename
= NULL
;
269 ret
.template_fragments
= snewn(ret
.ntfragments
, char *);
270 ret
.template_fragments
[0] = dupstr("%b");
271 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
272 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
273 ret
.author
= ret
.description
= NULL
;
274 ret
.restrict_charset
= CS_UTF8
;
275 ret
.output_charset
= CS_ASCII
;
276 ret
.htmlver
= HTML_4
;
277 ret
.index_text
= L
"Index";
278 ret
.contents_text
= L
"Contents";
279 ret
.preamble_text
= L
"Preamble";
280 ret
.title_separator
= L
" - ";
281 ret
.nav_prev_text
= L
"Previous";
282 ret
.nav_next_text
= L
"Next";
283 ret
.nav_separator
= L
" | ";
284 ret
.index_main_sep
= L
": ";
285 ret
.index_multi_sep
= L
", ";
286 ret
.pre_versionid
= L
"[";
287 ret
.post_versionid
= L
"]";
289 * Default quote characters are Unicode matched single quotes,
290 * falling back to ordinary ASCII ".
292 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
293 ret
.rquote
= uadv(ret
.lquote
);
296 * Two-pass configuration so that we can pick up global config
297 * (e.g. `quotes') before having it overridden by specific
298 * config (`html-quotes'), irrespective of the order in which
301 for (p
= source
; p
; p
= p
->next
) {
302 if (p
->type
== para_Config
) {
303 if (!ustricmp(p
->keyword
, L
"quotes")) {
304 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
305 ret
.lquote
= uadv(p
->keyword
);
306 ret
.rquote
= uadv(ret
.lquote
);
308 } else if (!ustricmp(p
->keyword
, L
"index")) {
309 ret
.index_text
= uadv(p
->keyword
);
310 } else if (!ustricmp(p
->keyword
, L
"contents")) {
311 ret
.contents_text
= uadv(p
->keyword
);
316 for (p
= source
; p
; p
= p
->next
) {
317 if (p
->type
== para_Config
) {
318 wchar_t *k
= p
->keyword
;
320 if (!ustrnicmp(k
, L
"xhtml-", 6))
321 k
++; /* treat `xhtml-' and `html-' the same */
323 if (!ustricmp(k
, L
"html-restrict-charset")) {
324 ret
.restrict_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
325 } else if (!ustricmp(k
, L
"html-output-charset")) {
326 ret
.output_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
327 } else if (!ustricmp(k
, L
"html-version")) {
328 wchar_t *vername
= uadv(k
);
329 static const struct {
333 {L
"html3.2", HTML_3_2
},
335 {L
"iso-html", ISO_HTML
},
336 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
337 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
341 for (i
= 0; i
< (int)lenof(versions
); i
++)
342 if (!ustricmp(versions
[i
].name
, vername
))
345 if (i
== lenof(versions
))
346 error(err_htmlver
, &p
->fpos
, vername
);
348 ret
.htmlver
= versions
[i
].ver
;
349 } else if (!ustricmp(k
, L
"html-single-filename")) {
350 sfree(ret
.single_filename
);
351 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
352 } else if (!ustricmp(k
, L
"html-contents-filename")) {
353 sfree(ret
.contents_filename
);
354 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
355 } else if (!ustricmp(k
, L
"html-index-filename")) {
356 sfree(ret
.index_filename
);
357 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
358 } else if (!ustricmp(k
, L
"html-template-filename")) {
359 sfree(ret
.template_filename
);
360 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
361 } else if (!ustricmp(k
, L
"html-template-fragment")) {
362 char *frag
= adv(p
->origkeyword
);
364 while (ret
.ntfragments
--)
365 sfree(ret
.template_fragments
[ret
.ntfragments
]);
366 sfree(ret
.template_fragments
);
367 ret
.template_fragments
= NULL
;
371 ret
.template_fragments
=
372 sresize(ret
.template_fragments
,
373 ret
.ntfragments
, char *);
374 ret
.template_fragments
[ret
.ntfragments
-1] =
379 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 1);
380 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
381 ret
.achapter
.just_numbers
= utob(uadv(k
));
382 } else if (!ustricmp(k
, L
"html-suppress-navlinks")) {
383 ret
.navlinks
= !utob(uadv(k
));
384 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
385 ret
.achapter
.number_suffix
= uadv(k
);
386 } else if (!ustricmp(k
, L
"html-leaf-level")) {
387 wchar_t *u
= uadv(k
);
388 if (!ustricmp(u
, L
"infinite") ||
389 !ustricmp(u
, L
"infinity") ||
390 !ustricmp(u
, L
"inf"))
391 ret
.leaf_level
= -1; /* represents infinity */
393 ret
.leaf_level
= utoi(u
);
394 } else if (!ustricmp(k
, L
"html-section-numeric")) {
395 wchar_t *q
= uadv(k
);
401 if (n
>= ret
.nasect
) {
403 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
404 for (i
= ret
.nasect
; i
<= n
; i
++)
405 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
408 ret
.asect
[n
].just_numbers
= utob(q
);
409 } else if (!ustricmp(k
, L
"html-section-suffix")) {
410 wchar_t *q
= uadv(k
);
416 if (n
>= ret
.nasect
) {
418 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
419 for (i
= ret
.nasect
; i
<= n
; i
++) {
420 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
424 ret
.asect
[n
].number_suffix
= q
;
425 } else if (!ustricmp(k
, L
"html-contents-depth") ||
426 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
428 * Relic of old implementation: this directive used
429 * to be written as \cfg{html-contents-depth-3}{2}
430 * rather than the usual Halibut convention of
431 * \cfg{html-contents-depth}{3}{2}. We therefore
434 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
440 if (n
>= ret
.ncdepths
) {
442 ret
.contents_depths
=
443 sresize(ret
.contents_depths
, n
+1, int);
444 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
445 ret
.contents_depths
[i
] = i
+2;
449 ret
.contents_depths
[n
] = utoi(q
);
450 } else if (!ustricmp(k
, L
"html-head-end")) {
451 ret
.head_end
= adv(p
->origkeyword
);
452 } else if (!ustricmp(k
, L
"html-body-tag")) {
453 ret
.body_tag
= adv(p
->origkeyword
);
454 } else if (!ustricmp(k
, L
"html-body-start")) {
455 ret
.body_start
= adv(p
->origkeyword
);
456 } else if (!ustricmp(k
, L
"html-body-end")) {
457 ret
.body_end
= adv(p
->origkeyword
);
458 } else if (!ustricmp(k
, L
"html-address-start")) {
459 ret
.addr_start
= adv(p
->origkeyword
);
460 } else if (!ustricmp(k
, L
"html-address-end")) {
461 ret
.addr_end
= adv(p
->origkeyword
);
462 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
463 ret
.nav_attr
= adv(p
->origkeyword
);
464 } else if (!ustricmp(k
, L
"html-author")) {
465 ret
.author
= uadv(k
);
466 } else if (!ustricmp(k
, L
"html-description")) {
467 ret
.description
= uadv(k
);
468 } else if (!ustricmp(k
, L
"html-suppress-address")) {
469 ret
.address_section
= !utob(uadv(k
));
470 } else if (!ustricmp(k
, L
"html-versionid")) {
471 ret
.visible_version_id
= utob(uadv(k
));
472 } else if (!ustricmp(k
, L
"html-quotes")) {
473 if (*uadv(k
) && *uadv(uadv(k
))) {
474 ret
.lquote
= uadv(k
);
475 ret
.rquote
= uadv(ret
.lquote
);
477 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
478 ret
.leaf_contains_contents
= utob(uadv(k
));
479 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
480 ret
.leaf_smallest_contents
= utoi(uadv(k
));
481 } else if (!ustricmp(k
, L
"html-index-text")) {
482 ret
.index_text
= uadv(k
);
483 } else if (!ustricmp(k
, L
"html-contents-text")) {
484 ret
.contents_text
= uadv(k
);
485 } else if (!ustricmp(k
, L
"html-preamble-text")) {
486 ret
.preamble_text
= uadv(k
);
487 } else if (!ustricmp(k
, L
"html-title-separator")) {
488 ret
.title_separator
= uadv(k
);
489 } else if (!ustricmp(k
, L
"html-nav-prev-text")) {
490 ret
.nav_prev_text
= uadv(k
);
491 } else if (!ustricmp(k
, L
"html-nav-next-text")) {
492 ret
.nav_next_text
= uadv(k
);
493 } else if (!ustricmp(k
, L
"html-nav-separator")) {
494 ret
.nav_separator
= uadv(k
);
495 } else if (!ustricmp(k
, L
"html-index-main-separator")) {
496 ret
.index_main_sep
= uadv(k
);
497 } else if (!ustricmp(k
, L
"html-index-multiple-separator")) {
498 ret
.index_multi_sep
= uadv(k
);
499 } else if (!ustricmp(k
, L
"html-pre-versionid")) {
500 ret
.pre_versionid
= uadv(k
);
501 } else if (!ustricmp(k
, L
"html-post-versionid")) {
502 ret
.post_versionid
= uadv(k
);
503 } else if (!ustricmp(k
, L
"html-mshtmlhelp-chm")) {
504 sfree(ret
.chm_filename
);
505 ret
.chm_filename
= dupstr(adv(p
->origkeyword
));
506 } else if (!ustricmp(k
, L
"html-mshtmlhelp-project")) {
507 sfree(ret
.hhp_filename
);
508 ret
.hhp_filename
= dupstr(adv(p
->origkeyword
));
509 } else if (!ustricmp(k
, L
"html-mshtmlhelp-contents")) {
510 sfree(ret
.hhc_filename
);
511 ret
.hhc_filename
= dupstr(adv(p
->origkeyword
));
512 } else if (!ustricmp(k
, L
"html-mshtmlhelp-index")) {
513 sfree(ret
.hhk_filename
);
514 ret
.hhk_filename
= dupstr(adv(p
->origkeyword
));
520 * Enforce that the CHM and HHP filenames must either be both
521 * present or both absent. If one is present but not the other,
524 if (!ret
.chm_filename
^ !ret
.hhp_filename
) {
526 sfree(ret
.chm_filename
); ret
.chm_filename
= NULL
;
527 sfree(ret
.hhp_filename
); ret
.hhp_filename
= NULL
;
530 * And if we're not generating an HHP, there's no need for HHC
533 if (!ret
.hhp_filename
) {
534 sfree(ret
.hhc_filename
); ret
.hhc_filename
= NULL
;
535 sfree(ret
.hhk_filename
); ret
.hhk_filename
= NULL
;
539 * Now process fallbacks on quote characters.
541 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
542 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
543 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
544 ret
.lquote
= uadv(ret
.rquote
);
545 ret
.rquote
= uadv(ret
.lquote
);
551 paragraph
*html_config_filename(char *filename
)
554 * If the user passes in a single filename as a parameter to
555 * the `--html' command-line option, then we should assume it
556 * to imply _two_ config directives:
557 * \cfg{html-single-filename}{whatever} and
558 * \cfg{html-leaf-level}{0}; the rationale being that the user
559 * wants their output _in that file_.
563 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
564 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
569 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
570 indexdata
*idx
, void *unused
)
575 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
576 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
582 conf
= html_configure(sourceform
);
585 * We're going to make heavy use of paragraphs' private data
586 * fields in the forthcoming code. Clear them first, so we can
587 * reliably tell whether we have auxiliary data for a
588 * particular paragraph.
590 for (p
= sourceform
; p
; p
= p
->next
)
591 p
->private_data
= NULL
;
593 files
.frags
= newtree234(html_fragment_compare
);
594 files
.files
= newtree234(html_filename_compare
);
597 * Start by figuring out into which file each piece of the
598 * document should be put. We'll do this by inventing an
599 * `htmlsect' structure and stashing it in the private_data
600 * field of each section paragraph; we also need one additional
601 * htmlsect for the document index, which won't show up in the
602 * source form but needs to be consistently mentioned in
605 * While we're here, we'll also invent the HTML fragment name(s)
612 topsect
= html_new_sect(§s
, NULL
, &conf
);
614 topsect
->title
= NULL
;
615 topsect
->text
= sourceform
;
616 topsect
->contents_depth
= contents_depth(conf
, 0);
617 html_file_section(&conf
, &files
, topsect
, -1);
619 for (p
= sourceform
; p
; p
= p
->next
)
620 if (is_heading_type(p
->type
)) {
621 d
= heading_depth(p
);
623 if (p
->type
== para_Title
) {
628 sect
= html_new_sect(§s
, p
, &conf
);
629 sect
->text
= p
->next
;
631 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
634 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
635 assert(sect
->parent
!= NULL
);
637 sect
->parent
= topsect
;
638 p
->private_data
= sect
;
640 html_file_section(&conf
, &files
, sect
, d
);
644 for (i
=0; i
< conf
.ntfragments
; i
++) {
646 html_format(p
, conf
.template_fragments
[i
]);
648 html_sanitise_fragment(&files
, sect
->file
,
655 * And the index, if we have one. Note that we don't output
656 * an index as an HTML file if we're outputting one as a
659 has_index
= (count234(idx
->entries
) > 0);
660 if (has_index
&& !conf
.hhk_filename
) {
661 sect
= html_new_sect(§s
, NULL
, &conf
);
664 sect
->parent
= topsect
;
665 sect
->contents_depth
= 0;
666 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
667 sect
->fragments
[0] = utoa_dup(conf
.index_text
, CS_ASCII
);
668 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
670 files
.index
= sect
->file
;
675 * Go through the keyword list and sort out fragment IDs for
676 * all the potentially referenced paragraphs which _aren't_
684 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
685 paragraph
*q
, *p
= kw
->para
;
687 if (!is_heading_type(p
->type
)) {
691 * Find the paragraph's parent htmlsect, to
692 * determine which file it will end up in.
697 * Preamble paragraphs have no parent. So if we
698 * have a non-heading with no parent, it must
699 * be preamble, and therefore its parent
700 * htmlsect must be the preamble one.
703 sects
.head
->type
== TOP
);
706 parent
= (htmlsect
*)q
->private_data
;
709 * Now we can construct an htmlsect for this
710 * paragraph itself, taking care to put it in the
711 * list of non-sections rather than the list of
712 * sections (so that traverses of the `sects' list
713 * won't attempt to add it to the contents or
714 * anything weird like that).
716 sect
= html_new_sect(&nonsects
, p
, &conf
);
717 sect
->file
= parent
->file
;
718 sect
->parent
= parent
;
719 p
->private_data
= sect
;
722 * Fragment IDs for these paragraphs will simply be
723 * `p' followed by an integer.
725 sect
->fragments
[0] = snewn(40, char);
726 sprintf(sect
->fragments
[0], "p%d",
727 sect
->file
->last_fragment_number
++);
728 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
735 * Reset the fragment numbers in each file. I've just used them
736 * to generate `p' fragment IDs for non-section paragraphs
737 * (numbered list elements, bibliocited), and now I want to use
738 * them for `i' fragment IDs for index entries.
742 for (file
= files
.head
; file
; file
= file
->next
)
743 file
->last_fragment_number
= 0;
747 * Now sort out the index. This involves:
749 * - For each index term, we set up an htmlindex structure to
750 * store all the references to that term.
752 * - Then we make a pass over the actual document, finding
753 * every word_IndexRef; for each one, we actually figure out
754 * the HTML filename/fragment pair we will use to reference
755 * it, store that information in the private data field of
756 * the word_IndexRef itself (so we can recreate it when the
757 * time comes to output our HTML), and add a reference to it
758 * to the index term in question.
767 * Set up the htmlindex structures.
770 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
771 htmlindex
*hi
= snew(htmlindex
);
773 hi
->nrefs
= hi
->refsize
= 0;
776 entry
->backend_data
= hi
;
780 * Run over the document inventing fragments. Each fragment
781 * is of the form `i' followed by an integer.
783 lastsect
= sects
.head
; /* this is always the top section */
784 for (p
= sourceform
; p
; p
= p
->next
) {
785 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
786 lastsect
= (htmlsect
*)p
->private_data
;
788 for (w
= p
->words
; w
; w
= w
->next
)
789 if (w
->type
== word_IndexRef
) {
790 htmlindexref
*hr
= snew(htmlindexref
);
794 hr
->referenced
= hr
->generated
= FALSE
;
795 hr
->section
= lastsect
;
799 lastsect
->file
->last_fragment_number
++);
800 hr
->fragment
= dupstr(buf
);
802 html_sanitise_fragment(&files
, hr
->section
->file
,
805 w
->private_data
= hr
;
807 tag
= index_findtag(idx
, w
->text
);
811 for (i
= 0; i
< tag
->nrefs
; i
++) {
812 indexentry
*entry
= tag
->refs
[i
];
813 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
815 if (hi
->nrefs
>= hi
->refsize
) {
817 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
820 hi
->refs
[hi
->nrefs
++] = w
;
827 * Now we're ready to write out the actual HTML files.
831 * - we open that file and write its header
832 * - we run down the list of sections
833 * - for each section directly contained within that file, we
834 * output the section text
835 * - for each section which is not in the file but which has a
836 * parent that is, we output a contents entry for the
837 * section if appropriate
838 * - finally, we output the file trailer and close the file.
847 for (f
= files
.head
; f
; f
= f
->next
) {
850 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
851 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
852 struct stackelement
{
853 struct stackelement
*next
;
854 enum LISTTYPE listtype
;
855 enum ITEMTYPE itemtype
;
858 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
859 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
861 ho
.fp
= fopen(f
->filename
, "w");
863 error(err_cantopenw
, f
->filename
);
865 ho
.charset
= conf
.output_charset
;
866 ho
.restrict_charset
= conf
.restrict_charset
;
867 ho
.cstate
= charset_init_state
;
868 ho
.ver
= conf
.htmlver
;
869 ho
.state
= HO_NEUTRAL
;
870 ho
.contents_level
= 0;
871 ho
.hackflags
= 0; /* none of these thankyouverymuch */
875 switch (conf
.htmlver
) {
878 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
879 "HTML 3.2 Final//EN\">\n");
883 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
884 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
889 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
890 "15445:2000//DTD HTML//EN\">\n");
892 case XHTML_1_0_TRANSITIONAL
:
894 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
895 charset_to_mimeenc(conf
.output_charset
));
896 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
897 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
898 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
901 case XHTML_1_0_STRICT
:
903 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
904 charset_to_mimeenc(conf
.output_charset
));
905 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
906 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
907 "DTD/xhtml1-strict.dtd\">\n");
912 element_open(&ho
, "html");
913 if (is_xhtml(conf
.htmlver
)) {
914 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
918 element_open(&ho
, "head");
921 element_empty(&ho
, "meta");
922 element_attr(&ho
, "http-equiv", "content-type");
925 sprintf(buf
, "text/html; charset=%.150s",
926 charset_to_mimeenc(conf
.output_charset
));
927 element_attr(&ho
, "content", buf
);
932 element_empty(&ho
, "meta");
933 element_attr(&ho
, "name", "author");
934 element_attr_w(&ho
, "content", conf
.author
);
938 if (conf
.description
) {
939 element_empty(&ho
, "meta");
940 element_attr(&ho
, "name", "description");
941 element_attr_w(&ho
, "content", conf
.description
);
945 element_open(&ho
, "title");
946 if (f
->first
&& f
->first
->title
) {
947 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
951 if (f
->last
!= f
->first
&& f
->last
->title
) {
952 html_text(&ho
, conf
.title_separator
);
953 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
957 element_close(&ho
, "title");
961 html_raw(&ho
, conf
.head_end
);
964 * Add any <head> data defined in specific sections
965 * that go in this file. (This is mostly to allow <meta
966 * name="AppleTitle"> tags for Mac online help.)
968 for (s
= sects
.head
; s
; s
= s
->next
) {
969 if (s
->file
== f
&& s
->text
) {
971 p
&& (p
== s
->text
|| p
->type
== para_Title
||
972 !is_heading_type(p
->type
));
974 if (p
->type
== para_Config
) {
975 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
976 html_raw(&ho
, adv(p
->origkeyword
));
983 element_close(&ho
, "head");
987 html_raw(&ho
, conf
.body_tag
);
989 element_open(&ho
, "body");
993 html_raw(&ho
, conf
.body_start
);
996 * Write out a nav bar. Special case: we don't do this
997 * if there is only one file.
999 if (conf
.navlinks
&& files
.head
!= files
.tail
) {
1000 element_open(&ho
, "p");
1002 html_raw_as_attr(&ho
, conf
.nav_attr
);
1005 element_open(&ho
, "a");
1006 element_attr(&ho
, "href", prevf
->filename
);
1008 html_text(&ho
, conf
.nav_prev_text
);
1010 element_close(&ho
, "a");
1012 html_text(&ho
, conf
.nav_separator
);
1014 if (f
!= files
.head
) {
1015 element_open(&ho
, "a");
1016 element_attr(&ho
, "href", files
.head
->filename
);
1018 html_text(&ho
, conf
.contents_text
);
1019 if (f
!= files
.head
)
1020 element_close(&ho
, "a");
1022 if (has_index
&& files
.index
) {
1023 html_text(&ho
, conf
.nav_separator
);
1024 if (f
!= files
.index
) {
1025 element_open(&ho
, "a");
1026 element_attr(&ho
, "href", files
.index
->filename
);
1028 html_text(&ho
, conf
.index_text
);
1029 if (f
!= files
.index
)
1030 element_close(&ho
, "a");
1033 html_text(&ho
, conf
.nav_separator
);
1036 element_open(&ho
, "a");
1037 element_attr(&ho
, "href", f
->next
->filename
);
1039 html_text(&ho
, conf
.nav_next_text
);
1041 element_close(&ho
, "a");
1043 element_close(&ho
, "p");
1049 * Write out a prefix TOC for the file (if a leaf file).
1051 * We start by going through the section list and
1052 * collecting the sections which need to be added to
1053 * the contents. On the way, we also test to see if
1054 * this file is a leaf file (defined as one which
1055 * contains all descendants of any section it
1056 * contains), because this will play a part in our
1057 * decision on whether or not to _output_ the TOC.
1059 * Special case: we absolutely do not do this if we're
1060 * in single-file mode.
1062 if (files
.head
!= files
.tail
) {
1063 int ntoc
= 0, tocsize
= 0;
1064 htmlsect
**toc
= NULL
;
1067 for (s
= sects
.head
; s
; s
= s
->next
) {
1072 * Search up from this section until we find
1073 * the highest-level one which belongs in this
1078 for (ac
= s
; ac
; ac
= ac
->parent
) {
1079 if (ac
->file
== f
) {
1086 if (s
->file
!= f
&& a
!= NULL
)
1090 if (adepth
<= a
->contents_depth
) {
1091 if (ntoc
>= tocsize
) {
1093 toc
= sresize(toc
, tocsize
, htmlsect
*);
1100 if (leaf
&& conf
.leaf_contains_contents
&&
1101 ntoc
>= conf
.leaf_smallest_contents
) {
1104 for (i
= 0; i
< ntoc
; i
++) {
1105 htmlsect
*s
= toc
[i
];
1106 int hlevel
= (s
->type
== TOP ?
-1 :
1107 s
->type
== INDEX ?
0 :
1108 heading_depth(s
->title
))
1109 - f
->min_heading_depth
+ 1;
1111 assert(hlevel
>= 1);
1112 html_contents_entry(&ho
, hlevel
, s
,
1113 f
, keywords
, &conf
);
1115 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1120 * Now go through the document and output some real
1124 for (s
= sects
.head
; s
; s
= s
->next
) {
1127 * This section belongs in this file.
1133 * Doesn't belong in this file, but it may be
1134 * a descendant of a section which does, in
1135 * which case we should consider it for the
1136 * main TOC of this file (for non-leaf files).
1144 * Search up from this section until we find
1145 * the highest-level one which belongs in this
1150 for (ac
= s
; ac
; ac
= ac
->parent
) {
1151 if (ac
->file
== f
) {
1160 * This section does not belong in this
1161 * file, but an ancestor of it does. Write
1162 * out a contents table entry, if the depth
1163 * doesn't exceed the maximum contents
1164 * depth for the ancestor section.
1166 if (adepth
<= a
->contents_depth
) {
1167 html_contents_entry(&ho
, adepth
, s
,
1168 f
, keywords
, &conf
);
1177 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1180 * Display the section heading.
1183 hlevel
= (s
->type
== TOP ?
-1 :
1184 s
->type
== INDEX ?
0 :
1185 heading_depth(s
->title
))
1186 - f
->min_heading_depth
+ 1;
1187 assert(hlevel
>= 1);
1188 /* HTML headings only go up to <h6> */
1192 htag
[1] = '0' + hlevel
;
1194 element_open(&ho
, htag
);
1197 * Provide anchor(s) for cross-links to target.
1199 * (Also we'll have to do this separately in
1200 * other paragraph types - NumberedList and
1205 for (i
=0; i
< conf
.ntfragments
; i
++)
1206 if (s
->fragments
[i
])
1207 html_fragment(&ho
, s
->fragments
[i
]);
1210 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1212 element_close(&ho
, htag
);
1215 * Now display the section text.
1218 stackhead
= snew(struct stackelement
);
1219 stackhead
->next
= NULL
;
1220 stackhead
->listtype
= NOLIST
;
1221 stackhead
->itemtype
= NOITEM
;
1223 for (p
= s
->text
;; p
= p
->next
) {
1224 enum LISTTYPE listtype
;
1225 struct stackelement
*se
;
1228 * Preliminary switch to figure out what
1229 * sort of list we expect to be inside at
1232 * Since p may still be NULL at this point,
1233 * I invent a harmless paragraph type for
1236 switch (p ? p
->type
: para_Normal
) {
1239 case para_Copyright
:
1240 case para_BiblioCited
:
1242 case para_QuotePush
:
1246 case para_UnnumberedChapter
:
1257 case para_NumberedList
:
1261 case para_DescribedThing
:
1262 case para_Description
:
1266 case para_LcontPush
:
1267 se
= snew(struct stackelement
);
1268 se
->next
= stackhead
;
1269 se
->listtype
= NOLIST
;
1270 se
->itemtype
= NOITEM
;
1274 default: /* some totally non-printing para */
1281 * Terminate the most recent list item, if
1282 * any. (We left this until after
1283 * processing LcontPush, since in that case
1284 * the list item won't want to be
1285 * terminated until after the corresponding
1288 if (stackhead
->itemtype
!= NOITEM
) {
1289 element_close(&ho
, itemname(stackhead
->itemtype
));
1292 stackhead
->itemtype
= NOITEM
;
1295 * Terminate the current list, if it's not
1296 * the one we want to be in.
1298 if (listtype
!= stackhead
->listtype
&&
1299 stackhead
->listtype
!= NOLIST
) {
1300 element_close(&ho
, listname(stackhead
->listtype
));
1305 * Leave the loop if our time has come.
1307 if (!p
|| (is_heading_type(p
->type
) &&
1308 p
->type
!= para_Title
))
1309 break; /* end of section text */
1312 * Start a fresh list if necessary.
1314 if (listtype
!= stackhead
->listtype
&&
1316 element_open(&ho
, listname(listtype
));
1318 stackhead
->listtype
= listtype
;
1322 element_empty(&ho
, "hr");
1325 html_codepara(&ho
, p
->words
);
1328 case para_Copyright
:
1329 element_open(&ho
, "p");
1331 html_words(&ho
, p
->words
, ALL
,
1332 f
, keywords
, &conf
);
1334 element_close(&ho
, "p");
1336 case para_BiblioCited
:
1337 element_open(&ho
, "p");
1338 if (p
->private_data
) {
1339 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1341 for (i
=0; i
< conf
.ntfragments
; i
++)
1342 if (s
->fragments
[i
])
1343 html_fragment(&ho
, s
->fragments
[i
]);
1346 html_words(&ho
, p
->kwtext
, ALL
,
1347 f
, keywords
, &conf
);
1348 html_text(&ho
, L
" ");
1349 html_words(&ho
, p
->words
, ALL
,
1350 f
, keywords
, &conf
);
1352 element_close(&ho
, "p");
1355 case para_NumberedList
:
1356 element_open(&ho
, "li");
1357 if (p
->private_data
) {
1358 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1360 for (i
=0; i
< conf
.ntfragments
; i
++)
1361 if (s
->fragments
[i
])
1362 html_fragment(&ho
, s
->fragments
[i
]);
1365 stackhead
->itemtype
= LI
;
1366 html_words(&ho
, p
->words
, ALL
,
1367 f
, keywords
, &conf
);
1369 case para_DescribedThing
:
1370 element_open(&ho
, "dt");
1372 stackhead
->itemtype
= DT
;
1373 html_words(&ho
, p
->words
, ALL
,
1374 f
, keywords
, &conf
);
1376 case para_Description
:
1377 element_open(&ho
, "dd");
1379 stackhead
->itemtype
= DD
;
1380 html_words(&ho
, p
->words
, ALL
,
1381 f
, keywords
, &conf
);
1384 case para_QuotePush
:
1385 element_open(&ho
, "blockquote");
1388 element_close(&ho
, "blockquote");
1393 stackhead
= stackhead
->next
;
1400 assert(stackhead
&& !stackhead
->next
);
1404 if (s
->type
== INDEX
) {
1409 * This section is the index. I'll just
1410 * render it as a single paragraph, with a
1411 * colon between the index term and the
1412 * references, and <br> in between each
1415 element_open(&ho
, "p");
1417 for (i
= 0; (entry
=
1418 index234(idx
->entries
, i
)) != NULL
; i
++) {
1419 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1423 element_empty(&ho
, "br");
1426 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1427 f
, keywords
, &conf
);
1429 html_text(&ho
, conf
.index_main_sep
);
1431 for (j
= 0; j
< hi
->nrefs
; j
++) {
1433 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1434 paragraph
*p
= hr
->section
->title
;
1437 html_text(&ho
, conf
.index_multi_sep
);
1439 html_href(&ho
, f
, hr
->section
->file
,
1441 hr
->referenced
= TRUE
;
1443 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1444 f
, keywords
, &conf
);
1445 else if (p
&& p
->words
)
1446 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1447 f
, keywords
, &conf
);
1450 * If there is no title at all,
1451 * this must be because our
1452 * target section is the
1453 * preamble section and there
1454 * is no title. So we use the
1457 html_text(&ho
, conf
.preamble_text
);
1459 element_close(&ho
, "a");
1462 element_close(&ho
, "p");
1467 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1474 int done_version_ids
= FALSE
;
1476 if (conf
.address_section
)
1477 element_empty(&ho
, "hr");
1480 html_raw(&ho
, conf
.body_end
);
1482 if (conf
.address_section
) {
1483 int started
= FALSE
;
1484 if (conf
.htmlver
== ISO_HTML
) {
1486 * The ISO-HTML validator complains if
1487 * there isn't a <div> tag surrounding the
1488 * <address> tag. I'm uncertain of why this
1489 * should be - there appears to be no
1490 * mention of this in the ISO-HTML spec,
1491 * suggesting that it doesn't represent a
1492 * change from HTML 4, but nonetheless the
1493 * HTML 4 validator doesn't seem to mind.
1495 element_open(&ho
, "div");
1497 element_open(&ho
, "address");
1498 if (conf
.addr_start
) {
1499 html_raw(&ho
, conf
.addr_start
);
1503 if (conf
.visible_version_id
) {
1504 for (p
= sourceform
; p
; p
= p
->next
)
1505 if (p
->type
== para_VersionID
) {
1507 element_empty(&ho
, "br");
1509 html_text(&ho
, conf
.pre_versionid
);
1510 html_words(&ho
, p
->words
, NOTHING
,
1511 f
, keywords
, &conf
);
1512 html_text(&ho
, conf
.post_versionid
);
1515 done_version_ids
= TRUE
;
1517 if (conf
.addr_end
) {
1519 element_empty(&ho
, "br");
1520 html_raw(&ho
, conf
.addr_end
);
1522 element_close(&ho
, "address");
1523 if (conf
.htmlver
== ISO_HTML
)
1524 element_close(&ho
, "div");
1527 if (!done_version_ids
) {
1529 * If the user didn't want the version IDs
1530 * visible, I think we still have a duty to put
1531 * them in an HTML comment.
1533 int started
= FALSE
;
1534 for (p
= sourceform
; p
; p
= p
->next
)
1535 if (p
->type
== para_VersionID
) {
1537 html_raw(&ho
, "<!-- version IDs:\n");
1540 html_words(&ho
, p
->words
, NOTHING
,
1541 f
, keywords
, &conf
);
1545 html_raw(&ho
, "-->\n");
1549 element_close(&ho
, "body");
1551 element_close(&ho
, "html");
1558 * Before we start outputting the HTML Help files, check
1559 * whether there's even going to _be_ an index file: we omit it
1560 * if the index contains nothing.
1562 hhk_filename
= conf
.hhk_filename
;
1568 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1569 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1571 if (hi
->nrefs
> 0) {
1572 ok
= TRUE
; /* found an index entry */
1578 hhk_filename
= NULL
;
1582 * Output the MS HTML Help supporting files, if requested.
1584 if (conf
.hhp_filename
) {
1588 ho
.charset
= CS_CP1252
; /* as far as I know, HHP files are */
1589 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1590 ho
.cstate
= charset_init_state
;
1591 ho
.ver
= HTML_4
; /* *shrug* */
1592 ho
.state
= HO_NEUTRAL
;
1593 ho
.contents_level
= 0;
1594 ho
.hackflags
= HO_HACK_QUOTENOTHING
;
1596 ho
.fp
= fopen(conf
.hhp_filename
, "w");
1598 error(err_cantopenw
, conf
.hhp_filename
);
1602 "Compatibility=1.1 or later\n"
1603 "Compiled file=%s\n"
1604 "Default Window=main\n"
1605 "Default topic=%s\n"
1606 "Display compile progress=Yes\n"
1607 "Full-text search=Yes\n"
1608 "Title=", conf
.chm_filename
, files
.head
->filename
);
1611 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1612 NULL
, keywords
, &conf
);
1614 fprintf(ho
.fp
, "\n");
1617 * These two entries don't seem to be remotely necessary
1618 * for a successful run of the help _compiler_, but
1619 * omitting them causes the GUI Help Workshop to behave
1620 * rather strangely if you try to load the help project
1621 * into that and edit it.
1623 if (conf
.hhc_filename
)
1624 fprintf(ho
.fp
, "Contents file=%s\n", conf
.hhc_filename
);
1626 fprintf(ho
.fp
, "Index file=%s\n", hhk_filename
);
1628 fprintf(ho
.fp
, "\n[WINDOWS]\nmain=\"");
1630 ho
.hackflags
|= HO_HACK_OMITQUOTES
;
1632 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1633 NULL
, keywords
, &conf
);
1635 fprintf(ho
.fp
, "\",\"%s\",\"%s\",\"%s\",,,,,,"
1636 "0x42520,,0x3876,,,,,,,,0\n",
1637 conf
.hhc_filename ? conf
.hhc_filename
: "",
1638 hhk_filename ? hhk_filename
: "",
1639 files
.head
->filename
);
1642 * The [FILES] section is also not necessary for
1643 * compilation (hhc appears to build up a list of needed
1644 * files just by following links from the given starting
1645 * points), but useful for loading the project into HHW.
1647 fprintf(ho
.fp
, "\n[FILES]\n");
1648 for (f
= files
.head
; f
; f
= f
->next
)
1649 fprintf(ho
.fp
, "%s\n", f
->filename
);
1653 if (conf
.hhc_filename
) {
1659 ho
.fp
= fopen(conf
.hhc_filename
, "w");
1661 error(err_cantopenw
, conf
.hhc_filename
);
1663 ho
.charset
= CS_CP1252
; /* as far as I know, HHC files are */
1664 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1665 ho
.cstate
= charset_init_state
;
1666 ho
.ver
= HTML_4
; /* *shrug* */
1667 ho
.state
= HO_NEUTRAL
;
1668 ho
.contents_level
= 0;
1669 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1672 * Magic DOCTYPE which seems to work for .HHC files. I'm
1673 * wary of trying to change it!
1675 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1677 "<META HTTP-EQUIV=\"Content-Type\" "
1678 "CONTENT=\"text/html; charset=%s\">\n"
1679 "</HEAD><BODY><UL>\n",
1680 charset_to_mimeenc(conf
.output_charset
));
1682 for (f
= files
.head
; f
; f
= f
->next
) {
1684 * For each HTML file, write out a contents entry.
1686 int depth
, leaf
= TRUE
;
1689 * Determine the depth of this file in the contents
1692 * If the file contains no sections, it is assumed to
1697 for (a
= f
->first
->parent
; a
&& a
->type
!= TOP
; a
= a
->parent
)
1701 * Determine if this file is a leaf file, by
1702 * trawling the section list to see if there's any
1703 * section with an ancestor in this file but which
1704 * is not itself in this file.
1706 * Special case: for contents purposes, the TOP
1707 * file is not considered to be the parent of the
1708 * chapter files, so it's always a leaf.
1710 * A file with no sections in it is also a leaf.
1712 if (f
->first
&& f
->first
->type
!= TOP
) {
1713 for (s
= f
->first
; s
; s
= s
->next
) {
1716 if (leaf
&& s
->file
!= f
) {
1717 for (a
= s
; a
; a
= a
->parent
)
1727 * Now write out our contents entry.
1729 while (currdepth
< depth
) {
1730 fprintf(ho
.fp
, "<UL>\n");
1733 while (currdepth
> depth
) {
1734 fprintf(ho
.fp
, "</UL>\n");
1737 /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */
1738 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">"
1739 "<PARAM NAME=\"Name\" VALUE=\"");
1741 if (f
->first
->title
)
1742 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
1743 NULL
, keywords
, &conf
);
1744 else if (f
->first
->type
== INDEX
)
1745 html_text(&ho
, conf
.index_text
);
1746 fprintf(ho
.fp
, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">"
1747 "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n",
1748 f
->filename
, leaf ?
11 : 1);
1751 while (currdepth
> 0) {
1752 fprintf(ho
.fp
, "</UL>\n");
1756 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1767 * First make a pass over all HTML files and set their
1768 * `temp' fields to zero, because we're about to use them.
1770 for (f
= files
.head
; f
; f
= f
->next
)
1773 ho
.fp
= fopen(hhk_filename
, "w");
1775 error(err_cantopenw
, hhk_filename
);
1777 ho
.charset
= CS_CP1252
; /* as far as I know, HHK files are */
1778 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1779 ho
.cstate
= charset_init_state
;
1780 ho
.ver
= HTML_4
; /* *shrug* */
1781 ho
.state
= HO_NEUTRAL
;
1782 ho
.contents_level
= 0;
1783 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1786 * Magic DOCTYPE which seems to work for .HHK files. I'm
1787 * wary of trying to change it!
1789 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1791 "<META HTTP-EQUIV=\"Content-Type\" "
1792 "CONTENT=\"text/html; charset=%s\">\n"
1793 "</HEAD><BODY><UL>\n",
1794 charset_to_mimeenc(conf
.output_charset
));
1797 * Go through the index terms and output each one.
1799 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1800 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1803 if (hi
->nrefs
> 0) {
1804 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
1805 "<PARAM NAME=\"Name\" VALUE=\"");
1807 html_words(&ho
, entry
->text
, NOTHING
,
1808 NULL
, keywords
, &conf
);
1809 fprintf(ho
.fp
, "\">\n");
1811 for (j
= 0; j
< hi
->nrefs
; j
++) {
1813 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1816 * Use the temp field to ensure we don't
1817 * reference the same file more than once.
1819 if (!hr
->section
->file
->temp
) {
1820 fprintf(ho
.fp
, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n",
1821 hr
->section
->file
->filename
);
1822 hr
->section
->file
->temp
= 1;
1825 hr
->referenced
= TRUE
;
1828 fprintf(ho
.fp
, "</OBJECT>\n");
1831 * Now go through those files and re-clear the temp
1832 * fields ready for the _next_ index term.
1834 for (j
= 0; j
< hi
->nrefs
; j
++) {
1836 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1837 hr
->section
->file
->temp
= 0;
1842 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1847 * Go through and check that no index fragments were referenced
1848 * without being generated, or indeed vice versa.
1850 * (When I actually get round to freeing everything, this can
1851 * probably be the freeing loop as well.)
1853 for (p
= sourceform
; p
; p
= p
->next
) {
1855 for (w
= p
->words
; w
; w
= w
->next
)
1856 if (w
->type
== word_IndexRef
) {
1857 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1859 assert(!hr
->referenced
== !hr
->generated
);
1864 * Free all the working data.
1868 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1870 * frag->fragment is dynamically allocated, but will be
1871 * freed when we process the htmlsect structure which
1872 * it is attached to.
1876 freetree234(files
.frags
);
1879 * The strings in files.files are all owned by their containing
1880 * htmlfile structures, so there's no need to free them here.
1882 freetree234(files
.files
);
1884 htmlsect
*sect
, *tmp
;
1889 for (i
=0; i
< conf
.ntfragments
; i
++)
1890 sfree(sect
->fragments
[i
]);
1891 sfree(sect
->fragments
);
1895 sect
= nonsects
.head
;
1899 for (i
=0; i
< conf
.ntfragments
; i
++)
1900 sfree(sect
->fragments
[i
]);
1901 sfree(sect
->fragments
);
1907 htmlfile
*file
, *tmp
;
1911 sfree(file
->filename
);
1919 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1920 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1927 for (p
= sourceform
; p
; p
= p
->next
)
1928 for (w
= p
->words
; w
; w
= w
->next
)
1929 if (w
->type
== word_IndexRef
) {
1930 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1932 sfree(hr
->fragment
);
1937 sfree(conf
.single_filename
);
1938 sfree(conf
.contents_filename
);
1939 sfree(conf
.index_filename
);
1940 sfree(conf
.template_filename
);
1941 while (conf
.ntfragments
--)
1942 sfree(conf
.template_fragments
[conf
.ntfragments
]);
1943 sfree(conf
.template_fragments
);
1946 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1947 htmlsect
*sect
, int depth
)
1953 * `depth' is derived from the heading_depth() macro at the top
1954 * of this file, which counts title as -1, chapter as 0,
1955 * heading as 1 and subsection as 2. However, the semantics of
1956 * cfg->leaf_level are defined to count chapter as 1, heading
1957 * as 2 etc. So first I increment depth :-(
1961 if (cfg
->leaf_level
== 0) {
1963 * leaf_level==0 is a special case, in which everything is
1964 * put into a single file.
1967 files
->single
= html_new_file(files
, cfg
->single_filename
);
1969 file
= files
->single
;
1972 * If the depth of this section is at or above leaf_level,
1973 * we invent a fresh file and put this section at its head.
1974 * Otherwise, we put it in the same file as its parent
1977 * Another special value of cfg->leaf_level is -1, which
1978 * means infinity (i.e. it's considered to always be
1979 * greater than depth).
1981 if (cfg
->leaf_level
> 0 && ldepth
> cfg
->leaf_level
) {
1983 * We know that sect->parent cannot be NULL. The only
1984 * circumstance in which it can be is if sect is at
1985 * chapter or appendix level, i.e. ldepth==1; and if
1986 * that's the case, then we cannot have entered this
1987 * branch unless cfg->leaf_level==0, in which case we
1988 * would be in the single-file case above and not here
1991 assert(sect
->parent
);
1993 file
= sect
->parent
->file
;
1995 if (sect
->type
== TOP
) {
1996 file
= html_new_file(files
, cfg
->contents_filename
);
1997 } else if (sect
->type
== INDEX
) {
1998 file
= html_new_file(files
, cfg
->index_filename
);
2002 assert(ldepth
> 0 && sect
->title
);
2003 title
= html_format(sect
->title
, cfg
->template_filename
);
2004 file
= html_new_file(files
, title
);
2012 if (file
->min_heading_depth
> depth
) {
2014 * This heading is at a higher level than any heading we
2015 * have so far placed in this file; so we set the `first'
2018 file
->min_heading_depth
= depth
;
2022 if (file
->min_heading_depth
== depth
)
2026 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
2028 htmlfile
*ret
= snew(htmlfile
);
2032 list
->tail
->next
= ret
;
2037 ret
->filename
= html_sanitise_filename(list
, dupstr(filename
));
2038 add234(list
->files
, ret
->filename
);
2039 ret
->last_fragment_number
= 0;
2040 ret
->min_heading_depth
= INT_MAX
;
2041 ret
->first
= ret
->last
= NULL
;
2046 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
2049 htmlsect
*ret
= snew(htmlsect
);
2053 list
->tail
->next
= ret
;
2063 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
2066 for (i
=0; i
< cfg
->ntfragments
; i
++)
2067 ret
->fragments
[i
] = NULL
;
2073 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
2074 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
2080 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
2081 case word_HyperLink
:
2082 if (flags
& LINKS
) {
2083 element_open(ho
, "a");
2084 c
= utoa_dup(w
->text
, CS_ASCII
);
2085 element_attr(ho
, "href", c
);
2089 case word_UpperXref
:
2090 case word_LowerXref
:
2091 if (flags
& LINKS
) {
2092 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
2098 s
= (htmlsect
*)p
->private_data
;
2102 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
2108 element_close(ho
, "a");
2111 if (flags
& INDEXENTS
) {
2112 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2113 html_fragment(ho
, hr
->fragment
);
2114 hr
->generated
= TRUE
;
2121 case word_WhiteSpace
:
2122 case word_EmphSpace
:
2123 case word_CodeSpace
:
2124 case word_WkCodeSpace
:
2126 case word_EmphQuote
:
2127 case word_CodeQuote
:
2128 case word_WkCodeQuote
:
2129 style
= towordstyle(w
->type
);
2130 type
= removeattr(w
->type
);
2131 if (style
== word_Emph
&&
2132 (attraux(w
->aux
) == attr_First
||
2133 attraux(w
->aux
) == attr_Only
) &&
2135 element_open(ho
, "em");
2136 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2137 (attraux(w
->aux
) == attr_First
||
2138 attraux(w
->aux
) == attr_Only
) &&
2140 element_open(ho
, "code");
2142 if (type
== word_WhiteSpace
)
2143 html_text(ho
, L
" ");
2144 else if (type
== word_Quote
) {
2145 if (quoteaux(w
->aux
) == quote_Open
)
2146 html_text(ho
, cfg
->lquote
);
2148 html_text(ho
, cfg
->rquote
);
2150 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
2151 html_text_nbsp(ho
, w
->text
);
2153 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
2156 if (style
== word_Emph
&&
2157 (attraux(w
->aux
) == attr_Last
||
2158 attraux(w
->aux
) == attr_Only
) &&
2160 element_close(ho
, "em");
2161 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2162 (attraux(w
->aux
) == attr_Last
||
2163 attraux(w
->aux
) == attr_Only
) &&
2165 element_close(ho
, "code");
2171 static void html_codepara(htmloutput
*ho
, word
*words
)
2173 element_open(ho
, "pre");
2174 element_open(ho
, "code");
2175 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
2180 if (words
->next
&& words
->next
->type
== word_Emph
) {
2181 e
= words
->next
->text
;
2182 words
= words
->next
;
2186 while (e
&& *e
&& *t
) {
2190 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
2198 element_open(ho
, open_tag
);
2200 html_text_limit(ho
, t
, n
);
2203 element_close(ho
, open_tag
);
2211 element_close(ho
, "code");
2212 element_close(ho
, "pre");
2215 static void html_charset_cleanup(htmloutput
*ho
)
2220 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
2221 ho
->charset
, &ho
->cstate
, NULL
);
2222 if (ho
->fp
&& bytes
> 0)
2223 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2226 static void return_mostly_to_neutral(htmloutput
*ho
)
2229 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
2230 fprintf(ho
->fp
, " />");
2231 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
2232 fprintf(ho
->fp
, ">");
2236 ho
->state
= HO_NEUTRAL
;
2239 static void return_to_neutral(htmloutput
*ho
)
2241 if (ho
->state
== HO_IN_TEXT
) {
2242 html_charset_cleanup(ho
);
2245 return_mostly_to_neutral(ho
);
2248 static void element_open(htmloutput
*ho
, char const *name
)
2250 return_to_neutral(ho
);
2252 fprintf(ho
->fp
, "<%s", name
);
2253 ho
->state
= HO_IN_TAG
;
2256 static void element_close(htmloutput
*ho
, char const *name
)
2258 return_to_neutral(ho
);
2260 fprintf(ho
->fp
, "</%s>", name
);
2261 ho
->state
= HO_NEUTRAL
;
2264 static void element_empty(htmloutput
*ho
, char const *name
)
2266 return_to_neutral(ho
);
2268 fprintf(ho
->fp
, "<%s", name
);
2269 ho
->state
= HO_IN_EMPTY_TAG
;
2272 static void html_nl(htmloutput
*ho
)
2274 return_to_neutral(ho
);
2276 fputc('\n', ho
->fp
);
2279 static void html_raw(htmloutput
*ho
, char *text
)
2281 return_to_neutral(ho
);
2283 fputs(text
, ho
->fp
);
2286 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
2288 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2291 fputs(text
, ho
->fp
);
2295 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
2297 html_charset_cleanup(ho
);
2298 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2300 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
2303 static void element_attr_w(htmloutput
*ho
, char const *name
,
2304 wchar_t const *value
)
2306 html_charset_cleanup(ho
);
2308 fprintf(ho
->fp
, " %s=\"", name
);
2309 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
2310 html_charset_cleanup(ho
);
2315 static void html_text(htmloutput
*ho
, wchar_t const *text
)
2317 return_mostly_to_neutral(ho
);
2318 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
2321 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
2323 return_mostly_to_neutral(ho
);
2324 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
2327 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
2329 return_mostly_to_neutral(ho
);
2330 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
2333 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
2334 int maxlen
, int quote_quotes
, int nbsp
)
2336 int textlen
= ustrlen(text
);
2340 if (ho
->hackflags
& (HO_HACK_QUOTEQUOTES
| HO_HACK_OMITQUOTES
))
2341 quote_quotes
= TRUE
; /* override the input value */
2343 if (maxlen
> 0 && textlen
> maxlen
)
2345 if (ho
->hacklimit
>= 0) {
2346 if (textlen
> ho
->hacklimit
)
2347 textlen
= ho
->hacklimit
;
2348 ho
->hacklimit
-= textlen
;
2351 while (textlen
> 0) {
2352 /* Scan ahead for characters we really can't display in HTML. */
2353 int lenbefore
, lenafter
;
2354 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
2355 if (text
[lenbefore
] == L
'<' ||
2356 text
[lenbefore
] == L
'>' ||
2357 text
[lenbefore
] == L
'&' ||
2358 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
2359 (text
[lenbefore
] == L
' ' && nbsp
))
2361 lenafter
= lenbefore
;
2362 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
2363 ho
->charset
, &ho
->cstate
, &err
);
2364 textlen
-= (lenbefore
- lenafter
);
2365 if (bytes
> 0 && ho
->fp
)
2366 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2369 * We have encountered a character that cannot be
2370 * displayed in the selected output charset. Therefore,
2371 * we use an HTML numeric entity reference.
2373 assert(textlen
> 0);
2375 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
2377 } else if (lenafter
== 0 && textlen
> 0) {
2379 * We have encountered a character which is special to
2383 if (*text
== L
'"' && (ho
->hackflags
& HO_HACK_OMITQUOTES
)) {
2384 fputc('\'', ho
->fp
);
2385 } else if (ho
->hackflags
& HO_HACK_QUOTENOTHING
) {
2386 fputc(*text
, ho
->fp
);
2389 fprintf(ho
->fp
, "<");
2390 else if (*text
== L
'>')
2391 fprintf(ho
->fp
, ">");
2392 else if (*text
== L
'&')
2393 fprintf(ho
->fp
, "&");
2394 else if (*text
== L
'"')
2395 fprintf(ho
->fp
, """);
2396 else if (*text
== L
' ') {
2398 fprintf(ho
->fp
, " ");
2400 assert(!"Can't happen");
2408 static void cleanup(htmloutput
*ho
)
2410 return_to_neutral(ho
);
2415 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
2416 htmlfile
*targetfile
, char *targetfrag
)
2418 rdstringc rs
= { 0, 0, NULL
};
2421 if (targetfile
!= thisfile
)
2422 rdaddsc(&rs
, targetfile
->filename
);
2425 rdaddsc(&rs
, targetfrag
);
2429 element_open(ho
, "a");
2430 element_attr(ho
, "href", url
);
2434 static void html_fragment(htmloutput
*ho
, char const *fragment
)
2436 element_open(ho
, "a");
2437 element_attr(ho
, "name", fragment
);
2438 if (is_xhtml(ho
->ver
))
2439 element_attr(ho
, "id", fragment
);
2440 element_close(ho
, "a");
2443 static char *html_format(paragraph
*p
, char *template_string
)
2447 wchar_t *ws
, wsbuf
[2];
2448 rdstringc rs
= { 0, 0, NULL
};
2450 t
= template_string
;
2452 if (*t
== '%' && t
[1]) {
2466 if (p
->kwtext
&& fmt
== 'n')
2468 else if (p
->kwtext2
&& fmt
== 'b') {
2470 * HTML fragment names must start with a letter, so
2471 * simply `1.2.3' is not adequate. In this case I'm
2472 * going to cheat slightly by prepending the first
2473 * character of the first word of kwtext, so that
2474 * we get `C1' for chapter 1, `S2.3' for section
2477 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2480 wsbuf
[0] = p
->kwtext
->text
[0];
2483 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2486 /* %N comes here; also failure cases of other fmts */
2490 c
= utoa_dup(ws
, CS_ASCII
);
2496 if (removeattr(w
->type
) == word_Normal
) {
2497 c
= utoa_dup(w
->text
, CS_ASCII
);
2508 return rdtrimc(&rs
);
2511 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2515 * The HTML 4 spec's strictest definition of fragment names (<a
2516 * name> and "id" attributes) says that they `must begin with a
2517 * letter and may be followed by any number of letters, digits,
2518 * hyphens, underscores, colons, and periods'.
2520 * So here we unceremoniously rip out any characters not
2521 * conforming to this limitation.
2523 char *p
= text
, *q
= text
;
2525 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2527 if ((*q
++ = *p
++) != '\0') {
2529 if ((*p
>='A' && *p
<='Z') ||
2530 (*p
>='a' && *p
<='z') ||
2531 (*p
>='0' && *p
<='9') ||
2532 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2540 /* If there's nothing left, make something valid up */
2542 static const char anonfrag
[] = "anon";
2543 text
= sresize(text
, lenof(anonfrag
), char);
2544 strcpy(text
, anonfrag
);
2548 * Now we check for clashes with other fragment names, and
2549 * adjust this one if necessary by appending a hyphen followed
2553 htmlfragment
*frag
= snew(htmlfragment
);
2554 int len
= 0; /* >0 indicates we have resized */
2558 frag
->fragment
= text
;
2560 while (add234(files
->frags
, frag
) != frag
) {
2563 frag
->fragment
= text
= sresize(text
, len
+20, char);
2566 sprintf(text
+ len
, "-%d", ++suffix
);
2573 static char *html_sanitise_filename(htmlfilelist
*files
, char *text
)
2576 * Unceremoniously rip out any character that might cause
2577 * difficulty in some filesystem or another, or be otherwise
2580 * That doesn't leave much punctuation. I permit alphanumerics
2583 char *p
= text
, *q
= text
;
2586 if ((*p
>='A' && *p
<='Z') ||
2587 (*p
>='a' && *p
<='z') ||
2588 (*p
>='0' && *p
<='9') ||
2589 *p
=='-' || *p
=='_' || *p
=='+' || *p
=='.' || *p
=='=')
2595 /* If there's nothing left, make something valid up */
2597 static const char anonfrag
[] = "anon.html";
2598 text
= sresize(text
, lenof(anonfrag
), char);
2599 strcpy(text
, anonfrag
);
2603 * Now we check for clashes with other filenames, and adjust
2604 * this one if necessary by appending a hyphen followed by a
2605 * number just before the file extension (if any).
2613 while (find234(files
->files
, text
, NULL
)) {
2617 text
= snewn(len
+20, char);
2619 for (extpos
= len
; extpos
> 0 && p
[extpos
-1] != '.'; extpos
--);
2626 sprintf(text
, "%.*s-%d%s", extpos
, p
, ++suffix
, p
+extpos
);
2636 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2637 htmlfile
*thisfile
, keywordlist
*keywords
,
2640 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2641 element_close(ho
, "li");
2645 while (ho
->contents_level
> depth
) {
2646 element_close(ho
, "ul");
2647 ho
->contents_level
--;
2648 if (ho
->contents_level
> 0) {
2649 element_close(ho
, "li");
2654 while (ho
->contents_level
< depth
) {
2656 element_open(ho
, "ul");
2658 ho
->contents_level
++;
2664 element_open(ho
, "li");
2665 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2666 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2667 element_close(ho
, "a");
2668 /* <li> will be closed by a later invocation */
2671 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2672 keywordlist
*keywords
, htmlconfig
*cfg
,
2678 int depth
= heading_depth(s
->title
);
2682 else if (depth
== 0)
2683 sl
= &cfg
->achapter
;
2684 else if (depth
<= cfg
->nasect
)
2685 sl
= &cfg
->asect
[depth
-1];
2687 sl
= &cfg
->asect
[cfg
->nasect
-1];
2691 else if (sl
->just_numbers
)
2692 number
= s
->title
->kwtext2
;
2694 number
= s
->title
->kwtext
;
2697 html_words(ho
, number
, MARKUP
,
2698 thisfile
, keywords
, cfg
);
2699 html_text(ho
, sl
->number_suffix
);
2702 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2703 thisfile
, keywords
, cfg
);
2705 assert(s
->type
!= NORMAL
);
2707 * If we're printing the full document title for _real_ and
2708 * there isn't one, we don't want to print `Preamble' at
2709 * the top of what ought to just be some text. If we need
2710 * it in any other context such as TOCs, we need to print
2713 if (s
->type
== TOP
&& !real
)
2714 html_text(ho
, cfg
->preamble_text
);
2715 else if (s
->type
== INDEX
)
2716 html_text(ho
, cfg
->index_text
);