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 * * index_text, contents_text, preamble_text, title_separator,
16 * nav_prev_text, nav_next_text, nav_separator,
17 * index_main_sep, index_multi_sep, pre_versionid,
20 * - nonbreaking spaces.
22 * - free up all the data we have allocated while running this
32 #define is_heading_type(type) ( (type) == para_Title || \
33 (type) == para_Chapter || \
34 (type) == para_Appendix || \
35 (type) == para_UnnumberedChapter || \
36 (type) == para_Heading || \
37 (type) == para_Subsect)
39 #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
40 (p)->type == para_Heading ? 1 : \
41 (p)->type == para_Title ? -1 : 0 )
45 wchar_t *number_suffix
;
50 sectlevel achapter
, *asect
;
51 int *contents_depths
; /* 0=main, 1=chapter, 2=sect etc */
53 int address_section
, visible_version_id
;
54 int leaf_contains_contents
, leaf_smallest_contents
;
55 char *contents_filename
;
57 char *template_filename
;
58 char *single_filename
;
59 char *template_fragment
;
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 */
93 htmlsect
*next
, *parent
;
95 paragraph
*title
, *text
;
96 enum { NORMAL
, TOP
, INDEX
} type
;
102 htmlfile
*head
, *tail
;
103 htmlfile
*single
, *index
;
108 htmlsect
*head
, *tail
;
124 int generated
, referenced
;
129 * This level deals with charset conversion, starting and
130 * ending tags, and writing to the file. It's the lexical
134 int charset
, restrict_charset
;
135 charset_state cstate
;
138 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
141 * Stuff beyond here deals with the higher syntactic level: it
142 * tracks how many levels of <ul> are currently open when
143 * producing a contents list, for example.
148 static int html_fragment_compare(void *av
, void *bv
)
150 htmlfragment
*a
= (htmlfragment
*)av
;
151 htmlfragment
*b
= (htmlfragment
*)bv
;
154 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
157 return strcmp(a
->fragment
, b
->fragment
);
160 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
161 htmlsect
*sect
, int depth
);
163 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
164 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
);
166 /* Flags for html_words() flags parameter */
170 #define INDEXENTS 0x04
172 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
173 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
174 static void html_codepara(htmloutput
*ho
, word
*words
);
176 static void element_open(htmloutput
*ho
, char const *name
);
177 static void element_close(htmloutput
*ho
, char const *name
);
178 static void element_empty(htmloutput
*ho
, char const *name
);
179 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
180 static void element_attr_w(htmloutput
*ho
, char const *name
,
181 wchar_t const *value
);
182 static void html_text(htmloutput
*ho
, wchar_t const *str
);
183 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
184 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
185 int maxlen
, int quote_quotes
);
186 static void html_nl(htmloutput
*ho
);
187 static void html_raw(htmloutput
*ho
, char *text
);
188 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
189 static void cleanup(htmloutput
*ho
);
191 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
192 htmlfile
*targetfile
, char *targetfrag
);
193 static void html_fragment(htmloutput
*ho
, char const *fragment
);
195 static char *html_format(paragraph
*p
, char *template_string
);
196 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
199 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
200 htmlfile
*thisfile
, keywordlist
*keywords
,
202 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
203 htmlfile
*thisfile
, keywordlist
*keywords
,
204 htmlconfig
*cfg
, int real
);
206 static htmlconfig
html_configure(paragraph
*source
) {
214 ret
.achapter
.just_numbers
= FALSE
;
215 ret
.achapter
.number_suffix
= L
": ";
217 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
218 ret
.asect
[0].just_numbers
= TRUE
;
219 ret
.asect
[0].number_suffix
= L
" ";
221 ret
.contents_depths
= 0;
222 ret
.visible_version_id
= TRUE
;
223 ret
.address_section
= TRUE
;
224 ret
.leaf_contains_contents
= FALSE
;
225 ret
.leaf_smallest_contents
= 4;
226 ret
.single_filename
= dupstr("Manual.html");
227 ret
.contents_filename
= dupstr("Contents.html");
228 ret
.index_filename
= dupstr("IndexPage.html");
229 ret
.template_filename
= dupstr("%n.html");
230 ret
.template_fragment
= dupstr("%b");
231 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
232 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
233 ret
.author
= ret
.description
= NULL
;
234 ret
.restrict_charset
= CS_UTF8
;
235 ret
.output_charset
= CS_ASCII
;
236 ret
.htmlver
= HTML_4
;
237 ret
.index_text
= L
"Index";
238 ret
.contents_text
= L
"Contents";
239 ret
.preamble_text
= L
"Preamble";
240 ret
.title_separator
= L
" - ";
241 ret
.nav_prev_text
= L
"Previous";
242 ret
.nav_next_text
= L
"Next";
243 ret
.nav_separator
= L
" | ";
244 ret
.index_main_sep
= L
": ";
245 ret
.index_multi_sep
= L
", ";
246 ret
.pre_versionid
= L
"[";
247 ret
.post_versionid
= L
"]";
249 * Default quote characters are Unicode matched single quotes,
250 * falling back to ordinary ASCII ".
252 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
253 ret
.rquote
= uadv(ret
.lquote
);
256 * Two-pass configuration so that we can pick up global config
257 * (e.g. `quotes') before having it overridden by specific
258 * config (`html-quotes'), irrespective of the order in which
261 for (p
= source
; p
; p
= p
->next
) {
262 if (p
->type
== para_Config
) {
263 if (!ustricmp(p
->keyword
, L
"quotes")) {
264 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
265 ret
.lquote
= uadv(p
->keyword
);
266 ret
.rquote
= uadv(ret
.lquote
);
272 for (p
= source
; p
; p
= p
->next
) {
273 if (p
->type
== para_Config
) {
274 wchar_t *k
= p
->keyword
;
276 if (!ustrnicmp(k
, L
"xhtml-", 6))
277 k
++; /* treat `xhtml-' and `html-' the same */
279 if (!ustricmp(k
, L
"html-restrict-charset")) {
280 char *csname
= utoa_dup(uadv(k
), CS_ASCII
);
281 ret
.restrict_charset
= charset_from_localenc(csname
);
283 } else if (!ustricmp(k
, L
"html-output-charset")) {
284 char *csname
= utoa_dup(uadv(k
), CS_ASCII
);
285 ret
.output_charset
= charset_from_localenc(csname
);
287 } else if (!ustricmp(k
, L
"html-version")) {
288 wchar_t *vername
= uadv(k
);
289 static const struct {
293 {L
"html3.2", HTML_3_2
},
295 {L
"iso-html", ISO_HTML
},
296 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
297 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
301 for (i
= 0; i
< (int)lenof(versions
); i
++)
302 if (!ustricmp(versions
[i
].name
, vername
))
305 if (i
== lenof(versions
))
306 error(err_htmlver
, &p
->fpos
, vername
);
308 ret
.htmlver
= versions
[i
].ver
;
309 } else if (!ustricmp(k
, L
"html-single-filename")) {
310 sfree(ret
.single_filename
);
311 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
312 } else if (!ustricmp(k
, L
"html-contents-filename")) {
313 sfree(ret
.contents_filename
);
314 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
315 } else if (!ustricmp(k
, L
"html-index-filename")) {
316 sfree(ret
.index_filename
);
317 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
318 } else if (!ustricmp(k
, L
"html-template-filename")) {
319 sfree(ret
.template_filename
);
320 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
321 } else if (!ustricmp(k
, L
"html-template-fragment")) {
322 sfree(ret
.template_fragment
);
323 ret
.template_fragment
= dupstr(adv(p
->origkeyword
));
324 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
325 ret
.achapter
.just_numbers
= utob(uadv(k
));
326 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
327 ret
.achapter
.number_suffix
= uadv(k
);
328 } else if (!ustricmp(k
, L
"html-leaf-level")) {
329 ret
.leaf_level
= utoi(uadv(k
));
330 } else if (!ustricmp(k
, L
"html-section-numeric")) {
331 wchar_t *q
= uadv(k
);
337 if (n
>= ret
.nasect
) {
339 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
340 for (i
= ret
.nasect
; i
<= n
; i
++)
341 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
344 ret
.asect
[n
].just_numbers
= utob(q
);
345 } else if (!ustricmp(k
, L
"html-section-suffix")) {
346 wchar_t *q
= uadv(k
);
352 if (n
>= ret
.nasect
) {
354 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
355 for (i
= ret
.nasect
; i
<= n
; i
++) {
356 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
360 ret
.asect
[n
].number_suffix
= q
;
361 } else if (!ustricmp(k
, L
"html-contents-depth") ||
362 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
364 * Relic of old implementation: this directive used
365 * to be written as \cfg{html-contents-depth-3}{2}
366 * rather than the usual Halibut convention of
367 * \cfg{html-contents-depth}{3}{2}. We therefore
370 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
376 if (n
>= ret
.ncdepths
) {
378 ret
.contents_depths
=
379 sresize(ret
.contents_depths
, n
+1, int);
380 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
381 ret
.contents_depths
[i
] = i
+2;
385 ret
.contents_depths
[n
] = utoi(q
);
386 } else if (!ustricmp(k
, L
"html-head-end")) {
387 ret
.head_end
= adv(p
->origkeyword
);
388 } else if (!ustricmp(k
, L
"html-body-tag")) {
389 ret
.body_tag
= adv(p
->origkeyword
);
390 } else if (!ustricmp(k
, L
"html-body-start")) {
391 ret
.body_start
= adv(p
->origkeyword
);
392 } else if (!ustricmp(k
, L
"html-body-end")) {
393 ret
.body_end
= adv(p
->origkeyword
);
394 } else if (!ustricmp(k
, L
"html-address-start")) {
395 ret
.addr_start
= adv(p
->origkeyword
);
396 } else if (!ustricmp(k
, L
"html-address-end")) {
397 ret
.addr_end
= adv(p
->origkeyword
);
398 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
399 ret
.nav_attr
= adv(p
->origkeyword
);
400 } else if (!ustricmp(k
, L
"html-author")) {
401 ret
.author
= uadv(k
);
402 } else if (!ustricmp(k
, L
"html-description")) {
403 ret
.description
= uadv(k
);
404 } else if (!ustricmp(k
, L
"html-suppress-address")) {
405 ret
.address_section
= !utob(uadv(k
));
406 } else if (!ustricmp(k
, L
"html-versionid")) {
407 ret
.visible_version_id
= utob(uadv(k
));
408 } else if (!ustricmp(k
, L
"html-quotes")) {
409 if (*uadv(k
) && *uadv(uadv(k
))) {
410 ret
.lquote
= uadv(k
);
411 ret
.rquote
= uadv(ret
.lquote
);
413 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
414 ret
.leaf_contains_contents
= utob(uadv(k
));
415 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
416 ret
.leaf_smallest_contents
= utoi(uadv(k
));
422 * Now process fallbacks on quote characters.
424 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
425 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
426 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
427 ret
.lquote
= uadv(ret
.rquote
);
428 ret
.rquote
= uadv(ret
.lquote
);
434 paragraph
*html_config_filename(char *filename
)
437 * If the user passes in a single filename as a parameter to
438 * the `--html' command-line option, then we should assume it
439 * to imply _two_ config directives:
440 * \cfg{html-single-filename}{whatever} and
441 * \cfg{html-leaf-level}{0}; the rationale being that the user
442 * wants their output _in that file_.
446 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
447 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
452 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
453 indexdata
*idx
, void *unused
) {
456 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
};
457 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
461 conf
= html_configure(sourceform
);
464 * We're going to make heavy use of paragraphs' private data
465 * fields in the forthcoming code. Clear them first, so we can
466 * reliably tell whether we have auxiliary data for a
467 * particular paragraph.
469 for (p
= sourceform
; p
; p
= p
->next
)
470 p
->private_data
= NULL
;
472 files
.frags
= newtree234(html_fragment_compare
);
475 * Start by figuring out into which file each piece of the
476 * document should be put. We'll do this by inventing an
477 * `htmlsect' structure and stashing it in the private_data
478 * field of each section paragraph; we also need one additional
479 * htmlsect for the document index, which won't show up in the
480 * source form but needs to be consistently mentioned in
483 * While we're here, we'll also invent the HTML fragment name
487 htmlsect
*topsect
, *sect
;
490 topsect
= html_new_sect(§s
, NULL
);
492 topsect
->title
= NULL
;
493 topsect
->text
= sourceform
;
494 topsect
->contents_depth
= contents_depth(conf
, 0);
495 html_file_section(&conf
, &files
, topsect
, -1);
496 topsect
->fragment
= NULL
;
498 for (p
= sourceform
; p
; p
= p
->next
)
499 if (is_heading_type(p
->type
)) {
500 d
= heading_depth(p
);
502 if (p
->type
== para_Title
) {
507 sect
= html_new_sect(§s
, p
);
508 sect
->text
= p
->next
;
510 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
513 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
514 assert(sect
->parent
!= NULL
);
516 sect
->parent
= topsect
;
517 p
->private_data
= sect
;
519 html_file_section(&conf
, &files
, sect
, d
);
521 sect
->fragment
= html_format(p
, conf
.template_fragment
);
522 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
527 sect
= html_new_sect(§s
, NULL
);
530 sect
->parent
= topsect
;
531 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
532 sect
->fragment
= utoa_dup(conf
.index_text
, CS_ASCII
);
533 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
535 files
.index
= sect
->file
;
539 * Go through the keyword list and sort out fragment IDs for
540 * all the potentially referenced paragraphs which _aren't_
548 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
549 paragraph
*q
, *p
= kw
->para
;
551 if (!is_heading_type(p
->type
)) {
555 * Find the paragraph's parent htmlsect, to
556 * determine which file it will end up in.
561 * Preamble paragraphs have no parent. So if we
562 * have a non-heading with no parent, it must
563 * be preamble, and therefore its parent
564 * htmlsect must be the preamble one.
567 sects
.head
->type
== TOP
);
570 parent
= (htmlsect
*)q
->private_data
;
573 * Now we can construct an htmlsect for this
574 * paragraph itself, taking care to put it in the
575 * list of non-sections rather than the list of
576 * sections (so that traverses of the `sects' list
577 * won't attempt to add it to the contents or
578 * anything weird like that).
580 sect
= html_new_sect(&nonsects
, p
);
581 sect
->file
= parent
->file
;
582 sect
->parent
= parent
;
583 p
->private_data
= sect
;
586 * Fragment IDs for these paragraphs will simply be
587 * `p' followed by an integer.
589 sect
->fragment
= snewn(40, char);
590 sprintf(sect
->fragment
, "p%d",
591 sect
->file
->last_fragment_number
++);
592 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
599 * Reset the fragment numbers in each file. I've just used them
600 * to generate `p' fragment IDs for non-section paragraphs
601 * (numbered list elements, bibliocited), and now I want to use
602 * them for `i' fragment IDs for index entries.
606 for (file
= files
.head
; file
; file
= file
->next
)
607 file
->last_fragment_number
= 0;
611 * Now sort out the index. This involves:
613 * - For each index term, we set up an htmlindex structure to
614 * store all the references to that term.
616 * - Then we make a pass over the actual document, finding
617 * every word_IndexRef; for each one, we actually figure out
618 * the HTML filename/fragment pair we will use to reference
619 * it, store that information in the private data field of
620 * the word_IndexRef itself (so we can recreate it when the
621 * time comes to output our HTML), and add a reference to it
622 * to the index term in question.
631 * Set up the htmlindex structures.
634 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
635 htmlindex
*hi
= snew(htmlindex
);
637 hi
->nrefs
= hi
->refsize
= 0;
640 entry
->backend_data
= hi
;
644 * Run over the document inventing fragments. Each fragment
645 * is of the form `i' followed by an integer.
647 lastsect
= sects
.head
; /* this is always the top section */
648 for (p
= sourceform
; p
; p
= p
->next
) {
649 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
650 lastsect
= (htmlsect
*)p
->private_data
;
652 for (w
= p
->words
; w
; w
= w
->next
)
653 if (w
->type
== word_IndexRef
) {
654 htmlindexref
*hr
= snew(htmlindexref
);
658 hr
->referenced
= hr
->generated
= FALSE
;
659 hr
->section
= lastsect
;
663 lastsect
->file
->last_fragment_number
++);
664 hr
->fragment
= dupstr(buf
);
666 html_sanitise_fragment(&files
, hr
->section
->file
,
669 w
->private_data
= hr
;
671 tag
= index_findtag(idx
, w
->text
);
675 for (i
= 0; i
< tag
->nrefs
; i
++) {
676 indexentry
*entry
= tag
->refs
[i
];
677 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
679 if (hi
->nrefs
>= hi
->refsize
) {
681 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
684 hi
->refs
[hi
->nrefs
++] = w
;
691 * Now we're ready to write out the actual HTML files.
695 * - we open that file and write its header
696 * - we run down the list of sections
697 * - for each section directly contained within that file, we
698 * output the section text
699 * - for each section which is not in the file but which has a
700 * parent that is, we output a contents entry for the
701 * section if appropriate
702 * - finally, we output the file trailer and close the file.
711 for (f
= files
.head
; f
; f
= f
->next
) {
714 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
715 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
716 struct stackelement
{
717 struct stackelement
*next
;
718 enum LISTTYPE listtype
;
719 enum ITEMTYPE itemtype
;
722 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
723 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
725 ho
.fp
= fopen(f
->filename
, "w");
726 ho
.charset
= conf
.output_charset
;
727 ho
.restrict_charset
= conf
.restrict_charset
;
728 ho
.cstate
= charset_init_state
;
729 ho
.ver
= conf
.htmlver
;
730 ho
.state
= HO_NEUTRAL
;
731 ho
.contents_level
= 0;
734 switch (conf
.htmlver
) {
736 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
737 "HTML 3.2 Final//EN\">\n");
740 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
741 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
745 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
746 "15445:2000//DTD HTML//EN\">\n");
748 case XHTML_1_0_TRANSITIONAL
:
749 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
750 charset_to_mimeenc(conf
.output_charset
));
751 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
752 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
753 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
755 case XHTML_1_0_STRICT
:
756 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
757 charset_to_mimeenc(conf
.output_charset
));
758 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
759 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
760 "DTD/xhtml1-strict.dtd\">\n");
764 element_open(&ho
, "html");
765 if (is_xhtml(conf
.htmlver
)) {
766 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
770 element_open(&ho
, "head");
773 element_empty(&ho
, "meta");
774 element_attr(&ho
, "http-equiv", "content-type");
777 sprintf(buf
, "text/html; charset=%.150s",
778 charset_to_mimeenc(conf
.output_charset
));
779 element_attr(&ho
, "content", buf
);
784 element_empty(&ho
, "meta");
785 element_attr(&ho
, "name", "author");
786 element_attr_w(&ho
, "content", conf
.author
);
790 if (conf
.description
) {
791 element_empty(&ho
, "meta");
792 element_attr(&ho
, "name", "description");
793 element_attr_w(&ho
, "content", conf
.description
);
797 element_open(&ho
, "title");
798 if (f
->first
&& f
->first
->title
) {
799 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
803 if (f
->last
!= f
->first
&& f
->last
->title
) {
804 html_text(&ho
, conf
.title_separator
);
805 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
809 element_close(&ho
, "title");
813 html_raw(&ho
, conf
.head_end
);
815 element_close(&ho
, "head");
819 html_raw(&ho
, conf
.body_tag
);
821 element_open(&ho
, "body");
825 html_raw(&ho
, conf
.body_start
);
828 * Write out a nav bar. Special case: we don't do this
829 * if there is only one file.
831 if (files
.head
!= files
.tail
) {
832 element_open(&ho
, "p");
834 html_raw_as_attr(&ho
, conf
.nav_attr
);
837 element_open(&ho
, "a");
838 element_attr(&ho
, "href", prevf
->filename
);
840 html_text(&ho
, conf
.nav_prev_text
);
842 element_close(&ho
, "a");
844 html_text(&ho
, conf
.nav_separator
);
846 if (f
!= files
.head
) {
847 element_open(&ho
, "a");
848 element_attr(&ho
, "href", files
.head
->filename
);
850 html_text(&ho
, conf
.contents_text
);
852 element_close(&ho
, "a");
854 html_text(&ho
, conf
.nav_separator
);
856 if (f
!= files
.index
) {
857 element_open(&ho
, "a");
858 element_attr(&ho
, "href", files
.index
->filename
);
860 html_text(&ho
, conf
.index_text
);
861 if (f
!= files
.index
)
862 element_close(&ho
, "a");
864 html_text(&ho
, conf
.nav_separator
);
867 element_open(&ho
, "a");
868 element_attr(&ho
, "href", f
->next
->filename
);
870 html_text(&ho
, conf
.nav_next_text
);
872 element_close(&ho
, "a");
874 element_close(&ho
, "p");
880 * Write out a prefix TOC for the file.
882 * We start by going through the section list and
883 * collecting the sections which need to be added to
884 * the contents. On the way, we also test to see if
885 * this file is a leaf file (defined as one which
886 * contains all descendants of any section it
887 * contains), because this will play a part in our
888 * decision on whether or not to _output_ the TOC.
890 * Special case: we absolutely do not do this if we're
891 * in single-file mode.
893 if (files
.head
!= files
.tail
) {
894 int ntoc
= 0, tocsize
= 0;
895 htmlsect
**toc
= NULL
;
898 for (s
= sects
.head
; s
; s
= s
->next
) {
903 * Search up from this section until we find
904 * the highest-level one which belongs in this
909 for (ac
= s
; ac
; ac
= ac
->parent
) {
917 if (s
->file
!= f
&& a
!= NULL
)
921 if (adepth
<= a
->contents_depth
) {
922 if (ntoc
>= tocsize
) {
924 toc
= sresize(toc
, tocsize
, htmlsect
*);
931 if (leaf
&& conf
.leaf_contains_contents
&&
932 ntoc
>= conf
.leaf_smallest_contents
) {
935 for (i
= 0; i
< ntoc
; i
++) {
936 htmlsect
*s
= toc
[i
];
937 int hlevel
= (s
->type
== TOP ?
-1 :
938 s
->type
== INDEX ?
0 :
939 heading_depth(s
->title
))
940 - f
->min_heading_depth
+ 1;
943 html_contents_entry(&ho
, hlevel
, s
,
946 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
951 * Now go through the document and output some real
955 for (s
= sects
.head
; s
; s
= s
->next
) {
958 * This section belongs in this file.
969 * Search up from this section until we find
970 * the highest-level one which belongs in this
975 for (ac
= s
; ac
; ac
= ac
->parent
) {
985 * This section does not belong in this
986 * file, but an ancestor of it does. Write
987 * out a contents table entry, if the depth
988 * doesn't exceed the maximum contents
989 * depth for the ancestor section.
991 if (adepth
<= a
->contents_depth
) {
992 html_contents_entry(&ho
, adepth
, s
,
1002 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1005 * Display the section heading.
1008 hlevel
= (s
->type
== TOP ?
-1 :
1009 s
->type
== INDEX ?
0 :
1010 heading_depth(s
->title
))
1011 - f
->min_heading_depth
+ 1;
1012 assert(hlevel
>= 1);
1013 /* HTML headings only go up to <h6> */
1017 htag
[1] = '0' + hlevel
;
1019 element_open(&ho
, htag
);
1022 * Provide anchor for cross-links to target.
1024 * (Also we'll have to do this separately in
1025 * other paragraph types - NumberedList and
1029 html_fragment(&ho
, s
->fragment
);
1031 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1033 element_close(&ho
, htag
);
1036 * Now display the section text.
1039 stackhead
= snew(struct stackelement
);
1040 stackhead
->next
= NULL
;
1041 stackhead
->listtype
= NOLIST
;
1042 stackhead
->itemtype
= NOITEM
;
1044 for (p
= s
->text
;; p
= p
->next
) {
1045 enum LISTTYPE listtype
;
1046 struct stackelement
*se
;
1049 * Preliminary switch to figure out what
1050 * sort of list we expect to be inside at
1053 * Since p may still be NULL at this point,
1054 * I invent a harmless paragraph type for
1057 switch (p ? p
->type
: para_Normal
) {
1060 case para_Copyright
:
1061 case para_BiblioCited
:
1063 case para_QuotePush
:
1067 case para_UnnumberedChapter
:
1078 case para_NumberedList
:
1082 case para_DescribedThing
:
1083 case para_Description
:
1087 case para_LcontPush
:
1088 se
= snew(struct stackelement
);
1089 se
->next
= stackhead
;
1090 se
->listtype
= NOLIST
;
1091 se
->itemtype
= NOITEM
;
1095 default: /* some totally non-printing para */
1102 * Terminate the most recent list item, if
1103 * any. (We left this until after
1104 * processing LcontPush, since in that case
1105 * the list item won't want to be
1106 * terminated until after the corresponding
1109 if (stackhead
->itemtype
!= NOITEM
) {
1110 element_close(&ho
, itemname(stackhead
->itemtype
));
1113 stackhead
->itemtype
= NOITEM
;
1116 * Terminate the current list, if it's not
1117 * the one we want to be in.
1119 if (listtype
!= stackhead
->listtype
&&
1120 stackhead
->listtype
!= NOLIST
) {
1121 element_close(&ho
, listname(stackhead
->listtype
));
1126 * Leave the loop if our time has come.
1128 if (!p
|| (is_heading_type(p
->type
) &&
1129 p
->type
!= para_Title
))
1130 break; /* end of section text */
1133 * Start a fresh list if necessary.
1135 if (listtype
!= stackhead
->listtype
&&
1137 element_open(&ho
, listname(listtype
));
1139 stackhead
->listtype
= listtype
;
1143 element_empty(&ho
, "hr");
1146 html_codepara(&ho
, p
->words
);
1149 case para_Copyright
:
1150 element_open(&ho
, "p");
1152 html_words(&ho
, p
->words
, ALL
,
1153 f
, keywords
, &conf
);
1155 element_close(&ho
, "p");
1157 case para_BiblioCited
:
1158 element_open(&ho
, "p");
1159 if (p
->private_data
) {
1160 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1161 html_fragment(&ho
, s
->fragment
);
1164 html_words(&ho
, p
->kwtext
, ALL
,
1165 f
, keywords
, &conf
);
1166 html_text(&ho
, L
" ");
1167 html_words(&ho
, p
->words
, ALL
,
1168 f
, keywords
, &conf
);
1170 element_close(&ho
, "p");
1173 case para_NumberedList
:
1174 element_open(&ho
, "li");
1175 if (p
->private_data
) {
1176 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1177 html_fragment(&ho
, s
->fragment
);
1180 stackhead
->itemtype
= LI
;
1181 html_words(&ho
, p
->words
, ALL
,
1182 f
, keywords
, &conf
);
1184 case para_DescribedThing
:
1185 element_open(&ho
, "dt");
1187 stackhead
->itemtype
= DT
;
1188 html_words(&ho
, p
->words
, ALL
,
1189 f
, keywords
, &conf
);
1191 case para_Description
:
1192 element_open(&ho
, "dd");
1194 stackhead
->itemtype
= DD
;
1195 html_words(&ho
, p
->words
, ALL
,
1196 f
, keywords
, &conf
);
1199 case para_QuotePush
:
1200 element_open(&ho
, "blockquote");
1203 element_close(&ho
, "blockquote");
1208 stackhead
= stackhead
->next
;
1215 assert(stackhead
&& !stackhead
->next
);
1219 if (s
->type
== INDEX
) {
1224 * This section is the index. I'll just
1225 * render it as a single paragraph, with a
1226 * colon between the index term and the
1227 * references, and <br> in between each
1230 element_open(&ho
, "p");
1232 for (i
= 0; (entry
=
1233 index234(idx
->entries
, i
)) != NULL
; i
++) {
1234 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1238 element_empty(&ho
, "br");
1241 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1242 f
, keywords
, &conf
);
1244 html_text(&ho
, conf
.index_main_sep
);
1246 for (j
= 0; j
< hi
->nrefs
; j
++) {
1248 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1249 paragraph
*p
= hr
->section
->title
;
1252 html_text(&ho
, conf
.index_multi_sep
);
1254 html_href(&ho
, f
, hr
->section
->file
,
1256 hr
->referenced
= TRUE
;
1258 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1259 f
, keywords
, &conf
);
1260 else if (p
&& p
->words
)
1261 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1262 f
, keywords
, &conf
);
1265 * If there is no title at all,
1266 * this must be because our
1267 * target section is the
1268 * preamble section and there
1269 * is no title. So we use the
1272 html_text(&ho
, conf
.preamble_text
);
1274 element_close(&ho
, "a");
1277 element_close(&ho
, "p");
1282 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1289 int done_version_ids
= FALSE
;
1291 element_empty(&ho
, "hr");
1294 html_raw(&ho
, conf
.body_end
);
1296 if (conf
.address_section
) {
1297 int started
= FALSE
;
1298 if (conf
.htmlver
== ISO_HTML
) {
1300 * The ISO-HTML validator complains if
1301 * there isn't a <div> tag surrounding the
1302 * <address> tag. I'm uncertain of why this
1303 * should be - there appears to be no
1304 * mention of this in the ISO-HTML spec,
1305 * suggesting that it doesn't represent a
1306 * change from HTML 4, but nonetheless the
1307 * HTML 4 validator doesn't seem to mind.
1309 element_open(&ho
, "div");
1311 element_open(&ho
, "address");
1312 if (conf
.addr_start
) {
1313 html_raw(&ho
, conf
.addr_start
);
1317 if (conf
.visible_version_id
) {
1318 for (p
= sourceform
; p
; p
= p
->next
)
1319 if (p
->type
== para_VersionID
) {
1321 element_empty(&ho
, "br");
1323 html_text(&ho
, conf
.pre_versionid
);
1324 html_words(&ho
, p
->words
, NOTHING
,
1325 f
, keywords
, &conf
);
1326 html_text(&ho
, conf
.post_versionid
);
1329 done_version_ids
= TRUE
;
1331 if (conf
.addr_end
) {
1333 element_empty(&ho
, "br");
1334 html_raw(&ho
, conf
.addr_end
);
1336 element_close(&ho
, "address");
1337 if (conf
.htmlver
== ISO_HTML
)
1338 element_close(&ho
, "div");
1341 if (!done_version_ids
) {
1343 * If the user didn't want the version IDs
1344 * visible, I think we still have a duty to put
1345 * them in an HTML comment.
1347 int started
= FALSE
;
1348 for (p
= sourceform
; p
; p
= p
->next
)
1349 if (p
->type
== para_VersionID
) {
1351 html_raw(&ho
, "<!-- version IDs:\n");
1354 html_words(&ho
, p
->words
, NOTHING
,
1355 f
, keywords
, &conf
);
1359 html_raw(&ho
, "-->\n");
1363 element_close(&ho
, "body");
1365 element_close(&ho
, "html");
1372 * Go through and check that no index fragments were referenced
1373 * without being generated, or indeed vice versa.
1375 * (When I actually get round to freeing everything, this can
1376 * probably be the freeing loop as well.)
1378 for (p
= sourceform
; p
; p
= p
->next
) {
1380 for (w
= p
->words
; w
; w
= w
->next
)
1381 if (w
->type
== word_IndexRef
) {
1382 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1384 assert(!hr
->referenced
== !hr
->generated
);
1389 * FIXME: Free all the working data.
1393 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1394 htmlsect
*sect
, int depth
)
1400 * `depth' is derived from the heading_depth() macro at the top
1401 * of this file, which counts title as -1, chapter as 0,
1402 * heading as 1 and subsection as 2. However, the semantics of
1403 * cfg->leaf_level are defined to count chapter as 1, heading
1404 * as 2 etc. So first I increment depth :-(
1408 if (cfg
->leaf_level
== 0) {
1410 * leaf_level==0 is a special case, in which everything is
1411 * put into a single file.
1414 files
->single
= html_new_file(files
, cfg
->single_filename
);
1416 file
= files
->single
;
1419 * If the depth of this section is at or above leaf_level,
1420 * we invent a fresh file and put this section at its head.
1421 * Otherwise, we put it in the same file as its parent
1424 if (ldepth
> cfg
->leaf_level
) {
1426 * We know that sect->parent cannot be NULL. The only
1427 * circumstance in which it can be is if sect is at
1428 * chapter or appendix level, i.e. ldepth==1; and if
1429 * that's the case, then we cannot have entered this
1430 * branch unless cfg->leaf_level==0, in which case we
1431 * would be in the single-file case above and not here
1434 assert(sect
->parent
);
1436 file
= sect
->parent
->file
;
1438 if (sect
->type
== TOP
) {
1439 file
= html_new_file(files
, cfg
->contents_filename
);
1440 } else if (sect
->type
== INDEX
) {
1441 file
= html_new_file(files
, cfg
->index_filename
);
1445 assert(ldepth
> 0 && sect
->title
);
1446 title
= html_format(sect
->title
, cfg
->template_filename
);
1447 file
= html_new_file(files
, title
);
1455 if (file
->min_heading_depth
> depth
) {
1457 * This heading is at a higher level than any heading we
1458 * have so far placed in this file; so we set the `first'
1461 file
->min_heading_depth
= depth
;
1465 if (file
->min_heading_depth
== depth
)
1469 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1471 htmlfile
*ret
= snew(htmlfile
);
1475 list
->tail
->next
= ret
;
1480 ret
->filename
= dupstr(filename
);
1481 ret
->last_fragment_number
= 0;
1482 ret
->min_heading_depth
= INT_MAX
;
1483 ret
->first
= ret
->last
= NULL
;
1488 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
)
1490 htmlsect
*ret
= snew(htmlsect
);
1494 list
->tail
->next
= ret
;
1507 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1508 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1514 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1515 case word_HyperLink
:
1516 if (flags
& LINKS
) {
1517 element_open(ho
, "a");
1518 c
= utoa_dup(w
->text
, CS_ASCII
);
1519 element_attr(ho
, "href", c
);
1523 case word_UpperXref
:
1524 case word_LowerXref
:
1525 if (flags
& LINKS
) {
1526 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1527 paragraph
*p
= kwl
->para
;
1528 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1532 html_href(ho
, file
, s
->file
, s
->fragment
);
1538 element_close(ho
, "a");
1541 if (flags
& INDEXENTS
) {
1542 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1543 html_fragment(ho
, hr
->fragment
);
1544 hr
->generated
= TRUE
;
1551 case word_WhiteSpace
:
1552 case word_EmphSpace
:
1553 case word_CodeSpace
:
1554 case word_WkCodeSpace
:
1556 case word_EmphQuote
:
1557 case word_CodeQuote
:
1558 case word_WkCodeQuote
:
1559 style
= towordstyle(w
->type
);
1560 type
= removeattr(w
->type
);
1561 if (style
== word_Emph
&&
1562 (attraux(w
->aux
) == attr_First
||
1563 attraux(w
->aux
) == attr_Only
) &&
1565 element_open(ho
, "em");
1566 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1567 (attraux(w
->aux
) == attr_First
||
1568 attraux(w
->aux
) == attr_Only
) &&
1570 element_open(ho
, "code");
1572 if (type
== word_WhiteSpace
)
1573 html_text(ho
, L
" ");
1574 else if (type
== word_Quote
) {
1575 if (quoteaux(w
->aux
) == quote_Open
)
1576 html_text(ho
, cfg
->lquote
);
1578 html_text(ho
, cfg
->rquote
);
1580 if (cvt_ok(ho
->restrict_charset
, w
->text
) || !w
->alt
)
1581 html_text(ho
, w
->text
);
1583 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1586 if (style
== word_Emph
&&
1587 (attraux(w
->aux
) == attr_Last
||
1588 attraux(w
->aux
) == attr_Only
) &&
1590 element_close(ho
, "em");
1591 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1592 (attraux(w
->aux
) == attr_Last
||
1593 attraux(w
->aux
) == attr_Only
) &&
1595 element_close(ho
, "code");
1601 static void html_codepara(htmloutput
*ho
, word
*words
)
1603 element_open(ho
, "pre");
1604 element_open(ho
, "code");
1605 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1610 if (words
->next
&& words
->next
->type
== word_Emph
) {
1611 e
= words
->next
->text
;
1612 words
= words
->next
;
1616 while (e
&& *e
&& *t
) {
1620 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1628 element_open(ho
, open_tag
);
1630 html_text_limit(ho
, t
, n
);
1633 element_close(ho
, open_tag
);
1641 element_close(ho
, "code");
1642 element_close(ho
, "pre");
1645 static void html_charset_cleanup(htmloutput
*ho
)
1650 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1651 ho
->charset
, &ho
->cstate
, NULL
);
1653 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1656 static void return_to_neutral(htmloutput
*ho
)
1658 if (ho
->state
== HO_IN_TEXT
) {
1659 html_charset_cleanup(ho
);
1660 } else if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1661 fprintf(ho
->fp
, " />");
1662 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1663 fprintf(ho
->fp
, ">");
1666 ho
->state
= HO_NEUTRAL
;
1669 static void element_open(htmloutput
*ho
, char const *name
)
1671 return_to_neutral(ho
);
1672 fprintf(ho
->fp
, "<%s", name
);
1673 ho
->state
= HO_IN_TAG
;
1676 static void element_close(htmloutput
*ho
, char const *name
)
1678 return_to_neutral(ho
);
1679 fprintf(ho
->fp
, "</%s>", name
);
1680 ho
->state
= HO_NEUTRAL
;
1683 static void element_empty(htmloutput
*ho
, char const *name
)
1685 return_to_neutral(ho
);
1686 fprintf(ho
->fp
, "<%s", name
);
1687 ho
->state
= HO_IN_EMPTY_TAG
;
1690 static void html_nl(htmloutput
*ho
)
1692 return_to_neutral(ho
);
1693 fputc('\n', ho
->fp
);
1696 static void html_raw(htmloutput
*ho
, char *text
)
1698 return_to_neutral(ho
);
1699 fputs(text
, ho
->fp
);
1702 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1704 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1706 fputs(text
, ho
->fp
);
1709 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1711 html_charset_cleanup(ho
);
1712 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1713 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1716 static void element_attr_w(htmloutput
*ho
, char const *name
,
1717 wchar_t const *value
)
1719 html_charset_cleanup(ho
);
1720 fprintf(ho
->fp
, " %s=\"", name
);
1721 html_text_limit_internal(ho
, value
, 0, TRUE
);
1722 html_charset_cleanup(ho
);
1726 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1728 html_text_limit(ho
, text
, 0);
1731 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1733 return_to_neutral(ho
);
1734 html_text_limit_internal(ho
, text
, maxlen
, FALSE
);
1737 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1738 int maxlen
, int quote_quotes
)
1740 int textlen
= ustrlen(text
);
1744 if (maxlen
> 0 && textlen
> maxlen
)
1747 while (textlen
> 0) {
1748 /* Scan ahead for characters we really can't display in HTML. */
1749 int lenbefore
, lenafter
;
1750 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1751 if (text
[lenbefore
] == L
'<' ||
1752 text
[lenbefore
] == L
'>' ||
1753 text
[lenbefore
] == L
'&' ||
1754 (text
[lenbefore
] == L
'"' && quote_quotes
))
1756 lenafter
= lenbefore
;
1757 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1758 ho
->charset
, &ho
->cstate
, &err
);
1759 textlen
-= (lenbefore
- lenafter
);
1761 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1764 * We have encountered a character that cannot be
1765 * displayed in the selected output charset. Therefore,
1766 * we use an HTML numeric entity reference.
1768 assert(textlen
> 0);
1769 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1771 } else if (lenafter
== 0 && textlen
> 0) {
1773 * We have encountered a character which is special to
1777 fprintf(ho
->fp
, "<");
1778 else if (*text
== L
'>')
1779 fprintf(ho
->fp
, ">");
1780 else if (*text
== L
'&')
1781 fprintf(ho
->fp
, "&");
1782 else if (*text
== L
'"')
1783 fprintf(ho
->fp
, """);
1785 assert(!"Can't happen");
1791 static void cleanup(htmloutput
*ho
)
1793 return_to_neutral(ho
);
1797 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
1798 htmlfile
*targetfile
, char *targetfrag
)
1800 rdstringc rs
= { 0, 0, NULL
};
1803 if (targetfile
!= thisfile
)
1804 rdaddsc(&rs
, targetfile
->filename
);
1807 rdaddsc(&rs
, targetfrag
);
1811 element_open(ho
, "a");
1812 element_attr(ho
, "href", url
);
1816 static void html_fragment(htmloutput
*ho
, char const *fragment
)
1818 element_open(ho
, "a");
1819 element_attr(ho
, "name", fragment
);
1820 if (is_xhtml(ho
->ver
))
1821 element_attr(ho
, "id", fragment
);
1822 element_close(ho
, "a");
1825 static char *html_format(paragraph
*p
, char *template_string
)
1829 wchar_t *ws
, wsbuf
[2];
1830 rdstringc rs
= { 0, 0, NULL
};
1832 t
= template_string
;
1834 if (*t
== '%' && t
[1]) {
1848 if (p
->kwtext
&& fmt
== 'n')
1850 else if (p
->kwtext2
&& fmt
== 'b') {
1852 * HTML fragment names must start with a letter, so
1853 * simply `1.2.3' is not adequate. In this case I'm
1854 * going to cheat slightly by prepending the first
1855 * character of the first word of kwtext, so that
1856 * we get `C1' for chapter 1, `S2.3' for section
1859 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
1862 wsbuf
[0] = p
->kwtext
->text
[0];
1865 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
1871 c
= utoa_dup(ws
, CS_ASCII
);
1877 if (removeattr(w
->type
) == word_Normal
) {
1878 c
= utoa_dup(w
->text
, CS_ASCII
);
1889 return rdtrimc(&rs
);
1892 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
1896 * The HTML 4 spec's strictest definition of fragment names (<a
1897 * name> and "id" attributes) says that they `must begin with a
1898 * letter and may be followed by any number of letters, digits,
1899 * hyphens, underscores, colons, and periods'.
1901 * So here we unceremoniously rip out any characters not
1902 * conforming to this limitation.
1904 char *p
= text
, *q
= text
;
1906 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
1908 if ((*q
++ = *p
++) != '\0') {
1910 if ((*p
>='A' && *p
<='Z') ||
1911 (*p
>='a' && *p
<='z') ||
1912 (*p
>='0' && *p
<='9') ||
1913 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
1922 * Now we check for clashes with other fragment names, and
1923 * adjust this one if necessary by appending a hyphen followed
1927 htmlfragment
*frag
= snew(htmlfragment
);
1928 int len
= 0; /* >0 indicates we have resized */
1932 frag
->fragment
= text
;
1934 while (add234(files
->frags
, frag
) != frag
) {
1937 frag
->fragment
= text
= sresize(text
, len
+20, char);
1940 sprintf(text
+ len
, "-%d", ++suffix
);
1947 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
1948 htmlfile
*thisfile
, keywordlist
*keywords
,
1951 while (ho
->contents_level
> depth
) {
1952 element_close(ho
, "ul");
1953 ho
->contents_level
--;
1956 while (ho
->contents_level
< depth
) {
1957 element_open(ho
, "ul");
1958 ho
->contents_level
++;
1964 element_open(ho
, "li");
1965 html_href(ho
, thisfile
, s
->file
, s
->fragment
);
1966 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
1967 element_close(ho
, "a");
1968 element_close(ho
, "li");
1971 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
1972 keywordlist
*keywords
, htmlconfig
*cfg
,
1978 int depth
= heading_depth(s
->title
);
1982 else if (depth
== 0)
1983 sl
= &cfg
->achapter
;
1984 else if (depth
<= cfg
->nasect
)
1985 sl
= &cfg
->asect
[depth
-1];
1987 sl
= &cfg
->asect
[cfg
->nasect
-1];
1991 else if (sl
->just_numbers
)
1992 number
= s
->title
->kwtext2
;
1994 number
= s
->title
->kwtext
;
1997 html_words(ho
, number
, MARKUP
,
1998 thisfile
, keywords
, cfg
);
1999 html_text(ho
, sl
->number_suffix
);
2002 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2003 thisfile
, keywords
, cfg
);
2005 assert(s
->type
!= NORMAL
);
2007 * If we're printing the full document title for _real_ and
2008 * there isn't one, we don't want to print `Preamble' at
2009 * the top of what ought to just be some text. If we need
2010 * it in any other context such as TOCs, we need to print
2013 if (s
->type
== TOP
&& !real
)
2014 html_text(ho
, cfg
->preamble_text
);
2015 else if (s
->type
== INDEX
)
2016 html_text(ho
, cfg
->index_text
);