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 * - new configurability:
15 * * a few new things explicitly labelled as `FIXME:
16 * configurable' or similar.
18 * * Some means of specifying the distinction between
19 * restrict-charset and output-charset. It seems to me that
20 * `html-charset' is output-charset, and that
21 * restrict-charset usually wants to be either output-charset
22 * or UTF-8 (the latter indicating that any Unicode character
23 * is fair game and it will be specified using &#foo; if it
24 * isn't in output-charset). However, since XHTML defaults to
25 * UTF-8 and it's fiddly to tell it otherwise, it's just
26 * possible that some user may need to set restrict-charset
27 * to their charset of choice while leaving _output_-charset
28 * at UTF-8. Figure out some configuration, and apply it.
30 * - test all HTML flavours and ensure they validate sensibly. Fix
31 * remaining confusion issues such as <?xml?> and obsoleteness
34 * - proper naming of all fragment IDs. The ones for sections are
35 * fine; the ones for numbered list and bibliociteds are utter
36 * crap; the ones for indexes _might_ do but it might be worth
37 * giving some thought to how to do them better.
39 * - nonbreaking spaces.
41 * - free up all the data we have allocated while running this
51 #define is_heading_type(type) ( (type) == para_Title || \
52 (type) == para_Chapter || \
53 (type) == para_Appendix || \
54 (type) == para_UnnumberedChapter || \
55 (type) == para_Heading || \
56 (type) == para_Subsect)
58 #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
59 (p)->type == para_Heading ? 1 : \
60 (p)->type == para_Title ? -1 : 0 )
64 wchar_t *number_suffix
;
69 sectlevel achapter
, *asect
;
70 int *contents_depths
; /* 0=main, 1=chapter, 2=sect etc */
72 int address_section
, visible_version_id
;
73 int leaf_contains_contents
, leaf_smallest_contents
;
74 char *contents_filename
;
76 char *template_filename
;
77 char *single_filename
;
78 char *template_fragment
;
79 char *head_end
, *body_start
, *body_end
, *addr_start
, *addr_end
;
80 char *body_tag
, *nav_attr
;
81 wchar_t *author
, *description
;
82 int restrict_charset
, output_charset
;
85 XHTML_1_0_TRANSITIONAL
, XHTML_1_0_STRICT
87 wchar_t *lquote
, *rquote
;
91 #define contents_depth(conf, level) \
92 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
94 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
96 typedef struct htmlfile htmlfile
;
97 typedef struct htmlsect htmlsect
;
102 int last_fragment_number
;
103 int min_heading_depth
;
104 htmlsect
*first
, *last
; /* first/last highest-level sections */
108 htmlsect
*next
, *parent
;
110 paragraph
*title
, *text
;
111 enum { NORMAL
, TOP
, INDEX
} type
;
117 htmlfile
*head
, *tail
;
118 htmlfile
*single
, *index
;
123 htmlsect
*head
, *tail
;
139 int generated
, referenced
;
144 * This level deals with charset conversion, starting and
145 * ending tags, and writing to the file. It's the lexical
150 charset_state cstate
;
153 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
156 * Stuff beyond here deals with the higher syntactic level: it
157 * tracks how many levels of <ul> are currently open when
158 * producing a contents list, for example.
163 static int html_fragment_compare(void *av
, void *bv
)
165 htmlfragment
*a
= (htmlfragment
*)av
;
166 htmlfragment
*b
= (htmlfragment
*)bv
;
169 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
172 return strcmp(a
->fragment
, b
->fragment
);
175 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
176 htmlsect
*sect
, int depth
);
178 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
179 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
);
181 /* Flags for html_words() flags parameter */
185 #define INDEXENTS 0x04
187 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
188 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
189 static void html_codepara(htmloutput
*ho
, word
*words
);
191 static void element_open(htmloutput
*ho
, char const *name
);
192 static void element_close(htmloutput
*ho
, char const *name
);
193 static void element_empty(htmloutput
*ho
, char const *name
);
194 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
195 static void element_attr_w(htmloutput
*ho
, char const *name
,
196 wchar_t const *value
);
197 static void html_text(htmloutput
*ho
, wchar_t const *str
);
198 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
199 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
200 int maxlen
, int quote_quotes
);
201 static void html_nl(htmloutput
*ho
);
202 static void html_raw(htmloutput
*ho
, char *text
);
203 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
204 static void cleanup(htmloutput
*ho
);
206 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
207 htmlfile
*targetfile
, char *targetfrag
);
209 static char *html_format(paragraph
*p
, char *template_string
);
210 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
213 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
214 htmlfile
*thisfile
, keywordlist
*keywords
,
216 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
217 htmlfile
*thisfile
, keywordlist
*keywords
,
218 htmlconfig
*cfg
, int real
);
220 static htmlconfig
html_configure(paragraph
*source
) {
228 ret
.achapter
.just_numbers
= FALSE
;
229 ret
.achapter
.number_suffix
= L
": ";
231 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
232 ret
.asect
[0].just_numbers
= TRUE
;
233 ret
.asect
[0].number_suffix
= L
" ";
235 ret
.contents_depths
= 0;
236 ret
.visible_version_id
= TRUE
;
237 ret
.address_section
= TRUE
;
238 ret
.leaf_contains_contents
= FALSE
;
239 ret
.leaf_smallest_contents
= 4;
240 ret
.single_filename
= dupstr("Manual.html");
241 ret
.contents_filename
= dupstr("Contents.html");
242 ret
.index_filename
= dupstr("IndexPage.html");
243 ret
.template_filename
= dupstr("%n.html");
244 ret
.template_fragment
= dupstr("%b");
245 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
246 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
247 ret
.author
= ret
.description
= NULL
;
248 ret
.restrict_charset
= CS_ASCII
;
249 ret
.output_charset
= CS_ASCII
;
250 ret
.htmlver
= HTML_4
;
252 * Default quote characters are Unicode matched single quotes,
253 * falling back to ordinary ASCII ".
255 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
256 ret
.rquote
= uadv(ret
.lquote
);
259 * Two-pass configuration so that we can pick up global config
260 * (e.g. `quotes') before having it overridden by specific
261 * config (`html-quotes'), irrespective of the order in which
264 for (p
= source
; p
; p
= p
->next
) {
265 if (p
->type
== para_Config
) {
266 if (!ustricmp(p
->keyword
, L
"quotes")) {
267 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
268 ret
.lquote
= uadv(p
->keyword
);
269 ret
.rquote
= uadv(ret
.lquote
);
275 for (p
= source
; p
; p
= p
->next
) {
276 if (p
->type
== para_Config
) {
277 wchar_t *k
= p
->keyword
;
279 if (!ustrnicmp(k
, L
"xhtml-", 6))
280 k
++; /* treat `xhtml-' and `html-' the same */
282 if (!ustricmp(k
, L
"html-charset")) {
283 char *csname
= utoa_dup(uadv(k
), CS_ASCII
);
284 ret
.restrict_charset
= ret
.output_charset
=
285 charset_from_localenc(csname
);
287 } else if (!ustricmp(k
, L
"html-single-filename")) {
288 sfree(ret
.single_filename
);
289 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
290 } else if (!ustricmp(k
, L
"html-contents-filename")) {
291 sfree(ret
.contents_filename
);
292 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
293 } else if (!ustricmp(k
, L
"html-index-filename")) {
294 sfree(ret
.index_filename
);
295 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
296 } else if (!ustricmp(k
, L
"html-template-filename")) {
297 sfree(ret
.template_filename
);
298 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
299 } else if (!ustricmp(k
, L
"html-template-fragment")) {
300 sfree(ret
.template_fragment
);
301 ret
.template_fragment
= dupstr(adv(p
->origkeyword
));
302 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
303 ret
.achapter
.just_numbers
= utob(uadv(k
));
304 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
305 ret
.achapter
.number_suffix
= uadv(k
);
306 } else if (!ustricmp(k
, L
"html-leaf-level")) {
307 ret
.leaf_level
= utoi(uadv(k
));
308 } else if (!ustricmp(k
, L
"html-section-numeric")) {
309 wchar_t *q
= uadv(k
);
315 if (n
>= ret
.nasect
) {
317 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
318 for (i
= ret
.nasect
; i
<= n
; i
++)
319 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
322 ret
.asect
[n
].just_numbers
= utob(q
);
323 } else if (!ustricmp(k
, L
"html-section-suffix")) {
324 wchar_t *q
= uadv(k
);
330 if (n
>= ret
.nasect
) {
332 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
333 for (i
= ret
.nasect
; i
<= n
; i
++) {
334 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
338 ret
.asect
[n
].number_suffix
= q
;
339 } else if (!ustricmp(k
, L
"html-contents-depth") ||
340 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
342 * Relic of old implementation: this directive used
343 * to be written as \cfg{html-contents-depth-3}{2}
344 * rather than the usual Halibut convention of
345 * \cfg{html-contents-depth}{3}{2}. We therefore
348 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
354 if (n
>= ret
.ncdepths
) {
356 ret
.contents_depths
=
357 sresize(ret
.contents_depths
, n
+1, int);
358 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
359 ret
.contents_depths
[i
] = i
+2;
363 ret
.contents_depths
[n
] = utoi(q
);
364 } else if (!ustricmp(k
, L
"html-head-end")) {
365 ret
.head_end
= adv(p
->origkeyword
);
366 } else if (!ustricmp(k
, L
"html-body-tag")) {
367 ret
.body_tag
= adv(p
->origkeyword
);
368 } else if (!ustricmp(k
, L
"html-body-start")) {
369 ret
.body_start
= adv(p
->origkeyword
);
370 } else if (!ustricmp(k
, L
"html-body-end")) {
371 ret
.body_end
= adv(p
->origkeyword
);
372 } else if (!ustricmp(k
, L
"html-address-start")) {
373 ret
.addr_start
= adv(p
->origkeyword
);
374 } else if (!ustricmp(k
, L
"html-address-end")) {
375 ret
.addr_end
= adv(p
->origkeyword
);
376 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
377 ret
.nav_attr
= adv(p
->origkeyword
);
378 } else if (!ustricmp(k
, L
"html-author")) {
379 ret
.author
= uadv(k
);
380 } else if (!ustricmp(k
, L
"html-description")) {
381 ret
.description
= uadv(k
);
382 } else if (!ustricmp(k
, L
"html-suppress-address")) {
383 ret
.address_section
= !utob(uadv(k
));
384 } else if (!ustricmp(k
, L
"html-versionid")) {
385 ret
.visible_version_id
= utob(uadv(k
));
386 } else if (!ustricmp(k
, L
"html-quotes")) {
387 if (*uadv(k
) && *uadv(uadv(k
))) {
388 ret
.lquote
= uadv(k
);
389 ret
.rquote
= uadv(ret
.lquote
);
391 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
392 ret
.leaf_contains_contents
= utob(uadv(k
));
393 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
394 ret
.leaf_smallest_contents
= utoi(uadv(k
));
400 * Now process fallbacks on quote characters.
402 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
403 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
404 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
405 ret
.lquote
= uadv(ret
.rquote
);
406 ret
.rquote
= uadv(ret
.lquote
);
412 paragraph
*html_config_filename(char *filename
)
415 * If the user passes in a single filename as a parameter to
416 * the `--html' command-line option, then we should assume it
417 * to imply _two_ config directives:
418 * \cfg{html-single-filename}{whatever} and
419 * \cfg{html-leaf-level}{0}; the rationale being that the user
420 * wants their output _in that file_.
424 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
425 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
430 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
431 indexdata
*idx
, void *unused
) {
434 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
};
435 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
439 conf
= html_configure(sourceform
);
442 * We're going to make heavy use of paragraphs' private data
443 * fields in the forthcoming code. Clear them first, so we can
444 * reliably tell whether we have auxiliary data for a
445 * particular paragraph.
447 for (p
= sourceform
; p
; p
= p
->next
)
448 p
->private_data
= NULL
;
450 files
.frags
= newtree234(html_fragment_compare
);
453 * Start by figuring out into which file each piece of the
454 * document should be put. We'll do this by inventing an
455 * `htmlsect' structure and stashing it in the private_data
456 * field of each section paragraph; we also need one additional
457 * htmlsect for the document index, which won't show up in the
458 * source form but needs to be consistently mentioned in
461 * While we're here, we'll also invent the HTML fragment name
465 htmlsect
*topsect
, *sect
;
468 topsect
= html_new_sect(§s
, p
);
470 topsect
->title
= NULL
;
471 topsect
->text
= sourceform
;
472 topsect
->contents_depth
= contents_depth(conf
, 0);
473 html_file_section(&conf
, &files
, topsect
, -1);
474 topsect
->fragment
= NULL
;
476 for (p
= sourceform
; p
; p
= p
->next
)
477 if (is_heading_type(p
->type
)) {
478 d
= heading_depth(p
);
480 if (p
->type
== para_Title
) {
485 sect
= html_new_sect(§s
, p
);
486 sect
->text
= p
->next
;
488 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
491 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
492 assert(sect
->parent
!= NULL
);
494 sect
->parent
= topsect
;
495 p
->private_data
= sect
;
497 html_file_section(&conf
, &files
, sect
, d
);
499 sect
->fragment
= html_format(p
, conf
.template_fragment
);
500 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
505 sect
= html_new_sect(§s
, NULL
);
508 sect
->parent
= topsect
;
509 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
510 sect
->fragment
= dupstr("Index"); /* FIXME: this _can't_ be right */
511 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
513 files
.index
= sect
->file
;
517 * Go through the keyword list and sort out fragment IDs for
518 * all the potentially referenced paragraphs which _aren't_
526 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
527 paragraph
*q
, *p
= kw
->para
;
529 if (!is_heading_type(p
->type
)) {
533 * Find the paragraph's parent htmlsect, to
534 * determine which file it will end up in.
539 * Preamble paragraphs have no parent. So if we
540 * have a non-heading with no parent, it must
541 * be preamble, and therefore its parent
542 * htmlsect must be the preamble one.
545 sects
.head
->type
== TOP
);
548 parent
= (htmlsect
*)q
->private_data
;
551 * Now we can construct an htmlsect for this
552 * paragraph itself, taking care to put it in the
553 * list of non-sections rather than the list of
554 * sections (so that traverses of the `sects' list
555 * won't attempt to add it to the contents or
556 * anything weird like that).
558 sect
= html_new_sect(&nonsects
, p
);
559 sect
->file
= parent
->file
;
560 sect
->parent
= parent
;
561 p
->private_data
= sect
;
564 * FIXME: We need a much better means of naming
565 * these, possibly involving an additional
566 * configuration template. For the moment I'll just
567 * invent something completely stupid.
569 sect
->fragment
= snewn(40, char);
570 sprintf(sect
->fragment
, "frag%p", sect
);
571 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
578 * Now sort out the index. This involves:
580 * - For each index term, we set up an htmlindex structure to
581 * store all the references to that term.
583 * - Then we make a pass over the actual document, finding
584 * every word_IndexRef; for each one, we actually figure out
585 * the HTML filename/fragment pair we will use to reference
586 * it, store that information in the private data field of
587 * the word_IndexRef itself (so we can recreate it when the
588 * time comes to output our HTML), and add a reference to it
589 * to the index term in question.
598 * Set up the htmlindex structures.
601 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
602 htmlindex
*hi
= snew(htmlindex
);
604 hi
->nrefs
= hi
->refsize
= 0;
607 entry
->backend_data
= hi
;
611 * Run over the document inventing fragments. Each fragment
612 * is of the form `i' followed by an integer.
615 for (p
= sourceform
; p
; p
= p
->next
) {
616 if (is_heading_type(p
->type
))
617 lastsect
= (htmlsect
*)p
->private_data
;
619 for (w
= p
->words
; w
; w
= w
->next
)
620 if (w
->type
== word_IndexRef
) {
621 htmlindexref
*hr
= snew(htmlindexref
);
625 hr
->referenced
= hr
->generated
= FALSE
;
626 hr
->section
= lastsect
;
630 lastsect
->file
->last_fragment_number
++);
631 hr
->fragment
= dupstr(buf
);
633 html_sanitise_fragment(&files
, hr
->section
->file
,
636 w
->private_data
= hr
;
638 tag
= index_findtag(idx
, w
->text
);
642 for (i
= 0; i
< tag
->nrefs
; i
++) {
643 indexentry
*entry
= tag
->refs
[i
];
644 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
646 if (hi
->nrefs
>= hi
->refsize
) {
648 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
651 hi
->refs
[hi
->nrefs
++] = w
;
658 * Now we're ready to write out the actual HTML files.
662 * - we open that file and write its header
663 * - we run down the list of sections
664 * - for each section directly contained within that file, we
665 * output the section text
666 * - for each section which is not in the file but which has a
667 * parent that is, we output a contents entry for the
668 * section if appropriate
669 * - finally, we output the file trailer and close the file.
678 for (f
= files
.head
; f
; f
= f
->next
) {
681 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
682 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
683 struct stackelement
{
684 struct stackelement
*next
;
685 enum LISTTYPE listtype
;
686 enum ITEMTYPE itemtype
;
689 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
690 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
692 ho
.fp
= fopen(f
->filename
, "w");
693 ho
.charset
= conf
.output_charset
;
694 ho
.cstate
= charset_init_state
;
695 ho
.ver
= conf
.htmlver
;
696 ho
.state
= HO_NEUTRAL
;
697 ho
.contents_level
= 0;
700 switch (conf
.htmlver
) {
702 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
703 "HTML 3.2 Final//EN\">\n");
706 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
707 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
710 case XHTML_1_0_TRANSITIONAL
:
711 /* FIXME: <?xml?> to specify character encoding.
712 * This breaks HTML backwards compat, so perhaps avoid, or
713 * perhaps only emit when not using the default UTF-8? */
714 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
715 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
716 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
718 case XHTML_1_0_STRICT
:
719 /* FIXME: <?xml?> to specify character encoding. */
720 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
721 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
722 "DTD/xhtml1-strict.dtd\">\n");
726 element_open(&ho
, "html");
727 if (is_xhtml(conf
.htmlver
)) {
728 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
732 element_open(&ho
, "head");
735 element_empty(&ho
, "meta");
736 element_attr(&ho
, "http-equiv", "content-type");
739 sprintf(buf
, "text/html; charset=%.150s",
740 charset_to_mimeenc(conf
.output_charset
));
741 element_attr(&ho
, "content", buf
);
746 element_empty(&ho
, "meta");
747 element_attr(&ho
, "name", "author");
748 element_attr_w(&ho
, "content", conf
.author
);
752 if (conf
.description
) {
753 element_empty(&ho
, "meta");
754 element_attr(&ho
, "name", "description");
755 element_attr_w(&ho
, "content", conf
.description
);
759 element_open(&ho
, "title");
760 if (f
->first
&& f
->first
->title
) {
761 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
765 if (f
->last
!= f
->first
&& f
->last
->title
) {
766 html_text(&ho
, L
" - "); /* FIXME: configurable? */
767 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
771 element_close(&ho
, "title");
775 html_raw(&ho
, conf
.head_end
);
777 element_close(&ho
, "head");
780 /* FIXME: need to be able to specify replacement for this */
782 html_raw(&ho
, conf
.body_tag
);
784 element_open(&ho
, "body");
788 html_raw(&ho
, conf
.body_start
);
791 * Write out a nav bar. Special case: we don't do this
792 * if there is only one file.
794 if (files
.head
!= files
.tail
) {
795 element_open(&ho
, "p");
797 html_raw_as_attr(&ho
, conf
.nav_attr
);
800 element_open(&ho
, "a");
801 element_attr(&ho
, "href", prevf
->filename
);
803 html_text(&ho
, L
"Previous");/* FIXME: conf? */
805 element_close(&ho
, "a");
807 html_text(&ho
, L
" | "); /* FIXME: conf? */
809 if (f
!= files
.head
) {
810 element_open(&ho
, "a");
811 element_attr(&ho
, "href", files
.head
->filename
);
813 html_text(&ho
, L
"Contents");/* FIXME: conf? */
815 element_close(&ho
, "a");
817 html_text(&ho
, L
" | "); /* FIXME: conf? */
819 if (f
!= files
.index
) {
820 element_open(&ho
, "a");
821 element_attr(&ho
, "href", files
.index
->filename
);
823 html_text(&ho
, L
"Index");/* FIXME: conf? */
824 if (f
!= files
.index
)
825 element_close(&ho
, "a");
827 html_text(&ho
, L
" | "); /* FIXME: conf? */
830 element_open(&ho
, "a");
831 element_attr(&ho
, "href", f
->next
->filename
);
833 html_text(&ho
, L
"Next"); /* FIXME: conf? */
835 element_close(&ho
, "a");
837 element_close(&ho
, "p");
843 * Write out a prefix TOC for the file.
845 * We start by going through the section list and
846 * collecting the sections which need to be added to
847 * the contents. On the way, we also test to see if
848 * this file is a leaf file (defined as one which
849 * contains all descendants of any section it
850 * contains), because this will play a part in our
851 * decision on whether or not to _output_ the TOC.
853 * Special case: we absolutely do not do this if we're
854 * in single-file mode.
856 if (files
.head
!= files
.tail
) {
857 int ntoc
= 0, tocsize
= 0;
858 htmlsect
**toc
= NULL
;
861 for (s
= sects
.head
; s
; s
= s
->next
) {
866 * Search up from this section until we find
867 * the highest-level one which belongs in this
872 for (ac
= s
; ac
; ac
= ac
->parent
) {
880 if (s
->file
!= f
&& a
!= NULL
)
884 if (adepth
<= a
->contents_depth
) {
885 if (ntoc
>= tocsize
) {
887 toc
= sresize(toc
, tocsize
, htmlsect
*);
894 if (leaf
&& conf
.leaf_contains_contents
&&
895 ntoc
>= conf
.leaf_smallest_contents
) {
898 for (i
= 0; i
< ntoc
; i
++) {
899 htmlsect
*s
= toc
[i
];
900 int hlevel
= (s
->type
== TOP ?
-1 :
901 s
->type
== INDEX ?
0 :
902 heading_depth(s
->title
))
903 - f
->min_heading_depth
+ 1;
906 html_contents_entry(&ho
, hlevel
, s
,
909 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
914 * Now go through the document and output some real
918 for (s
= sects
.head
; s
; s
= s
->next
) {
921 * This section belongs in this file.
932 * Search up from this section until we find
933 * the highest-level one which belongs in this
938 for (ac
= s
; ac
; ac
= ac
->parent
) {
948 * This section does not belong in this
949 * file, but an ancestor of it does. Write
950 * out a contents table entry, if the depth
951 * doesn't exceed the maximum contents
952 * depth for the ancestor section.
954 if (adepth
<= a
->contents_depth
) {
955 html_contents_entry(&ho
, adepth
, s
,
965 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
968 * Display the section heading.
971 hlevel
= (s
->type
== TOP ?
-1 :
972 s
->type
== INDEX ?
0 :
973 heading_depth(s
->title
))
974 - f
->min_heading_depth
+ 1;
976 /* HTML headings only go up to <h6> */
980 htag
[1] = '0' + hlevel
;
982 element_open(&ho
, htag
);
985 * Provide anchor for cross-links to target.
987 * FIXME: AIcurrentlyUI, this needs to be done
988 * differently in XHTML because <a name> is
989 * deprecated or obsolete.
991 * (Also we'll have to do this separately in
992 * other paragraph types - NumberedList and
995 element_open(&ho
, "a");
996 element_attr(&ho
, "name", s
->fragment
);
997 element_close(&ho
, "a");
999 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1001 element_close(&ho
, htag
);
1004 * Now display the section text.
1007 stackhead
= snew(struct stackelement
);
1008 stackhead
->next
= NULL
;
1009 stackhead
->listtype
= NOLIST
;
1010 stackhead
->itemtype
= NOITEM
;
1012 for (p
= s
->text
;; p
= p
->next
) {
1013 enum LISTTYPE listtype
;
1014 struct stackelement
*se
;
1017 * Preliminary switch to figure out what
1018 * sort of list we expect to be inside at
1021 * Since p may still be NULL at this point,
1022 * I invent a harmless paragraph type for
1025 switch (p ? p
->type
: para_Normal
) {
1028 case para_Copyright
:
1029 case para_BiblioCited
:
1031 case para_QuotePush
:
1035 case para_UnnumberedChapter
:
1046 case para_NumberedList
:
1050 case para_DescribedThing
:
1051 case para_Description
:
1055 case para_LcontPush
:
1056 se
= snew(struct stackelement
);
1057 se
->next
= stackhead
;
1058 se
->listtype
= NOLIST
;
1059 se
->itemtype
= NOITEM
;
1063 default: /* some totally non-printing para */
1070 * Terminate the most recent list item, if
1071 * any. (We left this until after
1072 * processing LcontPush, since in that case
1073 * the list item won't want to be
1074 * terminated until after the corresponding
1077 if (stackhead
->itemtype
!= NOITEM
) {
1078 element_close(&ho
, itemname(stackhead
->itemtype
));
1081 stackhead
->itemtype
= NOITEM
;
1084 * Terminate the current list, if it's not
1085 * the one we want to be in.
1087 if (listtype
!= stackhead
->listtype
&&
1088 stackhead
->listtype
!= NOLIST
) {
1089 element_close(&ho
, listname(stackhead
->listtype
));
1094 * Leave the loop if our time has come.
1096 if (!p
|| (is_heading_type(p
->type
) &&
1097 p
->type
!= para_Title
))
1098 break; /* end of section text */
1101 * Start a fresh list if necessary.
1103 if (listtype
!= stackhead
->listtype
&&
1105 element_open(&ho
, listname(listtype
));
1107 stackhead
->listtype
= listtype
;
1111 element_empty(&ho
, "hr");
1114 html_codepara(&ho
, p
->words
);
1117 case para_Copyright
:
1118 element_open(&ho
, "p");
1120 html_words(&ho
, p
->words
, ALL
,
1121 f
, keywords
, &conf
);
1123 element_close(&ho
, "p");
1125 case para_BiblioCited
:
1126 element_open(&ho
, "p");
1127 if (p
->private_data
) {
1128 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1129 element_open(&ho
, "a");
1130 element_attr(&ho
, "name", s
->fragment
);
1131 element_close(&ho
, "a");
1134 html_words(&ho
, p
->kwtext
, ALL
,
1135 f
, keywords
, &conf
);
1136 html_text(&ho
, L
" ");
1137 html_words(&ho
, p
->words
, ALL
,
1138 f
, keywords
, &conf
);
1140 element_close(&ho
, "p");
1143 case para_NumberedList
:
1144 element_open(&ho
, "li");
1145 if (p
->private_data
) {
1146 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1147 element_open(&ho
, "a");
1148 element_attr(&ho
, "name", s
->fragment
);
1149 element_close(&ho
, "a");
1152 stackhead
->itemtype
= LI
;
1153 html_words(&ho
, p
->words
, ALL
,
1154 f
, keywords
, &conf
);
1156 case para_DescribedThing
:
1157 element_open(&ho
, "dt");
1159 stackhead
->itemtype
= DT
;
1160 html_words(&ho
, p
->words
, ALL
,
1161 f
, keywords
, &conf
);
1163 case para_Description
:
1164 element_open(&ho
, "dd");
1166 stackhead
->itemtype
= DD
;
1167 html_words(&ho
, p
->words
, ALL
,
1168 f
, keywords
, &conf
);
1171 case para_QuotePush
:
1172 element_open(&ho
, "blockquote");
1175 element_close(&ho
, "blockquote");
1180 stackhead
= stackhead
->next
;
1187 assert(stackhead
&& !stackhead
->next
);
1191 if (s
->type
== INDEX
) {
1196 * This section is the index. I'll just
1197 * render it as a single paragraph, with a
1198 * colon between the index term and the
1199 * references, and <br> in between each
1202 element_open(&ho
, "p");
1204 for (i
= 0; (entry
=
1205 index234(idx
->entries
, i
)) != NULL
; i
++) {
1206 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1210 element_empty(&ho
, "br");
1213 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1214 f
, keywords
, &conf
);
1216 html_text(&ho
, L
": ");/* FIXME: configurable */
1218 for (j
= 0; j
< hi
->nrefs
; j
++) {
1220 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1221 paragraph
*p
= hr
->section
->title
;
1224 html_text(&ho
, L
", "); /* FIXME: conf */
1226 html_href(&ho
, f
, hr
->section
->file
,
1228 hr
->referenced
= TRUE
;
1230 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1231 f
, keywords
, &conf
);
1232 else if (p
&& p
->words
)
1233 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1234 f
, keywords
, &conf
);
1236 html_text(&ho
, L
"FIXME");
1237 element_close(&ho
, "a");
1240 element_close(&ho
, "p");
1245 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1252 int done_version_ids
= FALSE
;
1254 element_empty(&ho
, "hr");
1257 html_raw(&ho
, conf
.body_end
);
1259 if (conf
.address_section
) {
1260 element_open(&ho
, "address");
1261 if (conf
.addr_start
) {
1262 html_raw(&ho
, conf
.addr_start
);
1265 if (conf
.visible_version_id
) {
1266 int started
= FALSE
;
1267 for (p
= sourceform
; p
; p
= p
->next
)
1268 if (p
->type
== para_VersionID
) {
1270 element_open(&ho
, "p");
1272 element_empty(&ho
, "br");
1274 html_text(&ho
, L
"["); /* FIXME: conf? */
1275 html_words(&ho
, p
->words
, NOTHING
,
1276 f
, keywords
, &conf
);
1277 html_text(&ho
, L
"]"); /* FIXME: conf? */
1281 element_close(&ho
, "p");
1282 done_version_ids
= TRUE
;
1285 html_raw(&ho
, conf
.addr_end
);
1286 element_close(&ho
, "address");
1289 if (!done_version_ids
) {
1291 * If the user didn't want the version IDs
1292 * visible, I think we still have a duty to put
1293 * them in an HTML comment.
1295 int started
= FALSE
;
1296 for (p
= sourceform
; p
; p
= p
->next
)
1297 if (p
->type
== para_VersionID
) {
1299 html_raw(&ho
, "<!-- version IDs:\n");
1302 html_words(&ho
, p
->words
, NOTHING
,
1303 f
, keywords
, &conf
);
1307 html_raw(&ho
, "-->\n");
1311 element_close(&ho
, "body");
1313 element_close(&ho
, "html");
1320 * Go through and check that no index fragments were referenced
1321 * without being generated, or indeed vice versa.
1323 * (When I actually get round to freeing everything, this can
1324 * probably be the freeing loop as well.)
1326 for (p
= sourceform
; p
; p
= p
->next
) {
1328 for (w
= p
->words
; w
; w
= w
->next
)
1329 if (w
->type
== word_IndexRef
) {
1330 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1332 assert(!hr
->referenced
== !hr
->generated
);
1337 * FIXME: Free all the working data.
1341 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1342 htmlsect
*sect
, int depth
)
1348 * `depth' is derived from the heading_depth() macro at the top
1349 * of this file, which counts title as -1, chapter as 0,
1350 * heading as 1 and subsection as 2. However, the semantics of
1351 * cfg->leaf_level are defined to count chapter as 1, heading
1352 * as 2 etc. So first I increment depth :-(
1356 if (cfg
->leaf_level
== 0) {
1358 * leaf_level==0 is a special case, in which everything is
1359 * put into a single file.
1362 files
->single
= html_new_file(files
, cfg
->single_filename
);
1364 file
= files
->single
;
1367 * If the depth of this section is at or above leaf_level,
1368 * we invent a fresh file and put this section at its head.
1369 * Otherwise, we put it in the same file as its parent
1372 if (ldepth
> cfg
->leaf_level
) {
1374 * We know that sect->parent cannot be NULL. The only
1375 * circumstance in which it can be is if sect is at
1376 * chapter or appendix level, i.e. ldepth==1; and if
1377 * that's the case, then we cannot have entered this
1378 * branch unless cfg->leaf_level==0, in which case we
1379 * would be in the single-file case above and not here
1382 assert(sect
->parent
);
1384 file
= sect
->parent
->file
;
1386 if (sect
->type
== TOP
) {
1387 file
= html_new_file(files
, cfg
->contents_filename
);
1388 } else if (sect
->type
== INDEX
) {
1389 file
= html_new_file(files
, cfg
->index_filename
);
1393 assert(ldepth
> 0 && sect
->title
);
1394 title
= html_format(sect
->title
, cfg
->template_filename
);
1395 file
= html_new_file(files
, title
);
1403 if (file
->min_heading_depth
> depth
) {
1405 * This heading is at a higher level than any heading we
1406 * have so far placed in this file; so we set the `first'
1409 file
->min_heading_depth
= depth
;
1413 if (file
->min_heading_depth
== depth
)
1417 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1419 htmlfile
*ret
= snew(htmlfile
);
1423 list
->tail
->next
= ret
;
1428 ret
->filename
= dupstr(filename
);
1429 ret
->last_fragment_number
= 0;
1430 ret
->min_heading_depth
= INT_MAX
;
1431 ret
->first
= ret
->last
= NULL
;
1436 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
)
1438 htmlsect
*ret
= snew(htmlsect
);
1442 list
->tail
->next
= ret
;
1455 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1456 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1462 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1463 case word_HyperLink
:
1464 if (flags
& LINKS
) {
1465 element_open(ho
, "a");
1466 c
= utoa_dup(w
->text
, CS_ASCII
);
1467 element_attr(ho
, "href", c
);
1471 case word_UpperXref
:
1472 case word_LowerXref
:
1473 if (flags
& LINKS
) {
1474 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1475 paragraph
*p
= kwl
->para
;
1476 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1480 html_href(ho
, file
, s
->file
, s
->fragment
);
1486 element_close(ho
, "a");
1489 if (flags
& INDEXENTS
) {
1490 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1491 element_open(ho
, "a");
1492 element_attr(ho
, "name", hr
->fragment
);
1493 element_close(ho
, "a");
1494 hr
->generated
= TRUE
;
1501 case word_WhiteSpace
:
1502 case word_EmphSpace
:
1503 case word_CodeSpace
:
1504 case word_WkCodeSpace
:
1506 case word_EmphQuote
:
1507 case word_CodeQuote
:
1508 case word_WkCodeQuote
:
1509 style
= towordstyle(w
->type
);
1510 type
= removeattr(w
->type
);
1511 if (style
== word_Emph
&&
1512 (attraux(w
->aux
) == attr_First
||
1513 attraux(w
->aux
) == attr_Only
) &&
1515 element_open(ho
, "em");
1516 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1517 (attraux(w
->aux
) == attr_First
||
1518 attraux(w
->aux
) == attr_Only
) &&
1520 element_open(ho
, "code");
1522 if (type
== word_WhiteSpace
)
1523 html_text(ho
, L
" ");
1524 else if (type
== word_Quote
) {
1525 if (quoteaux(w
->aux
) == quote_Open
)
1526 html_text(ho
, cfg
->lquote
);
1528 html_text(ho
, cfg
->rquote
);
1530 if (cvt_ok(ho
->charset
, w
->text
) || !w
->alt
)
1531 html_text(ho
, w
->text
);
1533 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1536 if (style
== word_Emph
&&
1537 (attraux(w
->aux
) == attr_Last
||
1538 attraux(w
->aux
) == attr_Only
) &&
1540 element_close(ho
, "em");
1541 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1542 (attraux(w
->aux
) == attr_Last
||
1543 attraux(w
->aux
) == attr_Only
) &&
1545 element_close(ho
, "code");
1551 static void html_codepara(htmloutput
*ho
, word
*words
)
1553 element_open(ho
, "pre");
1554 element_open(ho
, "code");
1555 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1560 if (words
->next
&& words
->next
->type
== word_Emph
) {
1561 e
= words
->next
->text
;
1562 words
= words
->next
;
1566 while (e
&& *e
&& *t
) {
1570 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1578 element_open(ho
, open_tag
);
1580 html_text_limit(ho
, t
, n
);
1583 element_close(ho
, open_tag
);
1591 element_close(ho
, "code");
1592 element_close(ho
, "pre");
1595 static void html_charset_cleanup(htmloutput
*ho
)
1600 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1601 ho
->charset
, &ho
->cstate
, NULL
);
1603 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1606 static void return_to_neutral(htmloutput
*ho
)
1608 if (ho
->state
== HO_IN_TEXT
) {
1609 html_charset_cleanup(ho
);
1610 } else if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1611 fprintf(ho
->fp
, " />");
1612 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1613 fprintf(ho
->fp
, ">");
1616 ho
->state
= HO_NEUTRAL
;
1619 static void element_open(htmloutput
*ho
, char const *name
)
1621 return_to_neutral(ho
);
1622 fprintf(ho
->fp
, "<%s", name
);
1623 ho
->state
= HO_IN_TAG
;
1626 static void element_close(htmloutput
*ho
, char const *name
)
1628 return_to_neutral(ho
);
1629 fprintf(ho
->fp
, "</%s>", name
);
1630 ho
->state
= HO_NEUTRAL
;
1633 static void element_empty(htmloutput
*ho
, char const *name
)
1635 return_to_neutral(ho
);
1636 fprintf(ho
->fp
, "<%s", name
);
1637 ho
->state
= HO_IN_EMPTY_TAG
;
1640 static void html_nl(htmloutput
*ho
)
1642 return_to_neutral(ho
);
1643 fputc('\n', ho
->fp
);
1646 static void html_raw(htmloutput
*ho
, char *text
)
1648 return_to_neutral(ho
);
1649 fputs(text
, ho
->fp
);
1652 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1654 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1656 fputs(text
, ho
->fp
);
1659 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1661 html_charset_cleanup(ho
);
1662 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1663 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1666 static void element_attr_w(htmloutput
*ho
, char const *name
,
1667 wchar_t const *value
)
1669 html_charset_cleanup(ho
);
1670 fprintf(ho
->fp
, " %s=\"", name
);
1671 html_text_limit_internal(ho
, value
, 0, TRUE
);
1672 html_charset_cleanup(ho
);
1676 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1678 html_text_limit(ho
, text
, 0);
1681 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1683 return_to_neutral(ho
);
1684 html_text_limit_internal(ho
, text
, maxlen
, FALSE
);
1687 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1688 int maxlen
, int quote_quotes
)
1690 int textlen
= ustrlen(text
);
1694 if (maxlen
> 0 && textlen
> maxlen
)
1697 while (textlen
> 0) {
1698 /* Scan ahead for characters we really can't display in HTML. */
1699 int lenbefore
, lenafter
;
1700 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1701 if (text
[lenbefore
] == L
'<' ||
1702 text
[lenbefore
] == L
'>' ||
1703 text
[lenbefore
] == L
'&' ||
1704 (text
[lenbefore
] == L
'"' && quote_quotes
))
1706 lenafter
= lenbefore
;
1707 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1708 ho
->charset
, &ho
->cstate
, &err
);
1709 textlen
-= (lenbefore
- lenafter
);
1711 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1714 * We have encountered a character that cannot be
1715 * displayed in the selected output charset. Therefore,
1716 * we use an HTML numeric entity reference.
1718 assert(textlen
> 0);
1719 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1721 } else if (lenafter
== 0 && textlen
> 0) {
1723 * We have encountered a character which is special to
1727 fprintf(ho
->fp
, "<");
1728 else if (*text
== L
'>')
1729 fprintf(ho
->fp
, ">");
1730 else if (*text
== L
'&')
1731 fprintf(ho
->fp
, "&");
1732 else if (*text
== L
'"')
1733 fprintf(ho
->fp
, """);
1735 assert(!"Can't happen");
1741 static void cleanup(htmloutput
*ho
)
1743 return_to_neutral(ho
);
1747 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
1748 htmlfile
*targetfile
, char *targetfrag
)
1750 rdstringc rs
= { 0, 0, NULL
};
1753 if (targetfile
!= thisfile
)
1754 rdaddsc(&rs
, targetfile
->filename
);
1757 rdaddsc(&rs
, targetfrag
);
1761 element_open(ho
, "a");
1762 element_attr(ho
, "href", url
);
1766 static char *html_format(paragraph
*p
, char *template_string
)
1770 wchar_t *ws
, wsbuf
[2];
1771 rdstringc rs
= { 0, 0, NULL
};
1773 t
= template_string
;
1775 if (*t
== '%' && t
[1]) {
1789 if (p
->kwtext
&& fmt
== 'n')
1791 else if (p
->kwtext2
&& fmt
== 'b') {
1793 * HTML fragment names must start with a letter, so
1794 * simply `1.2.3' is not adequate. In this case I'm
1795 * going to cheat slightly by prepending the first
1796 * character of the first word of kwtext, so that
1797 * we get `C1' for chapter 1, `S2.3' for section
1800 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
1803 wsbuf
[0] = p
->kwtext
->text
[0];
1806 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
1812 c
= utoa_dup(ws
, CS_ASCII
);
1818 if (removeattr(w
->type
) == word_Normal
) {
1819 c
= utoa_dup(w
->text
, CS_ASCII
);
1830 return rdtrimc(&rs
);
1833 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
1837 * The HTML 4 spec's strictest definition of fragment names (<a
1838 * name> and "id" attributes) says that they `must begin with a
1839 * letter and may be followed by any number of letters, digits,
1840 * hyphens, underscores, colons, and periods'.
1842 * So here we unceremoniously rip out any characters not
1843 * conforming to this limitation.
1845 char *p
= text
, *q
= text
;
1847 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
1849 if ((*q
++ = *p
++) != '\0') {
1851 if ((*p
>='A' && *p
<='Z') ||
1852 (*p
>='a' && *p
<='z') ||
1853 (*p
>='0' && *p
<='9') ||
1854 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
1863 * Now we check for clashes with other fragment names, and
1864 * adjust this one if necessary by appending a hyphen followed
1868 htmlfragment
*frag
= snew(htmlfragment
);
1869 int len
= 0; /* >0 indicates we have resized */
1873 frag
->fragment
= text
;
1875 while (add234(files
->frags
, frag
) != frag
) {
1878 frag
->fragment
= text
= sresize(text
, len
+20, char);
1881 sprintf(text
+ len
, "-%d", ++suffix
);
1888 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
1889 htmlfile
*thisfile
, keywordlist
*keywords
,
1892 while (ho
->contents_level
> depth
) {
1893 element_close(ho
, "ul");
1894 ho
->contents_level
--;
1897 while (ho
->contents_level
< depth
) {
1898 element_open(ho
, "ul");
1899 ho
->contents_level
++;
1905 element_open(ho
, "li");
1906 html_href(ho
, thisfile
, s
->file
, s
->fragment
);
1907 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
1908 element_close(ho
, "a");
1909 element_close(ho
, "li");
1912 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
1913 keywordlist
*keywords
, htmlconfig
*cfg
,
1919 int depth
= heading_depth(s
->title
);
1923 else if (depth
== 0)
1924 sl
= &cfg
->achapter
;
1925 else if (depth
<= cfg
->nasect
)
1926 sl
= &cfg
->asect
[depth
-1];
1928 sl
= &cfg
->asect
[cfg
->nasect
-1];
1932 else if (sl
->just_numbers
)
1933 number
= s
->title
->kwtext2
;
1935 number
= s
->title
->kwtext
;
1938 html_words(ho
, number
, MARKUP
,
1939 thisfile
, keywords
, cfg
);
1940 html_text(ho
, sl
->number_suffix
);
1943 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
1944 thisfile
, keywords
, cfg
);
1946 assert(s
->type
!= NORMAL
);
1948 html_text(ho
, L
"Preamble");/* FIXME: configure */
1949 else if (s
->type
== INDEX
)
1950 html_text(ho
, L
"Index");/* FIXME: configure */