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_fragments
;
50 char *head_end
, *body_start
, *body_end
, *addr_start
, *addr_end
;
51 char *body_tag
, *nav_attr
;
52 wchar_t *author
, *description
;
53 wchar_t *index_text
, *contents_text
, *preamble_text
, *title_separator
;
54 wchar_t *nav_prev_text
, *nav_next_text
, *nav_separator
;
55 wchar_t *index_main_sep
, *index_multi_sep
;
56 wchar_t *pre_versionid
, *post_versionid
;
57 int restrict_charset
, output_charset
;
59 HTML_3_2
, HTML_4
, ISO_HTML
,
60 XHTML_1_0_TRANSITIONAL
, XHTML_1_0_STRICT
62 wchar_t *lquote
, *rquote
;
66 #define contents_depth(conf, level) \
67 ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
69 #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
71 typedef struct htmlfile htmlfile
;
72 typedef struct htmlsect htmlsect
;
77 int last_fragment_number
;
78 int min_heading_depth
;
79 htmlsect
*first
, *last
; /* first/last highest-level sections */
83 htmlsect
*next
, *parent
;
85 paragraph
*title
, *text
;
86 enum { NORMAL
, TOP
, INDEX
} type
;
92 htmlfile
*head
, *tail
;
93 htmlfile
*single
, *index
;
98 htmlsect
*head
, *tail
;
114 int generated
, referenced
;
119 * This level deals with charset conversion, starting and
120 * ending tags, and writing to the file. It's the lexical
124 int charset
, restrict_charset
;
125 charset_state cstate
;
128 HO_NEUTRAL
, HO_IN_TAG
, HO_IN_EMPTY_TAG
, HO_IN_TEXT
131 * Stuff beyond here deals with the higher syntactic level: it
132 * tracks how many levels of <ul> are currently open when
133 * producing a contents list, for example.
138 static int html_fragment_compare(void *av
, void *bv
)
140 htmlfragment
*a
= (htmlfragment
*)av
;
141 htmlfragment
*b
= (htmlfragment
*)bv
;
144 if ((cmp
= strcmp(a
->file
->filename
, b
->file
->filename
)) != 0)
147 return strcmp(a
->fragment
, b
->fragment
);
150 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
151 htmlsect
*sect
, int depth
);
153 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
);
154 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
157 /* Flags for html_words() flags parameter */
161 #define INDEXENTS 0x04
163 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
164 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
);
165 static void html_codepara(htmloutput
*ho
, word
*words
);
167 static void element_open(htmloutput
*ho
, char const *name
);
168 static void element_close(htmloutput
*ho
, char const *name
);
169 static void element_empty(htmloutput
*ho
, char const *name
);
170 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
);
171 static void element_attr_w(htmloutput
*ho
, char const *name
,
172 wchar_t const *value
);
173 static void html_text(htmloutput
*ho
, wchar_t const *str
);
174 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *str
);
175 static void html_text_limit(htmloutput
*ho
, wchar_t const *str
, int maxlen
);
176 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
177 int maxlen
, int quote_quotes
, int nbsp
);
178 static void html_nl(htmloutput
*ho
);
179 static void html_raw(htmloutput
*ho
, char *text
);
180 static void html_raw_as_attr(htmloutput
*ho
, char *text
);
181 static void cleanup(htmloutput
*ho
);
183 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
184 htmlfile
*targetfile
, char *targetfrag
);
185 static void html_fragment(htmloutput
*ho
, char const *fragment
);
187 static char *html_format(paragraph
*p
, char *template_string
);
188 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
191 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
192 htmlfile
*thisfile
, keywordlist
*keywords
,
194 static void html_section_title(htmloutput
*ho
, htmlsect
*s
,
195 htmlfile
*thisfile
, keywordlist
*keywords
,
196 htmlconfig
*cfg
, int real
);
198 static htmlconfig
html_configure(paragraph
*source
) {
206 ret
.achapter
.just_numbers
= FALSE
;
207 ret
.achapter
.number_suffix
= L
": ";
209 ret
.asect
= snewn(ret
.nasect
, sectlevel
);
210 ret
.asect
[0].just_numbers
= TRUE
;
211 ret
.asect
[0].number_suffix
= L
" ";
213 ret
.contents_depths
= 0;
214 ret
.visible_version_id
= TRUE
;
215 ret
.address_section
= TRUE
;
216 ret
.leaf_contains_contents
= FALSE
;
217 ret
.leaf_smallest_contents
= 4;
218 ret
.single_filename
= dupstr("Manual.html");
219 ret
.contents_filename
= dupstr("Contents.html");
220 ret
.index_filename
= dupstr("IndexPage.html");
221 ret
.template_filename
= dupstr("%n.html");
223 ret
.template_fragments
= snewn(ret
.ntfragments
, char *);
224 ret
.template_fragments
[0] = dupstr("%b");
225 ret
.head_end
= ret
.body_tag
= ret
.body_start
= ret
.body_end
=
226 ret
.addr_start
= ret
.addr_end
= ret
.nav_attr
= NULL
;
227 ret
.author
= ret
.description
= NULL
;
228 ret
.restrict_charset
= CS_UTF8
;
229 ret
.output_charset
= CS_ASCII
;
230 ret
.htmlver
= HTML_4
;
231 ret
.index_text
= L
"Index";
232 ret
.contents_text
= L
"Contents";
233 ret
.preamble_text
= L
"Preamble";
234 ret
.title_separator
= L
" - ";
235 ret
.nav_prev_text
= L
"Previous";
236 ret
.nav_next_text
= L
"Next";
237 ret
.nav_separator
= L
" | ";
238 ret
.index_main_sep
= L
": ";
239 ret
.index_multi_sep
= L
", ";
240 ret
.pre_versionid
= L
"[";
241 ret
.post_versionid
= L
"]";
243 * Default quote characters are Unicode matched single quotes,
244 * falling back to ordinary ASCII ".
246 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
247 ret
.rquote
= uadv(ret
.lquote
);
250 * Two-pass configuration so that we can pick up global config
251 * (e.g. `quotes') before having it overridden by specific
252 * config (`html-quotes'), irrespective of the order in which
255 for (p
= source
; p
; p
= p
->next
) {
256 if (p
->type
== para_Config
) {
257 if (!ustricmp(p
->keyword
, L
"quotes")) {
258 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
259 ret
.lquote
= uadv(p
->keyword
);
260 ret
.rquote
= uadv(ret
.lquote
);
266 for (p
= source
; p
; p
= p
->next
) {
267 if (p
->type
== para_Config
) {
268 wchar_t *k
= p
->keyword
;
270 if (!ustrnicmp(k
, L
"xhtml-", 6))
271 k
++; /* treat `xhtml-' and `html-' the same */
273 if (!ustricmp(k
, L
"html-restrict-charset")) {
274 ret
.restrict_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
275 } else if (!ustricmp(k
, L
"html-output-charset")) {
276 ret
.output_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
277 } else if (!ustricmp(k
, L
"html-version")) {
278 wchar_t *vername
= uadv(k
);
279 static const struct {
283 {L
"html3.2", HTML_3_2
},
285 {L
"iso-html", ISO_HTML
},
286 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
287 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
291 for (i
= 0; i
< (int)lenof(versions
); i
++)
292 if (!ustricmp(versions
[i
].name
, vername
))
295 if (i
== lenof(versions
))
296 error(err_htmlver
, &p
->fpos
, vername
);
298 ret
.htmlver
= versions
[i
].ver
;
299 } else if (!ustricmp(k
, L
"html-single-filename")) {
300 sfree(ret
.single_filename
);
301 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
302 } else if (!ustricmp(k
, L
"html-contents-filename")) {
303 sfree(ret
.contents_filename
);
304 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
305 } else if (!ustricmp(k
, L
"html-index-filename")) {
306 sfree(ret
.index_filename
);
307 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
308 } else if (!ustricmp(k
, L
"html-template-filename")) {
309 sfree(ret
.template_filename
);
310 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
311 } else if (!ustricmp(k
, L
"html-template-fragment")) {
312 char *frag
= adv(p
->origkeyword
);
314 while (ret
.ntfragments
--)
315 sfree(ret
.template_fragments
[ret
.ntfragments
]);
316 sfree(ret
.template_fragments
);
317 ret
.template_fragments
= NULL
;
321 ret
.template_fragments
=
322 sresize(ret
.template_fragments
,
323 ret
.ntfragments
, char *);
324 ret
.template_fragments
[ret
.ntfragments
-1] =
329 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 1);
330 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
331 ret
.achapter
.just_numbers
= utob(uadv(k
));
332 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
333 ret
.achapter
.number_suffix
= uadv(k
);
334 } else if (!ustricmp(k
, L
"html-leaf-level")) {
335 ret
.leaf_level
= utoi(uadv(k
));
336 } else if (!ustricmp(k
, L
"html-section-numeric")) {
337 wchar_t *q
= uadv(k
);
343 if (n
>= ret
.nasect
) {
345 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
346 for (i
= ret
.nasect
; i
<= n
; i
++)
347 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
350 ret
.asect
[n
].just_numbers
= utob(q
);
351 } else if (!ustricmp(k
, L
"html-section-suffix")) {
352 wchar_t *q
= uadv(k
);
358 if (n
>= ret
.nasect
) {
360 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
361 for (i
= ret
.nasect
; i
<= n
; i
++) {
362 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
366 ret
.asect
[n
].number_suffix
= q
;
367 } else if (!ustricmp(k
, L
"html-contents-depth") ||
368 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
370 * Relic of old implementation: this directive used
371 * to be written as \cfg{html-contents-depth-3}{2}
372 * rather than the usual Halibut convention of
373 * \cfg{html-contents-depth}{3}{2}. We therefore
376 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
382 if (n
>= ret
.ncdepths
) {
384 ret
.contents_depths
=
385 sresize(ret
.contents_depths
, n
+1, int);
386 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
387 ret
.contents_depths
[i
] = i
+2;
391 ret
.contents_depths
[n
] = utoi(q
);
392 } else if (!ustricmp(k
, L
"html-head-end")) {
393 ret
.head_end
= adv(p
->origkeyword
);
394 } else if (!ustricmp(k
, L
"html-body-tag")) {
395 ret
.body_tag
= adv(p
->origkeyword
);
396 } else if (!ustricmp(k
, L
"html-body-start")) {
397 ret
.body_start
= adv(p
->origkeyword
);
398 } else if (!ustricmp(k
, L
"html-body-end")) {
399 ret
.body_end
= adv(p
->origkeyword
);
400 } else if (!ustricmp(k
, L
"html-address-start")) {
401 ret
.addr_start
= adv(p
->origkeyword
);
402 } else if (!ustricmp(k
, L
"html-address-end")) {
403 ret
.addr_end
= adv(p
->origkeyword
);
404 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
405 ret
.nav_attr
= adv(p
->origkeyword
);
406 } else if (!ustricmp(k
, L
"html-author")) {
407 ret
.author
= uadv(k
);
408 } else if (!ustricmp(k
, L
"html-description")) {
409 ret
.description
= uadv(k
);
410 } else if (!ustricmp(k
, L
"html-suppress-address")) {
411 ret
.address_section
= !utob(uadv(k
));
412 } else if (!ustricmp(k
, L
"html-versionid")) {
413 ret
.visible_version_id
= utob(uadv(k
));
414 } else if (!ustricmp(k
, L
"html-quotes")) {
415 if (*uadv(k
) && *uadv(uadv(k
))) {
416 ret
.lquote
= uadv(k
);
417 ret
.rquote
= uadv(ret
.lquote
);
419 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
420 ret
.leaf_contains_contents
= utob(uadv(k
));
421 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
422 ret
.leaf_smallest_contents
= utoi(uadv(k
));
423 } else if (!ustricmp(k
, L
"html-index-text")) {
424 ret
.index_text
= uadv(k
);
425 } else if (!ustricmp(k
, L
"html-contents-text")) {
426 ret
.contents_text
= uadv(k
);
427 } else if (!ustricmp(k
, L
"html-preamble-text")) {
428 ret
.preamble_text
= uadv(k
);
429 } else if (!ustricmp(k
, L
"html-title-separator")) {
430 ret
.title_separator
= uadv(k
);
431 } else if (!ustricmp(k
, L
"html-nav-prev-text")) {
432 ret
.nav_prev_text
= uadv(k
);
433 } else if (!ustricmp(k
, L
"html-nav-next-text")) {
434 ret
.nav_next_text
= uadv(k
);
435 } else if (!ustricmp(k
, L
"html-nav-separator")) {
436 ret
.nav_separator
= uadv(k
);
437 } else if (!ustricmp(k
, L
"html-index-main-separator")) {
438 ret
.index_main_sep
= uadv(k
);
439 } else if (!ustricmp(k
, L
"html-index-multiple-separator")) {
440 ret
.index_multi_sep
= uadv(k
);
441 } else if (!ustricmp(k
, L
"html-pre-versionid")) {
442 ret
.pre_versionid
= uadv(k
);
443 } else if (!ustricmp(k
, L
"html-post-versionid")) {
444 ret
.post_versionid
= uadv(k
);
450 * Now process fallbacks on quote characters.
452 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
453 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
454 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
455 ret
.lquote
= uadv(ret
.rquote
);
456 ret
.rquote
= uadv(ret
.lquote
);
462 paragraph
*html_config_filename(char *filename
)
465 * If the user passes in a single filename as a parameter to
466 * the `--html' command-line option, then we should assume it
467 * to imply _two_ config directives:
468 * \cfg{html-single-filename}{whatever} and
469 * \cfg{html-leaf-level}{0}; the rationale being that the user
470 * wants their output _in that file_.
474 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
475 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
480 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
481 indexdata
*idx
, void *unused
)
485 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
};
486 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
491 conf
= html_configure(sourceform
);
494 * We're going to make heavy use of paragraphs' private data
495 * fields in the forthcoming code. Clear them first, so we can
496 * reliably tell whether we have auxiliary data for a
497 * particular paragraph.
499 for (p
= sourceform
; p
; p
= p
->next
)
500 p
->private_data
= NULL
;
502 files
.frags
= newtree234(html_fragment_compare
);
505 * Start by figuring out into which file each piece of the
506 * document should be put. We'll do this by inventing an
507 * `htmlsect' structure and stashing it in the private_data
508 * field of each section paragraph; we also need one additional
509 * htmlsect for the document index, which won't show up in the
510 * source form but needs to be consistently mentioned in
513 * While we're here, we'll also invent the HTML fragment name(s)
517 htmlsect
*topsect
, *sect
;
520 topsect
= html_new_sect(§s
, NULL
, &conf
);
522 topsect
->title
= NULL
;
523 topsect
->text
= sourceform
;
524 topsect
->contents_depth
= contents_depth(conf
, 0);
525 html_file_section(&conf
, &files
, topsect
, -1);
527 for (p
= sourceform
; p
; p
= p
->next
)
528 if (is_heading_type(p
->type
)) {
529 d
= heading_depth(p
);
531 if (p
->type
== para_Title
) {
536 sect
= html_new_sect(§s
, p
, &conf
);
537 sect
->text
= p
->next
;
539 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
542 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
543 assert(sect
->parent
!= NULL
);
545 sect
->parent
= topsect
;
546 p
->private_data
= sect
;
548 html_file_section(&conf
, &files
, sect
, d
);
552 for (i
=0; i
< conf
.ntfragments
; i
++) {
554 html_format(p
, conf
.template_fragments
[i
]);
556 html_sanitise_fragment(&files
, sect
->file
,
562 /* And the index, if we have one. */
563 has_index
= (count234(idx
->entries
) > 0);
565 sect
= html_new_sect(§s
, NULL
, &conf
);
568 sect
->parent
= topsect
;
569 sect
->contents_depth
= 0;
570 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
571 sect
->fragments
[0] = utoa_dup(conf
.index_text
, CS_ASCII
);
572 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
574 files
.index
= sect
->file
;
579 * Go through the keyword list and sort out fragment IDs for
580 * all the potentially referenced paragraphs which _aren't_
588 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
589 paragraph
*q
, *p
= kw
->para
;
591 if (!is_heading_type(p
->type
)) {
595 * Find the paragraph's parent htmlsect, to
596 * determine which file it will end up in.
601 * Preamble paragraphs have no parent. So if we
602 * have a non-heading with no parent, it must
603 * be preamble, and therefore its parent
604 * htmlsect must be the preamble one.
607 sects
.head
->type
== TOP
);
610 parent
= (htmlsect
*)q
->private_data
;
613 * Now we can construct an htmlsect for this
614 * paragraph itself, taking care to put it in the
615 * list of non-sections rather than the list of
616 * sections (so that traverses of the `sects' list
617 * won't attempt to add it to the contents or
618 * anything weird like that).
620 sect
= html_new_sect(&nonsects
, p
, &conf
);
621 sect
->file
= parent
->file
;
622 sect
->parent
= parent
;
623 p
->private_data
= sect
;
626 * Fragment IDs for these paragraphs will simply be
627 * `p' followed by an integer.
629 sect
->fragments
[0] = snewn(40, char);
630 sprintf(sect
->fragments
[0], "p%d",
631 sect
->file
->last_fragment_number
++);
632 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
639 * Reset the fragment numbers in each file. I've just used them
640 * to generate `p' fragment IDs for non-section paragraphs
641 * (numbered list elements, bibliocited), and now I want to use
642 * them for `i' fragment IDs for index entries.
646 for (file
= files
.head
; file
; file
= file
->next
)
647 file
->last_fragment_number
= 0;
651 * Now sort out the index. This involves:
653 * - For each index term, we set up an htmlindex structure to
654 * store all the references to that term.
656 * - Then we make a pass over the actual document, finding
657 * every word_IndexRef; for each one, we actually figure out
658 * the HTML filename/fragment pair we will use to reference
659 * it, store that information in the private data field of
660 * the word_IndexRef itself (so we can recreate it when the
661 * time comes to output our HTML), and add a reference to it
662 * to the index term in question.
671 * Set up the htmlindex structures.
674 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
675 htmlindex
*hi
= snew(htmlindex
);
677 hi
->nrefs
= hi
->refsize
= 0;
680 entry
->backend_data
= hi
;
684 * Run over the document inventing fragments. Each fragment
685 * is of the form `i' followed by an integer.
687 lastsect
= sects
.head
; /* this is always the top section */
688 for (p
= sourceform
; p
; p
= p
->next
) {
689 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
690 lastsect
= (htmlsect
*)p
->private_data
;
692 for (w
= p
->words
; w
; w
= w
->next
)
693 if (w
->type
== word_IndexRef
) {
694 htmlindexref
*hr
= snew(htmlindexref
);
698 hr
->referenced
= hr
->generated
= FALSE
;
699 hr
->section
= lastsect
;
703 lastsect
->file
->last_fragment_number
++);
704 hr
->fragment
= dupstr(buf
);
706 html_sanitise_fragment(&files
, hr
->section
->file
,
709 w
->private_data
= hr
;
711 tag
= index_findtag(idx
, w
->text
);
715 for (i
= 0; i
< tag
->nrefs
; i
++) {
716 indexentry
*entry
= tag
->refs
[i
];
717 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
719 if (hi
->nrefs
>= hi
->refsize
) {
721 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
724 hi
->refs
[hi
->nrefs
++] = w
;
731 * Now we're ready to write out the actual HTML files.
735 * - we open that file and write its header
736 * - we run down the list of sections
737 * - for each section directly contained within that file, we
738 * output the section text
739 * - for each section which is not in the file but which has a
740 * parent that is, we output a contents entry for the
741 * section if appropriate
742 * - finally, we output the file trailer and close the file.
751 for (f
= files
.head
; f
; f
= f
->next
) {
754 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
755 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
756 struct stackelement
{
757 struct stackelement
*next
;
758 enum LISTTYPE listtype
;
759 enum ITEMTYPE itemtype
;
762 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
763 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
765 ho
.fp
= fopen(f
->filename
, "w");
766 ho
.charset
= conf
.output_charset
;
767 ho
.restrict_charset
= conf
.restrict_charset
;
768 ho
.cstate
= charset_init_state
;
769 ho
.ver
= conf
.htmlver
;
770 ho
.state
= HO_NEUTRAL
;
771 ho
.contents_level
= 0;
774 switch (conf
.htmlver
) {
776 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
777 "HTML 3.2 Final//EN\">\n");
780 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
781 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
785 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
786 "15445:2000//DTD HTML//EN\">\n");
788 case XHTML_1_0_TRANSITIONAL
:
789 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
790 charset_to_mimeenc(conf
.output_charset
));
791 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
792 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
793 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
795 case XHTML_1_0_STRICT
:
796 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
797 charset_to_mimeenc(conf
.output_charset
));
798 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
799 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
800 "DTD/xhtml1-strict.dtd\">\n");
804 element_open(&ho
, "html");
805 if (is_xhtml(conf
.htmlver
)) {
806 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
810 element_open(&ho
, "head");
813 element_empty(&ho
, "meta");
814 element_attr(&ho
, "http-equiv", "content-type");
817 sprintf(buf
, "text/html; charset=%.150s",
818 charset_to_mimeenc(conf
.output_charset
));
819 element_attr(&ho
, "content", buf
);
824 element_empty(&ho
, "meta");
825 element_attr(&ho
, "name", "author");
826 element_attr_w(&ho
, "content", conf
.author
);
830 if (conf
.description
) {
831 element_empty(&ho
, "meta");
832 element_attr(&ho
, "name", "description");
833 element_attr_w(&ho
, "content", conf
.description
);
837 element_open(&ho
, "title");
838 if (f
->first
&& f
->first
->title
) {
839 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
843 if (f
->last
!= f
->first
&& f
->last
->title
) {
844 html_text(&ho
, conf
.title_separator
);
845 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
849 element_close(&ho
, "title");
853 html_raw(&ho
, conf
.head_end
);
856 * Add any <head> data defined in specific sections
857 * that go in this file. (This is mostly to allow <meta
858 * name="AppleTitle"> tags for Mac online help.)
860 for (s
= sects
.head
; s
; s
= s
->next
) {
861 if (s
->file
== f
&& s
->text
) {
863 p
&& (p
== s
->text
|| p
->type
== para_Title
||
864 !is_heading_type(p
->type
));
866 if (p
->type
== para_Config
) {
867 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
868 html_raw(&ho
, adv(p
->origkeyword
));
875 element_close(&ho
, "head");
879 html_raw(&ho
, conf
.body_tag
);
881 element_open(&ho
, "body");
885 html_raw(&ho
, conf
.body_start
);
888 * Write out a nav bar. Special case: we don't do this
889 * if there is only one file.
891 if (files
.head
!= files
.tail
) {
892 element_open(&ho
, "p");
894 html_raw_as_attr(&ho
, conf
.nav_attr
);
897 element_open(&ho
, "a");
898 element_attr(&ho
, "href", prevf
->filename
);
900 html_text(&ho
, conf
.nav_prev_text
);
902 element_close(&ho
, "a");
904 html_text(&ho
, conf
.nav_separator
);
906 if (f
!= files
.head
) {
907 element_open(&ho
, "a");
908 element_attr(&ho
, "href", files
.head
->filename
);
910 html_text(&ho
, conf
.contents_text
);
912 element_close(&ho
, "a");
915 html_text(&ho
, conf
.nav_separator
);
916 if (f
!= files
.index
) {
917 element_open(&ho
, "a");
918 element_attr(&ho
, "href", files
.index
->filename
);
920 html_text(&ho
, conf
.index_text
);
921 if (f
!= files
.index
)
922 element_close(&ho
, "a");
925 html_text(&ho
, conf
.nav_separator
);
928 element_open(&ho
, "a");
929 element_attr(&ho
, "href", f
->next
->filename
);
931 html_text(&ho
, conf
.nav_next_text
);
933 element_close(&ho
, "a");
935 element_close(&ho
, "p");
941 * Write out a prefix TOC for the file (if a leaf file).
943 * We start by going through the section list and
944 * collecting the sections which need to be added to
945 * the contents. On the way, we also test to see if
946 * this file is a leaf file (defined as one which
947 * contains all descendants of any section it
948 * contains), because this will play a part in our
949 * decision on whether or not to _output_ the TOC.
951 * Special case: we absolutely do not do this if we're
952 * in single-file mode.
954 if (files
.head
!= files
.tail
) {
955 int ntoc
= 0, tocsize
= 0;
956 htmlsect
**toc
= NULL
;
959 for (s
= sects
.head
; s
; s
= s
->next
) {
964 * Search up from this section until we find
965 * the highest-level one which belongs in this
970 for (ac
= s
; ac
; ac
= ac
->parent
) {
978 if (s
->file
!= f
&& a
!= NULL
)
982 if (adepth
<= a
->contents_depth
) {
983 if (ntoc
>= tocsize
) {
985 toc
= sresize(toc
, tocsize
, htmlsect
*);
992 if (leaf
&& conf
.leaf_contains_contents
&&
993 ntoc
>= conf
.leaf_smallest_contents
) {
996 for (i
= 0; i
< ntoc
; i
++) {
997 htmlsect
*s
= toc
[i
];
998 int hlevel
= (s
->type
== TOP ?
-1 :
999 s
->type
== INDEX ?
0 :
1000 heading_depth(s
->title
))
1001 - f
->min_heading_depth
+ 1;
1003 assert(hlevel
>= 1);
1004 html_contents_entry(&ho
, hlevel
, s
,
1005 f
, keywords
, &conf
);
1007 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1012 * Now go through the document and output some real
1016 for (s
= sects
.head
; s
; s
= s
->next
) {
1019 * This section belongs in this file.
1025 * Doesn't belong in this file, but it may be
1026 * a descendant of a section which does, in
1027 * which case we should consider it for the
1028 * main TOC of this file (for non-leaf files).
1036 * Search up from this section until we find
1037 * the highest-level one which belongs in this
1042 for (ac
= s
; ac
; ac
= ac
->parent
) {
1043 if (ac
->file
== f
) {
1052 * This section does not belong in this
1053 * file, but an ancestor of it does. Write
1054 * out a contents table entry, if the depth
1055 * doesn't exceed the maximum contents
1056 * depth for the ancestor section.
1058 if (adepth
<= a
->contents_depth
) {
1059 html_contents_entry(&ho
, adepth
, s
,
1060 f
, keywords
, &conf
);
1069 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1072 * Display the section heading.
1075 hlevel
= (s
->type
== TOP ?
-1 :
1076 s
->type
== INDEX ?
0 :
1077 heading_depth(s
->title
))
1078 - f
->min_heading_depth
+ 1;
1079 assert(hlevel
>= 1);
1080 /* HTML headings only go up to <h6> */
1084 htag
[1] = '0' + hlevel
;
1086 element_open(&ho
, htag
);
1089 * Provide anchor(s) for cross-links to target.
1091 * (Also we'll have to do this separately in
1092 * other paragraph types - NumberedList and
1097 for (i
=0; i
< conf
.ntfragments
; i
++)
1098 if (s
->fragments
[i
])
1099 html_fragment(&ho
, s
->fragments
[i
]);
1102 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1104 element_close(&ho
, htag
);
1107 * Now display the section text.
1110 stackhead
= snew(struct stackelement
);
1111 stackhead
->next
= NULL
;
1112 stackhead
->listtype
= NOLIST
;
1113 stackhead
->itemtype
= NOITEM
;
1115 for (p
= s
->text
;; p
= p
->next
) {
1116 enum LISTTYPE listtype
;
1117 struct stackelement
*se
;
1120 * Preliminary switch to figure out what
1121 * sort of list we expect to be inside at
1124 * Since p may still be NULL at this point,
1125 * I invent a harmless paragraph type for
1128 switch (p ? p
->type
: para_Normal
) {
1131 case para_Copyright
:
1132 case para_BiblioCited
:
1134 case para_QuotePush
:
1138 case para_UnnumberedChapter
:
1149 case para_NumberedList
:
1153 case para_DescribedThing
:
1154 case para_Description
:
1158 case para_LcontPush
:
1159 se
= snew(struct stackelement
);
1160 se
->next
= stackhead
;
1161 se
->listtype
= NOLIST
;
1162 se
->itemtype
= NOITEM
;
1166 default: /* some totally non-printing para */
1173 * Terminate the most recent list item, if
1174 * any. (We left this until after
1175 * processing LcontPush, since in that case
1176 * the list item won't want to be
1177 * terminated until after the corresponding
1180 if (stackhead
->itemtype
!= NOITEM
) {
1181 element_close(&ho
, itemname(stackhead
->itemtype
));
1184 stackhead
->itemtype
= NOITEM
;
1187 * Terminate the current list, if it's not
1188 * the one we want to be in.
1190 if (listtype
!= stackhead
->listtype
&&
1191 stackhead
->listtype
!= NOLIST
) {
1192 element_close(&ho
, listname(stackhead
->listtype
));
1197 * Leave the loop if our time has come.
1199 if (!p
|| (is_heading_type(p
->type
) &&
1200 p
->type
!= para_Title
))
1201 break; /* end of section text */
1204 * Start a fresh list if necessary.
1206 if (listtype
!= stackhead
->listtype
&&
1208 element_open(&ho
, listname(listtype
));
1210 stackhead
->listtype
= listtype
;
1214 element_empty(&ho
, "hr");
1217 html_codepara(&ho
, p
->words
);
1220 case para_Copyright
:
1221 element_open(&ho
, "p");
1223 html_words(&ho
, p
->words
, ALL
,
1224 f
, keywords
, &conf
);
1226 element_close(&ho
, "p");
1228 case para_BiblioCited
:
1229 element_open(&ho
, "p");
1230 if (p
->private_data
) {
1231 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1233 for (i
=0; i
< conf
.ntfragments
; i
++)
1234 if (s
->fragments
[i
])
1235 html_fragment(&ho
, s
->fragments
[i
]);
1238 html_words(&ho
, p
->kwtext
, ALL
,
1239 f
, keywords
, &conf
);
1240 html_text(&ho
, L
" ");
1241 html_words(&ho
, p
->words
, ALL
,
1242 f
, keywords
, &conf
);
1244 element_close(&ho
, "p");
1247 case para_NumberedList
:
1248 element_open(&ho
, "li");
1249 if (p
->private_data
) {
1250 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1252 for (i
=0; i
< conf
.ntfragments
; i
++)
1253 if (s
->fragments
[i
])
1254 html_fragment(&ho
, s
->fragments
[i
]);
1257 stackhead
->itemtype
= LI
;
1258 html_words(&ho
, p
->words
, ALL
,
1259 f
, keywords
, &conf
);
1261 case para_DescribedThing
:
1262 element_open(&ho
, "dt");
1264 stackhead
->itemtype
= DT
;
1265 html_words(&ho
, p
->words
, ALL
,
1266 f
, keywords
, &conf
);
1268 case para_Description
:
1269 element_open(&ho
, "dd");
1271 stackhead
->itemtype
= DD
;
1272 html_words(&ho
, p
->words
, ALL
,
1273 f
, keywords
, &conf
);
1276 case para_QuotePush
:
1277 element_open(&ho
, "blockquote");
1280 element_close(&ho
, "blockquote");
1285 stackhead
= stackhead
->next
;
1292 assert(stackhead
&& !stackhead
->next
);
1296 if (s
->type
== INDEX
) {
1301 * This section is the index. I'll just
1302 * render it as a single paragraph, with a
1303 * colon between the index term and the
1304 * references, and <br> in between each
1307 element_open(&ho
, "p");
1309 for (i
= 0; (entry
=
1310 index234(idx
->entries
, i
)) != NULL
; i
++) {
1311 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1315 element_empty(&ho
, "br");
1318 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1319 f
, keywords
, &conf
);
1321 html_text(&ho
, conf
.index_main_sep
);
1323 for (j
= 0; j
< hi
->nrefs
; j
++) {
1325 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1326 paragraph
*p
= hr
->section
->title
;
1329 html_text(&ho
, conf
.index_multi_sep
);
1331 html_href(&ho
, f
, hr
->section
->file
,
1333 hr
->referenced
= TRUE
;
1335 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1336 f
, keywords
, &conf
);
1337 else if (p
&& p
->words
)
1338 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1339 f
, keywords
, &conf
);
1342 * If there is no title at all,
1343 * this must be because our
1344 * target section is the
1345 * preamble section and there
1346 * is no title. So we use the
1349 html_text(&ho
, conf
.preamble_text
);
1351 element_close(&ho
, "a");
1354 element_close(&ho
, "p");
1359 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1366 int done_version_ids
= FALSE
;
1368 element_empty(&ho
, "hr");
1371 html_raw(&ho
, conf
.body_end
);
1373 if (conf
.address_section
) {
1374 int started
= FALSE
;
1375 if (conf
.htmlver
== ISO_HTML
) {
1377 * The ISO-HTML validator complains if
1378 * there isn't a <div> tag surrounding the
1379 * <address> tag. I'm uncertain of why this
1380 * should be - there appears to be no
1381 * mention of this in the ISO-HTML spec,
1382 * suggesting that it doesn't represent a
1383 * change from HTML 4, but nonetheless the
1384 * HTML 4 validator doesn't seem to mind.
1386 element_open(&ho
, "div");
1388 element_open(&ho
, "address");
1389 if (conf
.addr_start
) {
1390 html_raw(&ho
, conf
.addr_start
);
1394 if (conf
.visible_version_id
) {
1395 for (p
= sourceform
; p
; p
= p
->next
)
1396 if (p
->type
== para_VersionID
) {
1398 element_empty(&ho
, "br");
1400 html_text(&ho
, conf
.pre_versionid
);
1401 html_words(&ho
, p
->words
, NOTHING
,
1402 f
, keywords
, &conf
);
1403 html_text(&ho
, conf
.post_versionid
);
1406 done_version_ids
= TRUE
;
1408 if (conf
.addr_end
) {
1410 element_empty(&ho
, "br");
1411 html_raw(&ho
, conf
.addr_end
);
1413 element_close(&ho
, "address");
1414 if (conf
.htmlver
== ISO_HTML
)
1415 element_close(&ho
, "div");
1418 if (!done_version_ids
) {
1420 * If the user didn't want the version IDs
1421 * visible, I think we still have a duty to put
1422 * them in an HTML comment.
1424 int started
= FALSE
;
1425 for (p
= sourceform
; p
; p
= p
->next
)
1426 if (p
->type
== para_VersionID
) {
1428 html_raw(&ho
, "<!-- version IDs:\n");
1431 html_words(&ho
, p
->words
, NOTHING
,
1432 f
, keywords
, &conf
);
1436 html_raw(&ho
, "-->\n");
1440 element_close(&ho
, "body");
1442 element_close(&ho
, "html");
1449 * Go through and check that no index fragments were referenced
1450 * without being generated, or indeed vice versa.
1452 * (When I actually get round to freeing everything, this can
1453 * probably be the freeing loop as well.)
1455 for (p
= sourceform
; p
; p
= p
->next
) {
1457 for (w
= p
->words
; w
; w
= w
->next
)
1458 if (w
->type
== word_IndexRef
) {
1459 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1461 assert(!hr
->referenced
== !hr
->generated
);
1466 * Free all the working data.
1470 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1472 * frag->fragment is dynamically allocated, but will be
1473 * freed when we process the htmlsect structure which
1474 * it is attached to.
1478 freetree234(files
.frags
);
1481 htmlsect
*sect
, *tmp
;
1486 for (i
=0; i
< conf
.ntfragments
; i
++)
1487 sfree(sect
->fragments
[i
]);
1488 sfree(sect
->fragments
);
1492 sect
= nonsects
.head
;
1496 for (i
=0; i
< conf
.ntfragments
; i
++)
1497 sfree(sect
->fragments
[i
]);
1498 sfree(sect
->fragments
);
1504 htmlfile
*file
, *tmp
;
1508 sfree(file
->filename
);
1516 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1517 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1524 for (p
= sourceform
; p
; p
= p
->next
)
1525 for (w
= p
->words
; w
; w
= w
->next
)
1526 if (w
->type
== word_IndexRef
) {
1527 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1529 sfree(hr
->fragment
);
1534 sfree(conf
.single_filename
);
1535 sfree(conf
.contents_filename
);
1536 sfree(conf
.index_filename
);
1537 sfree(conf
.template_filename
);
1538 while (conf
.ntfragments
--)
1539 sfree(conf
.template_fragments
[conf
.ntfragments
]);
1540 sfree(conf
.template_fragments
);
1543 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1544 htmlsect
*sect
, int depth
)
1550 * `depth' is derived from the heading_depth() macro at the top
1551 * of this file, which counts title as -1, chapter as 0,
1552 * heading as 1 and subsection as 2. However, the semantics of
1553 * cfg->leaf_level are defined to count chapter as 1, heading
1554 * as 2 etc. So first I increment depth :-(
1558 if (cfg
->leaf_level
== 0) {
1560 * leaf_level==0 is a special case, in which everything is
1561 * put into a single file.
1564 files
->single
= html_new_file(files
, cfg
->single_filename
);
1566 file
= files
->single
;
1569 * If the depth of this section is at or above leaf_level,
1570 * we invent a fresh file and put this section at its head.
1571 * Otherwise, we put it in the same file as its parent
1574 if (ldepth
> cfg
->leaf_level
) {
1576 * We know that sect->parent cannot be NULL. The only
1577 * circumstance in which it can be is if sect is at
1578 * chapter or appendix level, i.e. ldepth==1; and if
1579 * that's the case, then we cannot have entered this
1580 * branch unless cfg->leaf_level==0, in which case we
1581 * would be in the single-file case above and not here
1584 assert(sect
->parent
);
1586 file
= sect
->parent
->file
;
1588 if (sect
->type
== TOP
) {
1589 file
= html_new_file(files
, cfg
->contents_filename
);
1590 } else if (sect
->type
== INDEX
) {
1591 file
= html_new_file(files
, cfg
->index_filename
);
1595 assert(ldepth
> 0 && sect
->title
);
1596 title
= html_format(sect
->title
, cfg
->template_filename
);
1597 file
= html_new_file(files
, title
);
1605 if (file
->min_heading_depth
> depth
) {
1607 * This heading is at a higher level than any heading we
1608 * have so far placed in this file; so we set the `first'
1611 file
->min_heading_depth
= depth
;
1615 if (file
->min_heading_depth
== depth
)
1619 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1621 htmlfile
*ret
= snew(htmlfile
);
1625 list
->tail
->next
= ret
;
1630 ret
->filename
= dupstr(filename
);
1631 ret
->last_fragment_number
= 0;
1632 ret
->min_heading_depth
= INT_MAX
;
1633 ret
->first
= ret
->last
= NULL
;
1638 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
1641 htmlsect
*ret
= snew(htmlsect
);
1645 list
->tail
->next
= ret
;
1655 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
1658 for (i
=0; i
< cfg
->ntfragments
; i
++)
1659 ret
->fragments
[i
] = NULL
;
1665 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1666 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1672 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1673 case word_HyperLink
:
1674 if (flags
& LINKS
) {
1675 element_open(ho
, "a");
1676 c
= utoa_dup(w
->text
, CS_ASCII
);
1677 element_attr(ho
, "href", c
);
1681 case word_UpperXref
:
1682 case word_LowerXref
:
1683 if (flags
& LINKS
) {
1684 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1690 s
= (htmlsect
*)p
->private_data
;
1694 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
1700 element_close(ho
, "a");
1703 if (flags
& INDEXENTS
) {
1704 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1705 html_fragment(ho
, hr
->fragment
);
1706 hr
->generated
= TRUE
;
1713 case word_WhiteSpace
:
1714 case word_EmphSpace
:
1715 case word_CodeSpace
:
1716 case word_WkCodeSpace
:
1718 case word_EmphQuote
:
1719 case word_CodeQuote
:
1720 case word_WkCodeQuote
:
1721 style
= towordstyle(w
->type
);
1722 type
= removeattr(w
->type
);
1723 if (style
== word_Emph
&&
1724 (attraux(w
->aux
) == attr_First
||
1725 attraux(w
->aux
) == attr_Only
) &&
1727 element_open(ho
, "em");
1728 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1729 (attraux(w
->aux
) == attr_First
||
1730 attraux(w
->aux
) == attr_Only
) &&
1732 element_open(ho
, "code");
1734 if (type
== word_WhiteSpace
)
1735 html_text(ho
, L
" ");
1736 else if (type
== word_Quote
) {
1737 if (quoteaux(w
->aux
) == quote_Open
)
1738 html_text(ho
, cfg
->lquote
);
1740 html_text(ho
, cfg
->rquote
);
1742 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
1743 html_text_nbsp(ho
, w
->text
);
1745 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1748 if (style
== word_Emph
&&
1749 (attraux(w
->aux
) == attr_Last
||
1750 attraux(w
->aux
) == attr_Only
) &&
1752 element_close(ho
, "em");
1753 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1754 (attraux(w
->aux
) == attr_Last
||
1755 attraux(w
->aux
) == attr_Only
) &&
1757 element_close(ho
, "code");
1763 static void html_codepara(htmloutput
*ho
, word
*words
)
1765 element_open(ho
, "pre");
1766 element_open(ho
, "code");
1767 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1772 if (words
->next
&& words
->next
->type
== word_Emph
) {
1773 e
= words
->next
->text
;
1774 words
= words
->next
;
1778 while (e
&& *e
&& *t
) {
1782 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1790 element_open(ho
, open_tag
);
1792 html_text_limit(ho
, t
, n
);
1795 element_close(ho
, open_tag
);
1803 element_close(ho
, "code");
1804 element_close(ho
, "pre");
1807 static void html_charset_cleanup(htmloutput
*ho
)
1812 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1813 ho
->charset
, &ho
->cstate
, NULL
);
1815 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1818 static void return_mostly_to_neutral(htmloutput
*ho
)
1820 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1821 fprintf(ho
->fp
, " />");
1822 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1823 fprintf(ho
->fp
, ">");
1826 ho
->state
= HO_NEUTRAL
;
1829 static void return_to_neutral(htmloutput
*ho
)
1831 if (ho
->state
== HO_IN_TEXT
) {
1832 html_charset_cleanup(ho
);
1835 return_mostly_to_neutral(ho
);
1838 static void element_open(htmloutput
*ho
, char const *name
)
1840 return_to_neutral(ho
);
1841 fprintf(ho
->fp
, "<%s", name
);
1842 ho
->state
= HO_IN_TAG
;
1845 static void element_close(htmloutput
*ho
, char const *name
)
1847 return_to_neutral(ho
);
1848 fprintf(ho
->fp
, "</%s>", name
);
1849 ho
->state
= HO_NEUTRAL
;
1852 static void element_empty(htmloutput
*ho
, char const *name
)
1854 return_to_neutral(ho
);
1855 fprintf(ho
->fp
, "<%s", name
);
1856 ho
->state
= HO_IN_EMPTY_TAG
;
1859 static void html_nl(htmloutput
*ho
)
1861 return_to_neutral(ho
);
1862 fputc('\n', ho
->fp
);
1865 static void html_raw(htmloutput
*ho
, char *text
)
1867 return_to_neutral(ho
);
1868 fputs(text
, ho
->fp
);
1871 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1873 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1875 fputs(text
, ho
->fp
);
1878 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1880 html_charset_cleanup(ho
);
1881 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1882 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1885 static void element_attr_w(htmloutput
*ho
, char const *name
,
1886 wchar_t const *value
)
1888 html_charset_cleanup(ho
);
1889 fprintf(ho
->fp
, " %s=\"", name
);
1890 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
1891 html_charset_cleanup(ho
);
1895 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1897 return_mostly_to_neutral(ho
);
1898 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
1901 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
1903 return_mostly_to_neutral(ho
);
1904 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
1907 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1909 return_mostly_to_neutral(ho
);
1910 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
1913 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1914 int maxlen
, int quote_quotes
, int nbsp
)
1916 int textlen
= ustrlen(text
);
1920 if (maxlen
> 0 && textlen
> maxlen
)
1923 while (textlen
> 0) {
1924 /* Scan ahead for characters we really can't display in HTML. */
1925 int lenbefore
, lenafter
;
1926 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1927 if (text
[lenbefore
] == L
'<' ||
1928 text
[lenbefore
] == L
'>' ||
1929 text
[lenbefore
] == L
'&' ||
1930 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
1931 (text
[lenbefore
] == L
' ' && nbsp
))
1933 lenafter
= lenbefore
;
1934 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1935 ho
->charset
, &ho
->cstate
, &err
);
1936 textlen
-= (lenbefore
- lenafter
);
1938 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1941 * We have encountered a character that cannot be
1942 * displayed in the selected output charset. Therefore,
1943 * we use an HTML numeric entity reference.
1945 assert(textlen
> 0);
1946 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1948 } else if (lenafter
== 0 && textlen
> 0) {
1950 * We have encountered a character which is special to
1954 fprintf(ho
->fp
, "<");
1955 else if (*text
== L
'>')
1956 fprintf(ho
->fp
, ">");
1957 else if (*text
== L
'&')
1958 fprintf(ho
->fp
, "&");
1959 else if (*text
== L
'"')
1960 fprintf(ho
->fp
, """);
1961 else if (*text
== L
' ') {
1963 fprintf(ho
->fp
, " ");
1965 assert(!"Can't happen");
1971 static void cleanup(htmloutput
*ho
)
1973 return_to_neutral(ho
);
1977 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
1978 htmlfile
*targetfile
, char *targetfrag
)
1980 rdstringc rs
= { 0, 0, NULL
};
1983 if (targetfile
!= thisfile
)
1984 rdaddsc(&rs
, targetfile
->filename
);
1987 rdaddsc(&rs
, targetfrag
);
1991 element_open(ho
, "a");
1992 element_attr(ho
, "href", url
);
1996 static void html_fragment(htmloutput
*ho
, char const *fragment
)
1998 element_open(ho
, "a");
1999 element_attr(ho
, "name", fragment
);
2000 if (is_xhtml(ho
->ver
))
2001 element_attr(ho
, "id", fragment
);
2002 element_close(ho
, "a");
2005 static char *html_format(paragraph
*p
, char *template_string
)
2009 wchar_t *ws
, wsbuf
[2];
2010 rdstringc rs
= { 0, 0, NULL
};
2012 t
= template_string
;
2014 if (*t
== '%' && t
[1]) {
2028 if (p
->kwtext
&& fmt
== 'n')
2030 else if (p
->kwtext2
&& fmt
== 'b') {
2032 * HTML fragment names must start with a letter, so
2033 * simply `1.2.3' is not adequate. In this case I'm
2034 * going to cheat slightly by prepending the first
2035 * character of the first word of kwtext, so that
2036 * we get `C1' for chapter 1, `S2.3' for section
2039 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2042 wsbuf
[0] = p
->kwtext
->text
[0];
2045 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2048 /* %N comes here; also failure cases of other fmts */
2052 c
= utoa_dup(ws
, CS_ASCII
);
2058 if (removeattr(w
->type
) == word_Normal
) {
2059 c
= utoa_dup(w
->text
, CS_ASCII
);
2070 return rdtrimc(&rs
);
2073 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2077 * The HTML 4 spec's strictest definition of fragment names (<a
2078 * name> and "id" attributes) says that they `must begin with a
2079 * letter and may be followed by any number of letters, digits,
2080 * hyphens, underscores, colons, and periods'.
2082 * So here we unceremoniously rip out any characters not
2083 * conforming to this limitation.
2085 char *p
= text
, *q
= text
;
2087 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2089 if ((*q
++ = *p
++) != '\0') {
2091 if ((*p
>='A' && *p
<='Z') ||
2092 (*p
>='a' && *p
<='z') ||
2093 (*p
>='0' && *p
<='9') ||
2094 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2102 /* If there's nothing left, make something valid up */
2104 static const char anonfrag
[] = "anon";
2105 text
= sresize(text
, lenof(anonfrag
), char);
2106 strcpy(text
, anonfrag
);
2110 * Now we check for clashes with other fragment names, and
2111 * adjust this one if necessary by appending a hyphen followed
2115 htmlfragment
*frag
= snew(htmlfragment
);
2116 int len
= 0; /* >0 indicates we have resized */
2120 frag
->fragment
= text
;
2122 while (add234(files
->frags
, frag
) != frag
) {
2125 frag
->fragment
= text
= sresize(text
, len
+20, char);
2128 sprintf(text
+ len
, "-%d", ++suffix
);
2135 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2136 htmlfile
*thisfile
, keywordlist
*keywords
,
2139 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2140 element_close(ho
, "li");
2144 while (ho
->contents_level
> depth
) {
2145 element_close(ho
, "ul");
2146 ho
->contents_level
--;
2147 if (ho
->contents_level
> 0) {
2148 element_close(ho
, "li");
2153 while (ho
->contents_level
< depth
) {
2155 element_open(ho
, "ul");
2157 ho
->contents_level
++;
2163 element_open(ho
, "li");
2164 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2165 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2166 element_close(ho
, "a");
2167 /* <li> will be closed by a later invocation */
2170 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2171 keywordlist
*keywords
, htmlconfig
*cfg
,
2177 int depth
= heading_depth(s
->title
);
2181 else if (depth
== 0)
2182 sl
= &cfg
->achapter
;
2183 else if (depth
<= cfg
->nasect
)
2184 sl
= &cfg
->asect
[depth
-1];
2186 sl
= &cfg
->asect
[cfg
->nasect
-1];
2190 else if (sl
->just_numbers
)
2191 number
= s
->title
->kwtext2
;
2193 number
= s
->title
->kwtext
;
2196 html_words(ho
, number
, MARKUP
,
2197 thisfile
, keywords
, cfg
);
2198 html_text(ho
, sl
->number_suffix
);
2201 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2202 thisfile
, keywords
, cfg
);
2204 assert(s
->type
!= NORMAL
);
2206 * If we're printing the full document title for _real_ and
2207 * there isn't one, we don't want to print `Preamble' at
2208 * the top of what ought to just be some text. If we need
2209 * it in any other context such as TOCs, we need to print
2212 if (s
->type
== TOP
&& !real
)
2213 html_text(ho
, cfg
->preamble_text
);
2214 else if (s
->type
== INDEX
)
2215 html_text(ho
, cfg
->index_text
);