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.)
21 #define is_heading_type(type) ( (type) == para_Title || \
22 (type) == para_Chapter || \
23 (type) == para_Appendix || \
24 (type) == para_UnnumberedChapter || \
25 (type) == para_Heading || \
26 (type) == para_Subsect)
28 #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
29 (p)->type == para_Heading ? 1 : \
30 (p)->type == para_Title ? -1 : 0 )
34 wchar_t *number_suffix
;
39 sectlevel achapter
, *asect
;
40 int *contents_depths
; /* 0=main, 1=chapter, 2=sect etc */
42 int address_section
, visible_version_id
;
43 int leaf_contains_contents
, leaf_smallest_contents
;
44 char *contents_filename
;
46 char *template_filename
;
47 char *single_filename
;
48 char *template_fragment
;
49 char *head_end
, *body_start
, *body_end
, *addr_start
, *addr_end
;
50 char *body_tag
, *nav_attr
;
51 wchar_t *author
, *description
;
52 wchar_t *index_text
, *contents_text
, *preamble_text
, *title_separator
;
53 wchar_t *nav_prev_text
, *nav_next_text
, *nav_separator
;
54 wchar_t *index_main_sep
, *index_multi_sep
;
55 wchar_t *pre_versionid
, *post_versionid
;
56 int restrict_charset
, output_charset
;
58 HTML_3_2
, HTML_4
, ISO_HTML
,
59 XHTML_1_0_TRANSITIONAL
, XHTML_1_0_STRICT
61 wchar_t *lquote
, *rquote
;
65 #define contents_depth(conf, level) \
66 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
68 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
70 typedef struct htmlfile htmlfile
;
71 typedef struct htmlsect htmlsect
;
76 int last_fragment_number
;
77 int min_heading_depth
;
78 htmlsect
*first
, *last
; /* first/last highest-level sections */
82 htmlsect
*next
, *parent
;
84 paragraph
*title
, *text
;
85 enum { NORMAL
, TOP
, INDEX
} type
;
91 htmlfile
*head
, *tail
;
92 htmlfile
*single
, *index
;
97 htmlsect
*head
, *tail
;
113 int generated
, referenced
;
118 * This level deals with charset conversion, starting and
119 * ending tags, and writing to the file. It's the lexical
123 int charset
, restrict_charset
;
124 charset_state cstate
;
127 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
130 * Stuff beyond here deals with the higher syntactic level: it
131 * tracks how many levels of <ul> are currently open when
132 * producing a contents list, for example.
137 static int html_fragment_compare(void *av
, void *bv
)
139 htmlfragment
*a
= (htmlfragment
*)av
;
140 htmlfragment
*b
= (htmlfragment
*)bv
;
143 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
146 return strcmp(a
->fragment
, b
->fragment
);
149 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
150 htmlsect
*sect
, int depth
);
152 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
153 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
);
155 /* Flags for html_words() flags parameter */
159 #define INDEXENTS 0x04
161 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
162 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
163 static void html_codepara(htmloutput
*ho
, word
*words
);
165 static void element_open(htmloutput
*ho
, char const *name
);
166 static void element_close(htmloutput
*ho
, char const *name
);
167 static void element_empty(htmloutput
*ho
, char const *name
);
168 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
169 static void element_attr_w(htmloutput
*ho
, char const *name
,
170 wchar_t const *value
);
171 static void html_text(htmloutput
*ho
, wchar_t const *str
);
172 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *str
);
173 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
174 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
175 int maxlen
, int quote_quotes
, int nbsp
);
176 static void html_nl(htmloutput
*ho
);
177 static void html_raw(htmloutput
*ho
, char *text
);
178 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
179 static void cleanup(htmloutput
*ho
);
181 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
182 htmlfile
*targetfile
, char *targetfrag
);
183 static void html_fragment(htmloutput
*ho
, char const *fragment
);
185 static char *html_format(paragraph
*p
, char *template_string
);
186 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
189 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
190 htmlfile
*thisfile
, keywordlist
*keywords
,
192 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
193 htmlfile
*thisfile
, keywordlist
*keywords
,
194 htmlconfig
*cfg
, int real
);
196 static htmlconfig
html_configure(paragraph
*source
) {
204 ret
.achapter
.just_numbers
= FALSE
;
205 ret
.achapter
.number_suffix
= L
": ";
207 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
208 ret
.asect
[0].just_numbers
= TRUE
;
209 ret
.asect
[0].number_suffix
= L
" ";
211 ret
.contents_depths
= 0;
212 ret
.visible_version_id
= TRUE
;
213 ret
.address_section
= TRUE
;
214 ret
.leaf_contains_contents
= FALSE
;
215 ret
.leaf_smallest_contents
= 4;
216 ret
.single_filename
= dupstr("Manual.html");
217 ret
.contents_filename
= dupstr("Contents.html");
218 ret
.index_filename
= dupstr("IndexPage.html");
219 ret
.template_filename
= dupstr("%n.html");
220 ret
.template_fragment
= dupstr("%b");
221 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
222 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
223 ret
.author
= ret
.description
= NULL
;
224 ret
.restrict_charset
= CS_UTF8
;
225 ret
.output_charset
= CS_ASCII
;
226 ret
.htmlver
= HTML_4
;
227 ret
.index_text
= L
"Index";
228 ret
.contents_text
= L
"Contents";
229 ret
.preamble_text
= L
"Preamble";
230 ret
.title_separator
= L
" - ";
231 ret
.nav_prev_text
= L
"Previous";
232 ret
.nav_next_text
= L
"Next";
233 ret
.nav_separator
= L
" | ";
234 ret
.index_main_sep
= L
": ";
235 ret
.index_multi_sep
= L
", ";
236 ret
.pre_versionid
= L
"[";
237 ret
.post_versionid
= L
"]";
239 * Default quote characters are Unicode matched single quotes,
240 * falling back to ordinary ASCII ".
242 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
243 ret
.rquote
= uadv(ret
.lquote
);
246 * Two-pass configuration so that we can pick up global config
247 * (e.g. `quotes') before having it overridden by specific
248 * config (`html-quotes'), irrespective of the order in which
251 for (p
= source
; p
; p
= p
->next
) {
252 if (p
->type
== para_Config
) {
253 if (!ustricmp(p
->keyword
, L
"quotes")) {
254 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
255 ret
.lquote
= uadv(p
->keyword
);
256 ret
.rquote
= uadv(ret
.lquote
);
262 for (p
= source
; p
; p
= p
->next
) {
263 if (p
->type
== para_Config
) {
264 wchar_t *k
= p
->keyword
;
266 if (!ustrnicmp(k
, L
"xhtml-", 6))
267 k
++; /* treat `xhtml-' and `html-' the same */
269 if (!ustricmp(k
, L
"html-restrict-charset")) {
270 ret
.restrict_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
271 } else if (!ustricmp(k
, L
"html-output-charset")) {
272 ret
.output_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
273 } else if (!ustricmp(k
, L
"html-version")) {
274 wchar_t *vername
= uadv(k
);
275 static const struct {
279 {L
"html3.2", HTML_3_2
},
281 {L
"iso-html", ISO_HTML
},
282 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
283 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
287 for (i
= 0; i
< (int)lenof(versions
); i
++)
288 if (!ustricmp(versions
[i
].name
, vername
))
291 if (i
== lenof(versions
))
292 error(err_htmlver
, &p
->fpos
, vername
);
294 ret
.htmlver
= versions
[i
].ver
;
295 } else if (!ustricmp(k
, L
"html-single-filename")) {
296 sfree(ret
.single_filename
);
297 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
298 } else if (!ustricmp(k
, L
"html-contents-filename")) {
299 sfree(ret
.contents_filename
);
300 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
301 } else if (!ustricmp(k
, L
"html-index-filename")) {
302 sfree(ret
.index_filename
);
303 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
304 } else if (!ustricmp(k
, L
"html-template-filename")) {
305 sfree(ret
.template_filename
);
306 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
307 } else if (!ustricmp(k
, L
"html-template-fragment")) {
308 sfree(ret
.template_fragment
);
309 ret
.template_fragment
= dupstr(adv(p
->origkeyword
));
310 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
311 ret
.achapter
.just_numbers
= utob(uadv(k
));
312 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
313 ret
.achapter
.number_suffix
= uadv(k
);
314 } else if (!ustricmp(k
, L
"html-leaf-level")) {
315 ret
.leaf_level
= utoi(uadv(k
));
316 } else if (!ustricmp(k
, L
"html-section-numeric")) {
317 wchar_t *q
= uadv(k
);
323 if (n
>= ret
.nasect
) {
325 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
326 for (i
= ret
.nasect
; i
<= n
; i
++)
327 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
330 ret
.asect
[n
].just_numbers
= utob(q
);
331 } else if (!ustricmp(k
, L
"html-section-suffix")) {
332 wchar_t *q
= uadv(k
);
338 if (n
>= ret
.nasect
) {
340 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
341 for (i
= ret
.nasect
; i
<= n
; i
++) {
342 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
346 ret
.asect
[n
].number_suffix
= q
;
347 } else if (!ustricmp(k
, L
"html-contents-depth") ||
348 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
350 * Relic of old implementation: this directive used
351 * to be written as \cfg{html-contents-depth-3}{2}
352 * rather than the usual Halibut convention of
353 * \cfg{html-contents-depth}{3}{2}. We therefore
356 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
362 if (n
>= ret
.ncdepths
) {
364 ret
.contents_depths
=
365 sresize(ret
.contents_depths
, n
+1, int);
366 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
367 ret
.contents_depths
[i
] = i
+2;
371 ret
.contents_depths
[n
] = utoi(q
);
372 } else if (!ustricmp(k
, L
"html-head-end")) {
373 ret
.head_end
= adv(p
->origkeyword
);
374 } else if (!ustricmp(k
, L
"html-body-tag")) {
375 ret
.body_tag
= adv(p
->origkeyword
);
376 } else if (!ustricmp(k
, L
"html-body-start")) {
377 ret
.body_start
= adv(p
->origkeyword
);
378 } else if (!ustricmp(k
, L
"html-body-end")) {
379 ret
.body_end
= adv(p
->origkeyword
);
380 } else if (!ustricmp(k
, L
"html-address-start")) {
381 ret
.addr_start
= adv(p
->origkeyword
);
382 } else if (!ustricmp(k
, L
"html-address-end")) {
383 ret
.addr_end
= adv(p
->origkeyword
);
384 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
385 ret
.nav_attr
= adv(p
->origkeyword
);
386 } else if (!ustricmp(k
, L
"html-author")) {
387 ret
.author
= uadv(k
);
388 } else if (!ustricmp(k
, L
"html-description")) {
389 ret
.description
= uadv(k
);
390 } else if (!ustricmp(k
, L
"html-suppress-address")) {
391 ret
.address_section
= !utob(uadv(k
));
392 } else if (!ustricmp(k
, L
"html-versionid")) {
393 ret
.visible_version_id
= utob(uadv(k
));
394 } else if (!ustricmp(k
, L
"html-quotes")) {
395 if (*uadv(k
) && *uadv(uadv(k
))) {
396 ret
.lquote
= uadv(k
);
397 ret
.rquote
= uadv(ret
.lquote
);
399 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
400 ret
.leaf_contains_contents
= utob(uadv(k
));
401 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
402 ret
.leaf_smallest_contents
= utoi(uadv(k
));
403 } else if (!ustricmp(k
, L
"html-index-text")) {
404 ret
.index_text
= uadv(k
);
405 } else if (!ustricmp(k
, L
"html-contents-text")) {
406 ret
.contents_text
= uadv(k
);
407 } else if (!ustricmp(k
, L
"html-preamble-text")) {
408 ret
.preamble_text
= uadv(k
);
409 } else if (!ustricmp(k
, L
"html-title-separator")) {
410 ret
.title_separator
= uadv(k
);
411 } else if (!ustricmp(k
, L
"html-nav-prev-text")) {
412 ret
.nav_prev_text
= uadv(k
);
413 } else if (!ustricmp(k
, L
"html-nav-next-text")) {
414 ret
.nav_next_text
= uadv(k
);
415 } else if (!ustricmp(k
, L
"html-nav-separator")) {
416 ret
.nav_separator
= uadv(k
);
417 } else if (!ustricmp(k
, L
"html-index-main-separator")) {
418 ret
.index_main_sep
= uadv(k
);
419 } else if (!ustricmp(k
, L
"html-index-multiple-separator")) {
420 ret
.index_multi_sep
= uadv(k
);
421 } else if (!ustricmp(k
, L
"html-pre-versionid")) {
422 ret
.pre_versionid
= uadv(k
);
423 } else if (!ustricmp(k
, L
"html-post-versionid")) {
424 ret
.post_versionid
= 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
)
465 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
};
466 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
471 conf
= html_configure(sourceform
);
474 * We're going to make heavy use of paragraphs' private data
475 * fields in the forthcoming code. Clear them first, so we can
476 * reliably tell whether we have auxiliary data for a
477 * particular paragraph.
479 for (p
= sourceform
; p
; p
= p
->next
)
480 p
->private_data
= NULL
;
482 files
.frags
= newtree234(html_fragment_compare
);
485 * Start by figuring out into which file each piece of the
486 * document should be put. We'll do this by inventing an
487 * `htmlsect' structure and stashing it in the private_data
488 * field of each section paragraph; we also need one additional
489 * htmlsect for the document index, which won't show up in the
490 * source form but needs to be consistently mentioned in
493 * While we're here, we'll also invent the HTML fragment name
497 htmlsect
*topsect
, *sect
;
500 topsect
= html_new_sect(§s
, NULL
);
502 topsect
->title
= NULL
;
503 topsect
->text
= sourceform
;
504 topsect
->contents_depth
= contents_depth(conf
, 0);
505 html_file_section(&conf
, &files
, topsect
, -1);
506 topsect
->fragment
= NULL
;
508 for (p
= sourceform
; p
; p
= p
->next
)
509 if (is_heading_type(p
->type
)) {
510 d
= heading_depth(p
);
512 if (p
->type
== para_Title
) {
517 sect
= html_new_sect(§s
, p
);
518 sect
->text
= p
->next
;
520 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
523 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
524 assert(sect
->parent
!= NULL
);
526 sect
->parent
= topsect
;
527 p
->private_data
= sect
;
529 html_file_section(&conf
, &files
, sect
, d
);
531 sect
->fragment
= html_format(p
, conf
.template_fragment
);
532 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
536 /* And the index, if we have one. */
537 has_index
= (count234(idx
->entries
) > 0);
539 sect
= html_new_sect(§s
, NULL
);
542 sect
->parent
= topsect
;
543 sect
->contents_depth
= 0;
544 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
545 sect
->fragment
= utoa_dup(conf
.index_text
, CS_ASCII
);
546 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
548 files
.index
= sect
->file
;
553 * Go through the keyword list and sort out fragment IDs for
554 * all the potentially referenced paragraphs which _aren't_
562 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
563 paragraph
*q
, *p
= kw
->para
;
565 if (!is_heading_type(p
->type
)) {
569 * Find the paragraph's parent htmlsect, to
570 * determine which file it will end up in.
575 * Preamble paragraphs have no parent. So if we
576 * have a non-heading with no parent, it must
577 * be preamble, and therefore its parent
578 * htmlsect must be the preamble one.
581 sects
.head
->type
== TOP
);
584 parent
= (htmlsect
*)q
->private_data
;
587 * Now we can construct an htmlsect for this
588 * paragraph itself, taking care to put it in the
589 * list of non-sections rather than the list of
590 * sections (so that traverses of the `sects' list
591 * won't attempt to add it to the contents or
592 * anything weird like that).
594 sect
= html_new_sect(&nonsects
, p
);
595 sect
->file
= parent
->file
;
596 sect
->parent
= parent
;
597 p
->private_data
= sect
;
600 * Fragment IDs for these paragraphs will simply be
601 * `p' followed by an integer.
603 sect
->fragment
= snewn(40, char);
604 sprintf(sect
->fragment
, "p%d",
605 sect
->file
->last_fragment_number
++);
606 sect
->fragment
= html_sanitise_fragment(&files
, sect
->file
,
613 * Reset the fragment numbers in each file. I've just used them
614 * to generate `p' fragment IDs for non-section paragraphs
615 * (numbered list elements, bibliocited), and now I want to use
616 * them for `i' fragment IDs for index entries.
620 for (file
= files
.head
; file
; file
= file
->next
)
621 file
->last_fragment_number
= 0;
625 * Now sort out the index. This involves:
627 * - For each index term, we set up an htmlindex structure to
628 * store all the references to that term.
630 * - Then we make a pass over the actual document, finding
631 * every word_IndexRef; for each one, we actually figure out
632 * the HTML filename/fragment pair we will use to reference
633 * it, store that information in the private data field of
634 * the word_IndexRef itself (so we can recreate it when the
635 * time comes to output our HTML), and add a reference to it
636 * to the index term in question.
645 * Set up the htmlindex structures.
648 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
649 htmlindex
*hi
= snew(htmlindex
);
651 hi
->nrefs
= hi
->refsize
= 0;
654 entry
->backend_data
= hi
;
658 * Run over the document inventing fragments. Each fragment
659 * is of the form `i' followed by an integer.
661 lastsect
= sects
.head
; /* this is always the top section */
662 for (p
= sourceform
; p
; p
= p
->next
) {
663 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
664 lastsect
= (htmlsect
*)p
->private_data
;
666 for (w
= p
->words
; w
; w
= w
->next
)
667 if (w
->type
== word_IndexRef
) {
668 htmlindexref
*hr
= snew(htmlindexref
);
672 hr
->referenced
= hr
->generated
= FALSE
;
673 hr
->section
= lastsect
;
677 lastsect
->file
->last_fragment_number
++);
678 hr
->fragment
= dupstr(buf
);
680 html_sanitise_fragment(&files
, hr
->section
->file
,
683 w
->private_data
= hr
;
685 tag
= index_findtag(idx
, w
->text
);
689 for (i
= 0; i
< tag
->nrefs
; i
++) {
690 indexentry
*entry
= tag
->refs
[i
];
691 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
693 if (hi
->nrefs
>= hi
->refsize
) {
695 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
698 hi
->refs
[hi
->nrefs
++] = w
;
705 * Now we're ready to write out the actual HTML files.
709 * - we open that file and write its header
710 * - we run down the list of sections
711 * - for each section directly contained within that file, we
712 * output the section text
713 * - for each section which is not in the file but which has a
714 * parent that is, we output a contents entry for the
715 * section if appropriate
716 * - finally, we output the file trailer and close the file.
725 for (f
= files
.head
; f
; f
= f
->next
) {
728 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
729 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
730 struct stackelement
{
731 struct stackelement
*next
;
732 enum LISTTYPE listtype
;
733 enum ITEMTYPE itemtype
;
736 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
737 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
739 ho
.fp
= fopen(f
->filename
, "w");
740 ho
.charset
= conf
.output_charset
;
741 ho
.restrict_charset
= conf
.restrict_charset
;
742 ho
.cstate
= charset_init_state
;
743 ho
.ver
= conf
.htmlver
;
744 ho
.state
= HO_NEUTRAL
;
745 ho
.contents_level
= 0;
748 switch (conf
.htmlver
) {
750 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
751 "HTML 3.2 Final//EN\">\n");
754 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
755 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
759 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
760 "15445:2000//DTD HTML//EN\">\n");
762 case XHTML_1_0_TRANSITIONAL
:
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 Transitional//EN\"\n\"http://www.w3.org/TR/"
767 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
769 case XHTML_1_0_STRICT
:
770 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
771 charset_to_mimeenc(conf
.output_charset
));
772 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
773 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
774 "DTD/xhtml1-strict.dtd\">\n");
778 element_open(&ho
, "html");
779 if (is_xhtml(conf
.htmlver
)) {
780 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
784 element_open(&ho
, "head");
787 element_empty(&ho
, "meta");
788 element_attr(&ho
, "http-equiv", "content-type");
791 sprintf(buf
, "text/html; charset=%.150s",
792 charset_to_mimeenc(conf
.output_charset
));
793 element_attr(&ho
, "content", buf
);
798 element_empty(&ho
, "meta");
799 element_attr(&ho
, "name", "author");
800 element_attr_w(&ho
, "content", conf
.author
);
804 if (conf
.description
) {
805 element_empty(&ho
, "meta");
806 element_attr(&ho
, "name", "description");
807 element_attr_w(&ho
, "content", conf
.description
);
811 element_open(&ho
, "title");
812 if (f
->first
&& f
->first
->title
) {
813 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
817 if (f
->last
!= f
->first
&& f
->last
->title
) {
818 html_text(&ho
, conf
.title_separator
);
819 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
823 element_close(&ho
, "title");
827 html_raw(&ho
, conf
.head_end
);
829 element_close(&ho
, "head");
833 html_raw(&ho
, conf
.body_tag
);
835 element_open(&ho
, "body");
839 html_raw(&ho
, conf
.body_start
);
842 * Write out a nav bar. Special case: we don't do this
843 * if there is only one file.
845 if (files
.head
!= files
.tail
) {
846 element_open(&ho
, "p");
848 html_raw_as_attr(&ho
, conf
.nav_attr
);
851 element_open(&ho
, "a");
852 element_attr(&ho
, "href", prevf
->filename
);
854 html_text(&ho
, conf
.nav_prev_text
);
856 element_close(&ho
, "a");
858 html_text(&ho
, conf
.nav_separator
);
860 if (f
!= files
.head
) {
861 element_open(&ho
, "a");
862 element_attr(&ho
, "href", files
.head
->filename
);
864 html_text(&ho
, conf
.contents_text
);
866 element_close(&ho
, "a");
868 html_text(&ho
, conf
.nav_separator
);
871 if (f
!= files
.index
) {
872 element_open(&ho
, "a");
873 element_attr(&ho
, "href", files
.index
->filename
);
875 html_text(&ho
, conf
.index_text
);
876 if (f
!= files
.index
)
877 element_close(&ho
, "a");
880 html_text(&ho
, conf
.nav_separator
);
883 element_open(&ho
, "a");
884 element_attr(&ho
, "href", f
->next
->filename
);
886 html_text(&ho
, conf
.nav_next_text
);
888 element_close(&ho
, "a");
890 element_close(&ho
, "p");
896 * Write out a prefix TOC for the file.
898 * We start by going through the section list and
899 * collecting the sections which need to be added to
900 * the contents. On the way, we also test to see if
901 * this file is a leaf file (defined as one which
902 * contains all descendants of any section it
903 * contains), because this will play a part in our
904 * decision on whether or not to _output_ the TOC.
906 * Special case: we absolutely do not do this if we're
907 * in single-file mode.
909 if (files
.head
!= files
.tail
) {
910 int ntoc
= 0, tocsize
= 0;
911 htmlsect
**toc
= NULL
;
914 for (s
= sects
.head
; s
; s
= s
->next
) {
919 * Search up from this section until we find
920 * the highest-level one which belongs in this
925 for (ac
= s
; ac
; ac
= ac
->parent
) {
933 if (s
->file
!= f
&& a
!= NULL
)
937 if (adepth
<= a
->contents_depth
) {
938 if (ntoc
>= tocsize
) {
940 toc
= sresize(toc
, tocsize
, htmlsect
*);
947 if (leaf
&& conf
.leaf_contains_contents
&&
948 ntoc
>= conf
.leaf_smallest_contents
) {
951 for (i
= 0; i
< ntoc
; i
++) {
952 htmlsect
*s
= toc
[i
];
953 int hlevel
= (s
->type
== TOP ?
-1 :
954 s
->type
== INDEX ?
0 :
955 heading_depth(s
->title
))
956 - f
->min_heading_depth
+ 1;
959 html_contents_entry(&ho
, hlevel
, s
,
962 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
967 * Now go through the document and output some real
971 for (s
= sects
.head
; s
; s
= s
->next
) {
974 * This section belongs in this file.
985 * Search up from this section until we find
986 * the highest-level one which belongs in this
991 for (ac
= s
; ac
; ac
= ac
->parent
) {
1001 * This section does not belong in this
1002 * file, but an ancestor of it does. Write
1003 * out a contents table entry, if the depth
1004 * doesn't exceed the maximum contents
1005 * depth for the ancestor section.
1007 if (adepth
<= a
->contents_depth
) {
1008 html_contents_entry(&ho
, adepth
, s
,
1009 f
, keywords
, &conf
);
1018 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1021 * Display the section heading.
1024 hlevel
= (s
->type
== TOP ?
-1 :
1025 s
->type
== INDEX ?
0 :
1026 heading_depth(s
->title
))
1027 - f
->min_heading_depth
+ 1;
1028 assert(hlevel
>= 1);
1029 /* HTML headings only go up to <h6> */
1033 htag
[1] = '0' + hlevel
;
1035 element_open(&ho
, htag
);
1038 * Provide anchor for cross-links to target.
1040 * (Also we'll have to do this separately in
1041 * other paragraph types - NumberedList and
1045 html_fragment(&ho
, s
->fragment
);
1047 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1049 element_close(&ho
, htag
);
1052 * Now display the section text.
1055 stackhead
= snew(struct stackelement
);
1056 stackhead
->next
= NULL
;
1057 stackhead
->listtype
= NOLIST
;
1058 stackhead
->itemtype
= NOITEM
;
1060 for (p
= s
->text
;; p
= p
->next
) {
1061 enum LISTTYPE listtype
;
1062 struct stackelement
*se
;
1065 * Preliminary switch to figure out what
1066 * sort of list we expect to be inside at
1069 * Since p may still be NULL at this point,
1070 * I invent a harmless paragraph type for
1073 switch (p ? p
->type
: para_Normal
) {
1076 case para_Copyright
:
1077 case para_BiblioCited
:
1079 case para_QuotePush
:
1083 case para_UnnumberedChapter
:
1094 case para_NumberedList
:
1098 case para_DescribedThing
:
1099 case para_Description
:
1103 case para_LcontPush
:
1104 se
= snew(struct stackelement
);
1105 se
->next
= stackhead
;
1106 se
->listtype
= NOLIST
;
1107 se
->itemtype
= NOITEM
;
1111 default: /* some totally non-printing para */
1118 * Terminate the most recent list item, if
1119 * any. (We left this until after
1120 * processing LcontPush, since in that case
1121 * the list item won't want to be
1122 * terminated until after the corresponding
1125 if (stackhead
->itemtype
!= NOITEM
) {
1126 element_close(&ho
, itemname(stackhead
->itemtype
));
1129 stackhead
->itemtype
= NOITEM
;
1132 * Terminate the current list, if it's not
1133 * the one we want to be in.
1135 if (listtype
!= stackhead
->listtype
&&
1136 stackhead
->listtype
!= NOLIST
) {
1137 element_close(&ho
, listname(stackhead
->listtype
));
1142 * Leave the loop if our time has come.
1144 if (!p
|| (is_heading_type(p
->type
) &&
1145 p
->type
!= para_Title
))
1146 break; /* end of section text */
1149 * Start a fresh list if necessary.
1151 if (listtype
!= stackhead
->listtype
&&
1153 element_open(&ho
, listname(listtype
));
1155 stackhead
->listtype
= listtype
;
1159 element_empty(&ho
, "hr");
1162 html_codepara(&ho
, p
->words
);
1165 case para_Copyright
:
1166 element_open(&ho
, "p");
1168 html_words(&ho
, p
->words
, ALL
,
1169 f
, keywords
, &conf
);
1171 element_close(&ho
, "p");
1173 case para_BiblioCited
:
1174 element_open(&ho
, "p");
1175 if (p
->private_data
) {
1176 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1177 html_fragment(&ho
, s
->fragment
);
1180 html_words(&ho
, p
->kwtext
, ALL
,
1181 f
, keywords
, &conf
);
1182 html_text(&ho
, L
" ");
1183 html_words(&ho
, p
->words
, ALL
,
1184 f
, keywords
, &conf
);
1186 element_close(&ho
, "p");
1189 case para_NumberedList
:
1190 element_open(&ho
, "li");
1191 if (p
->private_data
) {
1192 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1193 html_fragment(&ho
, s
->fragment
);
1196 stackhead
->itemtype
= LI
;
1197 html_words(&ho
, p
->words
, ALL
,
1198 f
, keywords
, &conf
);
1200 case para_DescribedThing
:
1201 element_open(&ho
, "dt");
1203 stackhead
->itemtype
= DT
;
1204 html_words(&ho
, p
->words
, ALL
,
1205 f
, keywords
, &conf
);
1207 case para_Description
:
1208 element_open(&ho
, "dd");
1210 stackhead
->itemtype
= DD
;
1211 html_words(&ho
, p
->words
, ALL
,
1212 f
, keywords
, &conf
);
1215 case para_QuotePush
:
1216 element_open(&ho
, "blockquote");
1219 element_close(&ho
, "blockquote");
1224 stackhead
= stackhead
->next
;
1231 assert(stackhead
&& !stackhead
->next
);
1235 if (s
->type
== INDEX
) {
1240 * This section is the index. I'll just
1241 * render it as a single paragraph, with a
1242 * colon between the index term and the
1243 * references, and <br> in between each
1246 element_open(&ho
, "p");
1248 for (i
= 0; (entry
=
1249 index234(idx
->entries
, i
)) != NULL
; i
++) {
1250 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1254 element_empty(&ho
, "br");
1257 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1258 f
, keywords
, &conf
);
1260 html_text(&ho
, conf
.index_main_sep
);
1262 for (j
= 0; j
< hi
->nrefs
; j
++) {
1264 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1265 paragraph
*p
= hr
->section
->title
;
1268 html_text(&ho
, conf
.index_multi_sep
);
1270 html_href(&ho
, f
, hr
->section
->file
,
1272 hr
->referenced
= TRUE
;
1274 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1275 f
, keywords
, &conf
);
1276 else if (p
&& p
->words
)
1277 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1278 f
, keywords
, &conf
);
1281 * If there is no title at all,
1282 * this must be because our
1283 * target section is the
1284 * preamble section and there
1285 * is no title. So we use the
1288 html_text(&ho
, conf
.preamble_text
);
1290 element_close(&ho
, "a");
1293 element_close(&ho
, "p");
1298 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1305 int done_version_ids
= FALSE
;
1307 element_empty(&ho
, "hr");
1310 html_raw(&ho
, conf
.body_end
);
1312 if (conf
.address_section
) {
1313 int started
= FALSE
;
1314 if (conf
.htmlver
== ISO_HTML
) {
1316 * The ISO-HTML validator complains if
1317 * there isn't a <div> tag surrounding the
1318 * <address> tag. I'm uncertain of why this
1319 * should be - there appears to be no
1320 * mention of this in the ISO-HTML spec,
1321 * suggesting that it doesn't represent a
1322 * change from HTML 4, but nonetheless the
1323 * HTML 4 validator doesn't seem to mind.
1325 element_open(&ho
, "div");
1327 element_open(&ho
, "address");
1328 if (conf
.addr_start
) {
1329 html_raw(&ho
, conf
.addr_start
);
1333 if (conf
.visible_version_id
) {
1334 for (p
= sourceform
; p
; p
= p
->next
)
1335 if (p
->type
== para_VersionID
) {
1337 element_empty(&ho
, "br");
1339 html_text(&ho
, conf
.pre_versionid
);
1340 html_words(&ho
, p
->words
, NOTHING
,
1341 f
, keywords
, &conf
);
1342 html_text(&ho
, conf
.post_versionid
);
1345 done_version_ids
= TRUE
;
1347 if (conf
.addr_end
) {
1349 element_empty(&ho
, "br");
1350 html_raw(&ho
, conf
.addr_end
);
1352 element_close(&ho
, "address");
1353 if (conf
.htmlver
== ISO_HTML
)
1354 element_close(&ho
, "div");
1357 if (!done_version_ids
) {
1359 * If the user didn't want the version IDs
1360 * visible, I think we still have a duty to put
1361 * them in an HTML comment.
1363 int started
= FALSE
;
1364 for (p
= sourceform
; p
; p
= p
->next
)
1365 if (p
->type
== para_VersionID
) {
1367 html_raw(&ho
, "<!-- version IDs:\n");
1370 html_words(&ho
, p
->words
, NOTHING
,
1371 f
, keywords
, &conf
);
1375 html_raw(&ho
, "-->\n");
1379 element_close(&ho
, "body");
1381 element_close(&ho
, "html");
1388 * Go through and check that no index fragments were referenced
1389 * without being generated, or indeed vice versa.
1391 * (When I actually get round to freeing everything, this can
1392 * probably be the freeing loop as well.)
1394 for (p
= sourceform
; p
; p
= p
->next
) {
1396 for (w
= p
->words
; w
; w
= w
->next
)
1397 if (w
->type
== word_IndexRef
) {
1398 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1400 assert(!hr
->referenced
== !hr
->generated
);
1405 * Free all the working data.
1408 sfree(conf
.single_filename
);
1409 sfree(conf
.contents_filename
);
1410 sfree(conf
.index_filename
);
1411 sfree(conf
.template_filename
);
1412 sfree(conf
.template_fragment
);
1415 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1417 * frag->fragment is dynamically allocated, but will be
1418 * freed when we process the htmlsect structure which
1419 * it is attached to.
1423 freetree234(files
.frags
);
1426 htmlsect
*sect
, *tmp
;
1430 sfree(sect
->fragment
);
1434 sect
= nonsects
.head
;
1437 sfree(sect
->fragment
);
1443 htmlfile
*file
, *tmp
;
1447 sfree(file
->filename
);
1455 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1456 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1463 for (p
= sourceform
; p
; p
= p
->next
)
1464 for (w
= p
->words
; w
; w
= w
->next
)
1465 if (w
->type
== word_IndexRef
) {
1466 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1468 sfree(hr
->fragment
);
1474 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1475 htmlsect
*sect
, int depth
)
1481 * `depth' is derived from the heading_depth() macro at the top
1482 * of this file, which counts title as -1, chapter as 0,
1483 * heading as 1 and subsection as 2. However, the semantics of
1484 * cfg->leaf_level are defined to count chapter as 1, heading
1485 * as 2 etc. So first I increment depth :-(
1489 if (cfg
->leaf_level
== 0) {
1491 * leaf_level==0 is a special case, in which everything is
1492 * put into a single file.
1495 files
->single
= html_new_file(files
, cfg
->single_filename
);
1497 file
= files
->single
;
1500 * If the depth of this section is at or above leaf_level,
1501 * we invent a fresh file and put this section at its head.
1502 * Otherwise, we put it in the same file as its parent
1505 if (ldepth
> cfg
->leaf_level
) {
1507 * We know that sect->parent cannot be NULL. The only
1508 * circumstance in which it can be is if sect is at
1509 * chapter or appendix level, i.e. ldepth==1; and if
1510 * that's the case, then we cannot have entered this
1511 * branch unless cfg->leaf_level==0, in which case we
1512 * would be in the single-file case above and not here
1515 assert(sect
->parent
);
1517 file
= sect
->parent
->file
;
1519 if (sect
->type
== TOP
) {
1520 file
= html_new_file(files
, cfg
->contents_filename
);
1521 } else if (sect
->type
== INDEX
) {
1522 file
= html_new_file(files
, cfg
->index_filename
);
1526 assert(ldepth
> 0 && sect
->title
);
1527 title
= html_format(sect
->title
, cfg
->template_filename
);
1528 file
= html_new_file(files
, title
);
1536 if (file
->min_heading_depth
> depth
) {
1538 * This heading is at a higher level than any heading we
1539 * have so far placed in this file; so we set the `first'
1542 file
->min_heading_depth
= depth
;
1546 if (file
->min_heading_depth
== depth
)
1550 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1552 htmlfile
*ret
= snew(htmlfile
);
1556 list
->tail
->next
= ret
;
1561 ret
->filename
= dupstr(filename
);
1562 ret
->last_fragment_number
= 0;
1563 ret
->min_heading_depth
= INT_MAX
;
1564 ret
->first
= ret
->last
= NULL
;
1569 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
)
1571 htmlsect
*ret
= snew(htmlsect
);
1575 list
->tail
->next
= ret
;
1588 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1589 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1595 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1596 case word_HyperLink
:
1597 if (flags
& LINKS
) {
1598 element_open(ho
, "a");
1599 c
= utoa_dup(w
->text
, CS_ASCII
);
1600 element_attr(ho
, "href", c
);
1604 case word_UpperXref
:
1605 case word_LowerXref
:
1606 if (flags
& LINKS
) {
1607 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1608 paragraph
*p
= kwl
->para
;
1609 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1613 html_href(ho
, file
, s
->file
, s
->fragment
);
1619 element_close(ho
, "a");
1622 if (flags
& INDEXENTS
) {
1623 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1624 html_fragment(ho
, hr
->fragment
);
1625 hr
->generated
= TRUE
;
1632 case word_WhiteSpace
:
1633 case word_EmphSpace
:
1634 case word_CodeSpace
:
1635 case word_WkCodeSpace
:
1637 case word_EmphQuote
:
1638 case word_CodeQuote
:
1639 case word_WkCodeQuote
:
1640 style
= towordstyle(w
->type
);
1641 type
= removeattr(w
->type
);
1642 if (style
== word_Emph
&&
1643 (attraux(w
->aux
) == attr_First
||
1644 attraux(w
->aux
) == attr_Only
) &&
1646 element_open(ho
, "em");
1647 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1648 (attraux(w
->aux
) == attr_First
||
1649 attraux(w
->aux
) == attr_Only
) &&
1651 element_open(ho
, "code");
1653 if (type
== word_WhiteSpace
)
1654 html_text(ho
, L
" ");
1655 else if (type
== word_Quote
) {
1656 if (quoteaux(w
->aux
) == quote_Open
)
1657 html_text(ho
, cfg
->lquote
);
1659 html_text(ho
, cfg
->rquote
);
1661 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
1662 html_text_nbsp(ho
, w
->text
);
1664 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1667 if (style
== word_Emph
&&
1668 (attraux(w
->aux
) == attr_Last
||
1669 attraux(w
->aux
) == attr_Only
) &&
1671 element_close(ho
, "em");
1672 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1673 (attraux(w
->aux
) == attr_Last
||
1674 attraux(w
->aux
) == attr_Only
) &&
1676 element_close(ho
, "code");
1682 static void html_codepara(htmloutput
*ho
, word
*words
)
1684 element_open(ho
, "pre");
1685 element_open(ho
, "code");
1686 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1691 if (words
->next
&& words
->next
->type
== word_Emph
) {
1692 e
= words
->next
->text
;
1693 words
= words
->next
;
1697 while (e
&& *e
&& *t
) {
1701 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1709 element_open(ho
, open_tag
);
1711 html_text_limit(ho
, t
, n
);
1714 element_close(ho
, open_tag
);
1722 element_close(ho
, "code");
1723 element_close(ho
, "pre");
1726 static void html_charset_cleanup(htmloutput
*ho
)
1731 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1732 ho
->charset
, &ho
->cstate
, NULL
);
1734 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1737 static void return_mostly_to_neutral(htmloutput
*ho
)
1739 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1740 fprintf(ho
->fp
, " />");
1741 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1742 fprintf(ho
->fp
, ">");
1745 ho
->state
= HO_NEUTRAL
;
1748 static void return_to_neutral(htmloutput
*ho
)
1750 if (ho
->state
== HO_IN_TEXT
) {
1751 html_charset_cleanup(ho
);
1754 return_mostly_to_neutral(ho
);
1757 static void element_open(htmloutput
*ho
, char const *name
)
1759 return_to_neutral(ho
);
1760 fprintf(ho
->fp
, "<%s", name
);
1761 ho
->state
= HO_IN_TAG
;
1764 static void element_close(htmloutput
*ho
, char const *name
)
1766 return_to_neutral(ho
);
1767 fprintf(ho
->fp
, "</%s>", name
);
1768 ho
->state
= HO_NEUTRAL
;
1771 static void element_empty(htmloutput
*ho
, char const *name
)
1773 return_to_neutral(ho
);
1774 fprintf(ho
->fp
, "<%s", name
);
1775 ho
->state
= HO_IN_EMPTY_TAG
;
1778 static void html_nl(htmloutput
*ho
)
1780 return_to_neutral(ho
);
1781 fputc('\n', ho
->fp
);
1784 static void html_raw(htmloutput
*ho
, char *text
)
1786 return_to_neutral(ho
);
1787 fputs(text
, ho
->fp
);
1790 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1792 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1794 fputs(text
, ho
->fp
);
1797 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1799 html_charset_cleanup(ho
);
1800 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1801 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1804 static void element_attr_w(htmloutput
*ho
, char const *name
,
1805 wchar_t const *value
)
1807 html_charset_cleanup(ho
);
1808 fprintf(ho
->fp
, " %s=\"", name
);
1809 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
1810 html_charset_cleanup(ho
);
1814 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1816 return_mostly_to_neutral(ho
);
1817 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
1820 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
1822 return_mostly_to_neutral(ho
);
1823 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
1826 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1828 return_mostly_to_neutral(ho
);
1829 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
1832 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1833 int maxlen
, int quote_quotes
, int nbsp
)
1835 int textlen
= ustrlen(text
);
1839 if (maxlen
> 0 && textlen
> maxlen
)
1842 while (textlen
> 0) {
1843 /* Scan ahead for characters we really can't display in HTML. */
1844 int lenbefore
, lenafter
;
1845 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1846 if (text
[lenbefore
] == L
'<' ||
1847 text
[lenbefore
] == L
'>' ||
1848 text
[lenbefore
] == L
'&' ||
1849 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
1850 (text
[lenbefore
] == L
' ' && nbsp
))
1852 lenafter
= lenbefore
;
1853 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1854 ho
->charset
, &ho
->cstate
, &err
);
1855 textlen
-= (lenbefore
- lenafter
);
1857 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1860 * We have encountered a character that cannot be
1861 * displayed in the selected output charset. Therefore,
1862 * we use an HTML numeric entity reference.
1864 assert(textlen
> 0);
1865 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1867 } else if (lenafter
== 0 && textlen
> 0) {
1869 * We have encountered a character which is special to
1873 fprintf(ho
->fp
, "<");
1874 else if (*text
== L
'>')
1875 fprintf(ho
->fp
, ">");
1876 else if (*text
== L
'&')
1877 fprintf(ho
->fp
, "&");
1878 else if (*text
== L
'"')
1879 fprintf(ho
->fp
, """);
1880 else if (*text
== L
' ') {
1882 fprintf(ho
->fp
, " ");
1884 assert(!"Can't happen");
1890 static void cleanup(htmloutput
*ho
)
1892 return_to_neutral(ho
);
1896 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
1897 htmlfile
*targetfile
, char *targetfrag
)
1899 rdstringc rs
= { 0, 0, NULL
};
1902 if (targetfile
!= thisfile
)
1903 rdaddsc(&rs
, targetfile
->filename
);
1906 rdaddsc(&rs
, targetfrag
);
1910 element_open(ho
, "a");
1911 element_attr(ho
, "href", url
);
1915 static void html_fragment(htmloutput
*ho
, char const *fragment
)
1917 element_open(ho
, "a");
1918 element_attr(ho
, "name", fragment
);
1919 if (is_xhtml(ho
->ver
))
1920 element_attr(ho
, "id", fragment
);
1921 element_close(ho
, "a");
1924 static char *html_format(paragraph
*p
, char *template_string
)
1928 wchar_t *ws
, wsbuf
[2];
1929 rdstringc rs
= { 0, 0, NULL
};
1931 t
= template_string
;
1933 if (*t
== '%' && t
[1]) {
1947 if (p
->kwtext
&& fmt
== 'n')
1949 else if (p
->kwtext2
&& fmt
== 'b') {
1951 * HTML fragment names must start with a letter, so
1952 * simply `1.2.3' is not adequate. In this case I'm
1953 * going to cheat slightly by prepending the first
1954 * character of the first word of kwtext, so that
1955 * we get `C1' for chapter 1, `S2.3' for section
1958 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
1961 wsbuf
[0] = p
->kwtext
->text
[0];
1964 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
1970 c
= utoa_dup(ws
, CS_ASCII
);
1976 if (removeattr(w
->type
) == word_Normal
) {
1977 c
= utoa_dup(w
->text
, CS_ASCII
);
1988 return rdtrimc(&rs
);
1991 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
1995 * The HTML 4 spec's strictest definition of fragment names (<a
1996 * name> and "id" attributes) says that they `must begin with a
1997 * letter and may be followed by any number of letters, digits,
1998 * hyphens, underscores, colons, and periods'.
2000 * So here we unceremoniously rip out any characters not
2001 * conforming to this limitation.
2003 char *p
= text
, *q
= text
;
2005 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2007 if ((*q
++ = *p
++) != '\0') {
2009 if ((*p
>='A' && *p
<='Z') ||
2010 (*p
>='a' && *p
<='z') ||
2011 (*p
>='0' && *p
<='9') ||
2012 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2021 * Now we check for clashes with other fragment names, and
2022 * adjust this one if necessary by appending a hyphen followed
2026 htmlfragment
*frag
= snew(htmlfragment
);
2027 int len
= 0; /* >0 indicates we have resized */
2031 frag
->fragment
= text
;
2033 while (add234(files
->frags
, frag
) != frag
) {
2036 frag
->fragment
= text
= sresize(text
, len
+20, char);
2039 sprintf(text
+ len
, "-%d", ++suffix
);
2046 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2047 htmlfile
*thisfile
, keywordlist
*keywords
,
2050 while (ho
->contents_level
> depth
) {
2051 element_close(ho
, "ul");
2052 ho
->contents_level
--;
2055 while (ho
->contents_level
< depth
) {
2056 element_open(ho
, "ul");
2057 ho
->contents_level
++;
2063 element_open(ho
, "li");
2064 html_href(ho
, thisfile
, s
->file
, s
->fragment
);
2065 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2066 element_close(ho
, "a");
2067 element_close(ho
, "li");
2070 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2071 keywordlist
*keywords
, htmlconfig
*cfg
,
2077 int depth
= heading_depth(s
->title
);
2081 else if (depth
== 0)
2082 sl
= &cfg
->achapter
;
2083 else if (depth
<= cfg
->nasect
)
2084 sl
= &cfg
->asect
[depth
-1];
2086 sl
= &cfg
->asect
[cfg
->nasect
-1];
2090 else if (sl
->just_numbers
)
2091 number
= s
->title
->kwtext2
;
2093 number
= s
->title
->kwtext
;
2096 html_words(ho
, number
, MARKUP
,
2097 thisfile
, keywords
, cfg
);
2098 html_text(ho
, sl
->number_suffix
);
2101 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2102 thisfile
, keywords
, cfg
);
2104 assert(s
->type
!= NORMAL
);
2106 * If we're printing the full document title for _real_ and
2107 * there isn't one, we don't want to print `Preamble' at
2108 * the top of what ought to just be some text. If we need
2109 * it in any other context such as TOCs, we need to print
2112 if (s
->type
== TOP
&& !real
)
2113 html_text(ho
, cfg
->preamble_text
);
2114 else if (s
->type
== INDEX
)
2115 html_text(ho
, cfg
->index_text
);