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_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;
263 ret
.rellinks
= FALSE
;
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_separator
= L
" | ";
286 ret
.index_main_sep
= L
": ";
287 ret
.index_multi_sep
= L
", ";
288 ret
.pre_versionid
= L
"[";
289 ret
.post_versionid
= L
"]";
291 * Default quote characters are Unicode matched single quotes,
292 * falling back to ordinary ASCII ".
294 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
295 ret
.rquote
= uadv(ret
.lquote
);
298 * Two-pass configuration so that we can pick up global config
299 * (e.g. `quotes') before having it overridden by specific
300 * config (`html-quotes'), irrespective of the order in which
303 for (p
= source
; p
; p
= p
->next
) {
304 if (p
->type
== para_Config
) {
305 if (!ustricmp(p
->keyword
, L
"quotes")) {
306 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
307 ret
.lquote
= uadv(p
->keyword
);
308 ret
.rquote
= uadv(ret
.lquote
);
310 } else if (!ustricmp(p
->keyword
, L
"index")) {
311 ret
.index_text
= uadv(p
->keyword
);
312 } else if (!ustricmp(p
->keyword
, L
"contents")) {
313 ret
.contents_text
= uadv(p
->keyword
);
318 for (p
= source
; p
; p
= p
->next
) {
319 if (p
->type
== para_Config
) {
320 wchar_t *k
= p
->keyword
;
322 if (!ustrnicmp(k
, L
"xhtml-", 6))
323 k
++; /* treat `xhtml-' and `html-' the same */
325 if (!ustricmp(k
, L
"html-restrict-charset")) {
326 ret
.restrict_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
327 } else if (!ustricmp(k
, L
"html-output-charset")) {
328 ret
.output_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
329 } else if (!ustricmp(k
, L
"html-version")) {
330 wchar_t *vername
= uadv(k
);
331 static const struct {
335 {L
"html3.2", HTML_3_2
},
337 {L
"iso-html", ISO_HTML
},
338 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
339 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
343 for (i
= 0; i
< (int)lenof(versions
); i
++)
344 if (!ustricmp(versions
[i
].name
, vername
))
347 if (i
== lenof(versions
))
348 error(err_htmlver
, &p
->fpos
, vername
);
350 ret
.htmlver
= versions
[i
].ver
;
351 } else if (!ustricmp(k
, L
"html-single-filename")) {
352 sfree(ret
.single_filename
);
353 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
354 } else if (!ustricmp(k
, L
"html-contents-filename")) {
355 sfree(ret
.contents_filename
);
356 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
357 } else if (!ustricmp(k
, L
"html-index-filename")) {
358 sfree(ret
.index_filename
);
359 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
360 } else if (!ustricmp(k
, L
"html-template-filename")) {
361 sfree(ret
.template_filename
);
362 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
363 } else if (!ustricmp(k
, L
"html-template-fragment")) {
364 char *frag
= adv(p
->origkeyword
);
366 while (ret
.ntfragments
--)
367 sfree(ret
.template_fragments
[ret
.ntfragments
]);
368 sfree(ret
.template_fragments
);
369 ret
.template_fragments
= NULL
;
373 ret
.template_fragments
=
374 sresize(ret
.template_fragments
,
375 ret
.ntfragments
, char *);
376 ret
.template_fragments
[ret
.ntfragments
-1] =
381 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 1);
382 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
383 ret
.achapter
.just_numbers
= utob(uadv(k
));
384 } else if (!ustricmp(k
, L
"html-suppress-navlinks")) {
385 ret
.navlinks
= !utob(uadv(k
));
386 } else if (!ustricmp(k
, L
"html-rellinks")) {
387 ret
.rellinks
= utob(uadv(k
));
388 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
389 ret
.achapter
.number_suffix
= uadv(k
);
390 } else if (!ustricmp(k
, L
"html-leaf-level")) {
391 wchar_t *u
= uadv(k
);
392 if (!ustricmp(u
, L
"infinite") ||
393 !ustricmp(u
, L
"infinity") ||
394 !ustricmp(u
, L
"inf"))
395 ret
.leaf_level
= -1; /* represents infinity */
397 ret
.leaf_level
= utoi(u
);
398 } else if (!ustricmp(k
, L
"html-section-numeric")) {
399 wchar_t *q
= uadv(k
);
405 if (n
>= ret
.nasect
) {
407 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
408 for (i
= ret
.nasect
; i
<= n
; i
++)
409 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
412 ret
.asect
[n
].just_numbers
= utob(q
);
413 } else if (!ustricmp(k
, L
"html-section-suffix")) {
414 wchar_t *q
= uadv(k
);
420 if (n
>= ret
.nasect
) {
422 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
423 for (i
= ret
.nasect
; i
<= n
; i
++) {
424 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
428 ret
.asect
[n
].number_suffix
= q
;
429 } else if (!ustricmp(k
, L
"html-contents-depth") ||
430 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
432 * Relic of old implementation: this directive used
433 * to be written as \cfg{html-contents-depth-3}{2}
434 * rather than the usual Halibut convention of
435 * \cfg{html-contents-depth}{3}{2}. We therefore
438 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
444 if (n
>= ret
.ncdepths
) {
446 ret
.contents_depths
=
447 sresize(ret
.contents_depths
, n
+1, int);
448 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
449 ret
.contents_depths
[i
] = i
+2;
453 ret
.contents_depths
[n
] = utoi(q
);
454 } else if (!ustricmp(k
, L
"html-head-end")) {
455 ret
.head_end
= adv(p
->origkeyword
);
456 } else if (!ustricmp(k
, L
"html-body-tag")) {
457 ret
.body_tag
= adv(p
->origkeyword
);
458 } else if (!ustricmp(k
, L
"html-body-start")) {
459 ret
.body_start
= adv(p
->origkeyword
);
460 } else if (!ustricmp(k
, L
"html-body-end")) {
461 ret
.body_end
= adv(p
->origkeyword
);
462 } else if (!ustricmp(k
, L
"html-address-start")) {
463 ret
.addr_start
= adv(p
->origkeyword
);
464 } else if (!ustricmp(k
, L
"html-address-end")) {
465 ret
.addr_end
= adv(p
->origkeyword
);
466 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
467 ret
.nav_attr
= adv(p
->origkeyword
);
468 } else if (!ustricmp(k
, L
"html-author")) {
469 ret
.author
= uadv(k
);
470 } else if (!ustricmp(k
, L
"html-description")) {
471 ret
.description
= uadv(k
);
472 } else if (!ustricmp(k
, L
"html-suppress-address")) {
473 ret
.address_section
= !utob(uadv(k
));
474 } else if (!ustricmp(k
, L
"html-versionid")) {
475 ret
.visible_version_id
= utob(uadv(k
));
476 } else if (!ustricmp(k
, L
"html-quotes")) {
477 if (*uadv(k
) && *uadv(uadv(k
))) {
478 ret
.lquote
= uadv(k
);
479 ret
.rquote
= uadv(ret
.lquote
);
481 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
482 ret
.leaf_contains_contents
= utob(uadv(k
));
483 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
484 ret
.leaf_smallest_contents
= utoi(uadv(k
));
485 } else if (!ustricmp(k
, L
"html-index-text")) {
486 ret
.index_text
= uadv(k
);
487 } else if (!ustricmp(k
, L
"html-contents-text")) {
488 ret
.contents_text
= uadv(k
);
489 } else if (!ustricmp(k
, L
"html-preamble-text")) {
490 ret
.preamble_text
= uadv(k
);
491 } else if (!ustricmp(k
, L
"html-title-separator")) {
492 ret
.title_separator
= uadv(k
);
493 } else if (!ustricmp(k
, L
"html-nav-prev-text")) {
494 ret
.nav_prev_text
= uadv(k
);
495 } else if (!ustricmp(k
, L
"html-nav-next-text")) {
496 ret
.nav_next_text
= uadv(k
);
497 } else if (!ustricmp(k
, L
"html-nav-separator")) {
498 ret
.nav_separator
= uadv(k
);
499 } else if (!ustricmp(k
, L
"html-index-main-separator")) {
500 ret
.index_main_sep
= uadv(k
);
501 } else if (!ustricmp(k
, L
"html-index-multiple-separator")) {
502 ret
.index_multi_sep
= uadv(k
);
503 } else if (!ustricmp(k
, L
"html-pre-versionid")) {
504 ret
.pre_versionid
= uadv(k
);
505 } else if (!ustricmp(k
, L
"html-post-versionid")) {
506 ret
.post_versionid
= uadv(k
);
507 } else if (!ustricmp(k
, L
"html-mshtmlhelp-chm")) {
508 sfree(ret
.chm_filename
);
509 ret
.chm_filename
= dupstr(adv(p
->origkeyword
));
510 } else if (!ustricmp(k
, L
"html-mshtmlhelp-project")) {
511 sfree(ret
.hhp_filename
);
512 ret
.hhp_filename
= dupstr(adv(p
->origkeyword
));
513 } else if (!ustricmp(k
, L
"html-mshtmlhelp-contents")) {
514 sfree(ret
.hhc_filename
);
515 ret
.hhc_filename
= dupstr(adv(p
->origkeyword
));
516 } else if (!ustricmp(k
, L
"html-mshtmlhelp-index")) {
517 sfree(ret
.hhk_filename
);
518 ret
.hhk_filename
= dupstr(adv(p
->origkeyword
));
524 * Enforce that the CHM and HHP filenames must either be both
525 * present or both absent. If one is present but not the other,
528 if (!ret
.chm_filename
^ !ret
.hhp_filename
) {
530 sfree(ret
.chm_filename
); ret
.chm_filename
= NULL
;
531 sfree(ret
.hhp_filename
); ret
.hhp_filename
= NULL
;
534 * And if we're not generating an HHP, there's no need for HHC
537 if (!ret
.hhp_filename
) {
538 sfree(ret
.hhc_filename
); ret
.hhc_filename
= NULL
;
539 sfree(ret
.hhk_filename
); ret
.hhk_filename
= NULL
;
543 * Now process fallbacks on quote characters.
545 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
546 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
547 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
548 ret
.lquote
= uadv(ret
.rquote
);
549 ret
.rquote
= uadv(ret
.lquote
);
555 paragraph
*html_config_filename(char *filename
)
558 * If the user passes in a single filename as a parameter to
559 * the `--html' command-line option, then we should assume it
560 * to imply _two_ config directives:
561 * \cfg{html-single-filename}{whatever} and
562 * \cfg{html-leaf-level}{0}; the rationale being that the user
563 * wants their output _in that file_.
567 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
568 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
573 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
574 indexdata
*idx
, void *unused
)
579 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
580 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
586 conf
= html_configure(sourceform
);
589 * We're going to make heavy use of paragraphs' private data
590 * fields in the forthcoming code. Clear them first, so we can
591 * reliably tell whether we have auxiliary data for a
592 * particular paragraph.
594 for (p
= sourceform
; p
; p
= p
->next
)
595 p
->private_data
= NULL
;
597 files
.frags
= newtree234(html_fragment_compare
);
598 files
.files
= newtree234(html_filename_compare
);
601 * Start by figuring out into which file each piece of the
602 * document should be put. We'll do this by inventing an
603 * `htmlsect' structure and stashing it in the private_data
604 * field of each section paragraph; we also need one additional
605 * htmlsect for the document index, which won't show up in the
606 * source form but needs to be consistently mentioned in
609 * While we're here, we'll also invent the HTML fragment name(s)
616 topsect
= html_new_sect(§s
, NULL
, &conf
);
618 topsect
->title
= NULL
;
619 topsect
->text
= sourceform
;
620 topsect
->contents_depth
= contents_depth(conf
, 0);
621 html_file_section(&conf
, &files
, topsect
, -1);
623 for (p
= sourceform
; p
; p
= p
->next
)
624 if (is_heading_type(p
->type
)) {
625 d
= heading_depth(p
);
627 if (p
->type
== para_Title
) {
632 sect
= html_new_sect(§s
, p
, &conf
);
633 sect
->text
= p
->next
;
635 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
638 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
639 assert(sect
->parent
!= NULL
);
641 sect
->parent
= topsect
;
642 p
->private_data
= sect
;
644 html_file_section(&conf
, &files
, sect
, d
);
648 for (i
=0; i
< conf
.ntfragments
; i
++) {
650 html_format(p
, conf
.template_fragments
[i
]);
652 html_sanitise_fragment(&files
, sect
->file
,
659 * And the index, if we have one. Note that we don't output
660 * an index as an HTML file if we're outputting one as a
663 has_index
= (count234(idx
->entries
) > 0);
664 if (has_index
&& !conf
.hhk_filename
) {
665 sect
= html_new_sect(§s
, NULL
, &conf
);
668 sect
->parent
= topsect
;
669 sect
->contents_depth
= 0;
670 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
671 sect
->fragments
[0] = utoa_dup(conf
.index_text
, CS_ASCII
);
672 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
674 files
.index
= sect
->file
;
679 * Go through the keyword list and sort out fragment IDs for
680 * all the potentially referenced paragraphs which _aren't_
688 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
689 paragraph
*q
, *p
= kw
->para
;
691 if (!is_heading_type(p
->type
)) {
695 * Find the paragraph's parent htmlsect, to
696 * determine which file it will end up in.
701 * Preamble paragraphs have no parent. So if we
702 * have a non-heading with no parent, it must
703 * be preamble, and therefore its parent
704 * htmlsect must be the preamble one.
707 sects
.head
->type
== TOP
);
710 parent
= (htmlsect
*)q
->private_data
;
713 * Now we can construct an htmlsect for this
714 * paragraph itself, taking care to put it in the
715 * list of non-sections rather than the list of
716 * sections (so that traverses of the `sects' list
717 * won't attempt to add it to the contents or
718 * anything weird like that).
720 sect
= html_new_sect(&nonsects
, p
, &conf
);
721 sect
->file
= parent
->file
;
722 sect
->parent
= parent
;
723 p
->private_data
= sect
;
726 * Fragment IDs for these paragraphs will simply be
727 * `p' followed by an integer.
729 sect
->fragments
[0] = snewn(40, char);
730 sprintf(sect
->fragments
[0], "p%d",
731 sect
->file
->last_fragment_number
++);
732 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
739 * Reset the fragment numbers in each file. I've just used them
740 * to generate `p' fragment IDs for non-section paragraphs
741 * (numbered list elements, bibliocited), and now I want to use
742 * them for `i' fragment IDs for index entries.
746 for (file
= files
.head
; file
; file
= file
->next
)
747 file
->last_fragment_number
= 0;
751 * Now sort out the index. This involves:
753 * - For each index term, we set up an htmlindex structure to
754 * store all the references to that term.
756 * - Then we make a pass over the actual document, finding
757 * every word_IndexRef; for each one, we actually figure out
758 * the HTML filename/fragment pair we will use to reference
759 * it, store that information in the private data field of
760 * the word_IndexRef itself (so we can recreate it when the
761 * time comes to output our HTML), and add a reference to it
762 * to the index term in question.
771 * Set up the htmlindex structures.
774 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
775 htmlindex
*hi
= snew(htmlindex
);
777 hi
->nrefs
= hi
->refsize
= 0;
780 entry
->backend_data
= hi
;
784 * Run over the document inventing fragments. Each fragment
785 * is of the form `i' followed by an integer.
787 lastsect
= sects
.head
; /* this is always the top section */
788 for (p
= sourceform
; p
; p
= p
->next
) {
789 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
790 lastsect
= (htmlsect
*)p
->private_data
;
792 for (w
= p
->words
; w
; w
= w
->next
)
793 if (w
->type
== word_IndexRef
) {
794 htmlindexref
*hr
= snew(htmlindexref
);
798 hr
->referenced
= hr
->generated
= FALSE
;
799 hr
->section
= lastsect
;
803 lastsect
->file
->last_fragment_number
++);
804 hr
->fragment
= dupstr(buf
);
806 html_sanitise_fragment(&files
, hr
->section
->file
,
809 w
->private_data
= hr
;
811 tag
= index_findtag(idx
, w
->text
);
815 for (i
= 0; i
< tag
->nrefs
; i
++) {
816 indexentry
*entry
= tag
->refs
[i
];
817 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
819 if (hi
->nrefs
>= hi
->refsize
) {
821 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
824 hi
->refs
[hi
->nrefs
++] = w
;
831 * Now we're ready to write out the actual HTML files.
835 * - we open that file and write its header
836 * - we run down the list of sections
837 * - for each section directly contained within that file, we
838 * output the section text
839 * - for each section which is not in the file but which has a
840 * parent that is, we output a contents entry for the
841 * section if appropriate
842 * - finally, we output the file trailer and close the file.
851 for (f
= files
.head
; f
; f
= f
->next
) {
854 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
855 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
856 struct stackelement
{
857 struct stackelement
*next
;
858 enum LISTTYPE listtype
;
859 enum ITEMTYPE itemtype
;
862 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
863 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
865 ho
.fp
= fopen(f
->filename
, "w");
867 error(err_cantopenw
, f
->filename
);
869 ho
.charset
= conf
.output_charset
;
870 ho
.restrict_charset
= conf
.restrict_charset
;
871 ho
.cstate
= charset_init_state
;
872 ho
.ver
= conf
.htmlver
;
873 ho
.state
= HO_NEUTRAL
;
874 ho
.contents_level
= 0;
875 ho
.hackflags
= 0; /* none of these thankyouverymuch */
879 switch (conf
.htmlver
) {
882 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
883 "HTML 3.2 Final//EN\">\n");
887 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
888 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
893 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
894 "15445:2000//DTD HTML//EN\">\n");
896 case XHTML_1_0_TRANSITIONAL
:
898 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
899 charset_to_mimeenc(conf
.output_charset
));
900 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
901 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
902 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
905 case XHTML_1_0_STRICT
:
907 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
908 charset_to_mimeenc(conf
.output_charset
));
909 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
910 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
911 "DTD/xhtml1-strict.dtd\">\n");
916 element_open(&ho
, "html");
917 if (is_xhtml(conf
.htmlver
)) {
918 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
922 element_open(&ho
, "head");
925 element_empty(&ho
, "meta");
926 element_attr(&ho
, "http-equiv", "content-type");
929 sprintf(buf
, "text/html; charset=%.150s",
930 charset_to_mimeenc(conf
.output_charset
));
931 element_attr(&ho
, "content", buf
);
936 element_empty(&ho
, "meta");
937 element_attr(&ho
, "name", "author");
938 element_attr_w(&ho
, "content", conf
.author
);
942 if (conf
.description
) {
943 element_empty(&ho
, "meta");
944 element_attr(&ho
, "name", "description");
945 element_attr_w(&ho
, "content", conf
.description
);
949 element_open(&ho
, "title");
950 if (f
->first
&& f
->first
->title
) {
951 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
955 if (f
->last
!= f
->first
&& f
->last
->title
) {
956 html_text(&ho
, conf
.title_separator
);
957 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
961 element_close(&ho
, "title");
967 element_empty(&ho
, "link");
968 element_attr(&ho
, "rel", "previous");
969 element_attr(&ho
, "href", prevf
->filename
);
973 /* FIXME: link rel="up" */
975 if (f
!= files
.head
) {
976 element_empty(&ho
, "link");
977 element_attr(&ho
, "rel", "ToC");
978 element_attr(&ho
, "href", files
.head
->filename
);
982 if (has_index
&& files
.index
&& f
!= files
.index
) {
983 element_empty(&ho
, "link");
984 element_attr(&ho
, "rel", "index");
985 element_attr(&ho
, "href", files
.index
->filename
);
990 element_empty(&ho
, "link");
991 element_attr(&ho
, "rel", "next");
992 element_attr(&ho
, "href", f
->next
->filename
);
999 html_raw(&ho
, conf
.head_end
);
1002 * Add any <head> data defined in specific sections
1003 * that go in this file. (This is mostly to allow <meta
1004 * name="AppleTitle"> tags for Mac online help.)
1006 for (s
= sects
.head
; s
; s
= s
->next
) {
1007 if (s
->file
== f
&& s
->text
) {
1009 p
&& (p
== s
->text
|| p
->type
== para_Title
||
1010 !is_heading_type(p
->type
));
1012 if (p
->type
== para_Config
) {
1013 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
1014 html_raw(&ho
, adv(p
->origkeyword
));
1021 element_close(&ho
, "head");
1025 html_raw(&ho
, conf
.body_tag
);
1027 element_open(&ho
, "body");
1030 if (conf
.body_start
)
1031 html_raw(&ho
, conf
.body_start
);
1034 * Write out a nav bar. Special case: we don't do this
1035 * if there is only one file.
1037 if (conf
.navlinks
&& files
.head
!= files
.tail
) {
1038 element_open(&ho
, "p");
1040 html_raw_as_attr(&ho
, conf
.nav_attr
);
1043 element_open(&ho
, "a");
1044 element_attr(&ho
, "href", prevf
->filename
);
1046 html_text(&ho
, conf
.nav_prev_text
);
1048 element_close(&ho
, "a");
1050 html_text(&ho
, conf
.nav_separator
);
1052 if (f
!= files
.head
) {
1053 element_open(&ho
, "a");
1054 element_attr(&ho
, "href", files
.head
->filename
);
1056 html_text(&ho
, conf
.contents_text
);
1057 if (f
!= files
.head
)
1058 element_close(&ho
, "a");
1060 if (has_index
&& files
.index
) {
1061 html_text(&ho
, conf
.nav_separator
);
1062 if (f
!= files
.index
) {
1063 element_open(&ho
, "a");
1064 element_attr(&ho
, "href", files
.index
->filename
);
1066 html_text(&ho
, conf
.index_text
);
1067 if (f
!= files
.index
)
1068 element_close(&ho
, "a");
1071 html_text(&ho
, conf
.nav_separator
);
1074 element_open(&ho
, "a");
1075 element_attr(&ho
, "href", f
->next
->filename
);
1077 html_text(&ho
, conf
.nav_next_text
);
1079 element_close(&ho
, "a");
1081 element_close(&ho
, "p");
1087 * Write out a prefix TOC for the file (if a leaf file).
1089 * We start by going through the section list and
1090 * collecting the sections which need to be added to
1091 * the contents. On the way, we also test to see if
1092 * this file is a leaf file (defined as one which
1093 * contains all descendants of any section it
1094 * contains), because this will play a part in our
1095 * decision on whether or not to _output_ the TOC.
1097 * Special case: we absolutely do not do this if we're
1098 * in single-file mode.
1100 if (files
.head
!= files
.tail
) {
1101 int ntoc
= 0, tocsize
= 0;
1102 htmlsect
**toc
= NULL
;
1105 for (s
= sects
.head
; s
; s
= s
->next
) {
1110 * Search up from this section until we find
1111 * the highest-level one which belongs in this
1116 for (ac
= s
; ac
; ac
= ac
->parent
) {
1117 if (ac
->file
== f
) {
1124 if (s
->file
!= f
&& a
!= NULL
)
1128 if (adepth
<= a
->contents_depth
) {
1129 if (ntoc
>= tocsize
) {
1131 toc
= sresize(toc
, tocsize
, htmlsect
*);
1138 if (leaf
&& conf
.leaf_contains_contents
&&
1139 ntoc
>= conf
.leaf_smallest_contents
) {
1142 for (i
= 0; i
< ntoc
; i
++) {
1143 htmlsect
*s
= toc
[i
];
1144 int hlevel
= (s
->type
== TOP ?
-1 :
1145 s
->type
== INDEX ?
0 :
1146 heading_depth(s
->title
))
1147 - f
->min_heading_depth
+ 1;
1149 assert(hlevel
>= 1);
1150 html_contents_entry(&ho
, hlevel
, s
,
1151 f
, keywords
, &conf
);
1153 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1158 * Now go through the document and output some real
1162 for (s
= sects
.head
; s
; s
= s
->next
) {
1165 * This section belongs in this file.
1171 * Doesn't belong in this file, but it may be
1172 * a descendant of a section which does, in
1173 * which case we should consider it for the
1174 * main TOC of this file (for non-leaf files).
1182 * Search up from this section until we find
1183 * the highest-level one which belongs in this
1188 for (ac
= s
; ac
; ac
= ac
->parent
) {
1189 if (ac
->file
== f
) {
1198 * This section does not belong in this
1199 * file, but an ancestor of it does. Write
1200 * out a contents table entry, if the depth
1201 * doesn't exceed the maximum contents
1202 * depth for the ancestor section.
1204 if (adepth
<= a
->contents_depth
) {
1205 html_contents_entry(&ho
, adepth
, s
,
1206 f
, keywords
, &conf
);
1215 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1218 * Display the section heading.
1221 hlevel
= (s
->type
== TOP ?
-1 :
1222 s
->type
== INDEX ?
0 :
1223 heading_depth(s
->title
))
1224 - f
->min_heading_depth
+ 1;
1225 assert(hlevel
>= 1);
1226 /* HTML headings only go up to <h6> */
1230 htag
[1] = '0' + hlevel
;
1232 element_open(&ho
, htag
);
1235 * Provide anchor(s) for cross-links to target.
1237 * (Also we'll have to do this separately in
1238 * other paragraph types - NumberedList and
1243 for (i
=0; i
< conf
.ntfragments
; i
++)
1244 if (s
->fragments
[i
])
1245 html_fragment(&ho
, s
->fragments
[i
]);
1248 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1250 element_close(&ho
, htag
);
1253 * Now display the section text.
1256 stackhead
= snew(struct stackelement
);
1257 stackhead
->next
= NULL
;
1258 stackhead
->listtype
= NOLIST
;
1259 stackhead
->itemtype
= NOITEM
;
1261 for (p
= s
->text
;; p
= p
->next
) {
1262 enum LISTTYPE listtype
;
1263 struct stackelement
*se
;
1266 * Preliminary switch to figure out what
1267 * sort of list we expect to be inside at
1270 * Since p may still be NULL at this point,
1271 * I invent a harmless paragraph type for
1274 switch (p ? p
->type
: para_Normal
) {
1277 case para_Copyright
:
1278 case para_BiblioCited
:
1280 case para_QuotePush
:
1284 case para_UnnumberedChapter
:
1295 case para_NumberedList
:
1299 case para_DescribedThing
:
1300 case para_Description
:
1304 case para_LcontPush
:
1305 se
= snew(struct stackelement
);
1306 se
->next
= stackhead
;
1307 se
->listtype
= NOLIST
;
1308 se
->itemtype
= NOITEM
;
1312 default: /* some totally non-printing para */
1319 * Terminate the most recent list item, if
1320 * any. (We left this until after
1321 * processing LcontPush, since in that case
1322 * the list item won't want to be
1323 * terminated until after the corresponding
1326 if (stackhead
->itemtype
!= NOITEM
) {
1327 element_close(&ho
, itemname(stackhead
->itemtype
));
1330 stackhead
->itemtype
= NOITEM
;
1333 * Terminate the current list, if it's not
1334 * the one we want to be in.
1336 if (listtype
!= stackhead
->listtype
&&
1337 stackhead
->listtype
!= NOLIST
) {
1338 element_close(&ho
, listname(stackhead
->listtype
));
1343 * Leave the loop if our time has come.
1345 if (!p
|| (is_heading_type(p
->type
) &&
1346 p
->type
!= para_Title
))
1347 break; /* end of section text */
1350 * Start a fresh list if necessary.
1352 if (listtype
!= stackhead
->listtype
&&
1354 element_open(&ho
, listname(listtype
));
1356 stackhead
->listtype
= listtype
;
1360 element_empty(&ho
, "hr");
1363 html_codepara(&ho
, p
->words
);
1366 case para_Copyright
:
1367 element_open(&ho
, "p");
1369 html_words(&ho
, p
->words
, ALL
,
1370 f
, keywords
, &conf
);
1372 element_close(&ho
, "p");
1374 case para_BiblioCited
:
1375 element_open(&ho
, "p");
1376 if (p
->private_data
) {
1377 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1379 for (i
=0; i
< conf
.ntfragments
; i
++)
1380 if (s
->fragments
[i
])
1381 html_fragment(&ho
, s
->fragments
[i
]);
1384 html_words(&ho
, p
->kwtext
, ALL
,
1385 f
, keywords
, &conf
);
1386 html_text(&ho
, L
" ");
1387 html_words(&ho
, p
->words
, ALL
,
1388 f
, keywords
, &conf
);
1390 element_close(&ho
, "p");
1393 case para_NumberedList
:
1394 element_open(&ho
, "li");
1395 if (p
->private_data
) {
1396 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1398 for (i
=0; i
< conf
.ntfragments
; i
++)
1399 if (s
->fragments
[i
])
1400 html_fragment(&ho
, s
->fragments
[i
]);
1403 stackhead
->itemtype
= LI
;
1404 html_words(&ho
, p
->words
, ALL
,
1405 f
, keywords
, &conf
);
1407 case para_DescribedThing
:
1408 element_open(&ho
, "dt");
1410 stackhead
->itemtype
= DT
;
1411 html_words(&ho
, p
->words
, ALL
,
1412 f
, keywords
, &conf
);
1414 case para_Description
:
1415 element_open(&ho
, "dd");
1417 stackhead
->itemtype
= DD
;
1418 html_words(&ho
, p
->words
, ALL
,
1419 f
, keywords
, &conf
);
1422 case para_QuotePush
:
1423 element_open(&ho
, "blockquote");
1426 element_close(&ho
, "blockquote");
1431 stackhead
= stackhead
->next
;
1438 assert(stackhead
&& !stackhead
->next
);
1442 if (s
->type
== INDEX
) {
1447 * This section is the index. I'll just
1448 * render it as a single paragraph, with a
1449 * colon between the index term and the
1450 * references, and <br> in between each
1453 element_open(&ho
, "p");
1455 for (i
= 0; (entry
=
1456 index234(idx
->entries
, i
)) != NULL
; i
++) {
1457 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1461 element_empty(&ho
, "br");
1464 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1465 f
, keywords
, &conf
);
1467 html_text(&ho
, conf
.index_main_sep
);
1469 for (j
= 0; j
< hi
->nrefs
; j
++) {
1471 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1472 paragraph
*p
= hr
->section
->title
;
1475 html_text(&ho
, conf
.index_multi_sep
);
1477 html_href(&ho
, f
, hr
->section
->file
,
1479 hr
->referenced
= TRUE
;
1481 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1482 f
, keywords
, &conf
);
1483 else if (p
&& p
->words
)
1484 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1485 f
, keywords
, &conf
);
1488 * If there is no title at all,
1489 * this must be because our
1490 * target section is the
1491 * preamble section and there
1492 * is no title. So we use the
1495 html_text(&ho
, conf
.preamble_text
);
1497 element_close(&ho
, "a");
1500 element_close(&ho
, "p");
1505 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1512 int done_version_ids
= FALSE
;
1514 if (conf
.address_section
)
1515 element_empty(&ho
, "hr");
1518 html_raw(&ho
, conf
.body_end
);
1520 if (conf
.address_section
) {
1521 int started
= FALSE
;
1522 if (conf
.htmlver
== ISO_HTML
) {
1524 * The ISO-HTML validator complains if
1525 * there isn't a <div> tag surrounding the
1526 * <address> tag. I'm uncertain of why this
1527 * should be - there appears to be no
1528 * mention of this in the ISO-HTML spec,
1529 * suggesting that it doesn't represent a
1530 * change from HTML 4, but nonetheless the
1531 * HTML 4 validator doesn't seem to mind.
1533 element_open(&ho
, "div");
1535 element_open(&ho
, "address");
1536 if (conf
.addr_start
) {
1537 html_raw(&ho
, conf
.addr_start
);
1541 if (conf
.visible_version_id
) {
1542 for (p
= sourceform
; p
; p
= p
->next
)
1543 if (p
->type
== para_VersionID
) {
1545 element_empty(&ho
, "br");
1547 html_text(&ho
, conf
.pre_versionid
);
1548 html_words(&ho
, p
->words
, NOTHING
,
1549 f
, keywords
, &conf
);
1550 html_text(&ho
, conf
.post_versionid
);
1553 done_version_ids
= TRUE
;
1555 if (conf
.addr_end
) {
1557 element_empty(&ho
, "br");
1558 html_raw(&ho
, conf
.addr_end
);
1560 element_close(&ho
, "address");
1561 if (conf
.htmlver
== ISO_HTML
)
1562 element_close(&ho
, "div");
1565 if (!done_version_ids
) {
1567 * If the user didn't want the version IDs
1568 * visible, I think we still have a duty to put
1569 * them in an HTML comment.
1571 int started
= FALSE
;
1572 for (p
= sourceform
; p
; p
= p
->next
)
1573 if (p
->type
== para_VersionID
) {
1575 html_raw(&ho
, "<!-- version IDs:\n");
1578 html_words(&ho
, p
->words
, NOTHING
,
1579 f
, keywords
, &conf
);
1583 html_raw(&ho
, "-->\n");
1587 element_close(&ho
, "body");
1589 element_close(&ho
, "html");
1596 * Before we start outputting the HTML Help files, check
1597 * whether there's even going to _be_ an index file: we omit it
1598 * if the index contains nothing.
1600 hhk_filename
= conf
.hhk_filename
;
1606 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1607 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1609 if (hi
->nrefs
> 0) {
1610 ok
= TRUE
; /* found an index entry */
1616 hhk_filename
= NULL
;
1620 * Output the MS HTML Help supporting files, if requested.
1622 * A good unofficial reference for these is <http://chmspec.nongnu.org/>.
1624 if (conf
.hhp_filename
) {
1628 ho
.charset
= CS_CP1252
; /* as far as I know, HHP files are */
1629 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1630 ho
.cstate
= charset_init_state
;
1631 ho
.ver
= HTML_4
; /* *shrug* */
1632 ho
.state
= HO_NEUTRAL
;
1633 ho
.contents_level
= 0;
1634 ho
.hackflags
= HO_HACK_QUOTENOTHING
;
1636 ho
.fp
= fopen(conf
.hhp_filename
, "w");
1638 error(err_cantopenw
, conf
.hhp_filename
);
1642 /* Binary TOC required for Next/Previous nav to work */
1644 "Compatibility=1.1 or later\n"
1645 "Compiled file=%s\n"
1646 "Default Window=main\n"
1647 "Default topic=%s\n"
1648 "Display compile progress=Yes\n"
1649 "Full-text search=Yes\n"
1650 "Title=", conf
.chm_filename
, files
.head
->filename
);
1653 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1654 NULL
, keywords
, &conf
);
1656 fprintf(ho
.fp
, "\n");
1659 * These two entries don't seem to be remotely necessary
1660 * for a successful run of the help _compiler_, but
1661 * omitting them causes the GUI Help Workshop to behave
1662 * rather strangely if you try to load the help project
1663 * into that and edit it.
1665 if (conf
.hhc_filename
)
1666 fprintf(ho
.fp
, "Contents file=%s\n", conf
.hhc_filename
);
1668 fprintf(ho
.fp
, "Index file=%s\n", hhk_filename
);
1670 fprintf(ho
.fp
, "\n[WINDOWS]\nmain=\"");
1672 ho
.hackflags
|= HO_HACK_OMITQUOTES
;
1674 html_words(&ho
, topsect
->title
->words
, NOTHING
,
1675 NULL
, keywords
, &conf
);
1677 fprintf(ho
.fp
, "\",\"%s\",\"%s\",\"%s\",,,,,,"
1678 /* This first magic number is fsWinProperties, controlling
1679 * Navigation Pane options and the like.
1680 * Constants HHWIN_PROP_* in htmlhelp.h. */
1682 /* This second number is fsToolBarFlags, mainly controlling
1683 * toolbar buttons. Constants HHWIN_BUTTON_*.
1684 * NOTE: there are two pairs of bits for Next/Previous
1685 * buttons: 7/8 (which do nothing useful), and 21/22
1686 * (which work). (Neither of these are exposed in the HHW
1687 * UI, but they work fine in HH.) We use the latter. */
1688 "0x60304e,,,,,,,,0\n",
1689 conf
.hhc_filename ? conf
.hhc_filename
: "",
1690 hhk_filename ? hhk_filename
: "",
1691 files
.head
->filename
);
1694 * The [FILES] section is also not necessary for
1695 * compilation (hhc appears to build up a list of needed
1696 * files just by following links from the given starting
1697 * points), but useful for loading the project into HHW.
1699 fprintf(ho
.fp
, "\n[FILES]\n");
1700 for (f
= files
.head
; f
; f
= f
->next
)
1701 fprintf(ho
.fp
, "%s\n", f
->filename
);
1705 if (conf
.hhc_filename
) {
1711 ho
.fp
= fopen(conf
.hhc_filename
, "w");
1713 error(err_cantopenw
, conf
.hhc_filename
);
1715 ho
.charset
= CS_CP1252
; /* as far as I know, HHC files are */
1716 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1717 ho
.cstate
= charset_init_state
;
1718 ho
.ver
= HTML_4
; /* *shrug* */
1719 ho
.state
= HO_NEUTRAL
;
1720 ho
.contents_level
= 0;
1721 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1724 * Magic DOCTYPE which seems to work for .HHC files. I'm
1725 * wary of trying to change it!
1727 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1729 "<META HTTP-EQUIV=\"Content-Type\" "
1730 "CONTENT=\"text/html; charset=%s\">\n"
1731 "</HEAD><BODY><UL>\n",
1732 charset_to_mimeenc(conf
.output_charset
));
1734 for (f
= files
.head
; f
; f
= f
->next
) {
1736 * For each HTML file, write out a contents entry.
1738 int depth
, leaf
= TRUE
;
1741 * Determine the depth of this file in the contents
1744 * If the file contains no sections, it is assumed to
1749 for (a
= f
->first
->parent
; a
&& a
->type
!= TOP
; a
= a
->parent
)
1753 * Determine if this file is a leaf file, by
1754 * trawling the section list to see if there's any
1755 * section with an ancestor in this file but which
1756 * is not itself in this file.
1758 * Special case: for contents purposes, the TOP
1759 * file is not considered to be the parent of the
1760 * chapter files, so it's always a leaf.
1762 * A file with no sections in it is also a leaf.
1764 if (f
->first
&& f
->first
->type
!= TOP
) {
1765 for (s
= f
->first
; s
; s
= s
->next
) {
1768 if (leaf
&& s
->file
!= f
) {
1769 for (a
= s
; a
; a
= a
->parent
)
1779 * Now write out our contents entry.
1781 while (currdepth
< depth
) {
1782 fprintf(ho
.fp
, "<UL>\n");
1785 while (currdepth
> depth
) {
1786 fprintf(ho
.fp
, "</UL>\n");
1789 /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */
1790 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">"
1791 "<PARAM NAME=\"Name\" VALUE=\"");
1793 if (f
->first
->title
)
1794 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
1795 NULL
, keywords
, &conf
);
1796 else if (f
->first
->type
== INDEX
)
1797 html_text(&ho
, conf
.index_text
);
1798 fprintf(ho
.fp
, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">"
1799 "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n",
1800 f
->filename
, leaf ?
11 : 1);
1803 while (currdepth
> 0) {
1804 fprintf(ho
.fp
, "</UL>\n");
1808 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1819 * First make a pass over all HTML files and set their
1820 * `temp' fields to zero, because we're about to use them.
1822 for (f
= files
.head
; f
; f
= f
->next
)
1825 ho
.fp
= fopen(hhk_filename
, "w");
1827 error(err_cantopenw
, hhk_filename
);
1829 ho
.charset
= CS_CP1252
; /* as far as I know, HHK files are */
1830 ho
.restrict_charset
= CS_CP1252
; /* hardwired to this charset */
1831 ho
.cstate
= charset_init_state
;
1832 ho
.ver
= HTML_4
; /* *shrug* */
1833 ho
.state
= HO_NEUTRAL
;
1834 ho
.contents_level
= 0;
1835 ho
.hackflags
= HO_HACK_QUOTEQUOTES
;
1838 * Magic DOCTYPE which seems to work for .HHK files. I'm
1839 * wary of trying to change it!
1841 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n"
1843 "<META HTTP-EQUIV=\"Content-Type\" "
1844 "CONTENT=\"text/html; charset=%s\">\n"
1845 "</HEAD><BODY><UL>\n",
1846 charset_to_mimeenc(conf
.output_charset
));
1849 * Go through the index terms and output each one.
1851 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1852 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1855 if (hi
->nrefs
> 0) {
1856 fprintf(ho
.fp
, "<LI><OBJECT TYPE=\"text/sitemap\">\n"
1857 "<PARAM NAME=\"Name\" VALUE=\"");
1859 html_words(&ho
, entry
->text
, NOTHING
,
1860 NULL
, keywords
, &conf
);
1861 fprintf(ho
.fp
, "\">\n");
1863 for (j
= 0; j
< hi
->nrefs
; j
++) {
1865 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1868 * Use the temp field to ensure we don't
1869 * reference the same file more than once.
1871 if (!hr
->section
->file
->temp
) {
1872 fprintf(ho
.fp
, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n",
1873 hr
->section
->file
->filename
);
1874 hr
->section
->file
->temp
= 1;
1877 hr
->referenced
= TRUE
;
1880 fprintf(ho
.fp
, "</OBJECT>\n");
1883 * Now go through those files and re-clear the temp
1884 * fields ready for the _next_ index term.
1886 for (j
= 0; j
< hi
->nrefs
; j
++) {
1888 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1889 hr
->section
->file
->temp
= 0;
1894 fprintf(ho
.fp
, "</UL></BODY></HTML>\n");
1899 * Go through and check that no index fragments were referenced
1900 * without being generated, or indeed vice versa.
1902 * (When I actually get round to freeing everything, this can
1903 * probably be the freeing loop as well.)
1905 for (p
= sourceform
; p
; p
= p
->next
) {
1907 for (w
= p
->words
; w
; w
= w
->next
)
1908 if (w
->type
== word_IndexRef
) {
1909 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1911 assert(!hr
->referenced
== !hr
->generated
);
1916 * Free all the working data.
1920 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1922 * frag->fragment is dynamically allocated, but will be
1923 * freed when we process the htmlsect structure which
1924 * it is attached to.
1928 freetree234(files
.frags
);
1931 * The strings in files.files are all owned by their containing
1932 * htmlfile structures, so there's no need to free them here.
1934 freetree234(files
.files
);
1936 htmlsect
*sect
, *tmp
;
1941 for (i
=0; i
< conf
.ntfragments
; i
++)
1942 sfree(sect
->fragments
[i
]);
1943 sfree(sect
->fragments
);
1947 sect
= nonsects
.head
;
1951 for (i
=0; i
< conf
.ntfragments
; i
++)
1952 sfree(sect
->fragments
[i
]);
1953 sfree(sect
->fragments
);
1959 htmlfile
*file
, *tmp
;
1963 sfree(file
->filename
);
1971 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1972 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1979 for (p
= sourceform
; p
; p
= p
->next
)
1980 for (w
= p
->words
; w
; w
= w
->next
)
1981 if (w
->type
== word_IndexRef
) {
1982 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1984 sfree(hr
->fragment
);
1989 sfree(conf
.single_filename
);
1990 sfree(conf
.contents_filename
);
1991 sfree(conf
.index_filename
);
1992 sfree(conf
.template_filename
);
1993 while (conf
.ntfragments
--)
1994 sfree(conf
.template_fragments
[conf
.ntfragments
]);
1995 sfree(conf
.template_fragments
);
1998 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1999 htmlsect
*sect
, int depth
)
2005 * `depth' is derived from the heading_depth() macro at the top
2006 * of this file, which counts title as -1, chapter as 0,
2007 * heading as 1 and subsection as 2. However, the semantics of
2008 * cfg->leaf_level are defined to count chapter as 1, heading
2009 * as 2 etc. So first I increment depth :-(
2013 if (cfg
->leaf_level
== 0) {
2015 * leaf_level==0 is a special case, in which everything is
2016 * put into a single file.
2019 files
->single
= html_new_file(files
, cfg
->single_filename
);
2021 file
= files
->single
;
2024 * If the depth of this section is at or above leaf_level,
2025 * we invent a fresh file and put this section at its head.
2026 * Otherwise, we put it in the same file as its parent
2029 * Another special value of cfg->leaf_level is -1, which
2030 * means infinity (i.e. it's considered to always be
2031 * greater than depth).
2033 if (cfg
->leaf_level
> 0 && ldepth
> cfg
->leaf_level
) {
2035 * We know that sect->parent cannot be NULL. The only
2036 * circumstance in which it can be is if sect is at
2037 * chapter or appendix level, i.e. ldepth==1; and if
2038 * that's the case, then we cannot have entered this
2039 * branch unless cfg->leaf_level==0, in which case we
2040 * would be in the single-file case above and not here
2043 assert(sect
->parent
);
2045 file
= sect
->parent
->file
;
2047 if (sect
->type
== TOP
) {
2048 file
= html_new_file(files
, cfg
->contents_filename
);
2049 } else if (sect
->type
== INDEX
) {
2050 file
= html_new_file(files
, cfg
->index_filename
);
2054 assert(ldepth
> 0 && sect
->title
);
2055 title
= html_format(sect
->title
, cfg
->template_filename
);
2056 file
= html_new_file(files
, title
);
2064 if (file
->min_heading_depth
> depth
) {
2066 * This heading is at a higher level than any heading we
2067 * have so far placed in this file; so we set the `first'
2070 file
->min_heading_depth
= depth
;
2074 if (file
->min_heading_depth
== depth
)
2078 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
2080 htmlfile
*ret
= snew(htmlfile
);
2084 list
->tail
->next
= ret
;
2089 ret
->filename
= html_sanitise_filename(list
, dupstr(filename
));
2090 add234(list
->files
, ret
->filename
);
2091 ret
->last_fragment_number
= 0;
2092 ret
->min_heading_depth
= INT_MAX
;
2093 ret
->first
= ret
->last
= NULL
;
2098 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
2101 htmlsect
*ret
= snew(htmlsect
);
2105 list
->tail
->next
= ret
;
2115 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
2118 for (i
=0; i
< cfg
->ntfragments
; i
++)
2119 ret
->fragments
[i
] = NULL
;
2125 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
2126 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
2132 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
2133 case word_HyperLink
:
2134 if (flags
& LINKS
) {
2135 element_open(ho
, "a");
2136 c
= utoa_dup(w
->text
, CS_ASCII
);
2137 element_attr(ho
, "href", c
);
2141 case word_UpperXref
:
2142 case word_LowerXref
:
2143 if (flags
& LINKS
) {
2144 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
2150 s
= (htmlsect
*)p
->private_data
;
2154 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
2160 element_close(ho
, "a");
2163 if (flags
& INDEXENTS
) {
2164 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
2165 html_fragment(ho
, hr
->fragment
);
2166 hr
->generated
= TRUE
;
2173 case word_WhiteSpace
:
2174 case word_EmphSpace
:
2175 case word_CodeSpace
:
2176 case word_WkCodeSpace
:
2178 case word_EmphQuote
:
2179 case word_CodeQuote
:
2180 case word_WkCodeQuote
:
2181 style
= towordstyle(w
->type
);
2182 type
= removeattr(w
->type
);
2183 if (style
== word_Emph
&&
2184 (attraux(w
->aux
) == attr_First
||
2185 attraux(w
->aux
) == attr_Only
) &&
2187 element_open(ho
, "em");
2188 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2189 (attraux(w
->aux
) == attr_First
||
2190 attraux(w
->aux
) == attr_Only
) &&
2192 element_open(ho
, "code");
2194 if (type
== word_WhiteSpace
)
2195 html_text(ho
, L
" ");
2196 else if (type
== word_Quote
) {
2197 if (quoteaux(w
->aux
) == quote_Open
)
2198 html_text(ho
, cfg
->lquote
);
2200 html_text(ho
, cfg
->rquote
);
2202 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
2203 html_text_nbsp(ho
, w
->text
);
2205 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
2208 if (style
== word_Emph
&&
2209 (attraux(w
->aux
) == attr_Last
||
2210 attraux(w
->aux
) == attr_Only
) &&
2212 element_close(ho
, "em");
2213 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
2214 (attraux(w
->aux
) == attr_Last
||
2215 attraux(w
->aux
) == attr_Only
) &&
2217 element_close(ho
, "code");
2223 static void html_codepara(htmloutput
*ho
, word
*words
)
2225 element_open(ho
, "pre");
2226 element_open(ho
, "code");
2227 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
2232 if (words
->next
&& words
->next
->type
== word_Emph
) {
2233 e
= words
->next
->text
;
2234 words
= words
->next
;
2238 while (e
&& *e
&& *t
) {
2242 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
2250 element_open(ho
, open_tag
);
2252 html_text_limit(ho
, t
, n
);
2255 element_close(ho
, open_tag
);
2263 element_close(ho
, "code");
2264 element_close(ho
, "pre");
2267 static void html_charset_cleanup(htmloutput
*ho
)
2272 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
2273 ho
->charset
, &ho
->cstate
, NULL
);
2274 if (ho
->fp
&& bytes
> 0)
2275 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2278 static void return_mostly_to_neutral(htmloutput
*ho
)
2281 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
2282 fprintf(ho
->fp
, " />");
2283 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
2284 fprintf(ho
->fp
, ">");
2288 ho
->state
= HO_NEUTRAL
;
2291 static void return_to_neutral(htmloutput
*ho
)
2293 if (ho
->state
== HO_IN_TEXT
) {
2294 html_charset_cleanup(ho
);
2297 return_mostly_to_neutral(ho
);
2300 static void element_open(htmloutput
*ho
, char const *name
)
2302 return_to_neutral(ho
);
2304 fprintf(ho
->fp
, "<%s", name
);
2305 ho
->state
= HO_IN_TAG
;
2308 static void element_close(htmloutput
*ho
, char const *name
)
2310 return_to_neutral(ho
);
2312 fprintf(ho
->fp
, "</%s>", name
);
2313 ho
->state
= HO_NEUTRAL
;
2316 static void element_empty(htmloutput
*ho
, char const *name
)
2318 return_to_neutral(ho
);
2320 fprintf(ho
->fp
, "<%s", name
);
2321 ho
->state
= HO_IN_EMPTY_TAG
;
2324 static void html_nl(htmloutput
*ho
)
2326 return_to_neutral(ho
);
2328 fputc('\n', ho
->fp
);
2331 static void html_raw(htmloutput
*ho
, char *text
)
2333 return_to_neutral(ho
);
2335 fputs(text
, ho
->fp
);
2338 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
2340 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2343 fputs(text
, ho
->fp
);
2347 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
2349 html_charset_cleanup(ho
);
2350 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
2352 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
2355 static void element_attr_w(htmloutput
*ho
, char const *name
,
2356 wchar_t const *value
)
2358 html_charset_cleanup(ho
);
2360 fprintf(ho
->fp
, " %s=\"", name
);
2361 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
2362 html_charset_cleanup(ho
);
2367 static void html_text(htmloutput
*ho
, wchar_t const *text
)
2369 return_mostly_to_neutral(ho
);
2370 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
2373 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
2375 return_mostly_to_neutral(ho
);
2376 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
2379 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
2381 return_mostly_to_neutral(ho
);
2382 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
2385 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
2386 int maxlen
, int quote_quotes
, int nbsp
)
2388 int textlen
= ustrlen(text
);
2392 if (ho
->hackflags
& (HO_HACK_QUOTEQUOTES
| HO_HACK_OMITQUOTES
))
2393 quote_quotes
= TRUE
; /* override the input value */
2395 if (maxlen
> 0 && textlen
> maxlen
)
2397 if (ho
->hacklimit
>= 0) {
2398 if (textlen
> ho
->hacklimit
)
2399 textlen
= ho
->hacklimit
;
2400 ho
->hacklimit
-= textlen
;
2403 while (textlen
> 0) {
2404 /* Scan ahead for characters we really can't display in HTML. */
2405 int lenbefore
, lenafter
;
2406 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
2407 if (text
[lenbefore
] == L
'<' ||
2408 text
[lenbefore
] == L
'>' ||
2409 text
[lenbefore
] == L
'&' ||
2410 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
2411 (text
[lenbefore
] == L
' ' && nbsp
))
2413 lenafter
= lenbefore
;
2414 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
2415 ho
->charset
, &ho
->cstate
, &err
);
2416 textlen
-= (lenbefore
- lenafter
);
2417 if (bytes
> 0 && ho
->fp
)
2418 fwrite(outbuf
, 1, bytes
, ho
->fp
);
2421 * We have encountered a character that cannot be
2422 * displayed in the selected output charset. Therefore,
2423 * we use an HTML numeric entity reference.
2425 assert(textlen
> 0);
2427 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
2429 } else if (lenafter
== 0 && textlen
> 0) {
2431 * We have encountered a character which is special to
2435 if (*text
== L
'"' && (ho
->hackflags
& HO_HACK_OMITQUOTES
)) {
2436 fputc('\'', ho
->fp
);
2437 } else if (ho
->hackflags
& HO_HACK_QUOTENOTHING
) {
2438 fputc(*text
, ho
->fp
);
2441 fprintf(ho
->fp
, "<");
2442 else if (*text
== L
'>')
2443 fprintf(ho
->fp
, ">");
2444 else if (*text
== L
'&')
2445 fprintf(ho
->fp
, "&");
2446 else if (*text
== L
'"')
2447 fprintf(ho
->fp
, """);
2448 else if (*text
== L
' ') {
2450 fprintf(ho
->fp
, " ");
2452 assert(!"Can't happen");
2460 static void cleanup(htmloutput
*ho
)
2462 return_to_neutral(ho
);
2467 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
2468 htmlfile
*targetfile
, char *targetfrag
)
2470 rdstringc rs
= { 0, 0, NULL
};
2473 if (targetfile
!= thisfile
)
2474 rdaddsc(&rs
, targetfile
->filename
);
2477 rdaddsc(&rs
, targetfrag
);
2481 element_open(ho
, "a");
2482 element_attr(ho
, "href", url
);
2486 static void html_fragment(htmloutput
*ho
, char const *fragment
)
2488 element_open(ho
, "a");
2489 element_attr(ho
, "name", fragment
);
2490 if (is_xhtml(ho
->ver
))
2491 element_attr(ho
, "id", fragment
);
2492 element_close(ho
, "a");
2495 static char *html_format(paragraph
*p
, char *template_string
)
2499 wchar_t *ws
, wsbuf
[2];
2500 rdstringc rs
= { 0, 0, NULL
};
2502 t
= template_string
;
2504 if (*t
== '%' && t
[1]) {
2518 if (p
->kwtext
&& fmt
== 'n')
2520 else if (p
->kwtext2
&& fmt
== 'b') {
2522 * HTML fragment names must start with a letter, so
2523 * simply `1.2.3' is not adequate. In this case I'm
2524 * going to cheat slightly by prepending the first
2525 * character of the first word of kwtext, so that
2526 * we get `C1' for chapter 1, `S2.3' for section
2529 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2532 wsbuf
[0] = p
->kwtext
->text
[0];
2535 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2538 /* %N comes here; also failure cases of other fmts */
2542 c
= utoa_dup(ws
, CS_ASCII
);
2548 if (removeattr(w
->type
) == word_Normal
) {
2549 c
= utoa_dup(w
->text
, CS_ASCII
);
2560 return rdtrimc(&rs
);
2563 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2567 * The HTML 4 spec's strictest definition of fragment names (<a
2568 * name> and "id" attributes) says that they `must begin with a
2569 * letter and may be followed by any number of letters, digits,
2570 * hyphens, underscores, colons, and periods'.
2572 * So here we unceremoniously rip out any characters not
2573 * conforming to this limitation.
2575 char *p
= text
, *q
= text
;
2577 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2579 if ((*q
++ = *p
++) != '\0') {
2581 if ((*p
>='A' && *p
<='Z') ||
2582 (*p
>='a' && *p
<='z') ||
2583 (*p
>='0' && *p
<='9') ||
2584 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2592 /* If there's nothing left, make something valid up */
2594 static const char anonfrag
[] = "anon";
2595 text
= sresize(text
, lenof(anonfrag
), char);
2596 strcpy(text
, anonfrag
);
2600 * Now we check for clashes with other fragment names, and
2601 * adjust this one if necessary by appending a hyphen followed
2605 htmlfragment
*frag
= snew(htmlfragment
);
2606 int len
= 0; /* >0 indicates we have resized */
2610 frag
->fragment
= text
;
2612 while (add234(files
->frags
, frag
) != frag
) {
2615 frag
->fragment
= text
= sresize(text
, len
+20, char);
2618 sprintf(text
+ len
, "-%d", ++suffix
);
2625 static char *html_sanitise_filename(htmlfilelist
*files
, char *text
)
2628 * Unceremoniously rip out any character that might cause
2629 * difficulty in some filesystem or another, or be otherwise
2632 * That doesn't leave much punctuation. I permit alphanumerics
2635 char *p
= text
, *q
= text
;
2638 if ((*p
>='A' && *p
<='Z') ||
2639 (*p
>='a' && *p
<='z') ||
2640 (*p
>='0' && *p
<='9') ||
2641 *p
=='-' || *p
=='_' || *p
=='+' || *p
=='.' || *p
=='=')
2647 /* If there's nothing left, make something valid up */
2649 static const char anonfrag
[] = "anon.html";
2650 text
= sresize(text
, lenof(anonfrag
), char);
2651 strcpy(text
, anonfrag
);
2655 * Now we check for clashes with other filenames, and adjust
2656 * this one if necessary by appending a hyphen followed by a
2657 * number just before the file extension (if any).
2665 while (find234(files
->files
, text
, NULL
)) {
2669 text
= snewn(len
+20, char);
2671 for (extpos
= len
; extpos
> 0 && p
[extpos
-1] != '.'; extpos
--);
2678 sprintf(text
, "%.*s-%d%s", extpos
, p
, ++suffix
, p
+extpos
);
2688 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2689 htmlfile
*thisfile
, keywordlist
*keywords
,
2692 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2693 element_close(ho
, "li");
2697 while (ho
->contents_level
> depth
) {
2698 element_close(ho
, "ul");
2699 ho
->contents_level
--;
2700 if (ho
->contents_level
> 0) {
2701 element_close(ho
, "li");
2706 while (ho
->contents_level
< depth
) {
2708 element_open(ho
, "ul");
2710 ho
->contents_level
++;
2716 element_open(ho
, "li");
2717 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2718 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2719 element_close(ho
, "a");
2720 /* <li> will be closed by a later invocation */
2723 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2724 keywordlist
*keywords
, htmlconfig
*cfg
,
2730 int depth
= heading_depth(s
->title
);
2734 else if (depth
== 0)
2735 sl
= &cfg
->achapter
;
2736 else if (depth
<= cfg
->nasect
)
2737 sl
= &cfg
->asect
[depth
-1];
2739 sl
= &cfg
->asect
[cfg
->nasect
-1];
2743 else if (sl
->just_numbers
)
2744 number
= s
->title
->kwtext2
;
2746 number
= s
->title
->kwtext
;
2749 html_words(ho
, number
, MARKUP
,
2750 thisfile
, keywords
, cfg
);
2751 html_text(ho
, sl
->number_suffix
);
2754 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2755 thisfile
, keywords
, cfg
);
2757 assert(s
->type
!= NORMAL
);
2759 * If we're printing the full document title for _real_ and
2760 * there isn't one, we don't want to print `Preamble' at
2761 * the top of what ought to just be some text. If we need
2762 * it in any other context such as TOCs, we need to print
2765 if (s
->type
== TOP
&& !real
)
2766 html_text(ho
, cfg
->preamble_text
);
2767 else if (s
->type
== INDEX
)
2768 html_text(ho
, cfg
->index_text
);