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,
19 * * Some means of specifying the distinction between
20 * restrict-charset and output-charset. It seems to me that
21 * `html-charset' is output-charset, and that
22 * restrict-charset usually wants to be either output-charset
23 * or UTF-8 (the latter indicating that any Unicode character
24 * is fair game and it will be specified using &#foo; if it
25 * isn't in output-charset). However, since XHTML defaults to
26 * UTF-8 and it's fiddly to tell it otherwise, it's just
27 * possible that some user may need to set restrict-charset
28 * to their charset of choice while leaving _output_-charset
29 * at UTF-8. Figure out some configuration, and apply it.
31 * - nonbreaking spaces.
33 * - free up all the data we have allocated while running this
43 #define is_heading_type(type) ( (type) == para_Title || \
44 (type) == para_Chapter || \
45 (type) == para_Appendix || \
46 (type) == para_UnnumberedChapter || \
47 (type) == para_Heading || \
48 (type) == para_Subsect)
50 #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
51 (p)->type == para_Heading ? 1 : \
52 (p)->type == para_Title ? -1 : 0 )
56 wchar_t *number_suffix
;
61 sectlevel achapter
, *asect
;
62 int *contents_depths
; /* 0=main, 1=chapter, 2=sect etc */
64 int address_section
, visible_version_id
;
65 int leaf_contains_contents
, leaf_smallest_contents
;
66 char *contents_filename
;
68 char *template_filename
;
69 char *single_filename
;
70 char *template_fragment
;
71 char *head_end
, *body_start
, *body_end
, *addr_start
, *addr_end
;
72 char *body_tag
, *nav_attr
;
73 wchar_t *author
, *description
;
74 wchar_t *index_text
, *contents_text
, *preamble_text
, *title_separator
;
75 wchar_t *nav_prev_text
, *nav_next_text
, *nav_separator
;
76 wchar_t *index_main_sep
, *index_multi_sep
;
77 wchar_t *pre_versionid
, *post_versionid
;
78 int restrict_charset
, output_charset
;
80 HTML_3_2
, HTML_4
, ISO_HTML
,
81 XHTML_1_0_TRANSITIONAL
, XHTML_1_0_STRICT
83 wchar_t *lquote
, *rquote
;
87 #define contents_depth(conf, level) \
88 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
90 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
92 typedef struct htmlfile htmlfile
;
93 typedef struct htmlsect htmlsect
;
98 int last_fragment_number
;
99 int min_heading_depth
;
100 htmlsect
*first
, *last
; /* first/last highest-level sections */
104 htmlsect
*next
, *parent
;
106 paragraph
*title
, *text
;
107 enum { NORMAL
, TOP
, INDEX
} type
;
113 htmlfile
*head
, *tail
;
114 htmlfile
*single
, *index
;
119 htmlsect
*head
, *tail
;
135 int generated
, referenced
;
140 * This level deals with charset conversion, starting and
141 * ending tags, and writing to the file. It's the lexical
146 charset_state cstate
;
149 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
152 * Stuff beyond here deals with the higher syntactic level: it
153 * tracks how many levels of <ul> are currently open when
154 * producing a contents list, for example.
159 static int html_fragment_compare(void *av
, void *bv
)
161 htmlfragment
*a
= (htmlfragment
*)av
;
162 htmlfragment
*b
= (htmlfragment
*)bv
;
165 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
168 return strcmp(a
->fragment
, b
->fragment
);
171 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
172 htmlsect
*sect
, int depth
);
174 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
175 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
);
177 /* Flags for html_words() flags parameter */
181 #define INDEXENTS 0x04
183 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
184 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
185 static void html_codepara(htmloutput
*ho
, word
*words
);
187 static void element_open(htmloutput
*ho
, char const *name
);
188 static void element_close(htmloutput
*ho
, char const *name
);
189 static void element_empty(htmloutput
*ho
, char const *name
);
190 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
191 static void element_attr_w(htmloutput
*ho
, char const *name
,
192 wchar_t const *value
);
193 static void html_text(htmloutput
*ho
, wchar_t const *str
);
194 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
195 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
196 int maxlen
, int quote_quotes
);
197 static void html_nl(htmloutput
*ho
);
198 static void html_raw(htmloutput
*ho
, char *text
);
199 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
200 static void cleanup(htmloutput
*ho
);
202 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
203 htmlfile
*targetfile
, char *targetfrag
);
204 static void html_fragment(htmloutput
*ho
, char const *fragment
);
206 static char *html_format(paragraph
*p
, char *template_string
);
207 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
210 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
211 htmlfile
*thisfile
, keywordlist
*keywords
,
213 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
214 htmlfile
*thisfile
, keywordlist
*keywords
,
215 htmlconfig
*cfg
, int real
);
217 static htmlconfig
html_configure(paragraph
*source
) {
225 ret
.achapter
.just_numbers
= FALSE
;
226 ret
.achapter
.number_suffix
= L
": ";
228 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
229 ret
.asect
[0].just_numbers
= TRUE
;
230 ret
.asect
[0].number_suffix
= L
" ";
232 ret
.contents_depths
= 0;
233 ret
.visible_version_id
= TRUE
;
234 ret
.address_section
= TRUE
;
235 ret
.leaf_contains_contents
= FALSE
;
236 ret
.leaf_smallest_contents
= 4;
237 ret
.single_filename
= dupstr("Manual.html");
238 ret
.contents_filename
= dupstr("Contents.html");
239 ret
.index_filename
= dupstr("IndexPage.html");
240 ret
.template_filename
= dupstr("%n.html");
241 ret
.template_fragment
= dupstr("%b");
242 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
243 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
244 ret
.author
= ret
.description
= NULL
;
245 ret
.restrict_charset
= CS_ASCII
;
246 ret
.output_charset
= CS_ASCII
;
247 ret
.htmlver
= HTML_4
;
248 ret
.index_text
= L
"Index";
249 ret
.contents_text
= L
"Contents";
250 ret
.preamble_text
= L
"Preamble";
251 ret
.title_separator
= L
" - ";
252 ret
.nav_prev_text
= L
"Previous";
253 ret
.nav_next_text
= L
"Next";
254 ret
.nav_separator
= L
" | ";
255 ret
.index_main_sep
= L
": ";
256 ret
.index_multi_sep
= L
", ";
257 ret
.pre_versionid
= L
"[";
258 ret
.post_versionid
= L
"]";
260 * Default quote characters are Unicode matched single quotes,
261 * falling back to ordinary ASCII ".
263 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
264 ret
.rquote
= uadv(ret
.lquote
);
267 * Two-pass configuration so that we can pick up global config
268 * (e.g. `quotes') before having it overridden by specific
269 * config (`html-quotes'), irrespective of the order in which
272 for (p
= source
; p
; p
= p
->next
) {
273 if (p
->type
== para_Config
) {
274 if (!ustricmp(p
->keyword
, L
"quotes")) {
275 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
276 ret
.lquote
= uadv(p
->keyword
);
277 ret
.rquote
= uadv(ret
.lquote
);
283 for (p
= source
; p
; p
= p
->next
) {
284 if (p
->type
== para_Config
) {
285 wchar_t *k
= p
->keyword
;
287 if (!ustrnicmp(k
, L
"xhtml-", 6))
288 k
++; /* treat `xhtml-' and `html-' the same */
290 if (!ustricmp(k
, L
"html-charset")) {
291 char *csname
= utoa_dup(uadv(k
), CS_ASCII
);
292 ret
.restrict_charset
= ret
.output_charset
=
293 charset_from_localenc(csname
);
295 } else if (!ustricmp(k
, L
"html-version")) {
296 wchar_t *vername
= uadv(k
);
297 static const struct {
301 {L
"html3.2", HTML_3_2
},
303 {L
"iso-html", ISO_HTML
},
304 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
305 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
309 for (i
= 0; i
< (int)lenof(versions
); i
++)
310 if (!ustricmp(versions
[i
].name
, vername
))
313 if (i
== lenof(versions
))
314 error(err_htmlver
, &p
->fpos
, vername
);
316 ret
.htmlver
= versions
[i
].ver
;
317 } else if (!ustricmp(k
, L
"html-single-filename")) {
318 sfree(ret
.single_filename
);
319 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
320 } else if (!ustricmp(k
, L
"html-contents-filename")) {
321 sfree(ret
.contents_filename
);
322 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
323 } else if (!ustricmp(k
, L
"html-index-filename")) {
324 sfree(ret
.index_filename
);
325 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
326 } else if (!ustricmp(k
, L
"html-template-filename")) {
327 sfree(ret
.template_filename
);
328 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
329 } else if (!ustricmp(k
, L
"html-template-fragment")) {
330 sfree(ret
.template_fragment
);
331 ret
.template_fragment
= dupstr(adv(p
->origkeyword
));
332 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
333 ret
.achapter
.just_numbers
= utob(uadv(k
));
334 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
335 ret
.achapter
.number_suffix
= uadv(k
);
336 } else if (!ustricmp(k
, L
"html-leaf-level")) {
337 ret
.leaf_level
= utoi(uadv(k
));
338 } else if (!ustricmp(k
, L
"html-section-numeric")) {
339 wchar_t *q
= uadv(k
);
345 if (n
>= ret
.nasect
) {
347 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
348 for (i
= ret
.nasect
; i
<= n
; i
++)
349 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
352 ret
.asect
[n
].just_numbers
= utob(q
);
353 } else if (!ustricmp(k
, L
"html-section-suffix")) {
354 wchar_t *q
= uadv(k
);
360 if (n
>= ret
.nasect
) {
362 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
363 for (i
= ret
.nasect
; i
<= n
; i
++) {
364 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
368 ret
.asect
[n
].number_suffix
= q
;
369 } else if (!ustricmp(k
, L
"html-contents-depth") ||
370 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
372 * Relic of old implementation: this directive used
373 * to be written as \cfg{html-contents-depth-3}{2}
374 * rather than the usual Halibut convention of
375 * \cfg{html-contents-depth}{3}{2}. We therefore
378 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
384 if (n
>= ret
.ncdepths
) {
386 ret
.contents_depths
=
387 sresize(ret
.contents_depths
, n
+1, int);
388 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
389 ret
.contents_depths
[i
] = i
+2;
393 ret
.contents_depths
[n
] = utoi(q
);
394 } else if (!ustricmp(k
, L
"html-head-end")) {
395 ret
.head_end
= adv(p
->origkeyword
);
396 } else if (!ustricmp(k
, L
"html-body-tag")) {
397 ret
.body_tag
= adv(p
->origkeyword
);
398 } else if (!ustricmp(k
, L
"html-body-start")) {
399 ret
.body_start
= adv(p
->origkeyword
);
400 } else if (!ustricmp(k
, L
"html-body-end")) {
401 ret
.body_end
= adv(p
->origkeyword
);
402 } else if (!ustricmp(k
, L
"html-address-start")) {
403 ret
.addr_start
= adv(p
->origkeyword
);
404 } else if (!ustricmp(k
, L
"html-address-end")) {
405 ret
.addr_end
= adv(p
->origkeyword
);
406 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
407 ret
.nav_attr
= adv(p
->origkeyword
);
408 } else if (!ustricmp(k
, L
"html-author")) {
409 ret
.author
= uadv(k
);
410 } else if (!ustricmp(k
, L
"html-description")) {
411 ret
.description
= uadv(k
);
412 } else if (!ustricmp(k
, L
"html-suppress-address")) {
413 ret
.address_section
= !utob(uadv(k
));
414 } else if (!ustricmp(k
, L
"html-versionid")) {
415 ret
.visible_version_id
= utob(uadv(k
));
416 } else if (!ustricmp(k
, L
"html-quotes")) {
417 if (*uadv(k
) && *uadv(uadv(k
))) {
418 ret
.lquote
= uadv(k
);
419 ret
.rquote
= uadv(ret
.lquote
);
421 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
422 ret
.leaf_contains_contents
= utob(uadv(k
));
423 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
424 ret
.leaf_smallest_contents
= utoi(uadv(k
));
430 * Now process fallbacks on quote characters.
432 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
433 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
434 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
435 ret
.lquote
= uadv(ret
.rquote
);
436 ret
.rquote
= uadv(ret
.lquote
);
442 paragraph
*html_config_filename(char *filename
)
445 * If the user passes in a single filename as a parameter to
446 * the `--html' command-line option, then we should assume it
447 * to imply _two_ config directives:
448 * \cfg{html-single-filename}{whatever} and
449 * \cfg{html-leaf-level}{0}; the rationale being that the user
450 * wants their output _in that file_.
454 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
455 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
460 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
461 indexdata
*idx
, void *unused
) {
464 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
};
465 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
469 conf
= html_configure(sourceform
);
472 * We're going to make heavy use of paragraphs' private data
473 * fields in the forthcoming code. Clear them first, so we can
474 * reliably tell whether we have auxiliary data for a
475 * particular paragraph.
477 for (p
= sourceform
; p
; p
= p
->next
)
478 p
->private_data
= NULL
;
480 files
.frags
= newtree234(html_fragment_compare
);
483 * Start by figuring out into which file each piece of the
484 * document should be put. We'll do this by inventing an
485 * `htmlsect' structure and stashing it in the private_data
486 * field of each section paragraph; we also need one additional
487 * htmlsect for the document index, which won't show up in the
488 * source form but needs to be consistently mentioned in
491 * While we're here, we'll also invent the HTML fragment name
495 htmlsect
*topsect
, *sect
;
498 topsect
= html_new_sect(§s
, NULL
);
500 topsect
->title
= NULL
;
501 topsect
->text
= sourceform
;
502 topsect
->contents_depth
= contents_depth(conf
, 0);
503 html_file_section(&conf
, &files
, topsect
, -1);
504 topsect
->fragment
= NULL
;
506 for (p
= sourceform
; p
; p
= p
->next
)
507 if (is_heading_type(p
->type
)) {
508 d
= heading_depth(p
);
510 if (p
->type
== para_Title
) {
515 sect
= html_new_sect(§s
, p
);
516 sect
->text
= p
->next
;
518 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
521 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
522 assert(sect
->parent
!= NULL
);
524 sect
->parent
= topsect
;
525 p
->private_data
= sect
;
527 html_file_section(&conf
, &files
, sect
, d
);
529 sect
->fragment
= html_format(p
, conf
.template_fragment
);
530 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
535 sect
= html_new_sect(§s
, NULL
);
538 sect
->parent
= topsect
;
539 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
540 sect
->fragment
= utoa_dup(conf
.index_text
, CS_ASCII
);
541 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
543 files
.index
= sect
->file
;
547 * Go through the keyword list and sort out fragment IDs for
548 * all the potentially referenced paragraphs which _aren't_
556 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
557 paragraph
*q
, *p
= kw
->para
;
559 if (!is_heading_type(p
->type
)) {
563 * Find the paragraph's parent htmlsect, to
564 * determine which file it will end up in.
569 * Preamble paragraphs have no parent. So if we
570 * have a non-heading with no parent, it must
571 * be preamble, and therefore its parent
572 * htmlsect must be the preamble one.
575 sects
.head
->type
== TOP
);
578 parent
= (htmlsect
*)q
->private_data
;
581 * Now we can construct an htmlsect for this
582 * paragraph itself, taking care to put it in the
583 * list of non-sections rather than the list of
584 * sections (so that traverses of the `sects' list
585 * won't attempt to add it to the contents or
586 * anything weird like that).
588 sect
= html_new_sect(&nonsects
, p
);
589 sect
->file
= parent
->file
;
590 sect
->parent
= parent
;
591 p
->private_data
= sect
;
594 * Fragment IDs for these paragraphs will simply be
595 * `p' followed by an integer.
597 sect
->fragment
= snewn(40, char);
598 sprintf(sect
->fragment
, "p%d",
599 sect
->file
->last_fragment_number
++);
600 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
607 * Reset the fragment numbers in each file. I've just used them
608 * to generate `p' fragment IDs for non-section paragraphs
609 * (numbered list elements, bibliocited), and now I want to use
610 * them for `i' fragment IDs for index entries.
614 for (file
= files
.head
; file
; file
= file
->next
)
615 file
->last_fragment_number
= 0;
619 * Now sort out the index. This involves:
621 * - For each index term, we set up an htmlindex structure to
622 * store all the references to that term.
624 * - Then we make a pass over the actual document, finding
625 * every word_IndexRef; for each one, we actually figure out
626 * the HTML filename/fragment pair we will use to reference
627 * it, store that information in the private data field of
628 * the word_IndexRef itself (so we can recreate it when the
629 * time comes to output our HTML), and add a reference to it
630 * to the index term in question.
639 * Set up the htmlindex structures.
642 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
643 htmlindex
*hi
= snew(htmlindex
);
645 hi
->nrefs
= hi
->refsize
= 0;
648 entry
->backend_data
= hi
;
652 * Run over the document inventing fragments. Each fragment
653 * is of the form `i' followed by an integer.
655 lastsect
= sects
.head
; /* this is always the top section */
656 for (p
= sourceform
; p
; p
= p
->next
) {
657 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
658 lastsect
= (htmlsect
*)p
->private_data
;
660 for (w
= p
->words
; w
; w
= w
->next
)
661 if (w
->type
== word_IndexRef
) {
662 htmlindexref
*hr
= snew(htmlindexref
);
666 hr
->referenced
= hr
->generated
= FALSE
;
667 hr
->section
= lastsect
;
671 lastsect
->file
->last_fragment_number
++);
672 hr
->fragment
= dupstr(buf
);
674 html_sanitise_fragment(&files
, hr
->section
->file
,
677 w
->private_data
= hr
;
679 tag
= index_findtag(idx
, w
->text
);
683 for (i
= 0; i
< tag
->nrefs
; i
++) {
684 indexentry
*entry
= tag
->refs
[i
];
685 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
687 if (hi
->nrefs
>= hi
->refsize
) {
689 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
692 hi
->refs
[hi
->nrefs
++] = w
;
699 * Now we're ready to write out the actual HTML files.
703 * - we open that file and write its header
704 * - we run down the list of sections
705 * - for each section directly contained within that file, we
706 * output the section text
707 * - for each section which is not in the file but which has a
708 * parent that is, we output a contents entry for the
709 * section if appropriate
710 * - finally, we output the file trailer and close the file.
719 for (f
= files
.head
; f
; f
= f
->next
) {
722 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
723 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
724 struct stackelement
{
725 struct stackelement
*next
;
726 enum LISTTYPE listtype
;
727 enum ITEMTYPE itemtype
;
730 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
731 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
733 ho
.fp
= fopen(f
->filename
, "w");
734 ho
.charset
= conf
.output_charset
;
735 ho
.cstate
= charset_init_state
;
736 ho
.ver
= conf
.htmlver
;
737 ho
.state
= HO_NEUTRAL
;
738 ho
.contents_level
= 0;
741 switch (conf
.htmlver
) {
743 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
744 "HTML 3.2 Final//EN\">\n");
747 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
748 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
752 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
753 "15445:2000//DTD HTML//EN\">\n");
755 case XHTML_1_0_TRANSITIONAL
:
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 Transitional//EN\"\n\"http://www.w3.org/TR/"
760 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
762 case XHTML_1_0_STRICT
:
763 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
764 charset_to_mimeenc(conf
.output_charset
));
765 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
766 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
767 "DTD/xhtml1-strict.dtd\">\n");
771 element_open(&ho
, "html");
772 if (is_xhtml(conf
.htmlver
)) {
773 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
777 element_open(&ho
, "head");
780 element_empty(&ho
, "meta");
781 element_attr(&ho
, "http-equiv", "content-type");
784 sprintf(buf
, "text/html; charset=%.150s",
785 charset_to_mimeenc(conf
.output_charset
));
786 element_attr(&ho
, "content", buf
);
791 element_empty(&ho
, "meta");
792 element_attr(&ho
, "name", "author");
793 element_attr_w(&ho
, "content", conf
.author
);
797 if (conf
.description
) {
798 element_empty(&ho
, "meta");
799 element_attr(&ho
, "name", "description");
800 element_attr_w(&ho
, "content", conf
.description
);
804 element_open(&ho
, "title");
805 if (f
->first
&& f
->first
->title
) {
806 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
810 if (f
->last
!= f
->first
&& f
->last
->title
) {
811 html_text(&ho
, conf
.title_separator
);
812 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
816 element_close(&ho
, "title");
820 html_raw(&ho
, conf
.head_end
);
822 element_close(&ho
, "head");
826 html_raw(&ho
, conf
.body_tag
);
828 element_open(&ho
, "body");
832 html_raw(&ho
, conf
.body_start
);
835 * Write out a nav bar. Special case: we don't do this
836 * if there is only one file.
838 if (files
.head
!= files
.tail
) {
839 element_open(&ho
, "p");
841 html_raw_as_attr(&ho
, conf
.nav_attr
);
844 element_open(&ho
, "a");
845 element_attr(&ho
, "href", prevf
->filename
);
847 html_text(&ho
, conf
.nav_prev_text
);
849 element_close(&ho
, "a");
851 html_text(&ho
, conf
.nav_separator
);
853 if (f
!= files
.head
) {
854 element_open(&ho
, "a");
855 element_attr(&ho
, "href", files
.head
->filename
);
857 html_text(&ho
, conf
.contents_text
);
859 element_close(&ho
, "a");
861 html_text(&ho
, conf
.nav_separator
);
863 if (f
!= files
.index
) {
864 element_open(&ho
, "a");
865 element_attr(&ho
, "href", files
.index
->filename
);
867 html_text(&ho
, conf
.index_text
);
868 if (f
!= files
.index
)
869 element_close(&ho
, "a");
871 html_text(&ho
, conf
.nav_separator
);
874 element_open(&ho
, "a");
875 element_attr(&ho
, "href", f
->next
->filename
);
877 html_text(&ho
, conf
.nav_next_text
);
879 element_close(&ho
, "a");
881 element_close(&ho
, "p");
887 * Write out a prefix TOC for the file.
889 * We start by going through the section list and
890 * collecting the sections which need to be added to
891 * the contents. On the way, we also test to see if
892 * this file is a leaf file (defined as one which
893 * contains all descendants of any section it
894 * contains), because this will play a part in our
895 * decision on whether or not to _output_ the TOC.
897 * Special case: we absolutely do not do this if we're
898 * in single-file mode.
900 if (files
.head
!= files
.tail
) {
901 int ntoc
= 0, tocsize
= 0;
902 htmlsect
**toc
= NULL
;
905 for (s
= sects
.head
; s
; s
= s
->next
) {
910 * Search up from this section until we find
911 * the highest-level one which belongs in this
916 for (ac
= s
; ac
; ac
= ac
->parent
) {
924 if (s
->file
!= f
&& a
!= NULL
)
928 if (adepth
<= a
->contents_depth
) {
929 if (ntoc
>= tocsize
) {
931 toc
= sresize(toc
, tocsize
, htmlsect
*);
938 if (leaf
&& conf
.leaf_contains_contents
&&
939 ntoc
>= conf
.leaf_smallest_contents
) {
942 for (i
= 0; i
< ntoc
; i
++) {
943 htmlsect
*s
= toc
[i
];
944 int hlevel
= (s
->type
== TOP ?
-1 :
945 s
->type
== INDEX ?
0 :
946 heading_depth(s
->title
))
947 - f
->min_heading_depth
+ 1;
950 html_contents_entry(&ho
, hlevel
, s
,
953 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
958 * Now go through the document and output some real
962 for (s
= sects
.head
; s
; s
= s
->next
) {
965 * This section belongs in this file.
976 * Search up from this section until we find
977 * the highest-level one which belongs in this
982 for (ac
= s
; ac
; ac
= ac
->parent
) {
992 * This section does not belong in this
993 * file, but an ancestor of it does. Write
994 * out a contents table entry, if the depth
995 * doesn't exceed the maximum contents
996 * depth for the ancestor section.
998 if (adepth
<= a
->contents_depth
) {
999 html_contents_entry(&ho
, adepth
, s
,
1000 f
, keywords
, &conf
);
1009 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1012 * Display the section heading.
1015 hlevel
= (s
->type
== TOP ?
-1 :
1016 s
->type
== INDEX ?
0 :
1017 heading_depth(s
->title
))
1018 - f
->min_heading_depth
+ 1;
1019 assert(hlevel
>= 1);
1020 /* HTML headings only go up to <h6> */
1024 htag
[1] = '0' + hlevel
;
1026 element_open(&ho
, htag
);
1029 * Provide anchor for cross-links to target.
1031 * (Also we'll have to do this separately in
1032 * other paragraph types - NumberedList and
1036 html_fragment(&ho
, s
->fragment
);
1038 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1040 element_close(&ho
, htag
);
1043 * Now display the section text.
1046 stackhead
= snew(struct stackelement
);
1047 stackhead
->next
= NULL
;
1048 stackhead
->listtype
= NOLIST
;
1049 stackhead
->itemtype
= NOITEM
;
1051 for (p
= s
->text
;; p
= p
->next
) {
1052 enum LISTTYPE listtype
;
1053 struct stackelement
*se
;
1056 * Preliminary switch to figure out what
1057 * sort of list we expect to be inside at
1060 * Since p may still be NULL at this point,
1061 * I invent a harmless paragraph type for
1064 switch (p ? p
->type
: para_Normal
) {
1067 case para_Copyright
:
1068 case para_BiblioCited
:
1070 case para_QuotePush
:
1074 case para_UnnumberedChapter
:
1085 case para_NumberedList
:
1089 case para_DescribedThing
:
1090 case para_Description
:
1094 case para_LcontPush
:
1095 se
= snew(struct stackelement
);
1096 se
->next
= stackhead
;
1097 se
->listtype
= NOLIST
;
1098 se
->itemtype
= NOITEM
;
1102 default: /* some totally non-printing para */
1109 * Terminate the most recent list item, if
1110 * any. (We left this until after
1111 * processing LcontPush, since in that case
1112 * the list item won't want to be
1113 * terminated until after the corresponding
1116 if (stackhead
->itemtype
!= NOITEM
) {
1117 element_close(&ho
, itemname(stackhead
->itemtype
));
1120 stackhead
->itemtype
= NOITEM
;
1123 * Terminate the current list, if it's not
1124 * the one we want to be in.
1126 if (listtype
!= stackhead
->listtype
&&
1127 stackhead
->listtype
!= NOLIST
) {
1128 element_close(&ho
, listname(stackhead
->listtype
));
1133 * Leave the loop if our time has come.
1135 if (!p
|| (is_heading_type(p
->type
) &&
1136 p
->type
!= para_Title
))
1137 break; /* end of section text */
1140 * Start a fresh list if necessary.
1142 if (listtype
!= stackhead
->listtype
&&
1144 element_open(&ho
, listname(listtype
));
1146 stackhead
->listtype
= listtype
;
1150 element_empty(&ho
, "hr");
1153 html_codepara(&ho
, p
->words
);
1156 case para_Copyright
:
1157 element_open(&ho
, "p");
1159 html_words(&ho
, p
->words
, ALL
,
1160 f
, keywords
, &conf
);
1162 element_close(&ho
, "p");
1164 case para_BiblioCited
:
1165 element_open(&ho
, "p");
1166 if (p
->private_data
) {
1167 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1168 html_fragment(&ho
, s
->fragment
);
1171 html_words(&ho
, p
->kwtext
, ALL
,
1172 f
, keywords
, &conf
);
1173 html_text(&ho
, L
" ");
1174 html_words(&ho
, p
->words
, ALL
,
1175 f
, keywords
, &conf
);
1177 element_close(&ho
, "p");
1180 case para_NumberedList
:
1181 element_open(&ho
, "li");
1182 if (p
->private_data
) {
1183 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1184 html_fragment(&ho
, s
->fragment
);
1187 stackhead
->itemtype
= LI
;
1188 html_words(&ho
, p
->words
, ALL
,
1189 f
, keywords
, &conf
);
1191 case para_DescribedThing
:
1192 element_open(&ho
, "dt");
1194 stackhead
->itemtype
= DT
;
1195 html_words(&ho
, p
->words
, ALL
,
1196 f
, keywords
, &conf
);
1198 case para_Description
:
1199 element_open(&ho
, "dd");
1201 stackhead
->itemtype
= DD
;
1202 html_words(&ho
, p
->words
, ALL
,
1203 f
, keywords
, &conf
);
1206 case para_QuotePush
:
1207 element_open(&ho
, "blockquote");
1210 element_close(&ho
, "blockquote");
1215 stackhead
= stackhead
->next
;
1222 assert(stackhead
&& !stackhead
->next
);
1226 if (s
->type
== INDEX
) {
1231 * This section is the index. I'll just
1232 * render it as a single paragraph, with a
1233 * colon between the index term and the
1234 * references, and <br> in between each
1237 element_open(&ho
, "p");
1239 for (i
= 0; (entry
=
1240 index234(idx
->entries
, i
)) != NULL
; i
++) {
1241 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1245 element_empty(&ho
, "br");
1248 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1249 f
, keywords
, &conf
);
1251 html_text(&ho
, conf
.index_main_sep
);
1253 for (j
= 0; j
< hi
->nrefs
; j
++) {
1255 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1256 paragraph
*p
= hr
->section
->title
;
1259 html_text(&ho
, conf
.index_multi_sep
);
1261 html_href(&ho
, f
, hr
->section
->file
,
1263 hr
->referenced
= TRUE
;
1265 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1266 f
, keywords
, &conf
);
1267 else if (p
&& p
->words
)
1268 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1269 f
, keywords
, &conf
);
1272 * If there is no title at all,
1273 * this must be because our
1274 * target section is the
1275 * preamble section and there
1276 * is no title. So we use the
1279 html_text(&ho
, conf
.preamble_text
);
1281 element_close(&ho
, "a");
1284 element_close(&ho
, "p");
1289 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1296 int done_version_ids
= FALSE
;
1298 element_empty(&ho
, "hr");
1301 html_raw(&ho
, conf
.body_end
);
1303 if (conf
.address_section
) {
1304 int started
= FALSE
;
1305 if (conf
.htmlver
== ISO_HTML
) {
1307 * The ISO-HTML validator complains if
1308 * there isn't a <div> tag surrounding the
1309 * <address> tag. I'm uncertain of why this
1310 * should be - there appears to be no
1311 * mention of this in the ISO-HTML spec,
1312 * suggesting that it doesn't represent a
1313 * change from HTML 4, but nonetheless the
1314 * HTML 4 validator doesn't seem to mind.
1316 element_open(&ho
, "div");
1318 element_open(&ho
, "address");
1319 if (conf
.addr_start
) {
1320 html_raw(&ho
, conf
.addr_start
);
1324 if (conf
.visible_version_id
) {
1325 for (p
= sourceform
; p
; p
= p
->next
)
1326 if (p
->type
== para_VersionID
) {
1328 element_empty(&ho
, "br");
1330 html_text(&ho
, conf
.pre_versionid
);
1331 html_words(&ho
, p
->words
, NOTHING
,
1332 f
, keywords
, &conf
);
1333 html_text(&ho
, conf
.post_versionid
);
1336 done_version_ids
= TRUE
;
1338 if (conf
.addr_end
) {
1340 element_empty(&ho
, "br");
1341 html_raw(&ho
, conf
.addr_end
);
1343 element_close(&ho
, "address");
1344 if (conf
.htmlver
== ISO_HTML
)
1345 element_close(&ho
, "div");
1348 if (!done_version_ids
) {
1350 * If the user didn't want the version IDs
1351 * visible, I think we still have a duty to put
1352 * them in an HTML comment.
1354 int started
= FALSE
;
1355 for (p
= sourceform
; p
; p
= p
->next
)
1356 if (p
->type
== para_VersionID
) {
1358 html_raw(&ho
, "<!-- version IDs:\n");
1361 html_words(&ho
, p
->words
, NOTHING
,
1362 f
, keywords
, &conf
);
1366 html_raw(&ho
, "-->\n");
1370 element_close(&ho
, "body");
1372 element_close(&ho
, "html");
1379 * Go through and check that no index fragments were referenced
1380 * without being generated, or indeed vice versa.
1382 * (When I actually get round to freeing everything, this can
1383 * probably be the freeing loop as well.)
1385 for (p
= sourceform
; p
; p
= p
->next
) {
1387 for (w
= p
->words
; w
; w
= w
->next
)
1388 if (w
->type
== word_IndexRef
) {
1389 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1391 assert(!hr
->referenced
== !hr
->generated
);
1396 * FIXME: Free all the working data.
1400 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1401 htmlsect
*sect
, int depth
)
1407 * `depth' is derived from the heading_depth() macro at the top
1408 * of this file, which counts title as -1, chapter as 0,
1409 * heading as 1 and subsection as 2. However, the semantics of
1410 * cfg->leaf_level are defined to count chapter as 1, heading
1411 * as 2 etc. So first I increment depth :-(
1415 if (cfg
->leaf_level
== 0) {
1417 * leaf_level==0 is a special case, in which everything is
1418 * put into a single file.
1421 files
->single
= html_new_file(files
, cfg
->single_filename
);
1423 file
= files
->single
;
1426 * If the depth of this section is at or above leaf_level,
1427 * we invent a fresh file and put this section at its head.
1428 * Otherwise, we put it in the same file as its parent
1431 if (ldepth
> cfg
->leaf_level
) {
1433 * We know that sect->parent cannot be NULL. The only
1434 * circumstance in which it can be is if sect is at
1435 * chapter or appendix level, i.e. ldepth==1; and if
1436 * that's the case, then we cannot have entered this
1437 * branch unless cfg->leaf_level==0, in which case we
1438 * would be in the single-file case above and not here
1441 assert(sect
->parent
);
1443 file
= sect
->parent
->file
;
1445 if (sect
->type
== TOP
) {
1446 file
= html_new_file(files
, cfg
->contents_filename
);
1447 } else if (sect
->type
== INDEX
) {
1448 file
= html_new_file(files
, cfg
->index_filename
);
1452 assert(ldepth
> 0 && sect
->title
);
1453 title
= html_format(sect
->title
, cfg
->template_filename
);
1454 file
= html_new_file(files
, title
);
1462 if (file
->min_heading_depth
> depth
) {
1464 * This heading is at a higher level than any heading we
1465 * have so far placed in this file; so we set the `first'
1468 file
->min_heading_depth
= depth
;
1472 if (file
->min_heading_depth
== depth
)
1476 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1478 htmlfile
*ret
= snew(htmlfile
);
1482 list
->tail
->next
= ret
;
1487 ret
->filename
= dupstr(filename
);
1488 ret
->last_fragment_number
= 0;
1489 ret
->min_heading_depth
= INT_MAX
;
1490 ret
->first
= ret
->last
= NULL
;
1495 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
)
1497 htmlsect
*ret
= snew(htmlsect
);
1501 list
->tail
->next
= ret
;
1514 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1515 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1521 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1522 case word_HyperLink
:
1523 if (flags
& LINKS
) {
1524 element_open(ho
, "a");
1525 c
= utoa_dup(w
->text
, CS_ASCII
);
1526 element_attr(ho
, "href", c
);
1530 case word_UpperXref
:
1531 case word_LowerXref
:
1532 if (flags
& LINKS
) {
1533 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1534 paragraph
*p
= kwl
->para
;
1535 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1539 html_href(ho
, file
, s
->file
, s
->fragment
);
1545 element_close(ho
, "a");
1548 if (flags
& INDEXENTS
) {
1549 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1550 html_fragment(ho
, hr
->fragment
);
1551 hr
->generated
= TRUE
;
1558 case word_WhiteSpace
:
1559 case word_EmphSpace
:
1560 case word_CodeSpace
:
1561 case word_WkCodeSpace
:
1563 case word_EmphQuote
:
1564 case word_CodeQuote
:
1565 case word_WkCodeQuote
:
1566 style
= towordstyle(w
->type
);
1567 type
= removeattr(w
->type
);
1568 if (style
== word_Emph
&&
1569 (attraux(w
->aux
) == attr_First
||
1570 attraux(w
->aux
) == attr_Only
) &&
1572 element_open(ho
, "em");
1573 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1574 (attraux(w
->aux
) == attr_First
||
1575 attraux(w
->aux
) == attr_Only
) &&
1577 element_open(ho
, "code");
1579 if (type
== word_WhiteSpace
)
1580 html_text(ho
, L
" ");
1581 else if (type
== word_Quote
) {
1582 if (quoteaux(w
->aux
) == quote_Open
)
1583 html_text(ho
, cfg
->lquote
);
1585 html_text(ho
, cfg
->rquote
);
1587 if (cvt_ok(ho
->charset
, w
->text
) || !w
->alt
)
1588 html_text(ho
, w
->text
);
1590 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1593 if (style
== word_Emph
&&
1594 (attraux(w
->aux
) == attr_Last
||
1595 attraux(w
->aux
) == attr_Only
) &&
1597 element_close(ho
, "em");
1598 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1599 (attraux(w
->aux
) == attr_Last
||
1600 attraux(w
->aux
) == attr_Only
) &&
1602 element_close(ho
, "code");
1608 static void html_codepara(htmloutput
*ho
, word
*words
)
1610 element_open(ho
, "pre");
1611 element_open(ho
, "code");
1612 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1617 if (words
->next
&& words
->next
->type
== word_Emph
) {
1618 e
= words
->next
->text
;
1619 words
= words
->next
;
1623 while (e
&& *e
&& *t
) {
1627 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1635 element_open(ho
, open_tag
);
1637 html_text_limit(ho
, t
, n
);
1640 element_close(ho
, open_tag
);
1648 element_close(ho
, "code");
1649 element_close(ho
, "pre");
1652 static void html_charset_cleanup(htmloutput
*ho
)
1657 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1658 ho
->charset
, &ho
->cstate
, NULL
);
1660 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1663 static void return_to_neutral(htmloutput
*ho
)
1665 if (ho
->state
== HO_IN_TEXT
) {
1666 html_charset_cleanup(ho
);
1667 } else if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1668 fprintf(ho
->fp
, " />");
1669 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1670 fprintf(ho
->fp
, ">");
1673 ho
->state
= HO_NEUTRAL
;
1676 static void element_open(htmloutput
*ho
, char const *name
)
1678 return_to_neutral(ho
);
1679 fprintf(ho
->fp
, "<%s", name
);
1680 ho
->state
= HO_IN_TAG
;
1683 static void element_close(htmloutput
*ho
, char const *name
)
1685 return_to_neutral(ho
);
1686 fprintf(ho
->fp
, "</%s>", name
);
1687 ho
->state
= HO_NEUTRAL
;
1690 static void element_empty(htmloutput
*ho
, char const *name
)
1692 return_to_neutral(ho
);
1693 fprintf(ho
->fp
, "<%s", name
);
1694 ho
->state
= HO_IN_EMPTY_TAG
;
1697 static void html_nl(htmloutput
*ho
)
1699 return_to_neutral(ho
);
1700 fputc('\n', ho
->fp
);
1703 static void html_raw(htmloutput
*ho
, char *text
)
1705 return_to_neutral(ho
);
1706 fputs(text
, ho
->fp
);
1709 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1711 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1713 fputs(text
, ho
->fp
);
1716 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1718 html_charset_cleanup(ho
);
1719 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1720 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1723 static void element_attr_w(htmloutput
*ho
, char const *name
,
1724 wchar_t const *value
)
1726 html_charset_cleanup(ho
);
1727 fprintf(ho
->fp
, " %s=\"", name
);
1728 html_text_limit_internal(ho
, value
, 0, TRUE
);
1729 html_charset_cleanup(ho
);
1733 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1735 html_text_limit(ho
, text
, 0);
1738 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1740 return_to_neutral(ho
);
1741 html_text_limit_internal(ho
, text
, maxlen
, FALSE
);
1744 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1745 int maxlen
, int quote_quotes
)
1747 int textlen
= ustrlen(text
);
1751 if (maxlen
> 0 && textlen
> maxlen
)
1754 while (textlen
> 0) {
1755 /* Scan ahead for characters we really can't display in HTML. */
1756 int lenbefore
, lenafter
;
1757 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1758 if (text
[lenbefore
] == L
'<' ||
1759 text
[lenbefore
] == L
'>' ||
1760 text
[lenbefore
] == L
'&' ||
1761 (text
[lenbefore
] == L
'"' && quote_quotes
))
1763 lenafter
= lenbefore
;
1764 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1765 ho
->charset
, &ho
->cstate
, &err
);
1766 textlen
-= (lenbefore
- lenafter
);
1768 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1771 * We have encountered a character that cannot be
1772 * displayed in the selected output charset. Therefore,
1773 * we use an HTML numeric entity reference.
1775 assert(textlen
> 0);
1776 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1778 } else if (lenafter
== 0 && textlen
> 0) {
1780 * We have encountered a character which is special to
1784 fprintf(ho
->fp
, "<");
1785 else if (*text
== L
'>')
1786 fprintf(ho
->fp
, ">");
1787 else if (*text
== L
'&')
1788 fprintf(ho
->fp
, "&");
1789 else if (*text
== L
'"')
1790 fprintf(ho
->fp
, """);
1792 assert(!"Can't happen");
1798 static void cleanup(htmloutput
*ho
)
1800 return_to_neutral(ho
);
1804 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
1805 htmlfile
*targetfile
, char *targetfrag
)
1807 rdstringc rs
= { 0, 0, NULL
};
1810 if (targetfile
!= thisfile
)
1811 rdaddsc(&rs
, targetfile
->filename
);
1814 rdaddsc(&rs
, targetfrag
);
1818 element_open(ho
, "a");
1819 element_attr(ho
, "href", url
);
1823 static void html_fragment(htmloutput
*ho
, char const *fragment
)
1825 element_open(ho
, "a");
1826 element_attr(ho
, "name", fragment
);
1827 if (is_xhtml(ho
->ver
))
1828 element_attr(ho
, "id", fragment
);
1829 element_close(ho
, "a");
1832 static char *html_format(paragraph
*p
, char *template_string
)
1836 wchar_t *ws
, wsbuf
[2];
1837 rdstringc rs
= { 0, 0, NULL
};
1839 t
= template_string
;
1841 if (*t
== '%' && t
[1]) {
1855 if (p
->kwtext
&& fmt
== 'n')
1857 else if (p
->kwtext2
&& fmt
== 'b') {
1859 * HTML fragment names must start with a letter, so
1860 * simply `1.2.3' is not adequate. In this case I'm
1861 * going to cheat slightly by prepending the first
1862 * character of the first word of kwtext, so that
1863 * we get `C1' for chapter 1, `S2.3' for section
1866 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
1869 wsbuf
[0] = p
->kwtext
->text
[0];
1872 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
1878 c
= utoa_dup(ws
, CS_ASCII
);
1884 if (removeattr(w
->type
) == word_Normal
) {
1885 c
= utoa_dup(w
->text
, CS_ASCII
);
1896 return rdtrimc(&rs
);
1899 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
1903 * The HTML 4 spec's strictest definition of fragment names (<a
1904 * name> and "id" attributes) says that they `must begin with a
1905 * letter and may be followed by any number of letters, digits,
1906 * hyphens, underscores, colons, and periods'.
1908 * So here we unceremoniously rip out any characters not
1909 * conforming to this limitation.
1911 char *p
= text
, *q
= text
;
1913 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
1915 if ((*q
++ = *p
++) != '\0') {
1917 if ((*p
>='A' && *p
<='Z') ||
1918 (*p
>='a' && *p
<='z') ||
1919 (*p
>='0' && *p
<='9') ||
1920 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
1929 * Now we check for clashes with other fragment names, and
1930 * adjust this one if necessary by appending a hyphen followed
1934 htmlfragment
*frag
= snew(htmlfragment
);
1935 int len
= 0; /* >0 indicates we have resized */
1939 frag
->fragment
= text
;
1941 while (add234(files
->frags
, frag
) != frag
) {
1944 frag
->fragment
= text
= sresize(text
, len
+20, char);
1947 sprintf(text
+ len
, "-%d", ++suffix
);
1954 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
1955 htmlfile
*thisfile
, keywordlist
*keywords
,
1958 while (ho
->contents_level
> depth
) {
1959 element_close(ho
, "ul");
1960 ho
->contents_level
--;
1963 while (ho
->contents_level
< depth
) {
1964 element_open(ho
, "ul");
1965 ho
->contents_level
++;
1971 element_open(ho
, "li");
1972 html_href(ho
, thisfile
, s
->file
, s
->fragment
);
1973 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
1974 element_close(ho
, "a");
1975 element_close(ho
, "li");
1978 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
1979 keywordlist
*keywords
, htmlconfig
*cfg
,
1985 int depth
= heading_depth(s
->title
);
1989 else if (depth
== 0)
1990 sl
= &cfg
->achapter
;
1991 else if (depth
<= cfg
->nasect
)
1992 sl
= &cfg
->asect
[depth
-1];
1994 sl
= &cfg
->asect
[cfg
->nasect
-1];
1998 else if (sl
->just_numbers
)
1999 number
= s
->title
->kwtext2
;
2001 number
= s
->title
->kwtext
;
2004 html_words(ho
, number
, MARKUP
,
2005 thisfile
, keywords
, cfg
);
2006 html_text(ho
, sl
->number_suffix
);
2009 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2010 thisfile
, keywords
, cfg
);
2012 assert(s
->type
!= NORMAL
);
2014 * If we're printing the full document title for _real_ and
2015 * there isn't one, we don't want to print `Preamble' at
2016 * the top of what ought to just be some text. If we need
2017 * it in any other context such as TOCs, we need to print
2020 if (s
->type
== TOP
&& !real
)
2021 html_text(ho
, cfg
->preamble_text
);
2022 else if (s
->type
== INDEX
)
2023 html_text(ho
, cfg
->index_text
);