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
;
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_suffix
= L
": ";
253 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
254 ret
.asect
[0].just_numbers
= TRUE
;
255 ret
.asect
[0].number_suffix
= L
" ";
257 ret
.contents_depths
= 0;
258 ret
.visible_version_id
= TRUE
;
259 ret
.address_section
= TRUE
;
260 ret
.leaf_contains_contents
= FALSE
;
261 ret
.leaf_smallest_contents
= 4;
264 ret
.single_filename
= dupstr("Manual.html");
265 ret
.contents_filename
= dupstr("Contents.html");
266 ret
.index_filename
= dupstr("IndexPage.html");
267 ret
.template_filename
= dupstr("%n.html");
268 ret
.chm_filename
= ret
.hhp_filename
= NULL
;
269 ret
.hhc_filename
= ret
.hhk_filename
= NULL
;
271 ret
.template_fragments
= snewn(ret
.ntfragments
, char *);
272 ret
.template_fragments
[0] = dupstr("%b");
273 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
274 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
275 ret
.author
= ret
.description
= NULL
;
276 ret
.restrict_charset
= CS_UTF8
;
277 ret
.output_charset
= CS_ASCII
;
278 ret
.htmlver
= HTML_4
;
279 ret
.index_text
= L
"Index";
280 ret
.contents_text
= L
"Contents";
281 ret
.preamble_text
= L
"Preamble";
282 ret
.title_separator
= L
" - ";
283 ret
.nav_prev_text
= L
"Previous";
284 ret
.nav_next_text
= L
"Next";
285 ret
.nav_up_text
= L
"Up";
286 ret
.nav_separator
= L
" | ";
287 ret
.index_main_sep
= L
": ";
288 ret
.index_multi_sep
= L
", ";
289 ret
.pre_versionid
= L
"[";
290 ret
.post_versionid
= L
"]";
292 * Default quote characters are Unicode matched single quotes,
293 * falling back to ordinary ASCII ".
295 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
296 ret
.rquote
= uadv(ret
.lquote
);
299 * Two-pass configuration so that we can pick up global config
300 * (e.g. `quotes') before having it overridden by specific
301 * config (`html-quotes'), irrespective of the order in which
304 for (p
= source
; p
; p
= p
->next
) {
305 if (p
->type
== para_Config
) {
306 if (!ustricmp(p
->keyword
, L
"quotes")) {
307 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
308 ret
.lquote
= uadv(p
->keyword
);
309 ret
.rquote
= uadv(ret
.lquote
);
311 } else if (!ustricmp(p
->keyword
, L
"index")) {
312 ret
.index_text
= uadv(p
->keyword
);
313 } else if (!ustricmp(p
->keyword
, L
"contents")) {
314 ret
.contents_text
= uadv(p
->keyword
);
319 for (p
= source
; p
; p
= p
->next
) {
320 if (p
->type
== para_Config
) {
321 wchar_t *k
= p
->keyword
;
323 if (!ustrnicmp(k
, L
"xhtml-", 6))
324 k
++; /* treat `xhtml-' and `html-' the same */
326 if (!ustricmp(k
, L
"html-restrict-charset")) {
327 ret
.restrict_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
328 } else if (!ustricmp(k
, L
"html-output-charset")) {
329 ret
.output_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
330 } else if (!ustricmp(k
, L
"html-version")) {
331 wchar_t *vername
= uadv(k
);
332 static const struct {
336 {L
"html3.2", HTML_3_2
},
338 {L
"iso-html", ISO_HTML
},
339 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
340 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
344 for (i
= 0; i
< (int)lenof(versions
); i
++)
345 if (!ustricmp(versions
[i
].name
, vername
))
348 if (i
== lenof(versions
))
349 error(err_htmlver
, &p
->fpos
, vername
);
351 ret
.htmlver
= versions
[i
].ver
;
352 } else if (!ustricmp(k
, L
"html-single-filename")) {
353 sfree(ret
.single_filename
);
354 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
355 } else if (!ustricmp(k
, L
"html-contents-filename")) {
356 sfree(ret
.contents_filename
);
357 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
358 } else if (!ustricmp(k
, L
"html-index-filename")) {
359 sfree(ret
.index_filename
);
360 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
361 } else if (!ustricmp(k
, L
"html-template-filename")) {
362 sfree(ret
.template_filename
);
363 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
364 } else if (!ustricmp(k
, L
"html-template-fragment")) {
365 char *frag
= adv(p
->origkeyword
);
367 while (ret
.ntfragments
--)
368 sfree(ret
.template_fragments
[ret
.ntfragments
]);
369 sfree(ret
.template_fragments
);
370 ret
.template_fragments
= NULL
;
374 ret
.template_fragments
=
375 sresize(ret
.template_fragments
,
376 ret
.ntfragments
, char *);
377 ret
.template_fragments
[ret
.ntfragments
-1] =
382 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 1);
383 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
384 ret
.achapter
.just_numbers
= utob(uadv(k
));
385 } else if (!ustricmp(k
, L
"html-suppress-navlinks")) {
386 ret
.navlinks
= !utob(uadv(k
));
387 } else if (!ustricmp(k
, L
"html-rellinks")) {
388 ret
.rellinks
= utob(uadv(k
));
389 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
390 ret
.achapter
.number_suffix
= uadv(k
);
391 } else if (!ustricmp(k
, L
"html-leaf-level")) {
392 wchar_t *u
= uadv(k
);
393 if (!ustricmp(u
, L
"infinite") ||
394 !ustricmp(u
, L
"infinity") ||
395 !ustricmp(u
, L
"inf"))
396 ret
.leaf_level
= -1; /* represents infinity */
398 ret
.leaf_level
= utoi(u
);
399 } else if (!ustricmp(k
, L
"html-section-numeric")) {
400 wchar_t *q
= uadv(k
);
406 if (n
>= ret
.nasect
) {
408 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
409 for (i
= ret
.nasect
; i
<= n
; i
++)
410 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
413 ret
.asect
[n
].just_numbers
= utob(q
);
414 } else if (!ustricmp(k
, L
"html-section-suffix")) {
415 wchar_t *q
= uadv(k
);
421 if (n
>= ret
.nasect
) {
423 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
424 for (i
= ret
.nasect
; i
<= n
; i
++) {
425 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
429 ret
.asect
[n
].number_suffix
= q
;
430 } else if (!ustricmp(k
, L
"html-contents-depth") ||
431 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
433 * Relic of old implementation: this directive used
434 * to be written as \cfg{html-contents-depth-3}{2}
435 * rather than the usual Halibut convention of
436 * \cfg{html-contents-depth}{3}{2}. We therefore
439 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
445 if (n
>= ret
.ncdepths
) {
447 ret
.contents_depths
=
448 sresize(ret
.contents_depths
, n
+1, int);
449 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
450 ret
.contents_depths
[i
] = i
+2;
454 ret
.contents_depths
[n
] = utoi(q
);
455 } else if (!ustricmp(k
, L
"html-head-end")) {
456 ret
.head_end
= adv(p
->origkeyword
);
457 } else if (!ustricmp(k
, L
"html-body-tag")) {
458 ret
.body_tag
= adv(p
->origkeyword
);
459 } else if (!ustricmp(k
, L
"html-body-start")) {
460 ret
.body_start
= adv(p
->origkeyword
);
461 } else if (!ustricmp(k
, L
"html-body-end")) {
462 ret
.body_end
= adv(p
->origkeyword
);
463 } else if (!ustricmp(k
, L
"html-address-start")) {
464 ret
.addr_start
= adv(p
->origkeyword
);
465 } else if (!ustricmp(k
, L
"html-address-end")) {
466 ret
.addr_end
= adv(p
->origkeyword
);
467 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
468 ret
.nav_attr
= adv(p
->origkeyword
);
469 } else if (!ustricmp(k
, L
"html-author")) {
470 ret
.author
= uadv(k
);
471 } else if (!ustricmp(k
, L
"html-description")) {
472 ret
.description
= uadv(k
);
473 } else if (!ustricmp(k
, L
"html-suppress-address")) {
474 ret
.address_section
= !utob(uadv(k
));
475 } else if (!ustricmp(k
, L
"html-versionid")) {
476 ret
.visible_version_id
= utob(uadv(k
));
477 } else if (!ustricmp(k
, L
"html-quotes")) {
478 if (*uadv(k
) && *uadv(uadv(k
))) {
479 ret
.lquote
= uadv(k
);
480 ret
.rquote
= uadv(ret
.lquote
);
482 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
483 ret
.leaf_contains_contents
= utob(uadv(k
));
484 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
485 ret
.leaf_smallest_contents
= utoi(uadv(k
));
486 } else if (!ustricmp(k
, L
"html-index-text")) {
487 ret
.index_text
= uadv(k
);
488 } else if (!ustricmp(k
, L
"html-contents-text")) {
489 ret
.contents_text
= uadv(k
);
490 } else if (!ustricmp(k
, L
"html-preamble-text")) {
491 ret
.preamble_text
= uadv(k
);
492 } else if (!ustricmp(k
, L
"html-title-separator")) {
493 ret
.title_separator
= uadv(k
);
494 } else if (!ustricmp(k
, L
"html-nav-prev-text")) {
495 ret
.nav_prev_text
= uadv(k
);
496 } else if (!ustricmp(k
, L
"html-nav-next-text")) {
497 ret
.nav_next_text
= uadv(k
);
498 } else if (!ustricmp(k
, L
"html-nav-up-text")) {
499 ret
.nav_up_text
= uadv(k
);
500 } else if (!ustricmp(k
, L
"html-nav-separator")) {
501 ret
.nav_separator
= uadv(k
);
502 } else if (!ustricmp(k
, L
"html-index-main-separator")) {
503 ret
.index_main_sep
= uadv(k
);
504 } else if (!ustricmp(k
, L
"html-index-multiple-separator")) {
505 ret
.index_multi_sep
= uadv(k
);
506 } else if (!ustricmp(k
, L
"html-pre-versionid")) {
507 ret
.pre_versionid
= uadv(k
);
508 } else if (!ustricmp(k
, L
"html-post-versionid")) {
509 ret
.post_versionid
= uadv(k
);
510 } else if (!ustricmp(k
, L
"html-mshtmlhelp-chm")) {
511 sfree(ret
.chm_filename
);
512 ret
.chm_filename
= dupstr(adv(p
->origkeyword
));
513 } else if (!ustricmp(k
, L
"html-mshtmlhelp-project")) {
514 sfree(ret
.hhp_filename
);
515 ret
.hhp_filename
= dupstr(adv(p
->origkeyword
));
516 } else if (!ustricmp(k
, L
"html-mshtmlhelp-contents")) {
517 sfree(ret
.hhc_filename
);
518 ret
.hhc_filename
= dupstr(adv(p
->origkeyword
));
519 } else if (!ustricmp(k
, L
"html-mshtmlhelp-index")) {
520 sfree(ret
.hhk_filename
);
521 ret
.hhk_filename
= dupstr(adv(p
->origkeyword
));
527 * Enforce that the CHM and HHP filenames must either be both
528 * present or both absent. If one is present but not the other,
531 if (!ret
.chm_filename
^ !ret
.hhp_filename
) {
533 sfree(ret
.chm_filename
); ret
.chm_filename
= NULL
;
534 sfree(ret
.hhp_filename
); ret
.hhp_filename
= NULL
;
537 * And if we're not generating an HHP, there's no need for HHC
540 if (!ret
.hhp_filename
) {
541 sfree(ret
.hhc_filename
); ret
.hhc_filename
= NULL
;
542 sfree(ret
.hhk_filename
); ret
.hhk_filename
= NULL
;
546 * Now process fallbacks on quote characters.
548 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
549 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
550 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
551 ret
.lquote
= uadv(ret
.rquote
);
552 ret
.rquote
= uadv(ret
.lquote
);
558 paragraph
*html_config_filename(char *filename
)
561 * If the user passes in a single filename as a parameter to
562 * the `--html' command-line option, then we should assume it
563 * to imply _two_ config directives:
564 * \cfg{html-single-filename}{whatever} and
565 * \cfg{html-leaf-level}{0}; the rationale being that the user
566 * wants their output _in that file_.
570 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
571 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
576 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
577 indexdata
*idx
, void *unused
)
582 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
583 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
589 conf
= html_configure(sourceform
);
592 * We're going to make heavy use of paragraphs' private data
593 * fields in the forthcoming code. Clear them first, so we can
594 * reliably tell whether we have auxiliary data for a
595 * particular paragraph.
597 for (p
= sourceform
; p
; p
= p
->next
)
598 p
->private_data
= NULL
;
600 files
.frags
= newtree234(html_fragment_compare
);
601 files
.files
= newtree234(html_filename_compare
);
604 * Start by figuring out into which file each piece of the
605 * document should be put. We'll do this by inventing an
606 * `htmlsect' structure and stashing it in the private_data
607 * field of each section paragraph; we also need one additional
608 * htmlsect for the document index, which won't show up in the
609 * source form but needs to be consistently mentioned in
612 * While we're here, we'll also invent the HTML fragment name(s)
619 topsect
= html_new_sect(§s
, NULL
, &conf
);
621 topsect
->title
= NULL
;
622 topsect
->text
= sourceform
;
623 topsect
->contents_depth
= contents_depth(conf
, 0);
624 html_file_section(&conf
, &files
, topsect
, -1);
626 for (p
= sourceform
; p
; p
= p
->next
)
627 if (is_heading_type(p
->type
)) {
628 d
= heading_depth(p
);
630 if (p
->type
== para_Title
) {
635 sect
= html_new_sect(§s
, p
, &conf
);
636 sect
->text
= p
->next
;
638 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
641 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
642 assert(sect
->parent
!= NULL
);
644 sect
->parent
= topsect
;
645 p
->private_data
= sect
;
647 html_file_section(&conf
, &files
, sect
, d
);
651 for (i
=0; i
< conf
.ntfragments
; i
++) {
653 html_format(p
, conf
.template_fragments
[i
]);
655 html_sanitise_fragment(&files
, sect
->file
,
662 * And the index, if we have one. Note that we don't output
663 * an index as an HTML file if we're outputting one as a
666 has_index
= (count234(idx
->entries
) > 0);
667 if (has_index
&& !conf
.hhk_filename
) {
668 sect
= html_new_sect(§s
, NULL
, &conf
);
671 sect
->parent
= topsect
;
672 sect
->contents_depth
= 0;
673 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
674 sect
->fragments
[0] = utoa_dup(conf
.index_text
, CS_ASCII
);
675 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
677 files
.index
= sect
->file
;
682 * Go through the keyword list and sort out fragment IDs for
683 * all the potentially referenced paragraphs which _aren't_
691 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
692 paragraph
*q
, *p
= kw
->para
;
694 if (!is_heading_type(p
->type
)) {
698 * Find the paragraph's parent htmlsect, to
699 * determine which file it will end up in.
704 * Preamble paragraphs have no parent. So if we
705 * have a non-heading with no parent, it must
706 * be preamble, and therefore its parent
707 * htmlsect must be the preamble one.
710 sects
.head
->type
== TOP
);
713 parent
= (htmlsect
*)q
->private_data
;
716 * Now we can construct an htmlsect for this
717 * paragraph itself, taking care to put it in the
718 * list of non-sections rather than the list of
719 * sections (so that traverses of the `sects' list
720 * won't attempt to add it to the contents or
721 * anything weird like that).
723 sect
= html_new_sect(&nonsects
, p
, &conf
);
724 sect
->file
= parent
->file
;
725 sect
->parent
= parent
;
726 p
->private_data
= sect
;
729 * Fragment IDs for these paragraphs will simply be
730 * `p' followed by an integer.
732 sect
->fragments
[0] = snewn(40, char);
733 sprintf(sect
->fragments
[0], "p%d",
734 sect
->file
->last_fragment_number
++);
735 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
742 * Reset the fragment numbers in each file. I've just used them
743 * to generate `p' fragment IDs for non-section paragraphs
744 * (numbered list elements, bibliocited), and now I want to use
745 * them for `i' fragment IDs for index entries.
749 for (file
= files
.head
; file
; file
= file
->next
)
750 file
->last_fragment_number
= 0;
754 * Now sort out the index. This involves:
756 * - For each index term, we set up an htmlindex structure to
757 * store all the references to that term.
759 * - Then we make a pass over the actual document, finding
760 * every word_IndexRef; for each one, we actually figure out
761 * the HTML filename/fragment pair we will use to reference
762 * it, store that information in the private data field of
763 * the word_IndexRef itself (so we can recreate it when the
764 * time comes to output our HTML), and add a reference to it
765 * to the index term in question.
774 * Set up the htmlindex structures.
777 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
778 htmlindex
*hi
= snew(htmlindex
);
780 hi
->nrefs
= hi
->refsize
= 0;
783 entry
->backend_data
= hi
;
787 * Run over the document inventing fragments. Each fragment
788 * is of the form `i' followed by an integer.
790 lastsect
= sects
.head
; /* this is always the top section */
791 for (p
= sourceform
; p
; p
= p
->next
) {
792 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
793 lastsect
= (htmlsect
*)p
->private_data
;
795 for (w
= p
->words
; w
; w
= w
->next
)
796 if (w
->type
== word_IndexRef
) {
797 htmlindexref
*hr
= snew(htmlindexref
);
801 hr
->referenced
= hr
->generated
= FALSE
;
802 hr
->section
= lastsect
;
806 lastsect
->file
->last_fragment_number
++);
807 hr
->fragment
= dupstr(buf
);
809 html_sanitise_fragment(&files
, hr
->section
->file
,
812 w
->private_data
= hr
;
814 tag
= index_findtag(idx
, w
->text
);
818 for (i
= 0; i
< tag
->nrefs
; i
++) {
819 indexentry
*entry
= tag
->refs
[i
];
820 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
822 if (hi
->nrefs
>= hi
->refsize
) {
824 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
827 hi
->refs
[hi
->nrefs
++] = w
;
834 * Now we're ready to write out the actual HTML files.
838 * - we open that file and write its header
839 * - we run down the list of sections
840 * - for each section directly contained within that file, we
841 * output the section text
842 * - for each section which is not in the file but which has a
843 * parent that is, we output a contents entry for the
844 * section if appropriate
845 * - finally, we output the file trailer and close the file.
854 for (f
= files
.head
; f
; f
= f
->next
) {
857 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
858 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
859 struct stackelement
{
860 struct stackelement
*next
;
861 enum LISTTYPE listtype
;
862 enum ITEMTYPE itemtype
;
865 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
866 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
868 ho
.fp
= fopen(f
->filename
, "w");
870 error(err_cantopenw
, f
->filename
);
872 ho
.charset
= conf
.output_charset
;
873 ho
.restrict_charset
= conf
.restrict_charset
;
874 ho
.cstate
= charset_init_state
;
875 ho
.ver
= conf
.htmlver
;
876 ho
.state
= HO_NEUTRAL
;
877 ho
.contents_level
= 0;
878 ho
.hackflags
= 0; /* none of these thankyouverymuch */
882 switch (conf
.htmlver
) {
885 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
886 "HTML 3.2 Final//EN\">\n");
890 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
891 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
896 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
897 "15445:2000//DTD HTML//EN\">\n");
899 case XHTML_1_0_TRANSITIONAL
:
901 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
902 charset_to_mimeenc(conf
.output_charset
));
903 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
904 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
905 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
908 case XHTML_1_0_STRICT
:
910 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
911 charset_to_mimeenc(conf
.output_charset
));
912 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
913 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
914 "DTD/xhtml1-strict.dtd\">\n");
919 element_open(&ho
, "html");
920 if (is_xhtml(conf
.htmlver
)) {
921 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
925 element_open(&ho
, "head");
928 element_empty(&ho
, "meta");
929 element_attr(&ho
, "http-equiv", "content-type");
932 sprintf(buf
, "text/html; charset=%.150s",
933 charset_to_mimeenc(conf
.output_charset
));
934 element_attr(&ho
, "content", buf
);
939 element_empty(&ho
, "meta");
940 element_attr(&ho
, "name", "author");
941 element_attr_w(&ho
, "content", conf
.author
);
945 if (conf
.description
) {
946 element_empty(&ho
, "meta");
947 element_attr(&ho
, "name", "description");
948 element_attr_w(&ho
, "content", conf
.description
);
952 element_open(&ho
, "title");
953 if (f
->first
&& f
->first
->title
) {
954 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
958 if (f
->last
!= f
->first
&& f
->last
->title
) {
959 html_text(&ho
, conf
.title_separator
);
960 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
964 element_close(&ho
, "title");
970 element_empty(&ho
, "link");
971 element_attr(&ho
, "rel", "previous");
972 element_attr(&ho
, "href", prevf
->filename
);
976 if (f
!= files
.head
) {
977 element_empty(&ho
, "link");
978 element_attr(&ho
, "rel", "ToC");
979 element_attr(&ho
, "href", files
.head
->filename
);
983 if (conf
.leaf_level
> 0) {
984 htmlsect
*p
= f
->first
->parent
;
985 assert(p
== f
->last
->parent
);
987 element_empty(&ho
, "link");
988 element_attr(&ho
, "rel", "up");
989 element_attr(&ho
, "href", p
->file
->filename
);
994 if (has_index
&& files
.index
&& f
!= files
.index
) {
995 element_empty(&ho
, "link");
996 element_attr(&ho
, "rel", "index");
997 element_attr(&ho
, "href", files
.index
->filename
);
1002 element_empty(&ho
, "link");
1003 element_attr(&ho
, "rel", "next");
1004 element_attr(&ho
, "href", f
->next
->filename
);
1011 html_raw(&ho
, conf
.head_end
);
1014 * Add any <head> data defined in specific sections
1015 * that go in this file. (This is mostly to allow <meta
1016 * name="AppleTitle"> tags for Mac online help.)
1018 for (s
= sects
.head
; s
; s
= s
->next
) {
1019 if (s
->file
== f
&& s
->text
) {
1021 p
&& (p
== s
->text
|| p
->type
== para_Title
||
1022 !is_heading_type(p
->type
));
1024 if (p
->type
== para_Config
) {
1025 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
1026 html_raw(&ho
, adv(p
->origkeyword
));
1033 element_close(&ho
, "head");
1037 html_raw(&ho
, conf
.body_tag
);
1039 element_open(&ho
, "body");
1042 if (conf
.body_start
)
1043 html_raw(&ho
, conf
.body_start
);
1046 * Write out a nav bar. Special case: we don't do this
1047 * if there is only one file.
1049 if (conf
.navlinks
&& files
.head
!= files
.tail
) {
1050 element_open(&ho
, "p");
1052 html_raw_as_attr(&ho
, conf
.nav_attr
);
1055 element_open(&ho
, "a");
1056 element_attr(&ho
, "href", prevf
->filename
);
1058 html_text(&ho
, conf
.nav_prev_text
);
1060 element_close(&ho
, "a");
1062 html_text(&ho
, conf
.nav_separator
);
1064 if (f
!= files
.head
) {
1065 element_open(&ho
, "a");
1066 element_attr(&ho
, "href", files
.head
->filename
);
1068 html_text(&ho
, conf
.contents_text
);
1069 if (f
!= files
.head
)
1070 element_close(&ho
, "a");
1072 /* We don't bother with "Up" links for leaf-level 1,
1073 * as they would be identical to the "Contents" links. */
1074 if (conf
.leaf_level
>= 2) {
1075 htmlsect
*p
= f
->first
->parent
;
1076 assert(p
== f
->last
->parent
);
1077 html_text(&ho
, conf
.nav_separator
);
1079 element_open(&ho
, "a");
1080 element_attr(&ho
, "href", p
->file
->filename
);
1082 html_text(&ho
, conf
.nav_up_text
);
1084 element_close(&ho
, "a");
1088 if (has_index
&& files
.index
) {
1089 html_text(&ho
, conf
.nav_separator
);
1090 if (f
!= files
.index
) {
1091 element_open(&ho
, "a");
1092 element_attr(&ho
, "href", files
.index
->filename
);
1094 html_text(&ho
, conf
.index_text
);
1095 if (f
!= files
.index
)
1096 element_close(&ho
, "a");
1099 html_text(&ho
, conf
.nav_separator
);
1102 element_open(&ho
, "a");
1103 element_attr(&ho
, "href", f
->next
->filename
);
1105 html_text(&ho
, conf
.nav_next_text
);
1107 element_close(&ho
, "a");
1109 element_close(&ho
, "p");
1115 * Write out a prefix TOC for the file (if a leaf file).
1117 * We start by going through the section list and
1118 * collecting the sections which need to be added to
1119 * the contents. On the way, we also test to see if
1120 * this file is a leaf file (defined as one which
1121 * contains all descendants of any section it
1122 * contains), because this will play a part in our
1123 * decision on whether or not to _output_ the TOC.
1125 * Special case: we absolutely do not do this if we're
1126 * in single-file mode.
1128 if (files
.head
!= files
.tail
) {
1129 int ntoc
= 0, tocsize
= 0;
1130 htmlsect
**toc
= NULL
;
1133 for (s
= sects
.head
; s
; s
= s
->next
) {
1138 * Search up from this section until we find
1139 * the highest-level one which belongs in this
1144 for (ac
= s
; ac
; ac
= ac
->parent
) {
1145 if (ac
->file
== f
) {
1152 if (s
->file
!= f
&& a
!= NULL
)
1156 if (adepth
<= a
->contents_depth
) {
1157 if (ntoc
>= tocsize
) {
1159 toc
= sresize(toc
, tocsize
, htmlsect
*);
1166 if (leaf
&& conf
.leaf_contains_contents
&&
1167 ntoc
>= conf
.leaf_smallest_contents
) {
1170 for (i
= 0; i
< ntoc
; i
++) {
1171 htmlsect
*s
= toc
[i
];
1172 int hlevel
= (s
->type
== TOP ?
-1 :
1173 s
->type
== INDEX ?
0 :
1174 heading_depth(s
->title
))
1175 - f
->min_heading_depth
+ 1;
1177 assert(hlevel
>= 1);
1178 html_contents_entry(&ho
, hlevel
, s
,
1179 f
, keywords
, &conf
);
1181 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1186 * Now go through the document and output some real
1190 for (s
= sects
.head
; s
; s
= s
->next
) {
1193 * This section belongs in this file.
1199 * Doesn't belong in this file, but it may be
1200 * a descendant of a section which does, in
1201 * which case we should consider it for the
1202 * main TOC of this file (for non-leaf files).
1210 * Search up from this section until we find
1211 * the highest-level one which belongs in this
1216 for (ac
= s
; ac
; ac
= ac
->parent
) {
1217 if (ac
->file
== f
) {
1226 * This section does not belong in this
1227 * file, but an ancestor of it does. Write
1228 * out a contents table entry, if the depth
1229 * doesn't exceed the maximum contents
1230 * depth for the ancestor section.
1232 if (adepth
<= a
->contents_depth
) {
1233 html_contents_entry(&ho
, adepth
, s
,
1234 f
, keywords
, &conf
);
1243 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1246 * Display the section heading.
1249 hlevel
= (s
->type
== TOP ?
-1 :
1250 s
->type
== INDEX ?
0 :
1251 heading_depth(s
->title
))
1252 - f
->min_heading_depth
+ 1;
1253 assert(hlevel
>= 1);
1254 /* HTML headings only go up to <h6> */
1258 htag
[1] = '0' + hlevel
;
1260 element_open(&ho
, htag
);
1263 * Provide anchor(s) for cross-links to target.
1265 * (Also we'll have to do this separately in
1266 * other paragraph types - NumberedList and
1271 for (i
=0; i
< conf
.ntfragments
; i
++)
1272 if (s
->fragments
[i
])
1273 html_fragment(&ho
, s
->fragments
[i
]);
1276 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1278 element_close(&ho
, htag
);
1281 * Now display the section text.
1284 stackhead
= snew(struct stackelement
);
1285 stackhead
->next
= NULL
;
1286 stackhead
->listtype
= NOLIST
;
1287 stackhead
->itemtype
= NOITEM
;
1289 for (p
= s
->text
;; p
= p
->next
) {
1290 enum LISTTYPE listtype
;
1291 struct stackelement
*se
;
1294 * Preliminary switch to figure out what
1295 * sort of list we expect to be inside at
1298 * Since p may still be NULL at this point,
1299 * I invent a harmless paragraph type for
1302 switch (p ? p
->type
: para_Normal
) {
1305 case para_Copyright
:
1306 case para_BiblioCited
:
1308 case para_QuotePush
:
1312 case para_UnnumberedChapter
:
1323 case para_NumberedList
:
1327 case para_DescribedThing
:
1328 case para_Description
:
1332 case para_LcontPush
:
1333 se
= snew(struct stackelement
);
1334 se
->next
= stackhead
;
1335 se
->listtype
= NOLIST
;
1336 se
->itemtype
= NOITEM
;
1340 default: /* some totally non-printing para */
1347 * Terminate the most recent list item, if
1348 * any. (We left this until after
1349 * processing LcontPush, since in that case
1350 * the list item won't want to be
1351 * terminated until after the corresponding
1354 if (stackhead
->itemtype
!= NOITEM
) {
1355 element_close(&ho
, itemname(stackhead
->itemtype
));
1358 stackhead
->itemtype
= NOITEM
;
1361 * Terminate the current list, if it's not
1362 * the one we want to be in.
1364 if (listtype
!= stackhead
->listtype
&&
1365 stackhead
->listtype
!= NOLIST
) {
1366 element_close(&ho
, listname(stackhead
->listtype
));
1371 * Leave the loop if our time has come.
1373 if (!p
|| (is_heading_type(p
->type
) &&
1374 p
->type
!= para_Title
))
1375 break; /* end of section text */
1378 * Start a fresh list if necessary.
1380 if (listtype
!= stackhead
->listtype
&&
1382 element_open(&ho
, listname(listtype
));
1384 stackhead
->listtype
= listtype
;
1388 element_empty(&ho
, "hr");
1391 html_codepara(&ho
, p
->words
);
1394 case para_Copyright
:
1395 element_open(&ho
, "p");
1397 html_words(&ho
, p
->words
, ALL
,
1398 f
, keywords
, &conf
);
1400 element_close(&ho
, "p");
1402 case para_BiblioCited
:
1403 element_open(&ho
, "p");
1404 if (p
->private_data
) {
1405 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1407 for (i
=0; i
< conf
.ntfragments
; i
++)
1408 if (s
->fragments
[i
])
1409 html_fragment(&ho
, s
->fragments
[i
]);
1412 html_words(&ho
, p
->kwtext
, ALL
,
1413 f
, keywords
, &conf
);
1414 html_text(&ho
, L
" ");
1415 html_words(&ho
, p
->words
, ALL
,
1416 f
, keywords
, &conf
);
1418 element_close(&ho
, "p");
1421 case para_NumberedList
:
1422 element_open(&ho
, "li");
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 stackhead
->itemtype
= LI
;
1432 html_words(&ho
, p
->words
, ALL
,
1433 f
, keywords
, &conf
);
1435 case para_DescribedThing
:
1436 element_open(&ho
, "dt");
1438 stackhead
->itemtype
= DT
;
1439 html_words(&ho
, p
->words
, ALL
,
1440 f
, keywords
, &conf
);
1442 case para_Description
:
1443 element_open(&ho
, "dd");
1445 stackhead
->itemtype
= DD
;
1446 html_words(&ho
, p
->words
, ALL
,
1447 f
, keywords
, &conf
);
1450 case para_QuotePush
:
1451 element_open(&ho
, "blockquote");
1454 element_close(&ho
, "blockquote");
1459 stackhead
= stackhead
->next
;
1466 assert(stackhead
&& !stackhead
->next
);
1470 if (s
->type
== INDEX
) {
1475 * This section is the index. I'll just
1476 * render it as a single paragraph, with a
1477 * colon between the index term and the
1478 * references, and <br> in between each
1481 element_open(&ho
, "p");
1483 for (i
= 0; (entry
=
1484 index234(idx
->entries
, i
)) != NULL
; i
++) {
1485 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1489 element_empty(&ho
, "br");
1492 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1493 f
, keywords
, &conf
);
1495 html_text(&ho
, conf
.index_main_sep
);
1497 for (j
= 0; j
< hi
->nrefs
; j
++) {
1499 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1500 paragraph
*p
= hr
->section
->title
;
1503 html_text(&ho
, conf
.index_multi_sep
);
1505 html_href(&ho
, f
, hr
->section
->file
,
1507 hr
->referenced
= TRUE
;
1509 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1510 f
, keywords
, &conf
);
1511 else if (p
&& p
->words
)
1512 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1513 f
, keywords
, &conf
);
1516 * If there is no title at all,
1517 * this must be because our
1518 * target section is the
1519 * preamble section and there
1520 * is no title. So we use the
1523 html_text(&ho
, conf
.preamble_text
);
1525 element_close(&ho
, "a");
1528 element_close(&ho
, "p");
1533 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1540 int done_version_ids
= FALSE
;
1542 if (conf
.address_section
)
1543 element_empty(&ho
, "hr");
1546 html_raw(&ho
, conf
.body_end
);
1548 if (conf
.address_section
) {
1549 int started
= FALSE
;
1550 if (conf
.htmlver
== ISO_HTML
) {
1552 * The ISO-HTML validator complains if
1553 * there isn't a <div> tag surrounding the
1554 * <address> tag. I'm uncertain of why this
1555 * should be - there appears to be no
1556 * mention of this in the ISO-HTML spec,
1557 * suggesting that it doesn't represent a
1558 * change from HTML 4, but nonetheless the
1559 * HTML 4 validator doesn't seem to mind.
1561 element_open(&ho
, "div");
1563 element_open(&ho
, "address");
1564 if (conf
.addr_start
) {
1565 html_raw(&ho
, conf
.addr_start
);
1569 if (conf
.visible_version_id
) {
1570 for (p
= sourceform
; p
; p
= p
->next
)
1571 if (p
->type
== para_VersionID
) {
1573 element_empty(&ho
, "br");
1575 html_text(&ho
, conf
.pre_versionid
);
1576 html_words(&ho
, p
->words
, NOTHING
,
1577 f
, keywords
, &conf
);
1578 html_text(&ho
, conf
.post_versionid
);
1581 done_version_ids
= TRUE
;
1583 if (conf
.addr_end
) {
1585 element_empty(&ho
, "br");
1586 html_raw(&ho
, conf
.addr_end
);
1588 element_close(&ho
, "address");
1589 if (conf
.htmlver
== ISO_HTML
)
1590 element_close(&ho
, "div");
1593 if (!done_version_ids
) {
1595 * If the user didn't want the version IDs
1596 * visible, I think we still have a duty to put
1597 * them in an HTML comment.
1599 int started
= FALSE
;
1600 for (p
= sourceform
; p
; p
= p
->next
)
1601 if (p
->type
== para_VersionID
) {
1603 html_raw(&ho
, "<!-- version IDs:\n");
1606 html_words(&ho
, p
->words
, NOTHING
,
1607 f
, keywords
, &conf
);
1611 html_raw(&ho
, "-->\n");
1615 element_close(&ho
, "body");
1617 element_close(&ho
, "html");
1624 * Before we start outputting the HTML Help files, check
1625 * whether there's even going to _be_ an index file: we omit it
1626 * if the index contains nothing.
1628 hhk_filename
= conf
.hhk_filename
;
1634 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1635 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1637 if (hi
->nrefs
> 0) {
1638 ok
= TRUE
; /* found an index entry */
1644 hhk_filename
= NULL
;
1648 * Output the MS HTML Help supporting files, if requested.
1650 * A good unofficial reference for these is <http://chmspec.nongnu.org/>.
1652 if (conf
.hhp_filename
) {
1656 ho
.charset
= CS_CP1252
; /* as far as I know, HHP files are */
1657 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1658 ho
.cstate
= charset_init_state
;
1659 ho
.ver
= HTML_4
; /* *shrug* */
1660 ho
.state
= HO_NEUTRAL
;
1661 ho
.contents_level
= 0;
1662 ho
.hackflags
= HO_HACK_QUOTENOTHING
;
1664 ho
.fp
= fopen(conf
.hhp_filename
, "w");
1666 error(err_cantopenw
, conf
.hhp_filename
);
1670 /* Binary TOC required for Next/Previous nav to work */
1672 "Compatibility=1.1 or later\n"
1673 "Compiled file=%s\n"
1674 "Default Window=main\n"
1675 "Default topic=%s\n"
1676 "Display compile progress=Yes\n"
1677 "Full-text search=Yes\n"
1678 "Title=", conf
.chm_filename
, files
.head
->filename
);
1681 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1682 NULL
, keywords
, &conf
);
1684 fprintf(ho
.fp
, "\n");
1687 * These two entries don't seem to be remotely necessary
1688 * for a successful run of the help _compiler_, but
1689 * omitting them causes the GUI Help Workshop to behave
1690 * rather strangely if you try to load the help project
1691 * into that and edit it.
1693 if (conf
.hhc_filename
)
1694 fprintf(ho
.fp
, "Contents file=%s\n", conf
.hhc_filename
);
1696 fprintf(ho
.fp
, "Index file=%s\n", hhk_filename
);
1698 fprintf(ho
.fp
, "\n[WINDOWS]\nmain=\"");
1700 ho
.hackflags
|= HO_HACK_OMITQUOTES
;
1702 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1703 NULL
, keywords
, &conf
);
1705 fprintf(ho
.fp
, "\",\"%s\",\"%s\",\"%s\",,,,,,"
1706 /* This first magic number is fsWinProperties, controlling
1707 * Navigation Pane options and the like.
1708 * Constants HHWIN_PROP_* in htmlhelp.h. */
1710 /* This second number is fsToolBarFlags, mainly controlling
1711 * toolbar buttons. Constants HHWIN_BUTTON_*.
1712 * NOTE: there are two pairs of bits for Next/Previous
1713 * buttons: 7/8 (which do nothing useful), and 21/22
1714 * (which work). (Neither of these are exposed in the HHW
1715 * UI, but they work fine in HH.) We use the latter. */
1716 "0x60304e,,,,,,,,0\n",
1717 conf
.hhc_filename ? conf
.hhc_filename
: "",
1718 hhk_filename ? hhk_filename
: "",
1719 files
.head
->filename
);
1722 * The [FILES] section is also not necessary for
1723 * compilation (hhc appears to build up a list of needed
1724 * files just by following links from the given starting
1725 * points), but useful for loading the project into HHW.
1727 fprintf(ho
.fp
, "\n[FILES]\n");
1728 for (f
= files
.head
; f
; f
= f
->next
)
1729 fprintf(ho
.fp
, "%s\n", f
->filename
);
1733 if (conf
.hhc_filename
) {
1739 ho
.fp
= fopen(conf
.hhc_filename
, "w");
1741 error(err_cantopenw
, conf
.hhc_filename
);
1743 ho
.charset
= CS_CP1252
; /* as far as I know, HHC files are */
1744 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1745 ho
.cstate
= charset_init_state
;
1746 ho
.ver
= HTML_4
; /* *shrug* */
1747 ho
.state
= HO_NEUTRAL
;
1748 ho
.contents_level
= 0;
1749 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1752 * Magic DOCTYPE which seems to work for .HHC files. I'm
1753 * wary of trying to change it!
1755 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1757 "<META HTTP-EQUIV=\"Content-Type\" "
1758 "CONTENT=\"text/html; charset=%s\">\n"
1759 "</HEAD><BODY><UL>\n",
1760 charset_to_mimeenc(conf
.output_charset
));
1762 for (f
= files
.head
; f
; f
= f
->next
) {
1764 * For each HTML file, write out a contents entry.
1766 int depth
, leaf
= TRUE
;
1769 * Determine the depth of this file in the contents
1772 * If the file contains no sections, it is assumed to
1777 for (a
= f
->first
->parent
; a
&& a
->type
!= TOP
; a
= a
->parent
)
1781 * Determine if this file is a leaf file, by
1782 * trawling the section list to see if there's any
1783 * section with an ancestor in this file but which
1784 * is not itself in this file.
1786 * Special case: for contents purposes, the TOP
1787 * file is not considered to be the parent of the
1788 * chapter files, so it's always a leaf.
1790 * A file with no sections in it is also a leaf.
1792 if (f
->first
&& f
->first
->type
!= TOP
) {
1793 for (s
= f
->first
; s
; s
= s
->next
) {
1796 if (leaf
&& s
->file
!= f
) {
1797 for (a
= s
; a
; a
= a
->parent
)
1807 * Now write out our contents entry.
1809 while (currdepth
< depth
) {
1810 fprintf(ho
.fp
, "<UL>\n");
1813 while (currdepth
> depth
) {
1814 fprintf(ho
.fp
, "</UL>\n");
1817 /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */
1818 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">"
1819 "<PARAM NAME=\"Name\" VALUE=\"");
1821 if (f
->first
->title
)
1822 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
1823 NULL
, keywords
, &conf
);
1824 else if (f
->first
->type
== INDEX
)
1825 html_text(&ho
, conf
.index_text
);
1826 fprintf(ho
.fp
, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">"
1827 "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n",
1828 f
->filename
, leaf ?
11 : 1);
1831 while (currdepth
> 0) {
1832 fprintf(ho
.fp
, "</UL>\n");
1836 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1847 * First make a pass over all HTML files and set their
1848 * `temp' fields to zero, because we're about to use them.
1850 for (f
= files
.head
; f
; f
= f
->next
)
1853 ho
.fp
= fopen(hhk_filename
, "w");
1855 error(err_cantopenw
, hhk_filename
);
1857 ho
.charset
= CS_CP1252
; /* as far as I know, HHK files are */
1858 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1859 ho
.cstate
= charset_init_state
;
1860 ho
.ver
= HTML_4
; /* *shrug* */
1861 ho
.state
= HO_NEUTRAL
;
1862 ho
.contents_level
= 0;
1863 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1866 * Magic DOCTYPE which seems to work for .HHK files. I'm
1867 * wary of trying to change it!
1869 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1871 "<META HTTP-EQUIV=\"Content-Type\" "
1872 "CONTENT=\"text/html; charset=%s\">\n"
1873 "</HEAD><BODY><UL>\n",
1874 charset_to_mimeenc(conf
.output_charset
));
1877 * Go through the index terms and output each one.
1879 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1880 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1883 if (hi
->nrefs
> 0) {
1884 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
1885 "<PARAM NAME=\"Name\" VALUE=\"");
1887 html_words(&ho
, entry
->text
, NOTHING
,
1888 NULL
, keywords
, &conf
);
1889 fprintf(ho
.fp
, "\">\n");
1891 for (j
= 0; j
< hi
->nrefs
; j
++) {
1893 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1896 * Use the temp field to ensure we don't
1897 * reference the same file more than once.
1899 if (!hr
->section
->file
->temp
) {
1900 fprintf(ho
.fp
, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n",
1901 hr
->section
->file
->filename
);
1902 hr
->section
->file
->temp
= 1;
1905 hr
->referenced
= TRUE
;
1908 fprintf(ho
.fp
, "</OBJECT>\n");
1911 * Now go through those files and re-clear the temp
1912 * fields ready for the _next_ index term.
1914 for (j
= 0; j
< hi
->nrefs
; j
++) {
1916 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1917 hr
->section
->file
->temp
= 0;
1922 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1927 * Go through and check that no index fragments were referenced
1928 * without being generated, or indeed vice versa.
1930 * (When I actually get round to freeing everything, this can
1931 * probably be the freeing loop as well.)
1933 for (p
= sourceform
; p
; p
= p
->next
) {
1935 for (w
= p
->words
; w
; w
= w
->next
)
1936 if (w
->type
== word_IndexRef
) {
1937 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1939 assert(!hr
->referenced
== !hr
->generated
);
1944 * Free all the working data.
1948 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1950 * frag->fragment is dynamically allocated, but will be
1951 * freed when we process the htmlsect structure which
1952 * it is attached to.
1956 freetree234(files
.frags
);
1959 * The strings in files.files are all owned by their containing
1960 * htmlfile structures, so there's no need to free them here.
1962 freetree234(files
.files
);
1964 htmlsect
*sect
, *tmp
;
1969 for (i
=0; i
< conf
.ntfragments
; i
++)
1970 sfree(sect
->fragments
[i
]);
1971 sfree(sect
->fragments
);
1975 sect
= nonsects
.head
;
1979 for (i
=0; i
< conf
.ntfragments
; i
++)
1980 sfree(sect
->fragments
[i
]);
1981 sfree(sect
->fragments
);
1987 htmlfile
*file
, *tmp
;
1991 sfree(file
->filename
);
1999 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
2000 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
2007 for (p
= sourceform
; p
; p
= p
->next
)
2008 for (w
= p
->words
; w
; w
= w
->next
)
2009 if (w
->type
== word_IndexRef
) {
2010 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2012 sfree(hr
->fragment
);
2017 sfree(conf
.single_filename
);
2018 sfree(conf
.contents_filename
);
2019 sfree(conf
.index_filename
);
2020 sfree(conf
.template_filename
);
2021 while (conf
.ntfragments
--)
2022 sfree(conf
.template_fragments
[conf
.ntfragments
]);
2023 sfree(conf
.template_fragments
);
2026 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
2027 htmlsect
*sect
, int depth
)
2033 * `depth' is derived from the heading_depth() macro at the top
2034 * of this file, which counts title as -1, chapter as 0,
2035 * heading as 1 and subsection as 2. However, the semantics of
2036 * cfg->leaf_level are defined to count chapter as 1, heading
2037 * as 2 etc. So first I increment depth :-(
2041 if (cfg
->leaf_level
== 0) {
2043 * leaf_level==0 is a special case, in which everything is
2044 * put into a single file.
2047 files
->single
= html_new_file(files
, cfg
->single_filename
);
2049 file
= files
->single
;
2052 * If the depth of this section is at or above leaf_level,
2053 * we invent a fresh file and put this section at its head.
2054 * Otherwise, we put it in the same file as its parent
2057 * Another special value of cfg->leaf_level is -1, which
2058 * means infinity (i.e. it's considered to always be
2059 * greater than depth).
2061 if (cfg
->leaf_level
> 0 && ldepth
> cfg
->leaf_level
) {
2063 * We know that sect->parent cannot be NULL. The only
2064 * circumstance in which it can be is if sect is at
2065 * chapter or appendix level, i.e. ldepth==1; and if
2066 * that's the case, then we cannot have entered this
2067 * branch unless cfg->leaf_level==0, in which case we
2068 * would be in the single-file case above and not here
2071 assert(sect
->parent
);
2073 file
= sect
->parent
->file
;
2075 if (sect
->type
== TOP
) {
2076 file
= html_new_file(files
, cfg
->contents_filename
);
2077 } else if (sect
->type
== INDEX
) {
2078 file
= html_new_file(files
, cfg
->index_filename
);
2082 assert(ldepth
> 0 && sect
->title
);
2083 title
= html_format(sect
->title
, cfg
->template_filename
);
2084 file
= html_new_file(files
, title
);
2092 if (file
->min_heading_depth
> depth
) {
2094 * This heading is at a higher level than any heading we
2095 * have so far placed in this file; so we set the `first'
2098 file
->min_heading_depth
= depth
;
2102 if (file
->min_heading_depth
== depth
)
2106 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
2108 htmlfile
*ret
= snew(htmlfile
);
2112 list
->tail
->next
= ret
;
2117 ret
->filename
= html_sanitise_filename(list
, dupstr(filename
));
2118 add234(list
->files
, ret
->filename
);
2119 ret
->last_fragment_number
= 0;
2120 ret
->min_heading_depth
= INT_MAX
;
2121 ret
->first
= ret
->last
= NULL
;
2126 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
2129 htmlsect
*ret
= snew(htmlsect
);
2133 list
->tail
->next
= ret
;
2143 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
2146 for (i
=0; i
< cfg
->ntfragments
; i
++)
2147 ret
->fragments
[i
] = NULL
;
2153 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
2154 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
2160 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
2161 case word_HyperLink
:
2162 if (flags
& LINKS
) {
2163 element_open(ho
, "a");
2164 c
= utoa_dup(w
->text
, CS_ASCII
);
2165 element_attr(ho
, "href", c
);
2169 case word_UpperXref
:
2170 case word_LowerXref
:
2171 if (flags
& LINKS
) {
2172 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
2178 s
= (htmlsect
*)p
->private_data
;
2182 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
2188 element_close(ho
, "a");
2191 if (flags
& INDEXENTS
) {
2192 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2193 html_fragment(ho
, hr
->fragment
);
2194 hr
->generated
= TRUE
;
2201 case word_WhiteSpace
:
2202 case word_EmphSpace
:
2203 case word_CodeSpace
:
2204 case word_WkCodeSpace
:
2206 case word_EmphQuote
:
2207 case word_CodeQuote
:
2208 case word_WkCodeQuote
:
2209 style
= towordstyle(w
->type
);
2210 type
= removeattr(w
->type
);
2211 if (style
== word_Emph
&&
2212 (attraux(w
->aux
) == attr_First
||
2213 attraux(w
->aux
) == attr_Only
) &&
2215 element_open(ho
, "em");
2216 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2217 (attraux(w
->aux
) == attr_First
||
2218 attraux(w
->aux
) == attr_Only
) &&
2220 element_open(ho
, "code");
2222 if (type
== word_WhiteSpace
)
2223 html_text(ho
, L
" ");
2224 else if (type
== word_Quote
) {
2225 if (quoteaux(w
->aux
) == quote_Open
)
2226 html_text(ho
, cfg
->lquote
);
2228 html_text(ho
, cfg
->rquote
);
2230 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
2231 html_text_nbsp(ho
, w
->text
);
2233 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
2236 if (style
== word_Emph
&&
2237 (attraux(w
->aux
) == attr_Last
||
2238 attraux(w
->aux
) == attr_Only
) &&
2240 element_close(ho
, "em");
2241 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2242 (attraux(w
->aux
) == attr_Last
||
2243 attraux(w
->aux
) == attr_Only
) &&
2245 element_close(ho
, "code");
2251 static void html_codepara(htmloutput
*ho
, word
*words
)
2253 element_open(ho
, "pre");
2254 element_open(ho
, "code");
2255 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
2260 if (words
->next
&& words
->next
->type
== word_Emph
) {
2261 e
= words
->next
->text
;
2262 words
= words
->next
;
2266 while (e
&& *e
&& *t
) {
2270 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
2278 element_open(ho
, open_tag
);
2280 html_text_limit(ho
, t
, n
);
2283 element_close(ho
, open_tag
);
2291 element_close(ho
, "code");
2292 element_close(ho
, "pre");
2295 static void html_charset_cleanup(htmloutput
*ho
)
2300 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
2301 ho
->charset
, &ho
->cstate
, NULL
);
2302 if (ho
->fp
&& bytes
> 0)
2303 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2306 static void return_mostly_to_neutral(htmloutput
*ho
)
2309 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
2310 fprintf(ho
->fp
, " />");
2311 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
2312 fprintf(ho
->fp
, ">");
2316 ho
->state
= HO_NEUTRAL
;
2319 static void return_to_neutral(htmloutput
*ho
)
2321 if (ho
->state
== HO_IN_TEXT
) {
2322 html_charset_cleanup(ho
);
2325 return_mostly_to_neutral(ho
);
2328 static void element_open(htmloutput
*ho
, char const *name
)
2330 return_to_neutral(ho
);
2332 fprintf(ho
->fp
, "<%s", name
);
2333 ho
->state
= HO_IN_TAG
;
2336 static void element_close(htmloutput
*ho
, char const *name
)
2338 return_to_neutral(ho
);
2340 fprintf(ho
->fp
, "</%s>", name
);
2341 ho
->state
= HO_NEUTRAL
;
2344 static void element_empty(htmloutput
*ho
, char const *name
)
2346 return_to_neutral(ho
);
2348 fprintf(ho
->fp
, "<%s", name
);
2349 ho
->state
= HO_IN_EMPTY_TAG
;
2352 static void html_nl(htmloutput
*ho
)
2354 return_to_neutral(ho
);
2356 fputc('\n', ho
->fp
);
2359 static void html_raw(htmloutput
*ho
, char *text
)
2361 return_to_neutral(ho
);
2363 fputs(text
, ho
->fp
);
2366 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
2368 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2371 fputs(text
, ho
->fp
);
2375 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
2377 html_charset_cleanup(ho
);
2378 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2380 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
2383 static void element_attr_w(htmloutput
*ho
, char const *name
,
2384 wchar_t const *value
)
2386 html_charset_cleanup(ho
);
2388 fprintf(ho
->fp
, " %s=\"", name
);
2389 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
2390 html_charset_cleanup(ho
);
2395 static void html_text(htmloutput
*ho
, wchar_t const *text
)
2397 return_mostly_to_neutral(ho
);
2398 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
2401 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
2403 return_mostly_to_neutral(ho
);
2404 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
2407 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
2409 return_mostly_to_neutral(ho
);
2410 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
2413 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
2414 int maxlen
, int quote_quotes
, int nbsp
)
2416 int textlen
= ustrlen(text
);
2420 if (ho
->hackflags
& (HO_HACK_QUOTEQUOTES
| HO_HACK_OMITQUOTES
))
2421 quote_quotes
= TRUE
; /* override the input value */
2423 if (maxlen
> 0 && textlen
> maxlen
)
2425 if (ho
->hacklimit
>= 0) {
2426 if (textlen
> ho
->hacklimit
)
2427 textlen
= ho
->hacklimit
;
2428 ho
->hacklimit
-= textlen
;
2431 while (textlen
> 0) {
2432 /* Scan ahead for characters we really can't display in HTML. */
2433 int lenbefore
, lenafter
;
2434 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
2435 if (text
[lenbefore
] == L
'<' ||
2436 text
[lenbefore
] == L
'>' ||
2437 text
[lenbefore
] == L
'&' ||
2438 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
2439 (text
[lenbefore
] == L
' ' && nbsp
))
2441 lenafter
= lenbefore
;
2442 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
2443 ho
->charset
, &ho
->cstate
, &err
);
2444 textlen
-= (lenbefore
- lenafter
);
2445 if (bytes
> 0 && ho
->fp
)
2446 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2449 * We have encountered a character that cannot be
2450 * displayed in the selected output charset. Therefore,
2451 * we use an HTML numeric entity reference.
2453 assert(textlen
> 0);
2455 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
2457 } else if (lenafter
== 0 && textlen
> 0) {
2459 * We have encountered a character which is special to
2463 if (*text
== L
'"' && (ho
->hackflags
& HO_HACK_OMITQUOTES
)) {
2464 fputc('\'', ho
->fp
);
2465 } else if (ho
->hackflags
& HO_HACK_QUOTENOTHING
) {
2466 fputc(*text
, ho
->fp
);
2469 fprintf(ho
->fp
, "<");
2470 else if (*text
== L
'>')
2471 fprintf(ho
->fp
, ">");
2472 else if (*text
== L
'&')
2473 fprintf(ho
->fp
, "&");
2474 else if (*text
== L
'"')
2475 fprintf(ho
->fp
, """);
2476 else if (*text
== L
' ') {
2478 fprintf(ho
->fp
, " ");
2480 assert(!"Can't happen");
2488 static void cleanup(htmloutput
*ho
)
2490 return_to_neutral(ho
);
2495 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
2496 htmlfile
*targetfile
, char *targetfrag
)
2498 rdstringc rs
= { 0, 0, NULL
};
2501 if (targetfile
!= thisfile
)
2502 rdaddsc(&rs
, targetfile
->filename
);
2505 rdaddsc(&rs
, targetfrag
);
2509 element_open(ho
, "a");
2510 element_attr(ho
, "href", url
);
2514 static void html_fragment(htmloutput
*ho
, char const *fragment
)
2516 element_open(ho
, "a");
2517 element_attr(ho
, "name", fragment
);
2518 if (is_xhtml(ho
->ver
))
2519 element_attr(ho
, "id", fragment
);
2520 element_close(ho
, "a");
2523 static char *html_format(paragraph
*p
, char *template_string
)
2527 wchar_t *ws
, wsbuf
[2];
2528 rdstringc rs
= { 0, 0, NULL
};
2530 t
= template_string
;
2532 if (*t
== '%' && t
[1]) {
2546 if (p
->kwtext
&& fmt
== 'n')
2548 else if (p
->kwtext2
&& fmt
== 'b') {
2550 * HTML fragment names must start with a letter, so
2551 * simply `1.2.3' is not adequate. In this case I'm
2552 * going to cheat slightly by prepending the first
2553 * character of the first word of kwtext, so that
2554 * we get `C1' for chapter 1, `S2.3' for section
2557 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2560 wsbuf
[0] = p
->kwtext
->text
[0];
2563 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2566 /* %N comes here; also failure cases of other fmts */
2570 c
= utoa_dup(ws
, CS_ASCII
);
2576 if (removeattr(w
->type
) == word_Normal
) {
2577 c
= utoa_dup(w
->text
, CS_ASCII
);
2588 return rdtrimc(&rs
);
2591 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2595 * The HTML 4 spec's strictest definition of fragment names (<a
2596 * name> and "id" attributes) says that they `must begin with a
2597 * letter and may be followed by any number of letters, digits,
2598 * hyphens, underscores, colons, and periods'.
2600 * So here we unceremoniously rip out any characters not
2601 * conforming to this limitation.
2603 char *p
= text
, *q
= text
;
2605 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2607 if ((*q
++ = *p
++) != '\0') {
2609 if ((*p
>='A' && *p
<='Z') ||
2610 (*p
>='a' && *p
<='z') ||
2611 (*p
>='0' && *p
<='9') ||
2612 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2620 /* If there's nothing left, make something valid up */
2622 static const char anonfrag
[] = "anon";
2623 text
= sresize(text
, lenof(anonfrag
), char);
2624 strcpy(text
, anonfrag
);
2628 * Now we check for clashes with other fragment names, and
2629 * adjust this one if necessary by appending a hyphen followed
2633 htmlfragment
*frag
= snew(htmlfragment
);
2634 int len
= 0; /* >0 indicates we have resized */
2638 frag
->fragment
= text
;
2640 while (add234(files
->frags
, frag
) != frag
) {
2643 frag
->fragment
= text
= sresize(text
, len
+20, char);
2646 sprintf(text
+ len
, "-%d", ++suffix
);
2653 static char *html_sanitise_filename(htmlfilelist
*files
, char *text
)
2656 * Unceremoniously rip out any character that might cause
2657 * difficulty in some filesystem or another, or be otherwise
2660 * That doesn't leave much punctuation. I permit alphanumerics
2663 char *p
= text
, *q
= text
;
2666 if ((*p
>='A' && *p
<='Z') ||
2667 (*p
>='a' && *p
<='z') ||
2668 (*p
>='0' && *p
<='9') ||
2669 *p
=='-' || *p
=='_' || *p
=='+' || *p
=='.' || *p
=='=')
2675 /* If there's nothing left, make something valid up */
2677 static const char anonfrag
[] = "anon.html";
2678 text
= sresize(text
, lenof(anonfrag
), char);
2679 strcpy(text
, anonfrag
);
2683 * Now we check for clashes with other filenames, and adjust
2684 * this one if necessary by appending a hyphen followed by a
2685 * number just before the file extension (if any).
2693 while (find234(files
->files
, text
, NULL
)) {
2697 text
= snewn(len
+20, char);
2699 for (extpos
= len
; extpos
> 0 && p
[extpos
-1] != '.'; extpos
--);
2706 sprintf(text
, "%.*s-%d%s", extpos
, p
, ++suffix
, p
+extpos
);
2716 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2717 htmlfile
*thisfile
, keywordlist
*keywords
,
2720 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2721 element_close(ho
, "li");
2725 while (ho
->contents_level
> depth
) {
2726 element_close(ho
, "ul");
2727 ho
->contents_level
--;
2728 if (ho
->contents_level
> 0) {
2729 element_close(ho
, "li");
2734 while (ho
->contents_level
< depth
) {
2736 element_open(ho
, "ul");
2738 ho
->contents_level
++;
2744 element_open(ho
, "li");
2745 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2746 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2747 element_close(ho
, "a");
2748 /* <li> will be closed by a later invocation */
2751 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2752 keywordlist
*keywords
, htmlconfig
*cfg
,
2758 int depth
= heading_depth(s
->title
);
2762 else if (depth
== 0)
2763 sl
= &cfg
->achapter
;
2764 else if (depth
<= cfg
->nasect
)
2765 sl
= &cfg
->asect
[depth
-1];
2767 sl
= &cfg
->asect
[cfg
->nasect
-1];
2771 else if (sl
->just_numbers
)
2772 number
= s
->title
->kwtext2
;
2774 number
= s
->title
->kwtext
;
2777 html_words(ho
, number
, MARKUP
,
2778 thisfile
, keywords
, cfg
);
2779 html_text(ho
, sl
->number_suffix
);
2782 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2783 thisfile
, keywords
, cfg
);
2785 assert(s
->type
!= NORMAL
);
2787 * If we're printing the full document title for _real_ and
2788 * there isn't one, we don't want to print `Preamble' at
2789 * the top of what ought to just be some text. If we need
2790 * it in any other context such as TOCs, we need to print
2793 if (s
->type
== TOP
&& !real
)
2794 html_text(ho
, cfg
->preamble_text
);
2795 else if (s
->type
== INDEX
)
2796 html_text(ho
, cfg
->index_text
);