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 * - nonbreaking spaces.
36 * - free up all the data we have allocated while running this
46 #define is_heading_type(type) ( (type) == para_Title || \
47 (type) == para_Chapter || \
48 (type) == para_Appendix || \
49 (type) == para_UnnumberedChapter || \
50 (type) == para_Heading || \
51 (type) == para_Subsect)
53 #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
54 (p)->type == para_Heading ? 1 : \
55 (p)->type == para_Title ? -1 : 0 )
59 wchar_t *number_suffix
;
64 sectlevel achapter
, *asect
;
65 int *contents_depths
; /* 0=main, 1=chapter, 2=sect etc */
67 int address_section
, visible_version_id
;
68 int leaf_contains_contents
, leaf_smallest_contents
;
69 char *contents_filename
;
71 char *template_filename
;
72 char *single_filename
;
73 char *template_fragment
;
74 char *head_end
, *body_start
, *body_end
, *addr_start
, *addr_end
;
75 char *body_tag
, *nav_attr
;
76 wchar_t *author
, *description
;
77 int restrict_charset
, output_charset
;
80 XHTML_1_0_TRANSITIONAL
, XHTML_1_0_STRICT
82 wchar_t *lquote
, *rquote
;
86 #define contents_depth(conf, level) \
87 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
89 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
91 typedef struct htmlfile htmlfile
;
92 typedef struct htmlsect htmlsect
;
97 int last_fragment_number
;
98 int min_heading_depth
;
99 htmlsect
*first
, *last
; /* first/last highest-level sections */
103 htmlsect
*next
, *parent
;
105 paragraph
*title
, *text
;
106 enum { NORMAL
, TOP
, INDEX
} type
;
112 htmlfile
*head
, *tail
;
113 htmlfile
*single
, *index
;
118 htmlsect
*head
, *tail
;
134 int generated
, referenced
;
139 * This level deals with charset conversion, starting and
140 * ending tags, and writing to the file. It's the lexical
145 charset_state cstate
;
148 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
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.
158 static int html_fragment_compare(void *av
, void *bv
)
160 htmlfragment
*a
= (htmlfragment
*)av
;
161 htmlfragment
*b
= (htmlfragment
*)bv
;
164 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
167 return strcmp(a
->fragment
, b
->fragment
);
170 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
171 htmlsect
*sect
, int depth
);
173 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
174 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
);
176 /* Flags for html_words() flags parameter */
180 #define INDEXENTS 0x04
182 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
183 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
184 static void html_codepara(htmloutput
*ho
, word
*words
);
186 static void element_open(htmloutput
*ho
, char const *name
);
187 static void element_close(htmloutput
*ho
, char const *name
);
188 static void element_empty(htmloutput
*ho
, char const *name
);
189 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
190 static void element_attr_w(htmloutput
*ho
, char const *name
,
191 wchar_t const *value
);
192 static void html_text(htmloutput
*ho
, wchar_t const *str
);
193 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
194 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
195 int maxlen
, int quote_quotes
);
196 static void html_nl(htmloutput
*ho
);
197 static void html_raw(htmloutput
*ho
, char *text
);
198 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
199 static void cleanup(htmloutput
*ho
);
201 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
202 htmlfile
*targetfile
, char *targetfrag
);
204 static char *html_format(paragraph
*p
, char *template_string
);
205 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
208 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
209 htmlfile
*thisfile
, keywordlist
*keywords
,
211 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
212 htmlfile
*thisfile
, keywordlist
*keywords
,
213 htmlconfig
*cfg
, int real
);
215 static htmlconfig
html_configure(paragraph
*source
) {
223 ret
.achapter
.just_numbers
= FALSE
;
224 ret
.achapter
.number_suffix
= L
": ";
226 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
227 ret
.asect
[0].just_numbers
= TRUE
;
228 ret
.asect
[0].number_suffix
= L
" ";
230 ret
.contents_depths
= 0;
231 ret
.visible_version_id
= TRUE
;
232 ret
.address_section
= TRUE
;
233 ret
.leaf_contains_contents
= FALSE
;
234 ret
.leaf_smallest_contents
= 4;
235 ret
.single_filename
= dupstr("Manual.html");
236 ret
.contents_filename
= dupstr("Contents.html");
237 ret
.index_filename
= dupstr("IndexPage.html");
238 ret
.template_filename
= dupstr("%n.html");
239 ret
.template_fragment
= dupstr("%b");
240 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
241 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
242 ret
.author
= ret
.description
= NULL
;
243 ret
.restrict_charset
= CS_ASCII
;
244 ret
.output_charset
= CS_ASCII
;
245 ret
.htmlver
= HTML_4
;
247 * Default quote characters are Unicode matched single quotes,
248 * falling back to ordinary ASCII ".
250 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
251 ret
.rquote
= uadv(ret
.lquote
);
254 * Two-pass configuration so that we can pick up global config
255 * (e.g. `quotes') before having it overridden by specific
256 * config (`html-quotes'), irrespective of the order in which
259 for (p
= source
; p
; p
= p
->next
) {
260 if (p
->type
== para_Config
) {
261 if (!ustricmp(p
->keyword
, L
"quotes")) {
262 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
263 ret
.lquote
= uadv(p
->keyword
);
264 ret
.rquote
= uadv(ret
.lquote
);
270 for (p
= source
; p
; p
= p
->next
) {
271 if (p
->type
== para_Config
) {
272 wchar_t *k
= p
->keyword
;
274 if (!ustrnicmp(k
, L
"xhtml-", 6))
275 k
++; /* treat `xhtml-' and `html-' the same */
277 if (!ustricmp(k
, L
"html-charset")) {
278 char *csname
= utoa_dup(uadv(k
), CS_ASCII
);
279 ret
.restrict_charset
= ret
.output_charset
=
280 charset_from_localenc(csname
);
282 } else if (!ustricmp(k
, L
"html-single-filename")) {
283 sfree(ret
.single_filename
);
284 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
285 } else if (!ustricmp(k
, L
"html-contents-filename")) {
286 sfree(ret
.contents_filename
);
287 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
288 } else if (!ustricmp(k
, L
"html-index-filename")) {
289 sfree(ret
.index_filename
);
290 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
291 } else if (!ustricmp(k
, L
"html-template-filename")) {
292 sfree(ret
.template_filename
);
293 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
294 } else if (!ustricmp(k
, L
"html-template-fragment")) {
295 sfree(ret
.template_fragment
);
296 ret
.template_fragment
= dupstr(adv(p
->origkeyword
));
297 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
298 ret
.achapter
.just_numbers
= utob(uadv(k
));
299 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
300 ret
.achapter
.number_suffix
= uadv(k
);
301 } else if (!ustricmp(k
, L
"html-leaf-level")) {
302 ret
.leaf_level
= utoi(uadv(k
));
303 } else if (!ustricmp(k
, L
"html-section-numeric")) {
304 wchar_t *q
= uadv(k
);
310 if (n
>= ret
.nasect
) {
312 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
313 for (i
= ret
.nasect
; i
<= n
; i
++)
314 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
317 ret
.asect
[n
].just_numbers
= utob(q
);
318 } else if (!ustricmp(k
, L
"html-section-suffix")) {
319 wchar_t *q
= uadv(k
);
325 if (n
>= ret
.nasect
) {
327 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
328 for (i
= ret
.nasect
; i
<= n
; i
++) {
329 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
333 ret
.asect
[n
].number_suffix
= q
;
334 } else if (!ustricmp(k
, L
"html-contents-depth") ||
335 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
337 * Relic of old implementation: this directive used
338 * to be written as \cfg{html-contents-depth-3}{2}
339 * rather than the usual Halibut convention of
340 * \cfg{html-contents-depth}{3}{2}. We therefore
343 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
349 if (n
>= ret
.ncdepths
) {
351 ret
.contents_depths
=
352 sresize(ret
.contents_depths
, n
+1, int);
353 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
354 ret
.contents_depths
[i
] = i
+2;
358 ret
.contents_depths
[n
] = utoi(q
);
359 } else if (!ustricmp(k
, L
"html-head-end")) {
360 ret
.head_end
= adv(p
->origkeyword
);
361 } else if (!ustricmp(k
, L
"html-body-tag")) {
362 ret
.body_tag
= adv(p
->origkeyword
);
363 } else if (!ustricmp(k
, L
"html-body-start")) {
364 ret
.body_start
= adv(p
->origkeyword
);
365 } else if (!ustricmp(k
, L
"html-body-end")) {
366 ret
.body_end
= adv(p
->origkeyword
);
367 } else if (!ustricmp(k
, L
"html-address-start")) {
368 ret
.addr_start
= adv(p
->origkeyword
);
369 } else if (!ustricmp(k
, L
"html-address-end")) {
370 ret
.addr_end
= adv(p
->origkeyword
);
371 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
372 ret
.nav_attr
= adv(p
->origkeyword
);
373 } else if (!ustricmp(k
, L
"html-author")) {
374 ret
.author
= uadv(k
);
375 } else if (!ustricmp(k
, L
"html-description")) {
376 ret
.description
= uadv(k
);
377 } else if (!ustricmp(k
, L
"html-suppress-address")) {
378 ret
.address_section
= !utob(uadv(k
));
379 } else if (!ustricmp(k
, L
"html-versionid")) {
380 ret
.visible_version_id
= utob(uadv(k
));
381 } else if (!ustricmp(k
, L
"html-quotes")) {
382 if (*uadv(k
) && *uadv(uadv(k
))) {
383 ret
.lquote
= uadv(k
);
384 ret
.rquote
= uadv(ret
.lquote
);
386 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
387 ret
.leaf_contains_contents
= utob(uadv(k
));
388 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
389 ret
.leaf_smallest_contents
= utoi(uadv(k
));
395 * Now process fallbacks on quote characters.
397 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
398 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
399 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
400 ret
.lquote
= uadv(ret
.rquote
);
401 ret
.rquote
= uadv(ret
.lquote
);
407 paragraph
*html_config_filename(char *filename
)
410 * If the user passes in a single filename as a parameter to
411 * the `--html' command-line option, then we should assume it
412 * to imply _two_ config directives:
413 * \cfg{html-single-filename}{whatever} and
414 * \cfg{html-leaf-level}{0}; the rationale being that the user
415 * wants their output _in that file_.
419 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
420 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
425 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
426 indexdata
*idx
, void *unused
) {
429 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
};
430 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
434 conf
= html_configure(sourceform
);
437 * We're going to make heavy use of paragraphs' private data
438 * fields in the forthcoming code. Clear them first, so we can
439 * reliably tell whether we have auxiliary data for a
440 * particular paragraph.
442 for (p
= sourceform
; p
; p
= p
->next
)
443 p
->private_data
= NULL
;
445 files
.frags
= newtree234(html_fragment_compare
);
448 * Start by figuring out into which file each piece of the
449 * document should be put. We'll do this by inventing an
450 * `htmlsect' structure and stashing it in the private_data
451 * field of each section paragraph; we also need one additional
452 * htmlsect for the document index, which won't show up in the
453 * source form but needs to be consistently mentioned in
456 * While we're here, we'll also invent the HTML fragment name
460 htmlsect
*topsect
, *sect
;
463 topsect
= html_new_sect(§s
, p
);
465 topsect
->title
= NULL
;
466 topsect
->text
= sourceform
;
467 topsect
->contents_depth
= contents_depth(conf
, 0);
468 html_file_section(&conf
, &files
, topsect
, -1);
469 topsect
->fragment
= NULL
;
471 for (p
= sourceform
; p
; p
= p
->next
)
472 if (is_heading_type(p
->type
)) {
473 d
= heading_depth(p
);
475 if (p
->type
== para_Title
) {
480 sect
= html_new_sect(§s
, p
);
481 sect
->text
= p
->next
;
483 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
486 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
487 assert(sect
->parent
!= NULL
);
489 sect
->parent
= topsect
;
490 p
->private_data
= sect
;
492 html_file_section(&conf
, &files
, sect
, d
);
494 sect
->fragment
= html_format(p
, conf
.template_fragment
);
495 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
500 sect
= html_new_sect(§s
, NULL
);
503 sect
->parent
= topsect
;
504 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
505 sect
->fragment
= dupstr("Index"); /* FIXME: this _can't_ be right */
506 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
508 files
.index
= sect
->file
;
512 * Go through the keyword list and sort out fragment IDs for
513 * all the potentially referenced paragraphs which _aren't_
521 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
522 paragraph
*q
, *p
= kw
->para
;
524 if (!is_heading_type(p
->type
)) {
528 * Find the paragraph's parent htmlsect, to
529 * determine which file it will end up in.
534 * Preamble paragraphs have no parent. So if we
535 * have a non-heading with no parent, it must
536 * be preamble, and therefore its parent
537 * htmlsect must be the preamble one.
540 sects
.head
->type
== TOP
);
543 parent
= (htmlsect
*)q
->private_data
;
546 * Now we can construct an htmlsect for this
547 * paragraph itself, taking care to put it in the
548 * list of non-sections rather than the list of
549 * sections (so that traverses of the `sects' list
550 * won't attempt to add it to the contents or
551 * anything weird like that).
553 sect
= html_new_sect(&nonsects
, p
);
554 sect
->file
= parent
->file
;
555 sect
->parent
= parent
;
556 p
->private_data
= sect
;
559 * Fragment IDs for these paragraphs will simply be
560 * `p' followed by an integer.
562 sect
->fragment
= snewn(40, char);
563 sprintf(sect
->fragment
, "p%d",
564 sect
->file
->last_fragment_number
++);
565 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
572 * Reset the fragment numbers in each file. I've just used them
573 * to generate `p' fragment IDs for non-section paragraphs
574 * (numbered list elements, bibliocited), and now I want to use
575 * them for `i' fragment IDs for index entries.
579 for (file
= files
.head
; file
; file
= file
->next
)
580 file
->last_fragment_number
= 0;
584 * Now sort out the index. This involves:
586 * - For each index term, we set up an htmlindex structure to
587 * store all the references to that term.
589 * - Then we make a pass over the actual document, finding
590 * every word_IndexRef; for each one, we actually figure out
591 * the HTML filename/fragment pair we will use to reference
592 * it, store that information in the private data field of
593 * the word_IndexRef itself (so we can recreate it when the
594 * time comes to output our HTML), and add a reference to it
595 * to the index term in question.
604 * Set up the htmlindex structures.
607 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
608 htmlindex
*hi
= snew(htmlindex
);
610 hi
->nrefs
= hi
->refsize
= 0;
613 entry
->backend_data
= hi
;
617 * Run over the document inventing fragments. Each fragment
618 * is of the form `i' followed by an integer.
621 for (p
= sourceform
; p
; p
= p
->next
) {
622 if (is_heading_type(p
->type
))
623 lastsect
= (htmlsect
*)p
->private_data
;
625 for (w
= p
->words
; w
; w
= w
->next
)
626 if (w
->type
== word_IndexRef
) {
627 htmlindexref
*hr
= snew(htmlindexref
);
631 hr
->referenced
= hr
->generated
= FALSE
;
632 hr
->section
= lastsect
;
636 lastsect
->file
->last_fragment_number
++);
637 hr
->fragment
= dupstr(buf
);
639 html_sanitise_fragment(&files
, hr
->section
->file
,
642 w
->private_data
= hr
;
644 tag
= index_findtag(idx
, w
->text
);
648 for (i
= 0; i
< tag
->nrefs
; i
++) {
649 indexentry
*entry
= tag
->refs
[i
];
650 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
652 if (hi
->nrefs
>= hi
->refsize
) {
654 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
657 hi
->refs
[hi
->nrefs
++] = w
;
664 * Now we're ready to write out the actual HTML files.
668 * - we open that file and write its header
669 * - we run down the list of sections
670 * - for each section directly contained within that file, we
671 * output the section text
672 * - for each section which is not in the file but which has a
673 * parent that is, we output a contents entry for the
674 * section if appropriate
675 * - finally, we output the file trailer and close the file.
684 for (f
= files
.head
; f
; f
= f
->next
) {
687 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
688 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
689 struct stackelement
{
690 struct stackelement
*next
;
691 enum LISTTYPE listtype
;
692 enum ITEMTYPE itemtype
;
695 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
696 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
698 ho
.fp
= fopen(f
->filename
, "w");
699 ho
.charset
= conf
.output_charset
;
700 ho
.cstate
= charset_init_state
;
701 ho
.ver
= conf
.htmlver
;
702 ho
.state
= HO_NEUTRAL
;
703 ho
.contents_level
= 0;
706 switch (conf
.htmlver
) {
708 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
709 "HTML 3.2 Final//EN\">\n");
712 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
713 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
716 case XHTML_1_0_TRANSITIONAL
:
717 /* FIXME: <?xml?> to specify character encoding.
718 * This breaks HTML backwards compat, so perhaps avoid, or
719 * perhaps only emit when not using the default UTF-8? */
720 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
721 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
722 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
724 case XHTML_1_0_STRICT
:
725 /* FIXME: <?xml?> to specify character encoding. */
726 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
727 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
728 "DTD/xhtml1-strict.dtd\">\n");
732 element_open(&ho
, "html");
733 if (is_xhtml(conf
.htmlver
)) {
734 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
738 element_open(&ho
, "head");
741 element_empty(&ho
, "meta");
742 element_attr(&ho
, "http-equiv", "content-type");
745 sprintf(buf
, "text/html; charset=%.150s",
746 charset_to_mimeenc(conf
.output_charset
));
747 element_attr(&ho
, "content", buf
);
752 element_empty(&ho
, "meta");
753 element_attr(&ho
, "name", "author");
754 element_attr_w(&ho
, "content", conf
.author
);
758 if (conf
.description
) {
759 element_empty(&ho
, "meta");
760 element_attr(&ho
, "name", "description");
761 element_attr_w(&ho
, "content", conf
.description
);
765 element_open(&ho
, "title");
766 if (f
->first
&& f
->first
->title
) {
767 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
771 if (f
->last
!= f
->first
&& f
->last
->title
) {
772 html_text(&ho
, L
" - "); /* FIXME: configurable? */
773 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
777 element_close(&ho
, "title");
781 html_raw(&ho
, conf
.head_end
);
783 element_close(&ho
, "head");
786 /* FIXME: need to be able to specify replacement for this */
788 html_raw(&ho
, conf
.body_tag
);
790 element_open(&ho
, "body");
794 html_raw(&ho
, conf
.body_start
);
797 * Write out a nav bar. Special case: we don't do this
798 * if there is only one file.
800 if (files
.head
!= files
.tail
) {
801 element_open(&ho
, "p");
803 html_raw_as_attr(&ho
, conf
.nav_attr
);
806 element_open(&ho
, "a");
807 element_attr(&ho
, "href", prevf
->filename
);
809 html_text(&ho
, L
"Previous");/* FIXME: conf? */
811 element_close(&ho
, "a");
813 html_text(&ho
, L
" | "); /* FIXME: conf? */
815 if (f
!= files
.head
) {
816 element_open(&ho
, "a");
817 element_attr(&ho
, "href", files
.head
->filename
);
819 html_text(&ho
, L
"Contents");/* FIXME: conf? */
821 element_close(&ho
, "a");
823 html_text(&ho
, L
" | "); /* FIXME: conf? */
825 if (f
!= files
.index
) {
826 element_open(&ho
, "a");
827 element_attr(&ho
, "href", files
.index
->filename
);
829 html_text(&ho
, L
"Index");/* FIXME: conf? */
830 if (f
!= files
.index
)
831 element_close(&ho
, "a");
833 html_text(&ho
, L
" | "); /* FIXME: conf? */
836 element_open(&ho
, "a");
837 element_attr(&ho
, "href", f
->next
->filename
);
839 html_text(&ho
, L
"Next"); /* FIXME: conf? */
841 element_close(&ho
, "a");
843 element_close(&ho
, "p");
849 * Write out a prefix TOC for the file.
851 * We start by going through the section list and
852 * collecting the sections which need to be added to
853 * the contents. On the way, we also test to see if
854 * this file is a leaf file (defined as one which
855 * contains all descendants of any section it
856 * contains), because this will play a part in our
857 * decision on whether or not to _output_ the TOC.
859 * Special case: we absolutely do not do this if we're
860 * in single-file mode.
862 if (files
.head
!= files
.tail
) {
863 int ntoc
= 0, tocsize
= 0;
864 htmlsect
**toc
= NULL
;
867 for (s
= sects
.head
; s
; s
= s
->next
) {
872 * Search up from this section until we find
873 * the highest-level one which belongs in this
878 for (ac
= s
; ac
; ac
= ac
->parent
) {
886 if (s
->file
!= f
&& a
!= NULL
)
890 if (adepth
<= a
->contents_depth
) {
891 if (ntoc
>= tocsize
) {
893 toc
= sresize(toc
, tocsize
, htmlsect
*);
900 if (leaf
&& conf
.leaf_contains_contents
&&
901 ntoc
>= conf
.leaf_smallest_contents
) {
904 for (i
= 0; i
< ntoc
; i
++) {
905 htmlsect
*s
= toc
[i
];
906 int hlevel
= (s
->type
== TOP ?
-1 :
907 s
->type
== INDEX ?
0 :
908 heading_depth(s
->title
))
909 - f
->min_heading_depth
+ 1;
912 html_contents_entry(&ho
, hlevel
, s
,
915 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
920 * Now go through the document and output some real
924 for (s
= sects
.head
; s
; s
= s
->next
) {
927 * This section belongs in this file.
938 * Search up from this section until we find
939 * the highest-level one which belongs in this
944 for (ac
= s
; ac
; ac
= ac
->parent
) {
954 * This section does not belong in this
955 * file, but an ancestor of it does. Write
956 * out a contents table entry, if the depth
957 * doesn't exceed the maximum contents
958 * depth for the ancestor section.
960 if (adepth
<= a
->contents_depth
) {
961 html_contents_entry(&ho
, adepth
, s
,
971 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
974 * Display the section heading.
977 hlevel
= (s
->type
== TOP ?
-1 :
978 s
->type
== INDEX ?
0 :
979 heading_depth(s
->title
))
980 - f
->min_heading_depth
+ 1;
982 /* HTML headings only go up to <h6> */
986 htag
[1] = '0' + hlevel
;
988 element_open(&ho
, htag
);
991 * Provide anchor for cross-links to target.
993 * FIXME: AIcurrentlyUI, this needs to be done
994 * differently in XHTML because <a name> is
995 * deprecated or obsolete.
997 * (Also we'll have to do this separately in
998 * other paragraph types - NumberedList and
1001 element_open(&ho
, "a");
1002 element_attr(&ho
, "name", s
->fragment
);
1003 element_close(&ho
, "a");
1005 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1007 element_close(&ho
, htag
);
1010 * Now display the section text.
1013 stackhead
= snew(struct stackelement
);
1014 stackhead
->next
= NULL
;
1015 stackhead
->listtype
= NOLIST
;
1016 stackhead
->itemtype
= NOITEM
;
1018 for (p
= s
->text
;; p
= p
->next
) {
1019 enum LISTTYPE listtype
;
1020 struct stackelement
*se
;
1023 * Preliminary switch to figure out what
1024 * sort of list we expect to be inside at
1027 * Since p may still be NULL at this point,
1028 * I invent a harmless paragraph type for
1031 switch (p ? p
->type
: para_Normal
) {
1034 case para_Copyright
:
1035 case para_BiblioCited
:
1037 case para_QuotePush
:
1041 case para_UnnumberedChapter
:
1052 case para_NumberedList
:
1056 case para_DescribedThing
:
1057 case para_Description
:
1061 case para_LcontPush
:
1062 se
= snew(struct stackelement
);
1063 se
->next
= stackhead
;
1064 se
->listtype
= NOLIST
;
1065 se
->itemtype
= NOITEM
;
1069 default: /* some totally non-printing para */
1076 * Terminate the most recent list item, if
1077 * any. (We left this until after
1078 * processing LcontPush, since in that case
1079 * the list item won't want to be
1080 * terminated until after the corresponding
1083 if (stackhead
->itemtype
!= NOITEM
) {
1084 element_close(&ho
, itemname(stackhead
->itemtype
));
1087 stackhead
->itemtype
= NOITEM
;
1090 * Terminate the current list, if it's not
1091 * the one we want to be in.
1093 if (listtype
!= stackhead
->listtype
&&
1094 stackhead
->listtype
!= NOLIST
) {
1095 element_close(&ho
, listname(stackhead
->listtype
));
1100 * Leave the loop if our time has come.
1102 if (!p
|| (is_heading_type(p
->type
) &&
1103 p
->type
!= para_Title
))
1104 break; /* end of section text */
1107 * Start a fresh list if necessary.
1109 if (listtype
!= stackhead
->listtype
&&
1111 element_open(&ho
, listname(listtype
));
1113 stackhead
->listtype
= listtype
;
1117 element_empty(&ho
, "hr");
1120 html_codepara(&ho
, p
->words
);
1123 case para_Copyright
:
1124 element_open(&ho
, "p");
1126 html_words(&ho
, p
->words
, ALL
,
1127 f
, keywords
, &conf
);
1129 element_close(&ho
, "p");
1131 case para_BiblioCited
:
1132 element_open(&ho
, "p");
1133 if (p
->private_data
) {
1134 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1135 element_open(&ho
, "a");
1136 element_attr(&ho
, "name", s
->fragment
);
1137 element_close(&ho
, "a");
1140 html_words(&ho
, p
->kwtext
, ALL
,
1141 f
, keywords
, &conf
);
1142 html_text(&ho
, L
" ");
1143 html_words(&ho
, p
->words
, ALL
,
1144 f
, keywords
, &conf
);
1146 element_close(&ho
, "p");
1149 case para_NumberedList
:
1150 element_open(&ho
, "li");
1151 if (p
->private_data
) {
1152 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1153 element_open(&ho
, "a");
1154 element_attr(&ho
, "name", s
->fragment
);
1155 element_close(&ho
, "a");
1158 stackhead
->itemtype
= LI
;
1159 html_words(&ho
, p
->words
, ALL
,
1160 f
, keywords
, &conf
);
1162 case para_DescribedThing
:
1163 element_open(&ho
, "dt");
1165 stackhead
->itemtype
= DT
;
1166 html_words(&ho
, p
->words
, ALL
,
1167 f
, keywords
, &conf
);
1169 case para_Description
:
1170 element_open(&ho
, "dd");
1172 stackhead
->itemtype
= DD
;
1173 html_words(&ho
, p
->words
, ALL
,
1174 f
, keywords
, &conf
);
1177 case para_QuotePush
:
1178 element_open(&ho
, "blockquote");
1181 element_close(&ho
, "blockquote");
1186 stackhead
= stackhead
->next
;
1193 assert(stackhead
&& !stackhead
->next
);
1197 if (s
->type
== INDEX
) {
1202 * This section is the index. I'll just
1203 * render it as a single paragraph, with a
1204 * colon between the index term and the
1205 * references, and <br> in between each
1208 element_open(&ho
, "p");
1210 for (i
= 0; (entry
=
1211 index234(idx
->entries
, i
)) != NULL
; i
++) {
1212 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1216 element_empty(&ho
, "br");
1219 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1220 f
, keywords
, &conf
);
1222 html_text(&ho
, L
": ");/* FIXME: configurable */
1224 for (j
= 0; j
< hi
->nrefs
; j
++) {
1226 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1227 paragraph
*p
= hr
->section
->title
;
1230 html_text(&ho
, L
", "); /* FIXME: conf */
1232 html_href(&ho
, f
, hr
->section
->file
,
1234 hr
->referenced
= TRUE
;
1236 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1237 f
, keywords
, &conf
);
1238 else if (p
&& p
->words
)
1239 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1240 f
, keywords
, &conf
);
1242 html_text(&ho
, L
"FIXME");
1243 element_close(&ho
, "a");
1246 element_close(&ho
, "p");
1251 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1258 int done_version_ids
= FALSE
;
1260 element_empty(&ho
, "hr");
1263 html_raw(&ho
, conf
.body_end
);
1265 if (conf
.address_section
) {
1266 element_open(&ho
, "address");
1267 if (conf
.addr_start
) {
1268 html_raw(&ho
, conf
.addr_start
);
1271 if (conf
.visible_version_id
) {
1272 int started
= FALSE
;
1273 for (p
= sourceform
; p
; p
= p
->next
)
1274 if (p
->type
== para_VersionID
) {
1276 element_open(&ho
, "p");
1278 element_empty(&ho
, "br");
1280 html_text(&ho
, L
"["); /* FIXME: conf? */
1281 html_words(&ho
, p
->words
, NOTHING
,
1282 f
, keywords
, &conf
);
1283 html_text(&ho
, L
"]"); /* FIXME: conf? */
1287 element_close(&ho
, "p");
1288 done_version_ids
= TRUE
;
1291 html_raw(&ho
, conf
.addr_end
);
1292 element_close(&ho
, "address");
1295 if (!done_version_ids
) {
1297 * If the user didn't want the version IDs
1298 * visible, I think we still have a duty to put
1299 * them in an HTML comment.
1301 int started
= FALSE
;
1302 for (p
= sourceform
; p
; p
= p
->next
)
1303 if (p
->type
== para_VersionID
) {
1305 html_raw(&ho
, "<!-- version IDs:\n");
1308 html_words(&ho
, p
->words
, NOTHING
,
1309 f
, keywords
, &conf
);
1313 html_raw(&ho
, "-->\n");
1317 element_close(&ho
, "body");
1319 element_close(&ho
, "html");
1326 * Go through and check that no index fragments were referenced
1327 * without being generated, or indeed vice versa.
1329 * (When I actually get round to freeing everything, this can
1330 * probably be the freeing loop as well.)
1332 for (p
= sourceform
; p
; p
= p
->next
) {
1334 for (w
= p
->words
; w
; w
= w
->next
)
1335 if (w
->type
== word_IndexRef
) {
1336 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1338 assert(!hr
->referenced
== !hr
->generated
);
1343 * FIXME: Free all the working data.
1347 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1348 htmlsect
*sect
, int depth
)
1354 * `depth' is derived from the heading_depth() macro at the top
1355 * of this file, which counts title as -1, chapter as 0,
1356 * heading as 1 and subsection as 2. However, the semantics of
1357 * cfg->leaf_level are defined to count chapter as 1, heading
1358 * as 2 etc. So first I increment depth :-(
1362 if (cfg
->leaf_level
== 0) {
1364 * leaf_level==0 is a special case, in which everything is
1365 * put into a single file.
1368 files
->single
= html_new_file(files
, cfg
->single_filename
);
1370 file
= files
->single
;
1373 * If the depth of this section is at or above leaf_level,
1374 * we invent a fresh file and put this section at its head.
1375 * Otherwise, we put it in the same file as its parent
1378 if (ldepth
> cfg
->leaf_level
) {
1380 * We know that sect->parent cannot be NULL. The only
1381 * circumstance in which it can be is if sect is at
1382 * chapter or appendix level, i.e. ldepth==1; and if
1383 * that's the case, then we cannot have entered this
1384 * branch unless cfg->leaf_level==0, in which case we
1385 * would be in the single-file case above and not here
1388 assert(sect
->parent
);
1390 file
= sect
->parent
->file
;
1392 if (sect
->type
== TOP
) {
1393 file
= html_new_file(files
, cfg
->contents_filename
);
1394 } else if (sect
->type
== INDEX
) {
1395 file
= html_new_file(files
, cfg
->index_filename
);
1399 assert(ldepth
> 0 && sect
->title
);
1400 title
= html_format(sect
->title
, cfg
->template_filename
);
1401 file
= html_new_file(files
, title
);
1409 if (file
->min_heading_depth
> depth
) {
1411 * This heading is at a higher level than any heading we
1412 * have so far placed in this file; so we set the `first'
1415 file
->min_heading_depth
= depth
;
1419 if (file
->min_heading_depth
== depth
)
1423 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1425 htmlfile
*ret
= snew(htmlfile
);
1429 list
->tail
->next
= ret
;
1434 ret
->filename
= dupstr(filename
);
1435 ret
->last_fragment_number
= 0;
1436 ret
->min_heading_depth
= INT_MAX
;
1437 ret
->first
= ret
->last
= NULL
;
1442 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
)
1444 htmlsect
*ret
= snew(htmlsect
);
1448 list
->tail
->next
= ret
;
1461 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1462 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1468 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1469 case word_HyperLink
:
1470 if (flags
& LINKS
) {
1471 element_open(ho
, "a");
1472 c
= utoa_dup(w
->text
, CS_ASCII
);
1473 element_attr(ho
, "href", c
);
1477 case word_UpperXref
:
1478 case word_LowerXref
:
1479 if (flags
& LINKS
) {
1480 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1481 paragraph
*p
= kwl
->para
;
1482 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1486 html_href(ho
, file
, s
->file
, s
->fragment
);
1492 element_close(ho
, "a");
1495 if (flags
& INDEXENTS
) {
1496 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1497 element_open(ho
, "a");
1498 element_attr(ho
, "name", hr
->fragment
);
1499 element_close(ho
, "a");
1500 hr
->generated
= TRUE
;
1507 case word_WhiteSpace
:
1508 case word_EmphSpace
:
1509 case word_CodeSpace
:
1510 case word_WkCodeSpace
:
1512 case word_EmphQuote
:
1513 case word_CodeQuote
:
1514 case word_WkCodeQuote
:
1515 style
= towordstyle(w
->type
);
1516 type
= removeattr(w
->type
);
1517 if (style
== word_Emph
&&
1518 (attraux(w
->aux
) == attr_First
||
1519 attraux(w
->aux
) == attr_Only
) &&
1521 element_open(ho
, "em");
1522 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1523 (attraux(w
->aux
) == attr_First
||
1524 attraux(w
->aux
) == attr_Only
) &&
1526 element_open(ho
, "code");
1528 if (type
== word_WhiteSpace
)
1529 html_text(ho
, L
" ");
1530 else if (type
== word_Quote
) {
1531 if (quoteaux(w
->aux
) == quote_Open
)
1532 html_text(ho
, cfg
->lquote
);
1534 html_text(ho
, cfg
->rquote
);
1536 if (cvt_ok(ho
->charset
, w
->text
) || !w
->alt
)
1537 html_text(ho
, w
->text
);
1539 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1542 if (style
== word_Emph
&&
1543 (attraux(w
->aux
) == attr_Last
||
1544 attraux(w
->aux
) == attr_Only
) &&
1546 element_close(ho
, "em");
1547 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1548 (attraux(w
->aux
) == attr_Last
||
1549 attraux(w
->aux
) == attr_Only
) &&
1551 element_close(ho
, "code");
1557 static void html_codepara(htmloutput
*ho
, word
*words
)
1559 element_open(ho
, "pre");
1560 element_open(ho
, "code");
1561 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1566 if (words
->next
&& words
->next
->type
== word_Emph
) {
1567 e
= words
->next
->text
;
1568 words
= words
->next
;
1572 while (e
&& *e
&& *t
) {
1576 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1584 element_open(ho
, open_tag
);
1586 html_text_limit(ho
, t
, n
);
1589 element_close(ho
, open_tag
);
1597 element_close(ho
, "code");
1598 element_close(ho
, "pre");
1601 static void html_charset_cleanup(htmloutput
*ho
)
1606 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1607 ho
->charset
, &ho
->cstate
, NULL
);
1609 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1612 static void return_to_neutral(htmloutput
*ho
)
1614 if (ho
->state
== HO_IN_TEXT
) {
1615 html_charset_cleanup(ho
);
1616 } else if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1617 fprintf(ho
->fp
, " />");
1618 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1619 fprintf(ho
->fp
, ">");
1622 ho
->state
= HO_NEUTRAL
;
1625 static void element_open(htmloutput
*ho
, char const *name
)
1627 return_to_neutral(ho
);
1628 fprintf(ho
->fp
, "<%s", name
);
1629 ho
->state
= HO_IN_TAG
;
1632 static void element_close(htmloutput
*ho
, char const *name
)
1634 return_to_neutral(ho
);
1635 fprintf(ho
->fp
, "</%s>", name
);
1636 ho
->state
= HO_NEUTRAL
;
1639 static void element_empty(htmloutput
*ho
, char const *name
)
1641 return_to_neutral(ho
);
1642 fprintf(ho
->fp
, "<%s", name
);
1643 ho
->state
= HO_IN_EMPTY_TAG
;
1646 static void html_nl(htmloutput
*ho
)
1648 return_to_neutral(ho
);
1649 fputc('\n', ho
->fp
);
1652 static void html_raw(htmloutput
*ho
, char *text
)
1654 return_to_neutral(ho
);
1655 fputs(text
, ho
->fp
);
1658 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1660 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1662 fputs(text
, ho
->fp
);
1665 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1667 html_charset_cleanup(ho
);
1668 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1669 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1672 static void element_attr_w(htmloutput
*ho
, char const *name
,
1673 wchar_t const *value
)
1675 html_charset_cleanup(ho
);
1676 fprintf(ho
->fp
, " %s=\"", name
);
1677 html_text_limit_internal(ho
, value
, 0, TRUE
);
1678 html_charset_cleanup(ho
);
1682 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1684 html_text_limit(ho
, text
, 0);
1687 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1689 return_to_neutral(ho
);
1690 html_text_limit_internal(ho
, text
, maxlen
, FALSE
);
1693 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1694 int maxlen
, int quote_quotes
)
1696 int textlen
= ustrlen(text
);
1700 if (maxlen
> 0 && textlen
> maxlen
)
1703 while (textlen
> 0) {
1704 /* Scan ahead for characters we really can't display in HTML. */
1705 int lenbefore
, lenafter
;
1706 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1707 if (text
[lenbefore
] == L
'<' ||
1708 text
[lenbefore
] == L
'>' ||
1709 text
[lenbefore
] == L
'&' ||
1710 (text
[lenbefore
] == L
'"' && quote_quotes
))
1712 lenafter
= lenbefore
;
1713 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1714 ho
->charset
, &ho
->cstate
, &err
);
1715 textlen
-= (lenbefore
- lenafter
);
1717 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1720 * We have encountered a character that cannot be
1721 * displayed in the selected output charset. Therefore,
1722 * we use an HTML numeric entity reference.
1724 assert(textlen
> 0);
1725 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1727 } else if (lenafter
== 0 && textlen
> 0) {
1729 * We have encountered a character which is special to
1733 fprintf(ho
->fp
, "<");
1734 else if (*text
== L
'>')
1735 fprintf(ho
->fp
, ">");
1736 else if (*text
== L
'&')
1737 fprintf(ho
->fp
, "&");
1738 else if (*text
== L
'"')
1739 fprintf(ho
->fp
, """);
1741 assert(!"Can't happen");
1747 static void cleanup(htmloutput
*ho
)
1749 return_to_neutral(ho
);
1753 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
1754 htmlfile
*targetfile
, char *targetfrag
)
1756 rdstringc rs
= { 0, 0, NULL
};
1759 if (targetfile
!= thisfile
)
1760 rdaddsc(&rs
, targetfile
->filename
);
1763 rdaddsc(&rs
, targetfrag
);
1767 element_open(ho
, "a");
1768 element_attr(ho
, "href", url
);
1772 static char *html_format(paragraph
*p
, char *template_string
)
1776 wchar_t *ws
, wsbuf
[2];
1777 rdstringc rs
= { 0, 0, NULL
};
1779 t
= template_string
;
1781 if (*t
== '%' && t
[1]) {
1795 if (p
->kwtext
&& fmt
== 'n')
1797 else if (p
->kwtext2
&& fmt
== 'b') {
1799 * HTML fragment names must start with a letter, so
1800 * simply `1.2.3' is not adequate. In this case I'm
1801 * going to cheat slightly by prepending the first
1802 * character of the first word of kwtext, so that
1803 * we get `C1' for chapter 1, `S2.3' for section
1806 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
1809 wsbuf
[0] = p
->kwtext
->text
[0];
1812 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
1818 c
= utoa_dup(ws
, CS_ASCII
);
1824 if (removeattr(w
->type
) == word_Normal
) {
1825 c
= utoa_dup(w
->text
, CS_ASCII
);
1836 return rdtrimc(&rs
);
1839 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
1843 * The HTML 4 spec's strictest definition of fragment names (<a
1844 * name> and "id" attributes) says that they `must begin with a
1845 * letter and may be followed by any number of letters, digits,
1846 * hyphens, underscores, colons, and periods'.
1848 * So here we unceremoniously rip out any characters not
1849 * conforming to this limitation.
1851 char *p
= text
, *q
= text
;
1853 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
1855 if ((*q
++ = *p
++) != '\0') {
1857 if ((*p
>='A' && *p
<='Z') ||
1858 (*p
>='a' && *p
<='z') ||
1859 (*p
>='0' && *p
<='9') ||
1860 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
1869 * Now we check for clashes with other fragment names, and
1870 * adjust this one if necessary by appending a hyphen followed
1874 htmlfragment
*frag
= snew(htmlfragment
);
1875 int len
= 0; /* >0 indicates we have resized */
1879 frag
->fragment
= text
;
1881 while (add234(files
->frags
, frag
) != frag
) {
1884 frag
->fragment
= text
= sresize(text
, len
+20, char);
1887 sprintf(text
+ len
, "-%d", ++suffix
);
1894 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
1895 htmlfile
*thisfile
, keywordlist
*keywords
,
1898 while (ho
->contents_level
> depth
) {
1899 element_close(ho
, "ul");
1900 ho
->contents_level
--;
1903 while (ho
->contents_level
< depth
) {
1904 element_open(ho
, "ul");
1905 ho
->contents_level
++;
1911 element_open(ho
, "li");
1912 html_href(ho
, thisfile
, s
->file
, s
->fragment
);
1913 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
1914 element_close(ho
, "a");
1915 element_close(ho
, "li");
1918 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
1919 keywordlist
*keywords
, htmlconfig
*cfg
,
1925 int depth
= heading_depth(s
->title
);
1929 else if (depth
== 0)
1930 sl
= &cfg
->achapter
;
1931 else if (depth
<= cfg
->nasect
)
1932 sl
= &cfg
->asect
[depth
-1];
1934 sl
= &cfg
->asect
[cfg
->nasect
-1];
1938 else if (sl
->just_numbers
)
1939 number
= s
->title
->kwtext2
;
1941 number
= s
->title
->kwtext
;
1944 html_words(ho
, number
, MARKUP
,
1945 thisfile
, keywords
, cfg
);
1946 html_text(ho
, sl
->number_suffix
);
1949 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
1950 thisfile
, keywords
, cfg
);
1952 assert(s
->type
!= NORMAL
);
1954 html_text(ho
, L
"Preamble");/* FIXME: configure */
1955 else if (s
->type
== INDEX
)
1956 html_text(ho
, L
"Index");/* FIXME: configure */