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");
767 error(err_cantopenw
, f
->filename
);
769 ho
.charset
= conf
.output_charset
;
770 ho
.restrict_charset
= conf
.restrict_charset
;
771 ho
.cstate
= charset_init_state
;
772 ho
.ver
= conf
.htmlver
;
773 ho
.state
= HO_NEUTRAL
;
774 ho
.contents_level
= 0;
777 switch (conf
.htmlver
) {
780 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
781 "HTML 3.2 Final//EN\">\n");
785 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
786 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
791 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
792 "15445:2000//DTD HTML//EN\">\n");
794 case XHTML_1_0_TRANSITIONAL
:
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 Transitional//EN\"\n\"http://www.w3.org/TR/"
800 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
803 case XHTML_1_0_STRICT
:
805 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
806 charset_to_mimeenc(conf
.output_charset
));
807 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
808 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
809 "DTD/xhtml1-strict.dtd\">\n");
814 element_open(&ho
, "html");
815 if (is_xhtml(conf
.htmlver
)) {
816 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
820 element_open(&ho
, "head");
823 element_empty(&ho
, "meta");
824 element_attr(&ho
, "http-equiv", "content-type");
827 sprintf(buf
, "text/html; charset=%.150s",
828 charset_to_mimeenc(conf
.output_charset
));
829 element_attr(&ho
, "content", buf
);
834 element_empty(&ho
, "meta");
835 element_attr(&ho
, "name", "author");
836 element_attr_w(&ho
, "content", conf
.author
);
840 if (conf
.description
) {
841 element_empty(&ho
, "meta");
842 element_attr(&ho
, "name", "description");
843 element_attr_w(&ho
, "content", conf
.description
);
847 element_open(&ho
, "title");
848 if (f
->first
&& f
->first
->title
) {
849 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
853 if (f
->last
!= f
->first
&& f
->last
->title
) {
854 html_text(&ho
, conf
.title_separator
);
855 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
859 element_close(&ho
, "title");
863 html_raw(&ho
, conf
.head_end
);
866 * Add any <head> data defined in specific sections
867 * that go in this file. (This is mostly to allow <meta
868 * name="AppleTitle"> tags for Mac online help.)
870 for (s
= sects
.head
; s
; s
= s
->next
) {
871 if (s
->file
== f
&& s
->text
) {
873 p
&& (p
== s
->text
|| p
->type
== para_Title
||
874 !is_heading_type(p
->type
));
876 if (p
->type
== para_Config
) {
877 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
878 html_raw(&ho
, adv(p
->origkeyword
));
885 element_close(&ho
, "head");
889 html_raw(&ho
, conf
.body_tag
);
891 element_open(&ho
, "body");
895 html_raw(&ho
, conf
.body_start
);
898 * Write out a nav bar. Special case: we don't do this
899 * if there is only one file.
901 if (files
.head
!= files
.tail
) {
902 element_open(&ho
, "p");
904 html_raw_as_attr(&ho
, conf
.nav_attr
);
907 element_open(&ho
, "a");
908 element_attr(&ho
, "href", prevf
->filename
);
910 html_text(&ho
, conf
.nav_prev_text
);
912 element_close(&ho
, "a");
914 html_text(&ho
, conf
.nav_separator
);
916 if (f
!= files
.head
) {
917 element_open(&ho
, "a");
918 element_attr(&ho
, "href", files
.head
->filename
);
920 html_text(&ho
, conf
.contents_text
);
922 element_close(&ho
, "a");
925 html_text(&ho
, conf
.nav_separator
);
926 if (f
!= files
.index
) {
927 element_open(&ho
, "a");
928 element_attr(&ho
, "href", files
.index
->filename
);
930 html_text(&ho
, conf
.index_text
);
931 if (f
!= files
.index
)
932 element_close(&ho
, "a");
935 html_text(&ho
, conf
.nav_separator
);
938 element_open(&ho
, "a");
939 element_attr(&ho
, "href", f
->next
->filename
);
941 html_text(&ho
, conf
.nav_next_text
);
943 element_close(&ho
, "a");
945 element_close(&ho
, "p");
951 * Write out a prefix TOC for the file (if a leaf file).
953 * We start by going through the section list and
954 * collecting the sections which need to be added to
955 * the contents. On the way, we also test to see if
956 * this file is a leaf file (defined as one which
957 * contains all descendants of any section it
958 * contains), because this will play a part in our
959 * decision on whether or not to _output_ the TOC.
961 * Special case: we absolutely do not do this if we're
962 * in single-file mode.
964 if (files
.head
!= files
.tail
) {
965 int ntoc
= 0, tocsize
= 0;
966 htmlsect
**toc
= NULL
;
969 for (s
= sects
.head
; s
; s
= s
->next
) {
974 * Search up from this section until we find
975 * the highest-level one which belongs in this
980 for (ac
= s
; ac
; ac
= ac
->parent
) {
988 if (s
->file
!= f
&& a
!= NULL
)
992 if (adepth
<= a
->contents_depth
) {
993 if (ntoc
>= tocsize
) {
995 toc
= sresize(toc
, tocsize
, htmlsect
*);
1002 if (leaf
&& conf
.leaf_contains_contents
&&
1003 ntoc
>= conf
.leaf_smallest_contents
) {
1006 for (i
= 0; i
< ntoc
; i
++) {
1007 htmlsect
*s
= toc
[i
];
1008 int hlevel
= (s
->type
== TOP ?
-1 :
1009 s
->type
== INDEX ?
0 :
1010 heading_depth(s
->title
))
1011 - f
->min_heading_depth
+ 1;
1013 assert(hlevel
>= 1);
1014 html_contents_entry(&ho
, hlevel
, s
,
1015 f
, keywords
, &conf
);
1017 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1022 * Now go through the document and output some real
1026 for (s
= sects
.head
; s
; s
= s
->next
) {
1029 * This section belongs in this file.
1035 * Doesn't belong in this file, but it may be
1036 * a descendant of a section which does, in
1037 * which case we should consider it for the
1038 * main TOC of this file (for non-leaf files).
1046 * Search up from this section until we find
1047 * the highest-level one which belongs in this
1052 for (ac
= s
; ac
; ac
= ac
->parent
) {
1053 if (ac
->file
== f
) {
1062 * This section does not belong in this
1063 * file, but an ancestor of it does. Write
1064 * out a contents table entry, if the depth
1065 * doesn't exceed the maximum contents
1066 * depth for the ancestor section.
1068 if (adepth
<= a
->contents_depth
) {
1069 html_contents_entry(&ho
, adepth
, s
,
1070 f
, keywords
, &conf
);
1079 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1082 * Display the section heading.
1085 hlevel
= (s
->type
== TOP ?
-1 :
1086 s
->type
== INDEX ?
0 :
1087 heading_depth(s
->title
))
1088 - f
->min_heading_depth
+ 1;
1089 assert(hlevel
>= 1);
1090 /* HTML headings only go up to <h6> */
1094 htag
[1] = '0' + hlevel
;
1096 element_open(&ho
, htag
);
1099 * Provide anchor(s) for cross-links to target.
1101 * (Also we'll have to do this separately in
1102 * other paragraph types - NumberedList and
1107 for (i
=0; i
< conf
.ntfragments
; i
++)
1108 if (s
->fragments
[i
])
1109 html_fragment(&ho
, s
->fragments
[i
]);
1112 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1114 element_close(&ho
, htag
);
1117 * Now display the section text.
1120 stackhead
= snew(struct stackelement
);
1121 stackhead
->next
= NULL
;
1122 stackhead
->listtype
= NOLIST
;
1123 stackhead
->itemtype
= NOITEM
;
1125 for (p
= s
->text
;; p
= p
->next
) {
1126 enum LISTTYPE listtype
;
1127 struct stackelement
*se
;
1130 * Preliminary switch to figure out what
1131 * sort of list we expect to be inside at
1134 * Since p may still be NULL at this point,
1135 * I invent a harmless paragraph type for
1138 switch (p ? p
->type
: para_Normal
) {
1141 case para_Copyright
:
1142 case para_BiblioCited
:
1144 case para_QuotePush
:
1148 case para_UnnumberedChapter
:
1159 case para_NumberedList
:
1163 case para_DescribedThing
:
1164 case para_Description
:
1168 case para_LcontPush
:
1169 se
= snew(struct stackelement
);
1170 se
->next
= stackhead
;
1171 se
->listtype
= NOLIST
;
1172 se
->itemtype
= NOITEM
;
1176 default: /* some totally non-printing para */
1183 * Terminate the most recent list item, if
1184 * any. (We left this until after
1185 * processing LcontPush, since in that case
1186 * the list item won't want to be
1187 * terminated until after the corresponding
1190 if (stackhead
->itemtype
!= NOITEM
) {
1191 element_close(&ho
, itemname(stackhead
->itemtype
));
1194 stackhead
->itemtype
= NOITEM
;
1197 * Terminate the current list, if it's not
1198 * the one we want to be in.
1200 if (listtype
!= stackhead
->listtype
&&
1201 stackhead
->listtype
!= NOLIST
) {
1202 element_close(&ho
, listname(stackhead
->listtype
));
1207 * Leave the loop if our time has come.
1209 if (!p
|| (is_heading_type(p
->type
) &&
1210 p
->type
!= para_Title
))
1211 break; /* end of section text */
1214 * Start a fresh list if necessary.
1216 if (listtype
!= stackhead
->listtype
&&
1218 element_open(&ho
, listname(listtype
));
1220 stackhead
->listtype
= listtype
;
1224 element_empty(&ho
, "hr");
1227 html_codepara(&ho
, p
->words
);
1230 case para_Copyright
:
1231 element_open(&ho
, "p");
1233 html_words(&ho
, p
->words
, ALL
,
1234 f
, keywords
, &conf
);
1236 element_close(&ho
, "p");
1238 case para_BiblioCited
:
1239 element_open(&ho
, "p");
1240 if (p
->private_data
) {
1241 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1243 for (i
=0; i
< conf
.ntfragments
; i
++)
1244 if (s
->fragments
[i
])
1245 html_fragment(&ho
, s
->fragments
[i
]);
1248 html_words(&ho
, p
->kwtext
, ALL
,
1249 f
, keywords
, &conf
);
1250 html_text(&ho
, L
" ");
1251 html_words(&ho
, p
->words
, ALL
,
1252 f
, keywords
, &conf
);
1254 element_close(&ho
, "p");
1257 case para_NumberedList
:
1258 element_open(&ho
, "li");
1259 if (p
->private_data
) {
1260 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1262 for (i
=0; i
< conf
.ntfragments
; i
++)
1263 if (s
->fragments
[i
])
1264 html_fragment(&ho
, s
->fragments
[i
]);
1267 stackhead
->itemtype
= LI
;
1268 html_words(&ho
, p
->words
, ALL
,
1269 f
, keywords
, &conf
);
1271 case para_DescribedThing
:
1272 element_open(&ho
, "dt");
1274 stackhead
->itemtype
= DT
;
1275 html_words(&ho
, p
->words
, ALL
,
1276 f
, keywords
, &conf
);
1278 case para_Description
:
1279 element_open(&ho
, "dd");
1281 stackhead
->itemtype
= DD
;
1282 html_words(&ho
, p
->words
, ALL
,
1283 f
, keywords
, &conf
);
1286 case para_QuotePush
:
1287 element_open(&ho
, "blockquote");
1290 element_close(&ho
, "blockquote");
1295 stackhead
= stackhead
->next
;
1302 assert(stackhead
&& !stackhead
->next
);
1306 if (s
->type
== INDEX
) {
1311 * This section is the index. I'll just
1312 * render it as a single paragraph, with a
1313 * colon between the index term and the
1314 * references, and <br> in between each
1317 element_open(&ho
, "p");
1319 for (i
= 0; (entry
=
1320 index234(idx
->entries
, i
)) != NULL
; i
++) {
1321 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1325 element_empty(&ho
, "br");
1328 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1329 f
, keywords
, &conf
);
1331 html_text(&ho
, conf
.index_main_sep
);
1333 for (j
= 0; j
< hi
->nrefs
; j
++) {
1335 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1336 paragraph
*p
= hr
->section
->title
;
1339 html_text(&ho
, conf
.index_multi_sep
);
1341 html_href(&ho
, f
, hr
->section
->file
,
1343 hr
->referenced
= TRUE
;
1345 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1346 f
, keywords
, &conf
);
1347 else if (p
&& p
->words
)
1348 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1349 f
, keywords
, &conf
);
1352 * If there is no title at all,
1353 * this must be because our
1354 * target section is the
1355 * preamble section and there
1356 * is no title. So we use the
1359 html_text(&ho
, conf
.preamble_text
);
1361 element_close(&ho
, "a");
1364 element_close(&ho
, "p");
1369 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1376 int done_version_ids
= FALSE
;
1378 element_empty(&ho
, "hr");
1381 html_raw(&ho
, conf
.body_end
);
1383 if (conf
.address_section
) {
1384 int started
= FALSE
;
1385 if (conf
.htmlver
== ISO_HTML
) {
1387 * The ISO-HTML validator complains if
1388 * there isn't a <div> tag surrounding the
1389 * <address> tag. I'm uncertain of why this
1390 * should be - there appears to be no
1391 * mention of this in the ISO-HTML spec,
1392 * suggesting that it doesn't represent a
1393 * change from HTML 4, but nonetheless the
1394 * HTML 4 validator doesn't seem to mind.
1396 element_open(&ho
, "div");
1398 element_open(&ho
, "address");
1399 if (conf
.addr_start
) {
1400 html_raw(&ho
, conf
.addr_start
);
1404 if (conf
.visible_version_id
) {
1405 for (p
= sourceform
; p
; p
= p
->next
)
1406 if (p
->type
== para_VersionID
) {
1408 element_empty(&ho
, "br");
1410 html_text(&ho
, conf
.pre_versionid
);
1411 html_words(&ho
, p
->words
, NOTHING
,
1412 f
, keywords
, &conf
);
1413 html_text(&ho
, conf
.post_versionid
);
1416 done_version_ids
= TRUE
;
1418 if (conf
.addr_end
) {
1420 element_empty(&ho
, "br");
1421 html_raw(&ho
, conf
.addr_end
);
1423 element_close(&ho
, "address");
1424 if (conf
.htmlver
== ISO_HTML
)
1425 element_close(&ho
, "div");
1428 if (!done_version_ids
) {
1430 * If the user didn't want the version IDs
1431 * visible, I think we still have a duty to put
1432 * them in an HTML comment.
1434 int started
= FALSE
;
1435 for (p
= sourceform
; p
; p
= p
->next
)
1436 if (p
->type
== para_VersionID
) {
1438 html_raw(&ho
, "<!-- version IDs:\n");
1441 html_words(&ho
, p
->words
, NOTHING
,
1442 f
, keywords
, &conf
);
1446 html_raw(&ho
, "-->\n");
1450 element_close(&ho
, "body");
1452 element_close(&ho
, "html");
1459 * Go through and check that no index fragments were referenced
1460 * without being generated, or indeed vice versa.
1462 * (When I actually get round to freeing everything, this can
1463 * probably be the freeing loop as well.)
1465 for (p
= sourceform
; p
; p
= p
->next
) {
1467 for (w
= p
->words
; w
; w
= w
->next
)
1468 if (w
->type
== word_IndexRef
) {
1469 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1471 assert(!hr
->referenced
== !hr
->generated
);
1476 * Free all the working data.
1480 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1482 * frag->fragment is dynamically allocated, but will be
1483 * freed when we process the htmlsect structure which
1484 * it is attached to.
1488 freetree234(files
.frags
);
1491 htmlsect
*sect
, *tmp
;
1496 for (i
=0; i
< conf
.ntfragments
; i
++)
1497 sfree(sect
->fragments
[i
]);
1498 sfree(sect
->fragments
);
1502 sect
= nonsects
.head
;
1506 for (i
=0; i
< conf
.ntfragments
; i
++)
1507 sfree(sect
->fragments
[i
]);
1508 sfree(sect
->fragments
);
1514 htmlfile
*file
, *tmp
;
1518 sfree(file
->filename
);
1526 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1527 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1534 for (p
= sourceform
; p
; p
= p
->next
)
1535 for (w
= p
->words
; w
; w
= w
->next
)
1536 if (w
->type
== word_IndexRef
) {
1537 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1539 sfree(hr
->fragment
);
1544 sfree(conf
.single_filename
);
1545 sfree(conf
.contents_filename
);
1546 sfree(conf
.index_filename
);
1547 sfree(conf
.template_filename
);
1548 while (conf
.ntfragments
--)
1549 sfree(conf
.template_fragments
[conf
.ntfragments
]);
1550 sfree(conf
.template_fragments
);
1553 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1554 htmlsect
*sect
, int depth
)
1560 * `depth' is derived from the heading_depth() macro at the top
1561 * of this file, which counts title as -1, chapter as 0,
1562 * heading as 1 and subsection as 2. However, the semantics of
1563 * cfg->leaf_level are defined to count chapter as 1, heading
1564 * as 2 etc. So first I increment depth :-(
1568 if (cfg
->leaf_level
== 0) {
1570 * leaf_level==0 is a special case, in which everything is
1571 * put into a single file.
1574 files
->single
= html_new_file(files
, cfg
->single_filename
);
1576 file
= files
->single
;
1579 * If the depth of this section is at or above leaf_level,
1580 * we invent a fresh file and put this section at its head.
1581 * Otherwise, we put it in the same file as its parent
1584 if (ldepth
> cfg
->leaf_level
) {
1586 * We know that sect->parent cannot be NULL. The only
1587 * circumstance in which it can be is if sect is at
1588 * chapter or appendix level, i.e. ldepth==1; and if
1589 * that's the case, then we cannot have entered this
1590 * branch unless cfg->leaf_level==0, in which case we
1591 * would be in the single-file case above and not here
1594 assert(sect
->parent
);
1596 file
= sect
->parent
->file
;
1598 if (sect
->type
== TOP
) {
1599 file
= html_new_file(files
, cfg
->contents_filename
);
1600 } else if (sect
->type
== INDEX
) {
1601 file
= html_new_file(files
, cfg
->index_filename
);
1605 assert(ldepth
> 0 && sect
->title
);
1606 title
= html_format(sect
->title
, cfg
->template_filename
);
1607 file
= html_new_file(files
, title
);
1615 if (file
->min_heading_depth
> depth
) {
1617 * This heading is at a higher level than any heading we
1618 * have so far placed in this file; so we set the `first'
1621 file
->min_heading_depth
= depth
;
1625 if (file
->min_heading_depth
== depth
)
1629 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1631 htmlfile
*ret
= snew(htmlfile
);
1635 list
->tail
->next
= ret
;
1640 ret
->filename
= dupstr(filename
);
1641 ret
->last_fragment_number
= 0;
1642 ret
->min_heading_depth
= INT_MAX
;
1643 ret
->first
= ret
->last
= NULL
;
1648 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
1651 htmlsect
*ret
= snew(htmlsect
);
1655 list
->tail
->next
= ret
;
1665 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
1668 for (i
=0; i
< cfg
->ntfragments
; i
++)
1669 ret
->fragments
[i
] = NULL
;
1675 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1676 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1682 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1683 case word_HyperLink
:
1684 if (flags
& LINKS
) {
1685 element_open(ho
, "a");
1686 c
= utoa_dup(w
->text
, CS_ASCII
);
1687 element_attr(ho
, "href", c
);
1691 case word_UpperXref
:
1692 case word_LowerXref
:
1693 if (flags
& LINKS
) {
1694 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1700 s
= (htmlsect
*)p
->private_data
;
1704 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
1710 element_close(ho
, "a");
1713 if (flags
& INDEXENTS
) {
1714 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1715 html_fragment(ho
, hr
->fragment
);
1716 hr
->generated
= TRUE
;
1723 case word_WhiteSpace
:
1724 case word_EmphSpace
:
1725 case word_CodeSpace
:
1726 case word_WkCodeSpace
:
1728 case word_EmphQuote
:
1729 case word_CodeQuote
:
1730 case word_WkCodeQuote
:
1731 style
= towordstyle(w
->type
);
1732 type
= removeattr(w
->type
);
1733 if (style
== word_Emph
&&
1734 (attraux(w
->aux
) == attr_First
||
1735 attraux(w
->aux
) == attr_Only
) &&
1737 element_open(ho
, "em");
1738 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1739 (attraux(w
->aux
) == attr_First
||
1740 attraux(w
->aux
) == attr_Only
) &&
1742 element_open(ho
, "code");
1744 if (type
== word_WhiteSpace
)
1745 html_text(ho
, L
" ");
1746 else if (type
== word_Quote
) {
1747 if (quoteaux(w
->aux
) == quote_Open
)
1748 html_text(ho
, cfg
->lquote
);
1750 html_text(ho
, cfg
->rquote
);
1752 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
1753 html_text_nbsp(ho
, w
->text
);
1755 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1758 if (style
== word_Emph
&&
1759 (attraux(w
->aux
) == attr_Last
||
1760 attraux(w
->aux
) == attr_Only
) &&
1762 element_close(ho
, "em");
1763 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1764 (attraux(w
->aux
) == attr_Last
||
1765 attraux(w
->aux
) == attr_Only
) &&
1767 element_close(ho
, "code");
1773 static void html_codepara(htmloutput
*ho
, word
*words
)
1775 element_open(ho
, "pre");
1776 element_open(ho
, "code");
1777 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1782 if (words
->next
&& words
->next
->type
== word_Emph
) {
1783 e
= words
->next
->text
;
1784 words
= words
->next
;
1788 while (e
&& *e
&& *t
) {
1792 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1800 element_open(ho
, open_tag
);
1802 html_text_limit(ho
, t
, n
);
1805 element_close(ho
, open_tag
);
1813 element_close(ho
, "code");
1814 element_close(ho
, "pre");
1817 static void html_charset_cleanup(htmloutput
*ho
)
1822 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1823 ho
->charset
, &ho
->cstate
, NULL
);
1824 if (ho
->fp
&& bytes
> 0)
1825 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1828 static void return_mostly_to_neutral(htmloutput
*ho
)
1831 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1832 fprintf(ho
->fp
, " />");
1833 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1834 fprintf(ho
->fp
, ">");
1838 ho
->state
= HO_NEUTRAL
;
1841 static void return_to_neutral(htmloutput
*ho
)
1843 if (ho
->state
== HO_IN_TEXT
) {
1844 html_charset_cleanup(ho
);
1847 return_mostly_to_neutral(ho
);
1850 static void element_open(htmloutput
*ho
, char const *name
)
1852 return_to_neutral(ho
);
1854 fprintf(ho
->fp
, "<%s", name
);
1855 ho
->state
= HO_IN_TAG
;
1858 static void element_close(htmloutput
*ho
, char const *name
)
1860 return_to_neutral(ho
);
1862 fprintf(ho
->fp
, "</%s>", name
);
1863 ho
->state
= HO_NEUTRAL
;
1866 static void element_empty(htmloutput
*ho
, char const *name
)
1868 return_to_neutral(ho
);
1870 fprintf(ho
->fp
, "<%s", name
);
1871 ho
->state
= HO_IN_EMPTY_TAG
;
1874 static void html_nl(htmloutput
*ho
)
1876 return_to_neutral(ho
);
1878 fputc('\n', ho
->fp
);
1881 static void html_raw(htmloutput
*ho
, char *text
)
1883 return_to_neutral(ho
);
1885 fputs(text
, ho
->fp
);
1888 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1890 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1893 fputs(text
, ho
->fp
);
1897 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1899 html_charset_cleanup(ho
);
1900 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1902 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1905 static void element_attr_w(htmloutput
*ho
, char const *name
,
1906 wchar_t const *value
)
1908 html_charset_cleanup(ho
);
1910 fprintf(ho
->fp
, " %s=\"", name
);
1911 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
1912 html_charset_cleanup(ho
);
1917 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1919 return_mostly_to_neutral(ho
);
1920 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
1923 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
1925 return_mostly_to_neutral(ho
);
1926 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
1929 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1931 return_mostly_to_neutral(ho
);
1932 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
1935 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1936 int maxlen
, int quote_quotes
, int nbsp
)
1938 int textlen
= ustrlen(text
);
1942 if (maxlen
> 0 && textlen
> maxlen
)
1945 while (textlen
> 0) {
1946 /* Scan ahead for characters we really can't display in HTML. */
1947 int lenbefore
, lenafter
;
1948 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1949 if (text
[lenbefore
] == L
'<' ||
1950 text
[lenbefore
] == L
'>' ||
1951 text
[lenbefore
] == L
'&' ||
1952 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
1953 (text
[lenbefore
] == L
' ' && nbsp
))
1955 lenafter
= lenbefore
;
1956 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1957 ho
->charset
, &ho
->cstate
, &err
);
1958 textlen
-= (lenbefore
- lenafter
);
1959 if (bytes
> 0 && ho
->fp
)
1960 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1963 * We have encountered a character that cannot be
1964 * displayed in the selected output charset. Therefore,
1965 * we use an HTML numeric entity reference.
1967 assert(textlen
> 0);
1969 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1971 } else if (lenafter
== 0 && textlen
> 0) {
1973 * We have encountered a character which is special to
1978 fprintf(ho
->fp
, "<");
1979 else if (*text
== L
'>')
1980 fprintf(ho
->fp
, ">");
1981 else if (*text
== L
'&')
1982 fprintf(ho
->fp
, "&");
1983 else if (*text
== L
'"')
1984 fprintf(ho
->fp
, """);
1985 else if (*text
== L
' ') {
1987 fprintf(ho
->fp
, " ");
1989 assert(!"Can't happen");
1996 static void cleanup(htmloutput
*ho
)
1998 return_to_neutral(ho
);
2003 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
2004 htmlfile
*targetfile
, char *targetfrag
)
2006 rdstringc rs
= { 0, 0, NULL
};
2009 if (targetfile
!= thisfile
)
2010 rdaddsc(&rs
, targetfile
->filename
);
2013 rdaddsc(&rs
, targetfrag
);
2017 element_open(ho
, "a");
2018 element_attr(ho
, "href", url
);
2022 static void html_fragment(htmloutput
*ho
, char const *fragment
)
2024 element_open(ho
, "a");
2025 element_attr(ho
, "name", fragment
);
2026 if (is_xhtml(ho
->ver
))
2027 element_attr(ho
, "id", fragment
);
2028 element_close(ho
, "a");
2031 static char *html_format(paragraph
*p
, char *template_string
)
2035 wchar_t *ws
, wsbuf
[2];
2036 rdstringc rs
= { 0, 0, NULL
};
2038 t
= template_string
;
2040 if (*t
== '%' && t
[1]) {
2054 if (p
->kwtext
&& fmt
== 'n')
2056 else if (p
->kwtext2
&& fmt
== 'b') {
2058 * HTML fragment names must start with a letter, so
2059 * simply `1.2.3' is not adequate. In this case I'm
2060 * going to cheat slightly by prepending the first
2061 * character of the first word of kwtext, so that
2062 * we get `C1' for chapter 1, `S2.3' for section
2065 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2068 wsbuf
[0] = p
->kwtext
->text
[0];
2071 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2074 /* %N comes here; also failure cases of other fmts */
2078 c
= utoa_dup(ws
, CS_ASCII
);
2084 if (removeattr(w
->type
) == word_Normal
) {
2085 c
= utoa_dup(w
->text
, CS_ASCII
);
2096 return rdtrimc(&rs
);
2099 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2103 * The HTML 4 spec's strictest definition of fragment names (<a
2104 * name> and "id" attributes) says that they `must begin with a
2105 * letter and may be followed by any number of letters, digits,
2106 * hyphens, underscores, colons, and periods'.
2108 * So here we unceremoniously rip out any characters not
2109 * conforming to this limitation.
2111 char *p
= text
, *q
= text
;
2113 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2115 if ((*q
++ = *p
++) != '\0') {
2117 if ((*p
>='A' && *p
<='Z') ||
2118 (*p
>='a' && *p
<='z') ||
2119 (*p
>='0' && *p
<='9') ||
2120 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2128 /* If there's nothing left, make something valid up */
2130 static const char anonfrag
[] = "anon";
2131 text
= sresize(text
, lenof(anonfrag
), char);
2132 strcpy(text
, anonfrag
);
2136 * Now we check for clashes with other fragment names, and
2137 * adjust this one if necessary by appending a hyphen followed
2141 htmlfragment
*frag
= snew(htmlfragment
);
2142 int len
= 0; /* >0 indicates we have resized */
2146 frag
->fragment
= text
;
2148 while (add234(files
->frags
, frag
) != frag
) {
2151 frag
->fragment
= text
= sresize(text
, len
+20, char);
2154 sprintf(text
+ len
, "-%d", ++suffix
);
2161 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2162 htmlfile
*thisfile
, keywordlist
*keywords
,
2165 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2166 element_close(ho
, "li");
2170 while (ho
->contents_level
> depth
) {
2171 element_close(ho
, "ul");
2172 ho
->contents_level
--;
2173 if (ho
->contents_level
> 0) {
2174 element_close(ho
, "li");
2179 while (ho
->contents_level
< depth
) {
2181 element_open(ho
, "ul");
2183 ho
->contents_level
++;
2189 element_open(ho
, "li");
2190 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2191 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2192 element_close(ho
, "a");
2193 /* <li> will be closed by a later invocation */
2196 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2197 keywordlist
*keywords
, htmlconfig
*cfg
,
2203 int depth
= heading_depth(s
->title
);
2207 else if (depth
== 0)
2208 sl
= &cfg
->achapter
;
2209 else if (depth
<= cfg
->nasect
)
2210 sl
= &cfg
->asect
[depth
-1];
2212 sl
= &cfg
->asect
[cfg
->nasect
-1];
2216 else if (sl
->just_numbers
)
2217 number
= s
->title
->kwtext2
;
2219 number
= s
->title
->kwtext
;
2222 html_words(ho
, number
, MARKUP
,
2223 thisfile
, keywords
, cfg
);
2224 html_text(ho
, sl
->number_suffix
);
2227 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2228 thisfile
, keywords
, cfg
);
2230 assert(s
->type
!= NORMAL
);
2232 * If we're printing the full document title for _real_ and
2233 * there isn't one, we don't want to print `Preamble' at
2234 * the top of what ought to just be some text. If we need
2235 * it in any other context such as TOCs, we need to print
2238 if (s
->type
== TOP
&& !real
)
2239 html_text(ho
, cfg
->preamble_text
);
2240 else if (s
->type
== INDEX
)
2241 html_text(ho
, cfg
->index_text
);