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
);
262 } else if (!ustricmp(p
->keyword
, L
"index")) {
263 ret
.index_text
= uadv(p
->keyword
);
264 } else if (!ustricmp(p
->keyword
, L
"contents")) {
265 ret
.contents_text
= uadv(p
->keyword
);
270 for (p
= source
; p
; p
= p
->next
) {
271 if (p
->type
== para_Config
) {
272 wchar_t *k
= p
->keyword
;
274 if (!ustrnicmp(k
, L
"xhtml-", 6))
275 k
++; /* treat `xhtml-' and `html-' the same */
277 if (!ustricmp(k
, L
"html-restrict-charset")) {
278 ret
.restrict_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
279 } else if (!ustricmp(k
, L
"html-output-charset")) {
280 ret
.output_charset
= charset_from_ustr(&p
->fpos
, uadv(k
));
281 } else if (!ustricmp(k
, L
"html-version")) {
282 wchar_t *vername
= uadv(k
);
283 static const struct {
287 {L
"html3.2", HTML_3_2
},
289 {L
"iso-html", ISO_HTML
},
290 {L
"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL
},
291 {L
"xhtml1.0strict", XHTML_1_0_STRICT
}
295 for (i
= 0; i
< (int)lenof(versions
); i
++)
296 if (!ustricmp(versions
[i
].name
, vername
))
299 if (i
== lenof(versions
))
300 error(err_htmlver
, &p
->fpos
, vername
);
302 ret
.htmlver
= versions
[i
].ver
;
303 } else if (!ustricmp(k
, L
"html-single-filename")) {
304 sfree(ret
.single_filename
);
305 ret
.single_filename
= dupstr(adv(p
->origkeyword
));
306 } else if (!ustricmp(k
, L
"html-contents-filename")) {
307 sfree(ret
.contents_filename
);
308 ret
.contents_filename
= dupstr(adv(p
->origkeyword
));
309 } else if (!ustricmp(k
, L
"html-index-filename")) {
310 sfree(ret
.index_filename
);
311 ret
.index_filename
= dupstr(adv(p
->origkeyword
));
312 } else if (!ustricmp(k
, L
"html-template-filename")) {
313 sfree(ret
.template_filename
);
314 ret
.template_filename
= dupstr(adv(p
->origkeyword
));
315 } else if (!ustricmp(k
, L
"html-template-fragment")) {
316 char *frag
= adv(p
->origkeyword
);
318 while (ret
.ntfragments
--)
319 sfree(ret
.template_fragments
[ret
.ntfragments
]);
320 sfree(ret
.template_fragments
);
321 ret
.template_fragments
= NULL
;
325 ret
.template_fragments
=
326 sresize(ret
.template_fragments
,
327 ret
.ntfragments
, char *);
328 ret
.template_fragments
[ret
.ntfragments
-1] =
333 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 1);
334 } else if (!ustricmp(k
, L
"html-chapter-numeric")) {
335 ret
.achapter
.just_numbers
= utob(uadv(k
));
336 } else if (!ustricmp(k
, L
"html-chapter-suffix")) {
337 ret
.achapter
.number_suffix
= uadv(k
);
338 } else if (!ustricmp(k
, L
"html-leaf-level")) {
339 ret
.leaf_level
= utoi(uadv(k
));
340 } else if (!ustricmp(k
, L
"html-section-numeric")) {
341 wchar_t *q
= uadv(k
);
347 if (n
>= ret
.nasect
) {
349 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
350 for (i
= ret
.nasect
; i
<= n
; i
++)
351 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
354 ret
.asect
[n
].just_numbers
= utob(q
);
355 } else if (!ustricmp(k
, L
"html-section-suffix")) {
356 wchar_t *q
= uadv(k
);
362 if (n
>= ret
.nasect
) {
364 ret
.asect
= sresize(ret
.asect
, n
+1, sectlevel
);
365 for (i
= ret
.nasect
; i
<= n
; i
++) {
366 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
370 ret
.asect
[n
].number_suffix
= q
;
371 } else if (!ustricmp(k
, L
"html-contents-depth") ||
372 !ustrnicmp(k
, L
"html-contents-depth-", 20)) {
374 * Relic of old implementation: this directive used
375 * to be written as \cfg{html-contents-depth-3}{2}
376 * rather than the usual Halibut convention of
377 * \cfg{html-contents-depth}{3}{2}. We therefore
380 wchar_t *q
= k
[19] ? k
+20 : uadv(k
);
386 if (n
>= ret
.ncdepths
) {
388 ret
.contents_depths
=
389 sresize(ret
.contents_depths
, n
+1, int);
390 for (i
= ret
.ncdepths
; i
<= n
; i
++) {
391 ret
.contents_depths
[i
] = i
+2;
395 ret
.contents_depths
[n
] = utoi(q
);
396 } else if (!ustricmp(k
, L
"html-head-end")) {
397 ret
.head_end
= adv(p
->origkeyword
);
398 } else if (!ustricmp(k
, L
"html-body-tag")) {
399 ret
.body_tag
= adv(p
->origkeyword
);
400 } else if (!ustricmp(k
, L
"html-body-start")) {
401 ret
.body_start
= adv(p
->origkeyword
);
402 } else if (!ustricmp(k
, L
"html-body-end")) {
403 ret
.body_end
= adv(p
->origkeyword
);
404 } else if (!ustricmp(k
, L
"html-address-start")) {
405 ret
.addr_start
= adv(p
->origkeyword
);
406 } else if (!ustricmp(k
, L
"html-address-end")) {
407 ret
.addr_end
= adv(p
->origkeyword
);
408 } else if (!ustricmp(k
, L
"html-navigation-attributes")) {
409 ret
.nav_attr
= adv(p
->origkeyword
);
410 } else if (!ustricmp(k
, L
"html-author")) {
411 ret
.author
= uadv(k
);
412 } else if (!ustricmp(k
, L
"html-description")) {
413 ret
.description
= uadv(k
);
414 } else if (!ustricmp(k
, L
"html-suppress-address")) {
415 ret
.address_section
= !utob(uadv(k
));
416 } else if (!ustricmp(k
, L
"html-versionid")) {
417 ret
.visible_version_id
= utob(uadv(k
));
418 } else if (!ustricmp(k
, L
"html-quotes")) {
419 if (*uadv(k
) && *uadv(uadv(k
))) {
420 ret
.lquote
= uadv(k
);
421 ret
.rquote
= uadv(ret
.lquote
);
423 } else if (!ustricmp(k
, L
"html-leaf-contains-contents")) {
424 ret
.leaf_contains_contents
= utob(uadv(k
));
425 } else if (!ustricmp(k
, L
"html-leaf-smallest-contents")) {
426 ret
.leaf_smallest_contents
= utoi(uadv(k
));
427 } else if (!ustricmp(k
, L
"html-index-text")) {
428 ret
.index_text
= uadv(k
);
429 } else if (!ustricmp(k
, L
"html-contents-text")) {
430 ret
.contents_text
= uadv(k
);
431 } else if (!ustricmp(k
, L
"html-preamble-text")) {
432 ret
.preamble_text
= uadv(k
);
433 } else if (!ustricmp(k
, L
"html-title-separator")) {
434 ret
.title_separator
= uadv(k
);
435 } else if (!ustricmp(k
, L
"html-nav-prev-text")) {
436 ret
.nav_prev_text
= uadv(k
);
437 } else if (!ustricmp(k
, L
"html-nav-next-text")) {
438 ret
.nav_next_text
= uadv(k
);
439 } else if (!ustricmp(k
, L
"html-nav-separator")) {
440 ret
.nav_separator
= uadv(k
);
441 } else if (!ustricmp(k
, L
"html-index-main-separator")) {
442 ret
.index_main_sep
= uadv(k
);
443 } else if (!ustricmp(k
, L
"html-index-multiple-separator")) {
444 ret
.index_multi_sep
= uadv(k
);
445 } else if (!ustricmp(k
, L
"html-pre-versionid")) {
446 ret
.pre_versionid
= uadv(k
);
447 } else if (!ustricmp(k
, L
"html-post-versionid")) {
448 ret
.post_versionid
= uadv(k
);
454 * Now process fallbacks on quote characters.
456 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
457 (!cvt_ok(ret
.restrict_charset
, ret
.lquote
) ||
458 !cvt_ok(ret
.restrict_charset
, ret
.rquote
))) {
459 ret
.lquote
= uadv(ret
.rquote
);
460 ret
.rquote
= uadv(ret
.lquote
);
466 paragraph
*html_config_filename(char *filename
)
469 * If the user passes in a single filename as a parameter to
470 * the `--html' command-line option, then we should assume it
471 * to imply _two_ config directives:
472 * \cfg{html-single-filename}{whatever} and
473 * \cfg{html-leaf-level}{0}; the rationale being that the user
474 * wants their output _in that file_.
478 p
= cmdline_cfg_simple("html-single-filename", filename
, NULL
);
479 q
= cmdline_cfg_simple("html-leaf-level", "0", NULL
);
484 void html_backend(paragraph
*sourceform
, keywordlist
*keywords
,
485 indexdata
*idx
, void *unused
)
489 htmlfilelist files
= { NULL
, NULL
, NULL
, NULL
, NULL
};
490 htmlsectlist sects
= { NULL
, NULL
}, nonsects
= { NULL
, NULL
};
495 conf
= html_configure(sourceform
);
498 * We're going to make heavy use of paragraphs' private data
499 * fields in the forthcoming code. Clear them first, so we can
500 * reliably tell whether we have auxiliary data for a
501 * particular paragraph.
503 for (p
= sourceform
; p
; p
= p
->next
)
504 p
->private_data
= NULL
;
506 files
.frags
= newtree234(html_fragment_compare
);
509 * Start by figuring out into which file each piece of the
510 * document should be put. We'll do this by inventing an
511 * `htmlsect' structure and stashing it in the private_data
512 * field of each section paragraph; we also need one additional
513 * htmlsect for the document index, which won't show up in the
514 * source form but needs to be consistently mentioned in
517 * While we're here, we'll also invent the HTML fragment name(s)
521 htmlsect
*topsect
, *sect
;
524 topsect
= html_new_sect(§s
, NULL
, &conf
);
526 topsect
->title
= NULL
;
527 topsect
->text
= sourceform
;
528 topsect
->contents_depth
= contents_depth(conf
, 0);
529 html_file_section(&conf
, &files
, topsect
, -1);
531 for (p
= sourceform
; p
; p
= p
->next
)
532 if (is_heading_type(p
->type
)) {
533 d
= heading_depth(p
);
535 if (p
->type
== para_Title
) {
540 sect
= html_new_sect(§s
, p
, &conf
);
541 sect
->text
= p
->next
;
543 sect
->contents_depth
= contents_depth(conf
, d
+1) - (d
+1);
546 sect
->parent
= (htmlsect
*)p
->parent
->private_data
;
547 assert(sect
->parent
!= NULL
);
549 sect
->parent
= topsect
;
550 p
->private_data
= sect
;
552 html_file_section(&conf
, &files
, sect
, d
);
556 for (i
=0; i
< conf
.ntfragments
; i
++) {
558 html_format(p
, conf
.template_fragments
[i
]);
560 html_sanitise_fragment(&files
, sect
->file
,
566 /* And the index, if we have one. */
567 has_index
= (count234(idx
->entries
) > 0);
569 sect
= html_new_sect(§s
, NULL
, &conf
);
572 sect
->parent
= topsect
;
573 sect
->contents_depth
= 0;
574 html_file_section(&conf
, &files
, sect
, 0); /* peer of chapters */
575 sect
->fragments
[0] = utoa_dup(conf
.index_text
, CS_ASCII
);
576 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
578 files
.index
= sect
->file
;
583 * Go through the keyword list and sort out fragment IDs for
584 * all the potentially referenced paragraphs which _aren't_
592 for (i
= 0; (kw
= index234(keywords
->keys
, i
)) != NULL
; i
++) {
593 paragraph
*q
, *p
= kw
->para
;
595 if (!is_heading_type(p
->type
)) {
599 * Find the paragraph's parent htmlsect, to
600 * determine which file it will end up in.
605 * Preamble paragraphs have no parent. So if we
606 * have a non-heading with no parent, it must
607 * be preamble, and therefore its parent
608 * htmlsect must be the preamble one.
611 sects
.head
->type
== TOP
);
614 parent
= (htmlsect
*)q
->private_data
;
617 * Now we can construct an htmlsect for this
618 * paragraph itself, taking care to put it in the
619 * list of non-sections rather than the list of
620 * sections (so that traverses of the `sects' list
621 * won't attempt to add it to the contents or
622 * anything weird like that).
624 sect
= html_new_sect(&nonsects
, p
, &conf
);
625 sect
->file
= parent
->file
;
626 sect
->parent
= parent
;
627 p
->private_data
= sect
;
630 * Fragment IDs for these paragraphs will simply be
631 * `p' followed by an integer.
633 sect
->fragments
[0] = snewn(40, char);
634 sprintf(sect
->fragments
[0], "p%d",
635 sect
->file
->last_fragment_number
++);
636 sect
->fragments
[0] = html_sanitise_fragment(&files
, sect
->file
,
643 * Reset the fragment numbers in each file. I've just used them
644 * to generate `p' fragment IDs for non-section paragraphs
645 * (numbered list elements, bibliocited), and now I want to use
646 * them for `i' fragment IDs for index entries.
650 for (file
= files
.head
; file
; file
= file
->next
)
651 file
->last_fragment_number
= 0;
655 * Now sort out the index. This involves:
657 * - For each index term, we set up an htmlindex structure to
658 * store all the references to that term.
660 * - Then we make a pass over the actual document, finding
661 * every word_IndexRef; for each one, we actually figure out
662 * the HTML filename/fragment pair we will use to reference
663 * it, store that information in the private data field of
664 * the word_IndexRef itself (so we can recreate it when the
665 * time comes to output our HTML), and add a reference to it
666 * to the index term in question.
675 * Set up the htmlindex structures.
678 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
679 htmlindex
*hi
= snew(htmlindex
);
681 hi
->nrefs
= hi
->refsize
= 0;
684 entry
->backend_data
= hi
;
688 * Run over the document inventing fragments. Each fragment
689 * is of the form `i' followed by an integer.
691 lastsect
= sects
.head
; /* this is always the top section */
692 for (p
= sourceform
; p
; p
= p
->next
) {
693 if (is_heading_type(p
->type
) && p
->type
!= para_Title
)
694 lastsect
= (htmlsect
*)p
->private_data
;
696 for (w
= p
->words
; w
; w
= w
->next
)
697 if (w
->type
== word_IndexRef
) {
698 htmlindexref
*hr
= snew(htmlindexref
);
702 hr
->referenced
= hr
->generated
= FALSE
;
703 hr
->section
= lastsect
;
707 lastsect
->file
->last_fragment_number
++);
708 hr
->fragment
= dupstr(buf
);
710 html_sanitise_fragment(&files
, hr
->section
->file
,
713 w
->private_data
= hr
;
715 tag
= index_findtag(idx
, w
->text
);
719 for (i
= 0; i
< tag
->nrefs
; i
++) {
720 indexentry
*entry
= tag
->refs
[i
];
721 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
723 if (hi
->nrefs
>= hi
->refsize
) {
725 hi
->refs
= sresize(hi
->refs
, hi
->refsize
, word
*);
728 hi
->refs
[hi
->nrefs
++] = w
;
735 * Now we're ready to write out the actual HTML files.
739 * - we open that file and write its header
740 * - we run down the list of sections
741 * - for each section directly contained within that file, we
742 * output the section text
743 * - for each section which is not in the file but which has a
744 * parent that is, we output a contents entry for the
745 * section if appropriate
746 * - finally, we output the file trailer and close the file.
755 for (f
= files
.head
; f
; f
= f
->next
) {
758 enum LISTTYPE
{ NOLIST
, UL
, OL
, DL
};
759 enum ITEMTYPE
{ NOITEM
, LI
, DT
, DD
};
760 struct stackelement
{
761 struct stackelement
*next
;
762 enum LISTTYPE listtype
;
763 enum ITEMTYPE itemtype
;
766 #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" )
767 #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" )
769 ho
.fp
= fopen(f
->filename
, "w");
771 error(err_cantopenw
, f
->filename
);
773 ho
.charset
= conf
.output_charset
;
774 ho
.restrict_charset
= conf
.restrict_charset
;
775 ho
.cstate
= charset_init_state
;
776 ho
.ver
= conf
.htmlver
;
777 ho
.state
= HO_NEUTRAL
;
778 ho
.contents_level
= 0;
781 switch (conf
.htmlver
) {
784 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
785 "HTML 3.2 Final//EN\">\n");
789 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML"
790 " 4.01//EN\"\n\"http://www.w3.org/TR/html4/"
795 fprintf(ho
.fp
, "<!DOCTYPE HTML PUBLIC \"ISO/IEC "
796 "15445:2000//DTD HTML//EN\">\n");
798 case XHTML_1_0_TRANSITIONAL
:
800 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
801 charset_to_mimeenc(conf
.output_charset
));
802 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
803 " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/"
804 "xhtml1/DTD/xhtml1-transitional.dtd\">\n");
807 case XHTML_1_0_STRICT
:
809 fprintf(ho
.fp
, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
810 charset_to_mimeenc(conf
.output_charset
));
811 fprintf(ho
.fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML"
812 " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/"
813 "DTD/xhtml1-strict.dtd\">\n");
818 element_open(&ho
, "html");
819 if (is_xhtml(conf
.htmlver
)) {
820 element_attr(&ho
, "xmlns", "http://www.w3.org/1999/xhtml");
824 element_open(&ho
, "head");
827 element_empty(&ho
, "meta");
828 element_attr(&ho
, "http-equiv", "content-type");
831 sprintf(buf
, "text/html; charset=%.150s",
832 charset_to_mimeenc(conf
.output_charset
));
833 element_attr(&ho
, "content", buf
);
838 element_empty(&ho
, "meta");
839 element_attr(&ho
, "name", "author");
840 element_attr_w(&ho
, "content", conf
.author
);
844 if (conf
.description
) {
845 element_empty(&ho
, "meta");
846 element_attr(&ho
, "name", "description");
847 element_attr_w(&ho
, "content", conf
.description
);
851 element_open(&ho
, "title");
852 if (f
->first
&& f
->first
->title
) {
853 html_words(&ho
, f
->first
->title
->words
, NOTHING
,
857 if (f
->last
!= f
->first
&& f
->last
->title
) {
858 html_text(&ho
, conf
.title_separator
);
859 html_words(&ho
, f
->last
->title
->words
, NOTHING
,
863 element_close(&ho
, "title");
867 html_raw(&ho
, conf
.head_end
);
870 * Add any <head> data defined in specific sections
871 * that go in this file. (This is mostly to allow <meta
872 * name="AppleTitle"> tags for Mac online help.)
874 for (s
= sects
.head
; s
; s
= s
->next
) {
875 if (s
->file
== f
&& s
->text
) {
877 p
&& (p
== s
->text
|| p
->type
== para_Title
||
878 !is_heading_type(p
->type
));
880 if (p
->type
== para_Config
) {
881 if (!ustricmp(p
->keyword
, L
"html-local-head")) {
882 html_raw(&ho
, adv(p
->origkeyword
));
889 element_close(&ho
, "head");
893 html_raw(&ho
, conf
.body_tag
);
895 element_open(&ho
, "body");
899 html_raw(&ho
, conf
.body_start
);
902 * Write out a nav bar. Special case: we don't do this
903 * if there is only one file.
905 if (files
.head
!= files
.tail
) {
906 element_open(&ho
, "p");
908 html_raw_as_attr(&ho
, conf
.nav_attr
);
911 element_open(&ho
, "a");
912 element_attr(&ho
, "href", prevf
->filename
);
914 html_text(&ho
, conf
.nav_prev_text
);
916 element_close(&ho
, "a");
918 html_text(&ho
, conf
.nav_separator
);
920 if (f
!= files
.head
) {
921 element_open(&ho
, "a");
922 element_attr(&ho
, "href", files
.head
->filename
);
924 html_text(&ho
, conf
.contents_text
);
926 element_close(&ho
, "a");
929 html_text(&ho
, conf
.nav_separator
);
930 if (f
!= files
.index
) {
931 element_open(&ho
, "a");
932 element_attr(&ho
, "href", files
.index
->filename
);
934 html_text(&ho
, conf
.index_text
);
935 if (f
!= files
.index
)
936 element_close(&ho
, "a");
939 html_text(&ho
, conf
.nav_separator
);
942 element_open(&ho
, "a");
943 element_attr(&ho
, "href", f
->next
->filename
);
945 html_text(&ho
, conf
.nav_next_text
);
947 element_close(&ho
, "a");
949 element_close(&ho
, "p");
955 * Write out a prefix TOC for the file (if a leaf file).
957 * We start by going through the section list and
958 * collecting the sections which need to be added to
959 * the contents. On the way, we also test to see if
960 * this file is a leaf file (defined as one which
961 * contains all descendants of any section it
962 * contains), because this will play a part in our
963 * decision on whether or not to _output_ the TOC.
965 * Special case: we absolutely do not do this if we're
966 * in single-file mode.
968 if (files
.head
!= files
.tail
) {
969 int ntoc
= 0, tocsize
= 0;
970 htmlsect
**toc
= NULL
;
973 for (s
= sects
.head
; s
; s
= s
->next
) {
978 * Search up from this section until we find
979 * the highest-level one which belongs in this
984 for (ac
= s
; ac
; ac
= ac
->parent
) {
992 if (s
->file
!= f
&& a
!= NULL
)
996 if (adepth
<= a
->contents_depth
) {
997 if (ntoc
>= tocsize
) {
999 toc
= sresize(toc
, tocsize
, htmlsect
*);
1006 if (leaf
&& conf
.leaf_contains_contents
&&
1007 ntoc
>= conf
.leaf_smallest_contents
) {
1010 for (i
= 0; i
< ntoc
; i
++) {
1011 htmlsect
*s
= toc
[i
];
1012 int hlevel
= (s
->type
== TOP ?
-1 :
1013 s
->type
== INDEX ?
0 :
1014 heading_depth(s
->title
))
1015 - f
->min_heading_depth
+ 1;
1017 assert(hlevel
>= 1);
1018 html_contents_entry(&ho
, hlevel
, s
,
1019 f
, keywords
, &conf
);
1021 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1026 * Now go through the document and output some real
1030 for (s
= sects
.head
; s
; s
= s
->next
) {
1033 * This section belongs in this file.
1039 * Doesn't belong in this file, but it may be
1040 * a descendant of a section which does, in
1041 * which case we should consider it for the
1042 * main TOC of this file (for non-leaf files).
1050 * Search up from this section until we find
1051 * the highest-level one which belongs in this
1056 for (ac
= s
; ac
; ac
= ac
->parent
) {
1057 if (ac
->file
== f
) {
1066 * This section does not belong in this
1067 * file, but an ancestor of it does. Write
1068 * out a contents table entry, if the depth
1069 * doesn't exceed the maximum contents
1070 * depth for the ancestor section.
1072 if (adepth
<= a
->contents_depth
) {
1073 html_contents_entry(&ho
, adepth
, s
,
1074 f
, keywords
, &conf
);
1083 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1086 * Display the section heading.
1089 hlevel
= (s
->type
== TOP ?
-1 :
1090 s
->type
== INDEX ?
0 :
1091 heading_depth(s
->title
))
1092 - f
->min_heading_depth
+ 1;
1093 assert(hlevel
>= 1);
1094 /* HTML headings only go up to <h6> */
1098 htag
[1] = '0' + hlevel
;
1100 element_open(&ho
, htag
);
1103 * Provide anchor(s) for cross-links to target.
1105 * (Also we'll have to do this separately in
1106 * other paragraph types - NumberedList and
1111 for (i
=0; i
< conf
.ntfragments
; i
++)
1112 if (s
->fragments
[i
])
1113 html_fragment(&ho
, s
->fragments
[i
]);
1116 html_section_title(&ho
, s
, f
, keywords
, &conf
, TRUE
);
1118 element_close(&ho
, htag
);
1121 * Now display the section text.
1124 stackhead
= snew(struct stackelement
);
1125 stackhead
->next
= NULL
;
1126 stackhead
->listtype
= NOLIST
;
1127 stackhead
->itemtype
= NOITEM
;
1129 for (p
= s
->text
;; p
= p
->next
) {
1130 enum LISTTYPE listtype
;
1131 struct stackelement
*se
;
1134 * Preliminary switch to figure out what
1135 * sort of list we expect to be inside at
1138 * Since p may still be NULL at this point,
1139 * I invent a harmless paragraph type for
1142 switch (p ? p
->type
: para_Normal
) {
1145 case para_Copyright
:
1146 case para_BiblioCited
:
1148 case para_QuotePush
:
1152 case para_UnnumberedChapter
:
1163 case para_NumberedList
:
1167 case para_DescribedThing
:
1168 case para_Description
:
1172 case para_LcontPush
:
1173 se
= snew(struct stackelement
);
1174 se
->next
= stackhead
;
1175 se
->listtype
= NOLIST
;
1176 se
->itemtype
= NOITEM
;
1180 default: /* some totally non-printing para */
1187 * Terminate the most recent list item, if
1188 * any. (We left this until after
1189 * processing LcontPush, since in that case
1190 * the list item won't want to be
1191 * terminated until after the corresponding
1194 if (stackhead
->itemtype
!= NOITEM
) {
1195 element_close(&ho
, itemname(stackhead
->itemtype
));
1198 stackhead
->itemtype
= NOITEM
;
1201 * Terminate the current list, if it's not
1202 * the one we want to be in.
1204 if (listtype
!= stackhead
->listtype
&&
1205 stackhead
->listtype
!= NOLIST
) {
1206 element_close(&ho
, listname(stackhead
->listtype
));
1211 * Leave the loop if our time has come.
1213 if (!p
|| (is_heading_type(p
->type
) &&
1214 p
->type
!= para_Title
))
1215 break; /* end of section text */
1218 * Start a fresh list if necessary.
1220 if (listtype
!= stackhead
->listtype
&&
1222 element_open(&ho
, listname(listtype
));
1224 stackhead
->listtype
= listtype
;
1228 element_empty(&ho
, "hr");
1231 html_codepara(&ho
, p
->words
);
1234 case para_Copyright
:
1235 element_open(&ho
, "p");
1237 html_words(&ho
, p
->words
, ALL
,
1238 f
, keywords
, &conf
);
1240 element_close(&ho
, "p");
1242 case para_BiblioCited
:
1243 element_open(&ho
, "p");
1244 if (p
->private_data
) {
1245 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1247 for (i
=0; i
< conf
.ntfragments
; i
++)
1248 if (s
->fragments
[i
])
1249 html_fragment(&ho
, s
->fragments
[i
]);
1252 html_words(&ho
, p
->kwtext
, ALL
,
1253 f
, keywords
, &conf
);
1254 html_text(&ho
, L
" ");
1255 html_words(&ho
, p
->words
, ALL
,
1256 f
, keywords
, &conf
);
1258 element_close(&ho
, "p");
1261 case para_NumberedList
:
1262 element_open(&ho
, "li");
1263 if (p
->private_data
) {
1264 htmlsect
*s
= (htmlsect
*)p
->private_data
;
1266 for (i
=0; i
< conf
.ntfragments
; i
++)
1267 if (s
->fragments
[i
])
1268 html_fragment(&ho
, s
->fragments
[i
]);
1271 stackhead
->itemtype
= LI
;
1272 html_words(&ho
, p
->words
, ALL
,
1273 f
, keywords
, &conf
);
1275 case para_DescribedThing
:
1276 element_open(&ho
, "dt");
1278 stackhead
->itemtype
= DT
;
1279 html_words(&ho
, p
->words
, ALL
,
1280 f
, keywords
, &conf
);
1282 case para_Description
:
1283 element_open(&ho
, "dd");
1285 stackhead
->itemtype
= DD
;
1286 html_words(&ho
, p
->words
, ALL
,
1287 f
, keywords
, &conf
);
1290 case para_QuotePush
:
1291 element_open(&ho
, "blockquote");
1294 element_close(&ho
, "blockquote");
1299 stackhead
= stackhead
->next
;
1306 assert(stackhead
&& !stackhead
->next
);
1310 if (s
->type
== INDEX
) {
1315 * This section is the index. I'll just
1316 * render it as a single paragraph, with a
1317 * colon between the index term and the
1318 * references, and <br> in between each
1321 element_open(&ho
, "p");
1323 for (i
= 0; (entry
=
1324 index234(idx
->entries
, i
)) != NULL
; i
++) {
1325 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1329 element_empty(&ho
, "br");
1332 html_words(&ho
, entry
->text
, MARKUP
|LINKS
,
1333 f
, keywords
, &conf
);
1335 html_text(&ho
, conf
.index_main_sep
);
1337 for (j
= 0; j
< hi
->nrefs
; j
++) {
1339 (htmlindexref
*)hi
->refs
[j
]->private_data
;
1340 paragraph
*p
= hr
->section
->title
;
1343 html_text(&ho
, conf
.index_multi_sep
);
1345 html_href(&ho
, f
, hr
->section
->file
,
1347 hr
->referenced
= TRUE
;
1349 html_words(&ho
, p
->kwtext
, MARKUP
|LINKS
,
1350 f
, keywords
, &conf
);
1351 else if (p
&& p
->words
)
1352 html_words(&ho
, p
->words
, MARKUP
|LINKS
,
1353 f
, keywords
, &conf
);
1356 * If there is no title at all,
1357 * this must be because our
1358 * target section is the
1359 * preamble section and there
1360 * is no title. So we use the
1363 html_text(&ho
, conf
.preamble_text
);
1365 element_close(&ho
, "a");
1368 element_close(&ho
, "p");
1373 html_contents_entry(&ho
, 0, NULL
, f
, keywords
, &conf
);
1380 int done_version_ids
= FALSE
;
1382 element_empty(&ho
, "hr");
1385 html_raw(&ho
, conf
.body_end
);
1387 if (conf
.address_section
) {
1388 int started
= FALSE
;
1389 if (conf
.htmlver
== ISO_HTML
) {
1391 * The ISO-HTML validator complains if
1392 * there isn't a <div> tag surrounding the
1393 * <address> tag. I'm uncertain of why this
1394 * should be - there appears to be no
1395 * mention of this in the ISO-HTML spec,
1396 * suggesting that it doesn't represent a
1397 * change from HTML 4, but nonetheless the
1398 * HTML 4 validator doesn't seem to mind.
1400 element_open(&ho
, "div");
1402 element_open(&ho
, "address");
1403 if (conf
.addr_start
) {
1404 html_raw(&ho
, conf
.addr_start
);
1408 if (conf
.visible_version_id
) {
1409 for (p
= sourceform
; p
; p
= p
->next
)
1410 if (p
->type
== para_VersionID
) {
1412 element_empty(&ho
, "br");
1414 html_text(&ho
, conf
.pre_versionid
);
1415 html_words(&ho
, p
->words
, NOTHING
,
1416 f
, keywords
, &conf
);
1417 html_text(&ho
, conf
.post_versionid
);
1420 done_version_ids
= TRUE
;
1422 if (conf
.addr_end
) {
1424 element_empty(&ho
, "br");
1425 html_raw(&ho
, conf
.addr_end
);
1427 element_close(&ho
, "address");
1428 if (conf
.htmlver
== ISO_HTML
)
1429 element_close(&ho
, "div");
1432 if (!done_version_ids
) {
1434 * If the user didn't want the version IDs
1435 * visible, I think we still have a duty to put
1436 * them in an HTML comment.
1438 int started
= FALSE
;
1439 for (p
= sourceform
; p
; p
= p
->next
)
1440 if (p
->type
== para_VersionID
) {
1442 html_raw(&ho
, "<!-- version IDs:\n");
1445 html_words(&ho
, p
->words
, NOTHING
,
1446 f
, keywords
, &conf
);
1450 html_raw(&ho
, "-->\n");
1454 element_close(&ho
, "body");
1456 element_close(&ho
, "html");
1463 * Go through and check that no index fragments were referenced
1464 * without being generated, or indeed vice versa.
1466 * (When I actually get round to freeing everything, this can
1467 * probably be the freeing loop as well.)
1469 for (p
= sourceform
; p
; p
= p
->next
) {
1471 for (w
= p
->words
; w
; w
= w
->next
)
1472 if (w
->type
== word_IndexRef
) {
1473 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1475 assert(!hr
->referenced
== !hr
->generated
);
1480 * Free all the working data.
1484 while ( (frag
= (htmlfragment
*)delpos234(files
.frags
, 0)) != NULL
) {
1486 * frag->fragment is dynamically allocated, but will be
1487 * freed when we process the htmlsect structure which
1488 * it is attached to.
1492 freetree234(files
.frags
);
1495 htmlsect
*sect
, *tmp
;
1500 for (i
=0; i
< conf
.ntfragments
; i
++)
1501 sfree(sect
->fragments
[i
]);
1502 sfree(sect
->fragments
);
1506 sect
= nonsects
.head
;
1510 for (i
=0; i
< conf
.ntfragments
; i
++)
1511 sfree(sect
->fragments
[i
]);
1512 sfree(sect
->fragments
);
1518 htmlfile
*file
, *tmp
;
1522 sfree(file
->filename
);
1530 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
1531 htmlindex
*hi
= (htmlindex
*)entry
->backend_data
;
1538 for (p
= sourceform
; p
; p
= p
->next
)
1539 for (w
= p
->words
; w
; w
= w
->next
)
1540 if (w
->type
== word_IndexRef
) {
1541 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1543 sfree(hr
->fragment
);
1548 sfree(conf
.single_filename
);
1549 sfree(conf
.contents_filename
);
1550 sfree(conf
.index_filename
);
1551 sfree(conf
.template_filename
);
1552 while (conf
.ntfragments
--)
1553 sfree(conf
.template_fragments
[conf
.ntfragments
]);
1554 sfree(conf
.template_fragments
);
1557 static void html_file_section(htmlconfig
*cfg
, htmlfilelist
*files
,
1558 htmlsect
*sect
, int depth
)
1564 * `depth' is derived from the heading_depth() macro at the top
1565 * of this file, which counts title as -1, chapter as 0,
1566 * heading as 1 and subsection as 2. However, the semantics of
1567 * cfg->leaf_level are defined to count chapter as 1, heading
1568 * as 2 etc. So first I increment depth :-(
1572 if (cfg
->leaf_level
== 0) {
1574 * leaf_level==0 is a special case, in which everything is
1575 * put into a single file.
1578 files
->single
= html_new_file(files
, cfg
->single_filename
);
1580 file
= files
->single
;
1583 * If the depth of this section is at or above leaf_level,
1584 * we invent a fresh file and put this section at its head.
1585 * Otherwise, we put it in the same file as its parent
1588 if (ldepth
> cfg
->leaf_level
) {
1590 * We know that sect->parent cannot be NULL. The only
1591 * circumstance in which it can be is if sect is at
1592 * chapter or appendix level, i.e. ldepth==1; and if
1593 * that's the case, then we cannot have entered this
1594 * branch unless cfg->leaf_level==0, in which case we
1595 * would be in the single-file case above and not here
1598 assert(sect
->parent
);
1600 file
= sect
->parent
->file
;
1602 if (sect
->type
== TOP
) {
1603 file
= html_new_file(files
, cfg
->contents_filename
);
1604 } else if (sect
->type
== INDEX
) {
1605 file
= html_new_file(files
, cfg
->index_filename
);
1609 assert(ldepth
> 0 && sect
->title
);
1610 title
= html_format(sect
->title
, cfg
->template_filename
);
1611 file
= html_new_file(files
, title
);
1619 if (file
->min_heading_depth
> depth
) {
1621 * This heading is at a higher level than any heading we
1622 * have so far placed in this file; so we set the `first'
1625 file
->min_heading_depth
= depth
;
1629 if (file
->min_heading_depth
== depth
)
1633 static htmlfile
*html_new_file(htmlfilelist
*list
, char *filename
)
1635 htmlfile
*ret
= snew(htmlfile
);
1639 list
->tail
->next
= ret
;
1644 ret
->filename
= dupstr(filename
);
1645 ret
->last_fragment_number
= 0;
1646 ret
->min_heading_depth
= INT_MAX
;
1647 ret
->first
= ret
->last
= NULL
;
1652 static htmlsect
*html_new_sect(htmlsectlist
*list
, paragraph
*title
,
1655 htmlsect
*ret
= snew(htmlsect
);
1659 list
->tail
->next
= ret
;
1669 ret
->fragments
= snewn(cfg
->ntfragments
, char *);
1672 for (i
=0; i
< cfg
->ntfragments
; i
++)
1673 ret
->fragments
[i
] = NULL
;
1679 static void html_words(htmloutput
*ho
, word
*words
, int flags
,
1680 htmlfile
*file
, keywordlist
*keywords
, htmlconfig
*cfg
)
1686 for (w
= words
; w
; w
= w
->next
) switch (w
->type
) {
1687 case word_HyperLink
:
1688 if (flags
& LINKS
) {
1689 element_open(ho
, "a");
1690 c
= utoa_dup(w
->text
, CS_ASCII
);
1691 element_attr(ho
, "href", c
);
1695 case word_UpperXref
:
1696 case word_LowerXref
:
1697 if (flags
& LINKS
) {
1698 keyword
*kwl
= kw_lookup(keywords
, w
->text
);
1704 s
= (htmlsect
*)p
->private_data
;
1708 html_href(ho
, file
, s
->file
, s
->fragments
[0]);
1714 element_close(ho
, "a");
1717 if (flags
& INDEXENTS
) {
1718 htmlindexref
*hr
= (htmlindexref
*)w
->private_data
;
1719 html_fragment(ho
, hr
->fragment
);
1720 hr
->generated
= TRUE
;
1727 case word_WhiteSpace
:
1728 case word_EmphSpace
:
1729 case word_CodeSpace
:
1730 case word_WkCodeSpace
:
1732 case word_EmphQuote
:
1733 case word_CodeQuote
:
1734 case word_WkCodeQuote
:
1735 style
= towordstyle(w
->type
);
1736 type
= removeattr(w
->type
);
1737 if (style
== word_Emph
&&
1738 (attraux(w
->aux
) == attr_First
||
1739 attraux(w
->aux
) == attr_Only
) &&
1741 element_open(ho
, "em");
1742 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1743 (attraux(w
->aux
) == attr_First
||
1744 attraux(w
->aux
) == attr_Only
) &&
1746 element_open(ho
, "code");
1748 if (type
== word_WhiteSpace
)
1749 html_text(ho
, L
" ");
1750 else if (type
== word_Quote
) {
1751 if (quoteaux(w
->aux
) == quote_Open
)
1752 html_text(ho
, cfg
->lquote
);
1754 html_text(ho
, cfg
->rquote
);
1756 if (!w
->alt
|| cvt_ok(ho
->restrict_charset
, w
->text
))
1757 html_text_nbsp(ho
, w
->text
);
1759 html_words(ho
, w
->alt
, flags
, file
, keywords
, cfg
);
1762 if (style
== word_Emph
&&
1763 (attraux(w
->aux
) == attr_Last
||
1764 attraux(w
->aux
) == attr_Only
) &&
1766 element_close(ho
, "em");
1767 else if ((style
== word_Code
|| style
== word_WeakCode
) &&
1768 (attraux(w
->aux
) == attr_Last
||
1769 attraux(w
->aux
) == attr_Only
) &&
1771 element_close(ho
, "code");
1777 static void html_codepara(htmloutput
*ho
, word
*words
)
1779 element_open(ho
, "pre");
1780 element_open(ho
, "code");
1781 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1786 if (words
->next
&& words
->next
->type
== word_Emph
) {
1787 e
= words
->next
->text
;
1788 words
= words
->next
;
1792 while (e
&& *e
&& *t
) {
1796 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1804 element_open(ho
, open_tag
);
1806 html_text_limit(ho
, t
, n
);
1809 element_close(ho
, open_tag
);
1817 element_close(ho
, "code");
1818 element_close(ho
, "pre");
1821 static void html_charset_cleanup(htmloutput
*ho
)
1826 bytes
= charset_from_unicode(NULL
, NULL
, outbuf
, lenof(outbuf
),
1827 ho
->charset
, &ho
->cstate
, NULL
);
1828 if (ho
->fp
&& bytes
> 0)
1829 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1832 static void return_mostly_to_neutral(htmloutput
*ho
)
1835 if (ho
->state
== HO_IN_EMPTY_TAG
&& is_xhtml(ho
->ver
)) {
1836 fprintf(ho
->fp
, " />");
1837 } else if (ho
->state
== HO_IN_EMPTY_TAG
|| ho
->state
== HO_IN_TAG
) {
1838 fprintf(ho
->fp
, ">");
1842 ho
->state
= HO_NEUTRAL
;
1845 static void return_to_neutral(htmloutput
*ho
)
1847 if (ho
->state
== HO_IN_TEXT
) {
1848 html_charset_cleanup(ho
);
1851 return_mostly_to_neutral(ho
);
1854 static void element_open(htmloutput
*ho
, char const *name
)
1856 return_to_neutral(ho
);
1858 fprintf(ho
->fp
, "<%s", name
);
1859 ho
->state
= HO_IN_TAG
;
1862 static void element_close(htmloutput
*ho
, char const *name
)
1864 return_to_neutral(ho
);
1866 fprintf(ho
->fp
, "</%s>", name
);
1867 ho
->state
= HO_NEUTRAL
;
1870 static void element_empty(htmloutput
*ho
, char const *name
)
1872 return_to_neutral(ho
);
1874 fprintf(ho
->fp
, "<%s", name
);
1875 ho
->state
= HO_IN_EMPTY_TAG
;
1878 static void html_nl(htmloutput
*ho
)
1880 return_to_neutral(ho
);
1882 fputc('\n', ho
->fp
);
1885 static void html_raw(htmloutput
*ho
, char *text
)
1887 return_to_neutral(ho
);
1889 fputs(text
, ho
->fp
);
1892 static void html_raw_as_attr(htmloutput
*ho
, char *text
)
1894 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1897 fputs(text
, ho
->fp
);
1901 static void element_attr(htmloutput
*ho
, char const *name
, char const *value
)
1903 html_charset_cleanup(ho
);
1904 assert(ho
->state
== HO_IN_TAG
|| ho
->state
== HO_IN_EMPTY_TAG
);
1906 fprintf(ho
->fp
, " %s=\"%s\"", name
, value
);
1909 static void element_attr_w(htmloutput
*ho
, char const *name
,
1910 wchar_t const *value
)
1912 html_charset_cleanup(ho
);
1914 fprintf(ho
->fp
, " %s=\"", name
);
1915 html_text_limit_internal(ho
, value
, 0, TRUE
, FALSE
);
1916 html_charset_cleanup(ho
);
1921 static void html_text(htmloutput
*ho
, wchar_t const *text
)
1923 return_mostly_to_neutral(ho
);
1924 html_text_limit_internal(ho
, text
, 0, FALSE
, FALSE
);
1927 static void html_text_nbsp(htmloutput
*ho
, wchar_t const *text
)
1929 return_mostly_to_neutral(ho
);
1930 html_text_limit_internal(ho
, text
, 0, FALSE
, TRUE
);
1933 static void html_text_limit(htmloutput
*ho
, wchar_t const *text
, int maxlen
)
1935 return_mostly_to_neutral(ho
);
1936 html_text_limit_internal(ho
, text
, maxlen
, FALSE
, FALSE
);
1939 static void html_text_limit_internal(htmloutput
*ho
, wchar_t const *text
,
1940 int maxlen
, int quote_quotes
, int nbsp
)
1942 int textlen
= ustrlen(text
);
1946 if (maxlen
> 0 && textlen
> maxlen
)
1949 while (textlen
> 0) {
1950 /* Scan ahead for characters we really can't display in HTML. */
1951 int lenbefore
, lenafter
;
1952 for (lenbefore
= 0; lenbefore
< textlen
; lenbefore
++)
1953 if (text
[lenbefore
] == L
'<' ||
1954 text
[lenbefore
] == L
'>' ||
1955 text
[lenbefore
] == L
'&' ||
1956 (text
[lenbefore
] == L
'"' && quote_quotes
) ||
1957 (text
[lenbefore
] == L
' ' && nbsp
))
1959 lenafter
= lenbefore
;
1960 bytes
= charset_from_unicode(&text
, &lenafter
, outbuf
, lenof(outbuf
),
1961 ho
->charset
, &ho
->cstate
, &err
);
1962 textlen
-= (lenbefore
- lenafter
);
1963 if (bytes
> 0 && ho
->fp
)
1964 fwrite(outbuf
, 1, bytes
, ho
->fp
);
1967 * We have encountered a character that cannot be
1968 * displayed in the selected output charset. Therefore,
1969 * we use an HTML numeric entity reference.
1971 assert(textlen
> 0);
1973 fprintf(ho
->fp
, "&#%ld;", (long int)*text
);
1975 } else if (lenafter
== 0 && textlen
> 0) {
1977 * We have encountered a character which is special to
1982 fprintf(ho
->fp
, "<");
1983 else if (*text
== L
'>')
1984 fprintf(ho
->fp
, ">");
1985 else if (*text
== L
'&')
1986 fprintf(ho
->fp
, "&");
1987 else if (*text
== L
'"')
1988 fprintf(ho
->fp
, """);
1989 else if (*text
== L
' ') {
1991 fprintf(ho
->fp
, " ");
1993 assert(!"Can't happen");
2000 static void cleanup(htmloutput
*ho
)
2002 return_to_neutral(ho
);
2007 static void html_href(htmloutput
*ho
, htmlfile
*thisfile
,
2008 htmlfile
*targetfile
, char *targetfrag
)
2010 rdstringc rs
= { 0, 0, NULL
};
2013 if (targetfile
!= thisfile
)
2014 rdaddsc(&rs
, targetfile
->filename
);
2017 rdaddsc(&rs
, targetfrag
);
2021 element_open(ho
, "a");
2022 element_attr(ho
, "href", url
);
2026 static void html_fragment(htmloutput
*ho
, char const *fragment
)
2028 element_open(ho
, "a");
2029 element_attr(ho
, "name", fragment
);
2030 if (is_xhtml(ho
->ver
))
2031 element_attr(ho
, "id", fragment
);
2032 element_close(ho
, "a");
2035 static char *html_format(paragraph
*p
, char *template_string
)
2039 wchar_t *ws
, wsbuf
[2];
2040 rdstringc rs
= { 0, 0, NULL
};
2042 t
= template_string
;
2044 if (*t
== '%' && t
[1]) {
2058 if (p
->kwtext
&& fmt
== 'n')
2060 else if (p
->kwtext2
&& fmt
== 'b') {
2062 * HTML fragment names must start with a letter, so
2063 * simply `1.2.3' is not adequate. In this case I'm
2064 * going to cheat slightly by prepending the first
2065 * character of the first word of kwtext, so that
2066 * we get `C1' for chapter 1, `S2.3' for section
2069 if (p
->kwtext
&& p
->kwtext
->text
[0]) {
2072 wsbuf
[0] = p
->kwtext
->text
[0];
2075 } else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
2078 /* %N comes here; also failure cases of other fmts */
2082 c
= utoa_dup(ws
, CS_ASCII
);
2088 if (removeattr(w
->type
) == word_Normal
) {
2089 c
= utoa_dup(w
->text
, CS_ASCII
);
2100 return rdtrimc(&rs
);
2103 static char *html_sanitise_fragment(htmlfilelist
*files
, htmlfile
*file
,
2107 * The HTML 4 spec's strictest definition of fragment names (<a
2108 * name> and "id" attributes) says that they `must begin with a
2109 * letter and may be followed by any number of letters, digits,
2110 * hyphens, underscores, colons, and periods'.
2112 * So here we unceremoniously rip out any characters not
2113 * conforming to this limitation.
2115 char *p
= text
, *q
= text
;
2117 while (*p
&& !((*p
>='A' && *p
<='Z') || (*p
>='a' && *p
<='z')))
2119 if ((*q
++ = *p
++) != '\0') {
2121 if ((*p
>='A' && *p
<='Z') ||
2122 (*p
>='a' && *p
<='z') ||
2123 (*p
>='0' && *p
<='9') ||
2124 *p
=='-' || *p
=='_' || *p
==':' || *p
=='.')
2132 /* If there's nothing left, make something valid up */
2134 static const char anonfrag
[] = "anon";
2135 text
= sresize(text
, lenof(anonfrag
), char);
2136 strcpy(text
, anonfrag
);
2140 * Now we check for clashes with other fragment names, and
2141 * adjust this one if necessary by appending a hyphen followed
2145 htmlfragment
*frag
= snew(htmlfragment
);
2146 int len
= 0; /* >0 indicates we have resized */
2150 frag
->fragment
= text
;
2152 while (add234(files
->frags
, frag
) != frag
) {
2155 frag
->fragment
= text
= sresize(text
, len
+20, char);
2158 sprintf(text
+ len
, "-%d", ++suffix
);
2165 static void html_contents_entry(htmloutput
*ho
, int depth
, htmlsect
*s
,
2166 htmlfile
*thisfile
, keywordlist
*keywords
,
2169 if (ho
->contents_level
>= depth
&& ho
->contents_level
> 0) {
2170 element_close(ho
, "li");
2174 while (ho
->contents_level
> depth
) {
2175 element_close(ho
, "ul");
2176 ho
->contents_level
--;
2177 if (ho
->contents_level
> 0) {
2178 element_close(ho
, "li");
2183 while (ho
->contents_level
< depth
) {
2185 element_open(ho
, "ul");
2187 ho
->contents_level
++;
2193 element_open(ho
, "li");
2194 html_href(ho
, thisfile
, s
->file
, s
->fragments
[0]);
2195 html_section_title(ho
, s
, thisfile
, keywords
, cfg
, FALSE
);
2196 element_close(ho
, "a");
2197 /* <li> will be closed by a later invocation */
2200 static void html_section_title(htmloutput
*ho
, htmlsect
*s
, htmlfile
*thisfile
,
2201 keywordlist
*keywords
, htmlconfig
*cfg
,
2207 int depth
= heading_depth(s
->title
);
2211 else if (depth
== 0)
2212 sl
= &cfg
->achapter
;
2213 else if (depth
<= cfg
->nasect
)
2214 sl
= &cfg
->asect
[depth
-1];
2216 sl
= &cfg
->asect
[cfg
->nasect
-1];
2220 else if (sl
->just_numbers
)
2221 number
= s
->title
->kwtext2
;
2223 number
= s
->title
->kwtext
;
2226 html_words(ho
, number
, MARKUP
,
2227 thisfile
, keywords
, cfg
);
2228 html_text(ho
, sl
->number_suffix
);
2231 html_words(ho
, s
->title
->words
, real ? ALL
: MARKUP
,
2232 thisfile
, keywords
, cfg
);
2234 assert(s
->type
!= NORMAL
);
2236 * If we're printing the full document title for _real_ and
2237 * there isn't one, we don't want to print `Preamble' at
2238 * the top of what ought to just be some text. If we need
2239 * it in any other context such as TOCs, we need to print
2242 if (s
->type
== TOP
&& !real
)
2243 html_text(ho
, cfg
->preamble_text
);
2244 else if (s
->type
== INDEX
)
2245 html_text(ho
, cfg
->index_text
);