Debian Codegraph @

Docs/src/bin/halibut/bk_xhtml.c

raw
1 /*
2 * xhtml backend for Halibut
3 * (initial implementation by James Aylett)
4 *
5 * Still to do:
6 *
7 * +++ doesn't handle non-breaking hyphens. Not sure how to yet.
8 * +++ entity names (from a file -- ideally supply normal SGML files)
9 * +++ configuration directive to file split where the current layout
10 * code wouldn't. Needs changes to _ponder_layout() and _do_paras(),
11 * perhaps others.
12 *
13 * Limitations:
14 *
15 * +++ biblio/index references target the nearest section marker, rather
16 * than having a dedicated target themselves. In large bibliographies
17 * this will cause problems. (The solution is to fake up a response
18 * from xhtml_find_section(), probably linking it into the sections
19 * chain just in case we need it again, and to make freeing it up
20 * easier.) docsrc.pl used to work as we do, however, and SGT agrees that
21 * this is acceptable for now.
22 * +++ can't cope with leaf-level == 0. It's all to do with the
23 * top-level file not being normal, probably not even having a valid
24 * section level, and stuff like that. I question whether this is an
25 * issue, frankly; small manuals that fit on one page should probably
26 * not be written in halibut at all.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 #include "halibut.h"
34
35 enum outputtype { OT_XHTML = 0x01, OT_CHM = 0x02, OT_HTML4 = 0x04, OT_HTML5 = 0x08 };
36
37 int g_outputtype = OT_XHTML;
38 #define is_chm() ( (g_outputtype & OT_CHM) == OT_CHM )
39 #define is_xhtml() ( (g_outputtype & OT_XHTML) == OT_XHTML )
40 #define is_html5() ( (g_outputtype & OT_HTML5) == OT_HTML5 )
41
42 #define gettagtxt_br() ( is_xhtml() ? "<br />" : "<br>" )
43 #define gettagtxt_hr() ( is_xhtml() ? "<hr />" : "<hr>" )
44
45 struct xhtmlsection_Struct {
46 struct xhtmlsection_Struct *next; /* next sibling (NULL if split across files) */
47 struct xhtmlsection_Struct *child; /* NULL if split across files */
48 struct xhtmlsection_Struct *parent; /* NULL if split across files */
49 struct xhtmlsection_Struct *chain; /* single structure independent of weird trees */
50 paragraph *para;
51 struct xhtmlfile_Struct *file; /* which file is this a part of? */
52 char *fragment; /* fragment id within the file */
53 int level;
54 };
55
56 struct xhtmlfile_Struct {
57 struct xhtmlfile_Struct *next;
58 struct xhtmlfile_Struct *child;
59 struct xhtmlfile_Struct *parent;
60 char *filename;
61 struct xhtmlsection_Struct *sections; /* sections within this file (only one for non-leaf) */
62 int is_leaf; /* is this file a leaf file, ie does it not have any children? */
63 };
64
65 typedef struct xhtmlsection_Struct xhtmlsection;
66 typedef struct xhtmlfile_Struct xhtmlfile;
67 typedef struct xhtmlindex_Struct xhtmlindex;
68
69 struct xhtmlindex_Struct {
70 int nsection;
71 int size;
72 xhtmlsection **sections;
73 };
74
75 typedef struct {
76 int just_numbers;
77 wchar_t *number_suffix;
78 } xhtmlheadfmt;
79
80 typedef struct {
81 int contents_depth[6];
82 int leaf_contains_contents;
83 int leaf_level;
84 int leaf_smallest_contents;
85 int include_version_id;
86 wchar_t *author, *description;
87 wchar_t *html_lang, *meta_charset;
88 wchar_t *head_start, *head_middle, *head_end, *body, *body_start, *body_end;
89 ustr_slist *meta_append;
90 wchar_t *address_start, *address_end, *nav_attrs;
91 wchar_t *rlink_prefix, *rlink_suffix;
92 wchar_t *chm_toc_file, *chm_ind_file;
93 int suppress_address;
94 xhtmlheadfmt fchapter, *fsect;
95 int nfsect;
96 int keywordfragments;
97 } xhtmlconfig;
98
99 /*static void xhtml_level(paragraph *, int);
100 static void xhtml_level_0(paragraph *);
101 static void xhtml_docontents(FILE *, paragraph *, int);
102 static void xhtml_dosections(FILE *, paragraph *, int);
103 static void xhtml_dobody(FILE *, paragraph *, int);*/
104
105 static void xhtml_doheader(FILE *, word *);
106 static void xhtml_dofooter(FILE *);
107 static void xhtml_versionid(FILE *, word *, int);
108
109 static void xhtml_utostr(wchar_t *, char **);
110 static int xhtml_para_level(paragraph *);
111 static int xhtml_reservedchar(int);
112
113 static int xhtml_convert(wchar_t *, char **, int);
114 static void xhtml_rdaddwc(rdstringc *, word *, word *);
115 static void xhtml_para(FILE *, word *);
116 static void xhtml_codepara(FILE *, word *);
117 static void xhtml_heading(FILE *, paragraph *);
118
119 static void chm_doheader(FILE *, word *);
120 static void chm_dofooter(FILE *);
121 /* File-global variables are much easier than passing these things
122 * all over the place. Evil, but easier. We can replace this with a single
123 * structure at some point.
124 */
125 static xhtmlconfig conf;
126 static keywordlist *keywords;
127 static indexdata *idx;
128 static xhtmlfile *topfile;
129 static xhtmlsection *topsection;
130 static paragraph *sourceparas;
131 static xhtmlfile *lastfile;
132 static xhtmlfile *xhtml_last_file = NULL;
133 static int last_level = -1;
134 static xhtmlsection *currentsection;
135 static FILE* chm_toc = NULL;
136 static FILE* chm_ind = NULL;
137
138 static const wchar_t* normalizehtmlkeywordprefix(const wchar_t*s) // [x]html... --> html...
139 {
140 return s && utolower(s[0]) == 'x' && utolower(s[1]) == 'h' && utolower(s[2]) == 't' ? s + 1 : s;
141 }
142
143 static const wchar_t* ishtmlkeyword(const wchar_t*a, const wchar_t*b)
144 {
145 return !ustricmp(normalizehtmlkeywordprefix(a), normalizehtmlkeywordprefix(b)) ? a : 0;
146 }
147
148 static wchar_t* configurekeyword(wchar_t**dst, const wchar_t*keyword, const paragraph*source)
149 {
150 if (ishtmlkeyword(source->keyword, keyword))
151 return *dst = uadv(source->keyword);
152 else
153 return 0;
154 }
155
156 static xhtmlconfig xhtml_configure(paragraph * source)
157 {
158 xhtmlconfig ret;
159
160 /*
161 * Defaults.
162 */
163 ret.contents_depth[0] = 2;
164 ret.contents_depth[1] = 3;
165 ret.contents_depth[2] = 4;
166 ret.contents_depth[3] = 5;
167 ret.contents_depth[4] = 6;
168 ret.contents_depth[5] = 7;
169 ret.leaf_level = 2;
170 ret.leaf_smallest_contents = 4;
171 ret.leaf_contains_contents = FALSE;
172 ret.include_version_id = TRUE;
173 ret.author = NULL;
174 ret.description = NULL;
175 ret.html_lang = NULL;
176 ret.meta_charset = NULL;
177 ret.head_start = ret.head_middle = ret.head_end = NULL;
178 ret.body = NULL;
179 ret.body_start = NULL;
180 ret.body_end = NULL;
181 ret.meta_append = NULL;
182 ret.address_start = NULL;
183 ret.address_end = NULL;
184 ret.nav_attrs = NULL;
185 ret.suppress_address = FALSE;
186 ret.chm_toc_file = NULL;
187 ret.chm_ind_file = NULL;
188 chm_toc = NULL;
189 chm_ind = NULL;
190 ret.fchapter.just_numbers = FALSE;
191 ret.fchapter.number_suffix = ustrdup(L": ");
192 ret.nfsect = 2;
193 ret.fsect = mknewa(xhtmlheadfmt, ret.nfsect);
194 ret.fsect[0].just_numbers = FALSE;
195 ret.fsect[0].number_suffix = ustrdup(L": ");
196 ret.fsect[1].just_numbers = TRUE;
197 ret.fsect[1].number_suffix = ustrdup(L" ");
198 ret.rlink_prefix = NULL;
199 ret.rlink_suffix = NULL;
200 ret.keywordfragments = TRUE;
201
202 for (; source; source = source->next)
203 {
204 if (source->type == para_Config)
205 {
206 if (ishtmlkeyword(source->keyword, L"html-version"))
207 {
208 const wchar_t* v = uadv(source->keyword);
209 if (!ustricmp(v, L"html4")) g_outputtype = OT_HTML4; // Note: Upstream treats this as "W3C HTML 4.01 Strict"
210 else if (!ustricmp(v, L"html5")) g_outputtype = OT_HTML5;
211 else if (!ustricmp(v, L"xhtml1.0transitional")) g_outputtype = OT_XHTML;
212 else error(err_whatever, "%ls unknown %ls", source->keyword, v);
213 } else if (ishtmlkeyword(source->keyword, L"xhtml-contents-depth-0"))
214 {
215 ret.contents_depth[0] = utoi(uadv(source->keyword));
216 } else if (ishtmlkeyword(source->keyword, L"xhtml-contents-depth-1"))
217 {
218 ret.contents_depth[1] = utoi(uadv(source->keyword));
219 } else if (ishtmlkeyword(source->keyword, L"xhtml-contents-depth-2"))
220 {
221 ret.contents_depth[2] = utoi(uadv(source->keyword));
222 } else if (ishtmlkeyword(source->keyword, L"xhtml-contents-depth-3"))
223 {
224 ret.contents_depth[3] = utoi(uadv(source->keyword));
225 } else if (ishtmlkeyword(source->keyword, L"xhtml-contents-depth-4"))
226 {
227 ret.contents_depth[4] = utoi(uadv(source->keyword));
228 } else if (ishtmlkeyword(source->keyword, L"xhtml-contents-depth-5"))
229 {
230 ret.contents_depth[5] = utoi(uadv(source->keyword));
231 } else if (ishtmlkeyword(source->keyword, L"xhtml-leaf-level"))
232 {
233 ret.leaf_level = utoi(uadv(source->keyword));
234 } else
235 if (ishtmlkeyword(source->keyword, L"xhtml-leaf-smallest-contents"))
236 {
237 ret.leaf_smallest_contents = utoi(uadv(source->keyword));
238 } else if (ishtmlkeyword(source->keyword, L"xhtml-versionid"))
239 {
240 ret.include_version_id = utob(uadv(source->keyword));
241 } else if (ishtmlkeyword(source->keyword, L"xhtml-leaf-contains-contents"))
242 {
243 ret.leaf_contains_contents = utob(uadv(source->keyword));
244 } else if (ishtmlkeyword(source->keyword, L"xhtml-suppress-address"))
245 {
246 ret.suppress_address = utob(uadv(source->keyword));
247 } else if (ishtmlkeyword(source->keyword, L"xhtml-author"))
248 {
249 ret.author = uadv(source->keyword);
250 } else if (!ustricmp(source->keyword, L"chm-toc-file"))
251 {
252 ret.chm_toc_file = uadv(source->keyword);
253 } else if (!ustricmp(source->keyword, L"chm-ind-file"))
254 {
255 ret.chm_ind_file = uadv(source->keyword);
256 } else if (ishtmlkeyword(source->keyword, L"xhtml-description"))
257 {
258 ret.description = uadv(source->keyword);
259 } else if (configurekeyword(&ret.html_lang, L"xhtml-lang", source)) {
260 } else if (configurekeyword(&ret.meta_charset, L"xhtml-meta-charset", source)) {
261 } else if (configurekeyword(&ret.head_start, L"xhtml-head-start", source)) {
262 } else if (configurekeyword(&ret.head_middle, L"xhtml-head-middle", source)) {
263 } else if (configurekeyword(&ret.head_end, L"xhtml-head-end", source)) {
264 } else if (ishtmlkeyword(source->keyword, L"xhtml-body-start"))
265 {
266 ret.body_start = uadv(source->keyword);
267 } else if (ishtmlkeyword(source->keyword, L"xhtml-body-tag"))
268 {
269 ret.body = uadv(source->keyword);
270 } else if (ishtmlkeyword(source->keyword, L"xhtml-body-end"))
271 {
272 ret.body_end = uadv(source->keyword);
273 } else if (ishtmlkeyword(source->keyword, L"html-append-meta"))
274 {
275 ustr_slist_append(&ret.meta_append, uadv(source->keyword));
276 } else if (ishtmlkeyword(source->keyword, L"xhtml-address-start"))
277 {
278 ret.address_start = uadv(source->keyword);
279 } else if (ishtmlkeyword(source->keyword, L"xhtml-address-end"))
280 {
281 ret.address_end = uadv(source->keyword);
282 } else
283 if (ishtmlkeyword(source->keyword, L"xhtml-navigation-attributes"))
284 {
285 ret.nav_attrs = uadv(source->keyword);
286 } else if (ishtmlkeyword(source->keyword, L"xhtml-chapter-numeric"))
287 {
288 ret.fchapter.just_numbers = utob(uadv(source->keyword));
289 } else if (ishtmlkeyword(source->keyword, L"xhtml-chapter-suffix"))
290 {
291 ustrreplacedup(&ret.fchapter.number_suffix, uadv(source->keyword));
292 } else if (ishtmlkeyword(source->keyword, L"xhtml-rlink-prefix"))
293 {
294 ret.rlink_prefix = uadv(source->keyword);
295 } else if (ishtmlkeyword(source->keyword, L"xhtml-rlink-suffix"))
296 {
297 ret.rlink_suffix = uadv(source->keyword);
298 } else if (ishtmlkeyword(source->keyword, L"xhtml-section-numeric"))
299 {
300 wchar_t *p = uadv(source->keyword);
301 int n = 0;
302 if (uisdigit(*p))
303 {
304 n = utoi(p);
305 p = uadv(p);
306 }
307 if (n >= ret.nfsect)
308 {
309 int i;
310 ret.fsect = resize(ret.fsect, n + 1);
311 for (i = ret.nfsect; i <= n; i++)
312 ret.fsect[i] = ret.fsect[ret.nfsect - 1];
313 ret.nfsect = n + 1;
314 }
315 ret.fsect[n].just_numbers = utob(p);
316 } else if (ishtmlkeyword(source->keyword, L"xhtml-section-suffix"))
317 {
318 wchar_t *p = uadv(source->keyword);
319 int n = 0;
320 if (uisdigit(*p))
321 {
322 n = utoi(p);
323 p = uadv(p);
324 }
325 if (n >= ret.nfsect)
326 {
327 int i;
328 ret.fsect = resize(ret.fsect, n + 1);
329 for (i = ret.nfsect; i <= n; i++)
330 ret.fsect[i] = ret.fsect[ret.nfsect - 1];
331 ret.nfsect = n + 1;
332 }
333 ustrreplacedup(&ret.fsect[n].number_suffix, p);
334 } else if (ishtmlkeyword(source->keyword, L"xhtml-keywordfragments"))
335 {
336 ret.keywordfragments = utob(uadv(source->keyword));
337 }
338 }
339 }
340
341 /* printf(" !!! leaf_level = %i\n", ret.leaf_level);
342 printf(" !!! contentdepth-0 = %i\n", ret.contents_depth[0]);
343 printf(" !!! contentdepth-1 = %i\n", ret.contents_depth[1]);
344 printf(" !!! contentdepth-2 = %i\n", ret.contents_depth[2]);
345 printf(" !!! contentdepth-3 = %i\n", ret.contents_depth[3]);
346 printf(" !!! contentdepth-4 = %i\n", ret.contents_depth[4]);
347 printf(" !!! contentdepth-5 = %i\n", ret.contents_depth[5]);
348 printf(" !!! leaf_contains_contents = %i\n", ret.leaf_contains_contents); */
349 return ret;
350 }
351
352 #define element_close(rs__, tagname__) rdaddsc((rs__), "</" tagname__ ">")
353 #define element_open(rs__, tagname__) rdaddsc((rs__), "<" tagname__ ">")
354
355 static xhtmlsection *xhtml_new_section(xhtmlsection * last)
356 {
357 xhtmlsection *ret = mknew(xhtmlsection);
358 ret->next = NULL;
359 ret->child = NULL;
360 ret->parent = NULL;
361 ret->chain = last;
362 ret->para = NULL;
363 ret->file = NULL;
364 ret->fragment = NULL;
365 ret->level = -1; /* marker: end of chain */
366 return ret;
367 }
368
369 /* Returns NULL or the section that marks that paragraph */
370 static xhtmlsection *xhtml_find_section(paragraph * p)
371 {
372 xhtmlsection *ret = topsection;
373 if (xhtml_para_level(p) == -1)
374 { /* first, we back-track to a section paragraph */
375 paragraph *p2 = sourceparas;
376 paragraph *p3 = NULL;
377 while (p2 && p2 != p)
378 {
379 if (xhtml_para_level(p2) != -1)
380 {
381 p3 = p2;
382 }
383 p2 = p2->next;
384 }
385 if (p3 == NULL)
386 { /* for some reason, we couldn't find a section before this paragraph ... ? */
387 /* Note that this can happen, if you have a cross-reference to before the first chapter starts.
388 * So don't do that, then.
389 */
390 return NULL;
391 }
392 p = p3;
393 }
394 while (ret && ret->para != p)
395 {
396 /* printf(" xhtml_find_section(): checking %s for para @ %p\n", ret->fragment, p);*/
397 ret = ret->chain;
398 }
399 return ret;
400 }
401
402 static xhtmlfile *xhtml_new_file(xhtmlsection * sect)
403 {
404 xhtmlfile *ret = mknew(xhtmlfile);
405
406 ret->next = NULL;
407 ret->child = NULL;
408 ret->parent = NULL;
409 ret->filename = NULL;
410 ret->sections = sect;
411 ret->is_leaf = (sect != NULL && sect->level == conf.leaf_level);
412 if (sect == NULL)
413 {
414 if (conf.leaf_level == 0)
415 { /* currently unused */
416 #define FILENAME_MANUAL "Manual.html"
417 #define FILENAME_CONTENTS "Contents.html"
418 ret->filename = smalloc(strlen(FILENAME_MANUAL) + 1);
419 sprintf(ret->filename, FILENAME_MANUAL);
420 } else
421 {
422 ret->filename = smalloc(strlen(FILENAME_CONTENTS) + 1);
423 sprintf(ret->filename, FILENAME_CONTENTS);
424 }
425 } else
426 {
427 paragraph *p = sect->para;
428 rdstringc fname_c = { 0, 0, NULL };
429 char *c;
430 word *w;
431 for (w = (p->kwtext) ? (p->kwtext) : (p->words); w; w = w->next)
432 {
433 switch (removeattr(w->type))
434 {
435 case word_Normal:
436 /*case word_Emph:
437 case word_Code:
438 case word_WeakCode: */
439 xhtml_utostr(w->text, &c);
440 rdaddsc(&fname_c, c);
441 sfree(c);
442 break;
443 }
444 }
445 rdaddsc(&fname_c, ".html");
446 ret->filename = rdtrimc(&fname_c);
447 }
448 /* printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false")); */
449 return ret;
450 }
451
452 /*
453 * Walk the tree fixing up files which are actually leaf (ie
454 * have no children) but aren't at leaf level, so they have the
455 * leaf flag set.
456 */
457 void xhtml_fixup_layout(xhtmlfile * file)
458 {
459 if (file->child == NULL)
460 {
461 file->is_leaf = TRUE;
462 } else
463 {
464 xhtml_fixup_layout(file->child);
465 }
466 if (file->next)
467 xhtml_fixup_layout(file->next);
468 }
469
470 /*
471 * Create the tree structure so we know where everything goes.
472 * Method:
473 *
474 * Ignoring file splitting, we have three choices with each new section:
475 *
476 * +-----------------+-----------------+
477 * | | |
478 * X +----X----+ (1)
479 * | |
480 * Y (2)
481 * |
482 * (3)
483 *
484 * Y is the last section we added (currentsect).
485 * If sect is the section we want to add, then:
486 *
487 * (1) if sect->level < currentsect->level
488 * (2) if sect->level == currentsect->level
489 * (3) if sect->level > currentsect->level
490 *
491 * This requires the constraint that you never skip section numbers
492 * (so you can't have a.b.c.d without all of a, a.b and a.b.c existing).
493 *
494 * Note that you _can_ have 1.1.1.1 followed by 1.2 - you can change
495 * more than one level at a time. Lots of asserts, and probably part of
496 * the algorithm here, rely on this being true. (It currently isn't
497 * enforced by halibut, however.)
498 *
499 * File splitting makes this harder. For instance, say we added at (3)
500 * above and now need to add another section. We are splitting at level
501 * 2, ie the level of Y. Z is the last section we added:
502 *
503 * +-----------------+-----------------+
504 * | | |
505 * X +----X----+ (1)
506 * | |
507 * +----Y----+ (1)
508 * | |
509 * Z (2)
510 * |
511 * (3)
512 *
513 * The (1) case is now split; we need to search upwards to find where
514 * to actually link in. The other two cases remain the same (and will
515 * always be like this).
516 *
517 * File splitting makes this harder, however. The decision of whether
518 * to split to a new file is always on the same condition, however (is
519 * the level of this section higher than the leaf_level configuration
520 * value or not).
521 *
522 * Treating the cases backwards:
523 *
524 * (3) same file if sect->level > conf.leaf_level, otherwise new file
525 *
526 * if in the same file, currentsect->child points to sect
527 * otherwise the linking is done through the file tree (which works
528 * in more or less the same way, ie currentfile->child points to
529 * the new file)
530 *
531 * (2) same file if sect->level > conf.leaf_level, otherwise new file
532 *
533 * if in the same file, currentsect->next points to sect
534 * otherwise file linking and currentfile->next points to the new
535 * file (we know that Z must have caused a new file to be created)
536 *
537 * (1) same file if sect->level > conf.leaf_level, otherwise new file
538 *
539 * this is actually effectively the same case as (2) here,
540 * except that we first have to travel up the sections to figure
541 * out which section this new one will be a sibling of. In doing
542 * so, we may disappear off the top of a file and have to go up
543 * to its parent in the file tree.
544 *
545 */
546 static void xhtml_ponder_layout(paragraph * p)
547 {
548 xhtmlsection *lastsection;
549 xhtmlsection *currentsect;
550 xhtmlfile *currentfile;
551
552 lastfile = NULL;
553 topsection = xhtml_new_section(NULL);
554 topfile = xhtml_new_file(NULL);
555 lastsection = topsection;
556 currentfile = topfile;
557 currentsect = topsection;
558
559 if (conf.leaf_level == 0)
560 {
561 topfile->is_leaf = 1;
562 topfile->sections = topsection;
563 topsection->file = topfile;
564 }
565
566 for (; p; p = p->next)
567 {
568 int level = xhtml_para_level(p);
569 if (level > 0)
570 { /* actually a section */
571 xhtmlsection *sect;
572 word *w;
573 char *c;
574 rdstringc fname_c = { 0, 0, NULL };
575
576 sect = xhtml_new_section(lastsection);
577 lastsection = sect;
578 sect->para = p;
579 for (w = (p->kwtext2) ? (p->kwtext2) : (p->words); w; w = w->next)
580 { /* kwtext2 because we want numbers only! */
581 switch (removeattr(w->type))
582 {
583 case word_Normal:
584 /*case word_Emph:
585 case word_Code:
586 case word_WeakCode: */
587 xhtml_utostr(w->text, &c);
588 rdaddsc(&fname_c, c);
589 sfree(c);
590 break;
591 }
592 }
593 /* rdaddsc(&fname_c, ".html");*/
594 sect->fragment = rdtrimc(&fname_c);
595 sect->level = level;
596 /* printf(" ! adding para @ %p as sect %s, level %i\n", sect->para, sect->fragment, level); */
597
598 if (level > currentsect->level)
599 { /* case (3) */
600 if (level > conf.leaf_level)
601 { /* same file */
602 assert(currentfile->is_leaf);
603 currentsect->child = sect;
604 sect->parent = currentsect;
605 sect->file = currentfile;
606 /* printf("connected '%s' to existing file '%s' [I]\n", sect->fragment, currentfile->filename); */
607 currentsect = sect;
608 } else
609 { /* new file */
610 xhtmlfile *file = xhtml_new_file(sect);
611 assert(!currentfile->is_leaf);
612 currentfile->child = file;
613 sect->file = file;
614 file->parent = currentfile;
615 /* printf("connected '%s' to new file '%s' [I]\n", sect->fragment, file->filename); */
616 currentfile = file;
617 currentsect = sect;
618 }
619 } else if (level >= currentsect->file->sections->level)
620 {
621 /* Case (1) or (2) *AND* still under the section that starts
622 * the current file.
623 *
624 * I'm not convinced that this couldn't be rolled in with the
625 * final else {} leg further down. It seems a lot of effort
626 * this way.
627 */
628 if (level > conf.leaf_level)
629 { /* stick within the same file */
630 assert(currentfile->is_leaf);
631 sect->file = currentfile;
632 while (currentsect && currentsect->level > level &&
633 currentsect->file == currentsect->parent->file)
634 {
635 currentsect = currentsect->parent;
636 }
637 assert(currentsect);
638 currentsect->next = sect;
639 assert(currentsect->level == sect->level);
640 sect->parent = currentsect->parent;
641 currentsect = sect;
642 /* printf("connected '%s' to existing file '%s' [II]\n", sect->fragment, currentfile->filename); */
643 } else
644 { /* new file */
645 xhtmlfile *file = xhtml_new_file(sect);
646 sect->file = file;
647 currentfile->next = file;
648 file->parent = currentfile->parent;
649 file->is_leaf = (level == conf.leaf_level);
650 file->sections = sect;
651 /* printf("connected '%s' to new file '%s' [II]\n", sect->fragment, file->filename); */
652 currentfile = file;
653 currentsect = sect;
654 }
655 } else
656 { /* Case (1) or (2) and we must move up the file tree first */
657 /* this loop is now probably irrelevant - we know we can't connect
658 * to anything in the current file */
659 while (currentsect && level < currentsect->level)
660 {
661 currentsect = currentsect->parent;
662 if (currentsect)
663 {
664 /* printf(" * up one level to '%s'\n", currentsect->fragment); */
665 } else
666 {
667 /* printf(" * up one level (off top of current file)\n"); */
668 }
669 }
670 if (currentsect)
671 {
672 /* I'm pretty sure this can now never fire */
673 assert(currentfile->is_leaf);
674 /* printf("connected '%s' to existing file '%s' [III]\n", sect->fragment, currentfile->filename); */
675 sect->file = currentfile;
676 currentsect->next = sect;
677 currentsect = sect;
678 } else
679 { /* find a file we can attach to */
680 while (currentfile && currentfile->sections
681 && level < currentfile->sections->level)
682 {
683 currentfile = currentfile->parent;
684 if (currentfile)
685 {
686 /* printf(" * up one file level to '%s'\n", currentfile->filename); */
687 } else
688 {
689 /* printf(" * up one file level (off top of tree)\n"); */
690 }
691 }
692 if (currentfile)
693 { /* new file (we had to skip up a file to
694 get here, so we must be dealing with a
695 level no lower than the configured
696 leaf_level */
697 xhtmlfile *file = xhtml_new_file(sect);
698 currentfile->next = file;
699 sect->file = file;
700 file->parent = currentfile->parent;
701 file->is_leaf = (level == conf.leaf_level);
702 file->sections = sect;
703 /* printf("connected '%s' to new file '%s' [III]\n", sect->fragment, file->filename); */
704 currentfile = file;
705 currentsect = sect;
706 } else
707 {
708 fatal(err_whatever,
709 "Ran off the top trying to connect sibling: strange document.");
710 }
711 }
712 }
713 }
714 }
715 topsection = lastsection; /* get correct end of the chain */
716 xhtml_fixup_layout(topfile); /* leaf files not at leaf level marked as such */
717 }
718
719 static void xhtml_hack_xhtmlify(word*words) // "\" "<br" "\" ">" --> "\" "<br /" "\" ">"
720 {
721 int prevwasslash = 0;
722 if (!is_xhtml()) return ;
723 for (;words; words = words->next)
724 {
725 if (!words->text) continue;
726 if (prevwasslash)
727 {
728 if (words->text[0] == '<' && !ustricmp(words->text, L"<br") && words->next && words->next->text && words->next->text[0] == '\\')
729 ustrreplacedup(&words->text, L"<br /");
730 }
731 else
732 prevwasslash = words->text[0] == '\\' && !words->text[1];
733 }
734 }
735
736 #define NAMEDFRAGMENT_MAXLEN 200 /* More than enough for our usage */
737 /*
738 * Get formatted fragment name for html anchor.
739 * Uses para->keyword if possible, falls back to the ?#.#.# default.
740 */
741 static char*
742 xhtml_get_fragmentname(const xhtmlsection * section, char * fragmentbuf)
743 {
744 if (conf.keywordfragments)
745 {
746 paragraph *para = section->para;
747 if (para && para->keyword && *para->keyword) return ustrtoa(para->keyword, fragmentbuf, NAMEDFRAGMENT_MAXLEN);
748 }
749 return section->fragment;
750 }
751
752 static void xhtml_do_index();
753 static void xhtml_do_file(xhtmlfile * file);
754 static void xhtml_do_top_file(xhtmlfile * file, paragraph * sourceform);
755 static void xhtml_do_paras(FILE * fp, paragraph * p);
756 static int xhtml_do_contents_limit(FILE * fp, xhtmlfile * file, int limit);
757 static int xhtml_do_contents_section_limit(FILE * fp, xhtmlsection * section, int limit);
758 static int xhtml_add_contents_entry(FILE * fp, xhtmlsection * section, int limit);
759 static int xhtml_do_contents(FILE * fp, xhtmlfile * file);
760 static int xhtml_do_naked_contents(FILE * fp, xhtmlfile * file);
761 static void xhtml_do_sections(FILE * fp, xhtmlsection * sections);
762
763 /*
764 * Do all the files in this structure.
765 */
766 static void xhtml_do_files(xhtmlfile * file)
767 {
768 xhtml_do_file(file);
769 if (file->child)
770 xhtml_do_files(file->child);
771 if (file->next)
772 xhtml_do_files(file->next);
773 }
774
775 /*
776 * Free up all memory used by the file tree from 'xfile' downwards
777 */
778 static void xhtml_free_file(xhtmlfile * xfile)
779 {
780 if (xfile == NULL)
781 {
782 return;
783 }
784
785 if (xfile->filename)
786 {
787 sfree(xfile->filename);
788 }
789 xhtml_free_file(xfile->child);
790 xhtml_free_file(xfile->next);
791 sfree(xfile);
792 }
793
794 /*
795 * Main function.
796 */
797 void
798 xhtml_backend(paragraph * sourceform, keywordlist * in_keywords,
799 indexdata * in_idx)
800 {
801 /* int i;*/
802 indexentry *ientry;
803 int ti;
804 xhtmlsection *xsect;
805
806 sourceparas = sourceform;
807 conf = xhtml_configure(sourceform);
808 keywords = in_keywords;
809 idx = in_idx;
810
811 /* Clear up the index entries backend data pointers */
812 for (ti = 0;
813 (ientry = (indexentry *) index234(idx->entries, ti)) != NULL; ti++)
814 {
815 ientry->backend_data = NULL;
816 }
817
818 xhtml_ponder_layout(sourceform);
819
820 /* old system ... (writes to *.alt, but gets some stuff wrong and is ugly) */
821 /* xhtml_level_0(sourceform);
822 for (i=1; i<=conf.leaf_level; i++)
823 {
824 xhtml_level(sourceform, i);
825 }*/
826
827 /* new system ... (writes to *.html, but isn't fully trusted) */
828 xhtml_do_top_file(topfile, sourceform);
829 assert(!topfile->next); /* shouldn't have a sibling at all */
830 if (topfile->child)
831 {
832 xhtml_do_files(topfile->child);
833 xhtml_do_index();
834 }
835
836 /* release file, section, index data structures */
837 xsect = topsection;
838 while (xsect)
839 {
840 xhtmlsection *tmp = xsect->chain;
841 if (xsect->fragment)
842 {
843 sfree(xsect->fragment);
844 }
845 sfree(xsect);
846 xsect = tmp;
847 }
848 xhtml_free_file(topfile);
849 for (ti = 0;
850 (ientry = (indexentry *) index234(idx->entries, ti)) != NULL; ti++)
851 {
852 if (ientry->backend_data != NULL)
853 {
854 xhtmlindex *xi = (xhtmlindex *) ientry->backend_data;
855 if (xi->sections != NULL)
856 {
857 sfree(xi->sections);
858 }
859 sfree(xi);
860 }
861 ientry->backend_data = NULL;
862 }
863 {
864 int i;
865 sfree(conf.fchapter.number_suffix);
866 for (i = 0; i < conf.nfsect; i++)
867 sfree(conf.fsect[i].number_suffix);
868 sfree(conf.fsect);
869 }
870 free_ustr_slist(conf.meta_append);
871 }
872
873 static int xhtml_para_level(paragraph * p)
874 {
875 switch (p->type)
876 {
877 case para_Title:
878 return 0;
879 break;
880 case para_UnnumberedChapter:
881 case para_Chapter:
882 case para_Appendix:
883 return 1;
884 break;
885 /* case para_BiblioCited:
886 return 2;
887 break;*/
888 case para_Heading:
889 case para_Subsect:
890 return p->aux + 2;
891 break;
892 default:
893 return -1;
894 break;
895 }
896 }
897
898 static char *xhtml_index_filename = "IndexPage.html";
899
900 /* Output the nav links for the current file.
901 * file == NULL means we're doing the index
902 */
903 static void xhtml_donavlinks(FILE * fp, xhtmlfile * file)
904 {
905 xhtmlfile *xhtml_next_file = NULL;
906 fputs("<p", fp);
907 if (conf.nav_attrs != NULL)
908 {
909 fprintf(fp, " %ls>", conf.nav_attrs);
910 } else
911 {
912 fputs(">", fp);
913 }
914 if (xhtml_last_file == NULL)
915 {
916 fputs("Previous | ", fp);
917 } else
918 {
919 fprintf(fp, "<a href='%s'>Previous</a> | ", xhtml_last_file->filename);
920 }
921 fputs("<a href='Contents.html'>Contents</a> | ", fp);
922 if (file != NULL)
923 { /* otherwise we're doing nav links for the index */
924 if (xhtml_next_file == NULL)
925 xhtml_next_file = file->child;
926 if (xhtml_next_file == NULL)
927 xhtml_next_file = file->next;
928 if (xhtml_next_file == NULL)
929 xhtml_next_file = file->parent->next;
930 }
931 if (xhtml_next_file == NULL)
932 {
933 if (file == NULL)
934 { /* index, so no next file */
935 fputs("Next ", fp);
936 } else
937 {
938 fprintf(fp, "<a href='%s'>Next</a>", xhtml_index_filename);
939 }
940 } else
941 {
942 fprintf(fp, "<a href='%s'>Next</a>", xhtml_next_file->filename);
943 }
944 fputs("</p>\n", fp);
945 }
946
947 /* Write out the index file */
948 static void xhtml_do_index_body(FILE * fp)
949 {
950 indexentry *y;
951 int ti;
952
953 if (count234(idx->entries) == 0)
954 return; /* don't write anything at all */
955
956 fputs("<dl>\n", fp);
957 /* iterate over idx->entries using the tree functions and display everything */
958 for (ti = 0; (y = (indexentry *) index234(idx->entries, ti)) != NULL;
959 ti++)
960 {
961 if (y->backend_data)
962 {
963 int i;
964 xhtmlindex *xi;
965
966 fputs("<dt>", fp);
967 xhtml_para(fp, y->text);
968 fputs("</dt>\n<dd>", fp);
969
970 xi = (xhtmlindex *) y->backend_data;
971 for (i = 0; i < xi->nsection; i++)
972 {
973 xhtmlsection *sect = xi->sections[i];
974 if (sect)
975 {
976 char fragmentbuf[NAMEDFRAGMENT_MAXLEN];
977 fprintf(fp, "<a href='%s#%s'>", conf.leaf_level ? sect->file->filename : "",
978 xhtml_get_fragmentname(sect, fragmentbuf));
979 if (sect->para->kwtext)
980 {
981 xhtml_para(fp, sect->para->kwtext);
982 } else if (sect->para->words)
983 {
984 xhtml_para(fp, sect->para->words);
985 }
986 fputs("</a>", fp);
987 if (i + 1 < xi->nsection)
988 {
989 fputs(", ", fp);
990 }
991 }
992 }
993 fputs("</dd>\n", fp);
994 }
995 }
996 fputs("</dl>\n", fp);
997 }
998 static void xhtml_do_index()
999 {
1000 word temp_word =
1001 { NULL, NULL, word_Normal, 0, 0, L"Index", {NULL, 0, 0} };
1002 FILE *fp = fopen(xhtml_index_filename, "w");
1003
1004 if (fp == NULL)
1005 fatal(err_cantopenw, xhtml_index_filename);
1006 xhtml_doheader(fp, &temp_word);
1007 xhtml_donavlinks(fp, NULL);
1008
1009 xhtml_do_index_body(fp);
1010
1011 xhtml_donavlinks(fp, NULL);
1012 xhtml_dofooter(fp);
1013 fclose(fp);
1014 }
1015
1016 /* Output the given file. This includes whatever contents at beginning and end, etc. etc. */
1017 static void xhtml_do_file(xhtmlfile * file)
1018 {
1019 FILE *fp = fopen(file->filename, "w");
1020 if (fp == NULL)
1021 fatal(err_cantopenw, file->filename);
1022
1023 if (file->sections->para->words)
1024 {
1025 xhtml_doheader(fp, file->sections->para->words);
1026 } else if (file->sections->para->kwtext)
1027 {
1028 xhtml_doheader(fp, file->sections->para->kwtext);
1029 } else
1030 {
1031 xhtml_doheader(fp, NULL);
1032 }
1033
1034 xhtml_donavlinks(fp, file);
1035
1036 if (file->is_leaf && conf.leaf_contains_contents &&
1037 xhtml_do_contents(NULL, file) >= conf.leaf_smallest_contents)
1038 xhtml_do_contents(fp, file);
1039 xhtml_do_sections(fp, file->sections);
1040 if (!file->is_leaf)
1041 xhtml_do_naked_contents(fp, file);
1042
1043 xhtml_donavlinks(fp, file);
1044
1045 xhtml_dofooter(fp);
1046 fclose(fp);
1047
1048 xhtml_last_file = file;
1049 }
1050
1051 /* Output the top-level file. */
1052 static void xhtml_do_top_file(xhtmlfile * file, paragraph * sourceform)
1053 {
1054 paragraph *p;
1055 char fname[4096];
1056 int done = FALSE;
1057
1058 FILE *fp = fopen(file->filename, "w");
1059 if (fp == NULL)
1060 fatal(err_cantopenw, file->filename);
1061
1062 if (conf.chm_toc_file || conf.chm_ind_file) g_outputtype = OT_CHM | (g_outputtype & (OT_XHTML|OT_HTML5));
1063
1064 ustrtoa(conf.chm_toc_file, fname, 4096);
1065 if(*fname)
1066 {
1067 chm_toc = fopen(fname, "w");
1068 if (chm_toc == NULL)
1069 fatal(err_cantopenw, fname);
1070 }
1071 else
1072 chm_toc = NULL;
1073
1074 ustrtoa(conf.chm_ind_file, fname, 4096);
1075 if(*fname){
1076 chm_ind = fopen(fname, "w");
1077 if (chm_ind == NULL)
1078 fatal(err_cantopenw, fname);
1079 }
1080 else
1081 chm_ind = NULL;
1082
1083 /* Do the title -- only one allowed */
1084 for (p = sourceform; p && !done; p = p->next)
1085 {
1086 if (p->type == para_Title)
1087 {
1088 xhtml_doheader(fp, p->words);
1089 if(chm_toc)chm_doheader(chm_toc, p->words);
1090 if(chm_ind)chm_doheader(chm_ind, p->words);
1091 done = TRUE;
1092 }
1093 }
1094 if (!done)
1095 xhtml_doheader(fp, NULL /* Eek! */ );
1096
1097 /*
1098 * Display the title.
1099 */
1100 for (p = sourceform; p; p = p->next)
1101 {
1102 if (p->type == para_Title)
1103 {
1104 xhtml_heading(fp, p);
1105 break;
1106 }
1107 }
1108
1109 /* Do the preamble and copyright */
1110 for (p = sourceform; p; p = p->next)
1111 {
1112 if (p->type == para_Preamble)
1113 {
1114 fputs("<p>", fp);
1115 xhtml_hack_xhtmlify(p->words);
1116 xhtml_para(fp, p->words);
1117 fputs("</p>\n", fp);
1118 }
1119 }
1120 for (p = sourceform; p; p = p->next)
1121 {
1122 if (p->type == para_Copyright)
1123 {
1124 fputs("<p>", fp);
1125 xhtml_hack_xhtmlify(p->words);
1126 xhtml_para(fp, p->words);
1127 fputs("</p>\n", fp);
1128 }
1129 }
1130
1131 xhtml_do_contents(fp, file);
1132 xhtml_do_sections(fp, file->sections);
1133
1134 /*
1135 * Put the index in the top file if we're in single-file mode
1136 * (leaf-level 0).
1137 */
1138 if (conf.leaf_level == 0 && count234(idx->entries) > 0)
1139 {
1140 fputs("<a name=\"index\"></a><h1>Index</h1>\n", fp);
1141 xhtml_do_index_body(fp);
1142 }
1143
1144 xhtml_dofooter(fp);
1145 if(chm_toc)chm_dofooter(chm_toc);
1146 if(chm_ind)chm_dofooter(chm_ind);
1147 fclose(fp);
1148 if(chm_toc)
1149 {
1150 fclose(chm_toc);
1151 chm_toc = NULL;
1152 }
1153 if(chm_ind)
1154 {
1155 fclose(chm_ind);
1156 chm_ind = NULL;
1157 }
1158 }
1159
1160 /* Convert a Unicode string to an ASCII one. '?' is
1161 * used for unmappable characters.
1162 */
1163 static void xhtml_utostr(wchar_t * in, char **out)
1164 {
1165 int l = ustrlen(in);
1166 int i;
1167 *out = smalloc(l + 1);
1168 for (i = 0; i < l; i++)
1169 {
1170 if (in[i] >= 32 && in[i] <= 126)
1171 (*out)[i] = (char) in[i];
1172 else
1173 (*out)[i] = '?';
1174 }
1175 (*out)[i] = 0;
1176 }
1177
1178 /*
1179 * Write contents for the given file, and subfiles, down to
1180 * the appropriate contents depth. Returns the number of
1181 * entries written.
1182 */
1183 static int xhtml_do_contents(FILE * fp, xhtmlfile * file)
1184 {
1185 int level, limit, start_level, count = 0;
1186 if (!file)
1187 return 0;
1188
1189 level = (file->sections) ? (file->sections->level) : (0);
1190 limit = conf.contents_depth[(level > 5) ? (5) : (level)];
1191 start_level = (file->is_leaf) ? (level - 1) : (level);
1192 last_level = start_level;
1193
1194 count += xhtml_do_contents_section_limit(fp, file->sections, limit);
1195 count += xhtml_do_contents_limit(fp, file->child, limit);
1196 if (fp != NULL)
1197 {
1198 while (last_level > start_level)
1199 {
1200 last_level--;
1201 fputs("</ul>\n", fp);
1202 if(chm_toc)fprintf(chm_toc, "</ul>\n");
1203 }
1204 }
1205 return count;
1206 }
1207
1208 /* As above, but doesn't do anything in the current file */
1209 static int xhtml_do_naked_contents(FILE * fp, xhtmlfile * file)
1210 {
1211 int level, limit, start_level, count = 0;
1212 if (!file)
1213 return 0;
1214
1215 level = (file->sections) ? (file->sections->level) : (0);
1216 limit = conf.contents_depth[(level > 5) ? (5) : (level)];
1217 start_level = (file->is_leaf) ? (level - 1) : (level);
1218 last_level = start_level;
1219
1220 count = xhtml_do_contents_limit(fp, file->child, limit);
1221 if (fp != NULL)
1222 {
1223 while (last_level > start_level)
1224 {
1225 last_level--;
1226 fputs("</ul>\n", fp);
1227 if(chm_toc)fprintf(chm_toc, "</ul>\n");
1228 }
1229 }
1230 return count;
1231 }
1232
1233 /*
1234 * Write contents for the given file, children, and siblings, down to
1235 * given limit contents depth.
1236 */
1237 static int xhtml_do_contents_limit(FILE * fp, xhtmlfile * file, int limit)
1238 {
1239 int count = 0;
1240 while (file)
1241 {
1242 count += xhtml_do_contents_section_limit(fp, file->sections, limit);
1243 count += xhtml_do_contents_limit(fp, file->child, limit);
1244 file = file->next;
1245 }
1246 return count;
1247 }
1248
1249 /*
1250 * Write contents entries for the given section tree, down to the
1251 * limit contents depth.
1252 */
1253 static int
1254 xhtml_do_contents_section_deep_limit(FILE * fp, xhtmlsection * section,
1255 int limit)
1256 {
1257 int count = 0;
1258 while (section)
1259 {
1260 if (!xhtml_add_contents_entry(fp, section, limit))
1261 return 0;
1262 else
1263 count++;
1264 count +=
1265 xhtml_do_contents_section_deep_limit(fp, section->child, limit);
1266 section = section->next;
1267 }
1268 return count;
1269 }
1270
1271 /*
1272 * Write contents entries for the given section tree, down to the
1273 * limit contents depth.
1274 */
1275 static int
1276 xhtml_do_contents_section_limit(FILE * fp, xhtmlsection * section, int limit)
1277 {
1278 int count = 0;
1279 if (!section)
1280 return 0;
1281 xhtml_add_contents_entry(fp, section, limit);
1282 count = 1;
1283 count += xhtml_do_contents_section_deep_limit(fp, section->child, limit);
1284 /* section=section->child;
1285 while (section && xhtml_add_contents_entry(fp, section, limit)) {
1286 section = section->next;
1287 } */
1288 return count;
1289 }
1290
1291 /*
1292 * Add a section entry, unless we're exceeding the limit, in which
1293 * case return FALSE (otherwise return TRUE).
1294 */
1295 static int
1296 xhtml_add_contents_entry(FILE * fp, xhtmlsection * section, int limit)
1297 {
1298 char fragmentbuf[NAMEDFRAGMENT_MAXLEN], *fragment, *filename;
1299 if (!section || section->level > limit)
1300 return FALSE;
1301 if (fp == NULL || section->level < 0)
1302 return TRUE;
1303 while (last_level > section->level)
1304 {
1305 last_level--;
1306 fputs("</ul>\n", fp);
1307 if(chm_toc)fprintf(chm_toc, "</ul>\n");
1308 }
1309 while (last_level < section->level)
1310 {
1311 last_level++;
1312 fputs("<ul>\n", fp);
1313 if(chm_toc)fprintf(chm_toc, "<ul>\n");
1314 }
1315 filename = conf.leaf_level ? section->file->filename : "";
1316 fragment = "";
1317 if (section->para->type != para_Chapter || !conf.leaf_level)
1318 fragment = xhtml_get_fragmentname(section, fragmentbuf);
1319
1320 fputs("<li>", fp);
1321 fprintf(fp, "<a %shref=\"%s#%s\">",
1322 (section->para->type == para_Chapter|| section->para->type == para_Appendix) ? "class=\"btitle\" " : "",
1323 filename, fragment);
1324 if(chm_toc)fprintf(chm_toc, "<li><OBJECT type=\"text/sitemap\"><param name=\"Local\" value=\"%s#%s\"><param name=\"Name\" value=\"",
1325 filename, fragment);
1326 if(chm_ind)fprintf(chm_ind, "<li><OBJECT type=\"text/sitemap\"><param name=\"Local\" value=\"%s#%s\"><param name=\"Name\" value=\"",
1327 filename, fragment);
1328 //%s
1329 if (section->para->type == para_Chapter
1330 || section->para->type == para_Appendix)
1331 fputs("<b>", fp);
1332 if ((section->para->type != para_Heading
1333 && section->para->type != para_Subsect) || (section->para->kwtext
1334 && !section->para->
1335 words))
1336 {
1337 xhtml_para(fp, section->para->kwtext);
1338 if(chm_toc)xhtml_para(chm_toc, section->para->kwtext);
1339 if (section->para->words){
1340 fputs(": ", fp);
1341 if(chm_toc)fprintf(chm_toc, ": ");
1342 }
1343 }
1344 if (section->para->type == para_Chapter
1345 || section->para->type == para_Appendix)
1346 fputs("</b>", fp);
1347 if (section->para->words)
1348 {
1349 xhtml_para(fp, section->para->words);
1350 if(chm_toc)xhtml_para(chm_toc, section->para->words);
1351 if(chm_ind)xhtml_para(chm_ind, section->para->words);
1352 }
1353 fputs("</a></li>\n", fp);
1354 if(chm_toc)fprintf(chm_toc,"\"></OBJECT></li>\n");
1355 if(chm_ind)fprintf(chm_ind,"\"></OBJECT></li>\n");
1356 return TRUE;
1357 }
1358
1359 /*
1360 * Write all the sections in this file. Do all paragraphs in this section, then all
1361 * children (recursively), then go on to the next one (tail recursively).
1362 */
1363 static void xhtml_do_sections(FILE * fp, xhtmlsection * sections)
1364 {
1365 while (sections)
1366 {
1367 currentsection = sections;
1368 xhtml_do_paras(fp, sections->para);
1369 xhtml_do_sections(fp, sections->child);
1370 sections = sections->next;
1371 }
1372 }
1373
1374 /* Write this list of paragraphs. Close off all lists at the end. */
1375 static void xhtml_do_paras(FILE * fp, paragraph * p)
1376 {
1377 int last_type = -1, first = TRUE;
1378 if (!p)
1379 return;
1380
1381 /* for (; p && (xhtml_para_level(p)>limit || xhtml_para_level(p)==-1 || first); p=p->next) {*/
1382 for (; p && (xhtml_para_level(p) == -1 || first); p = p->next)
1383 {
1384 first = FALSE;
1385 switch (p->type)
1386 {
1387 /*
1388 * Things we ignore because we've already processed them or
1389 * aren't going to touch them in this pass.
1390 */
1391 case para_IM:
1392 case para_BR:
1393 case para_Biblio: /* only touch BiblioCited */
1394 case para_VersionID:
1395 case para_Copyright:
1396 case para_Preamble:
1397 case para_NoCite:
1398 case para_Title:
1399 break;
1400
1401 /*
1402 * Chapter titles.
1403 */
1404 case para_Chapter:
1405 case para_Appendix:
1406 case para_UnnumberedChapter:
1407 xhtml_heading(fp, p);
1408 break;
1409
1410 case para_Heading:
1411 case para_Subsect:
1412 xhtml_heading(fp, p);
1413 break;
1414
1415 case para_Rule:
1416 fprintf(fp, "\n%s\n", gettagtxt_hr());
1417 break;
1418
1419 case para_Normal:
1420 fputs("\n<p>", fp);
1421 xhtml_para(fp, p->words);
1422 fputs("</p>\n", fp);
1423 break;
1424
1425 case para_Bullet:
1426 case para_NumberedList:
1427 case para_BiblioCited:
1428 if (last_type != p->type)
1429 {
1430 /* start up list if necessary */
1431 if (p->type == para_Bullet)
1432 {
1433 fputs("<ul>\n", fp);
1434 } else if (p->type == para_NumberedList)
1435 {
1436 fputs("<ol>\n", fp);
1437 } else if (p->type == para_BiblioCited)
1438 {
1439 fputs("<dl>\n", fp);
1440 }
1441 }
1442 if (p->type == para_Bullet || p->type == para_NumberedList)
1443 fputs("<li>", fp);
1444 else if (p->type == para_BiblioCited)
1445 {
1446 fputs("<dt>", fp);
1447 xhtml_para(fp, p->kwtext);
1448 fputs("</dt>\n<dd>", fp);
1449 }
1450 xhtml_para(fp, p->words);
1451 if (p->type == para_BiblioCited)
1452 {
1453 fputs("</dd>\n", fp);
1454 } else if (p->type == para_Bullet || p->type == para_NumberedList)
1455 {
1456 fputs("</li>", fp);
1457 }
1458 if (p->type == para_Bullet || p->type == para_NumberedList
1459 || p->type == para_BiblioCited)
1460 /* close off list if necessary */
1461 {
1462 paragraph *p2 = p->next;
1463 int close_off = FALSE;
1464 /* if (p2 && (xhtml_para_level(p2)>limit || xhtml_para_level(p2)==-1)) {*/
1465 if (p2 && xhtml_para_level(p2) == -1)
1466 {
1467 if (p2->type != p->type)
1468 close_off = TRUE;
1469 } else
1470 {
1471 close_off = TRUE;
1472 }
1473 if (close_off)
1474 {
1475 if (p->type == para_Bullet)
1476 {
1477 fputs("</ul>\n", fp);
1478 } else if (p->type == para_NumberedList)
1479 {
1480 fputs("</ol>\n", fp);
1481 } else if (p->type == para_BiblioCited)
1482 {
1483 fputs("</dl>\n", fp);
1484 }
1485 }
1486 }
1487 break;
1488
1489 case para_Code:
1490 xhtml_codepara(fp, p->words);
1491 break;
1492 }
1493 last_type = p->type;
1494 }
1495 }
1496
1497 static void printoptstr(FILE * fp, const char*prefix, const wchar_t*str, const char*suffix)
1498 {
1499 if (str)
1500 {
1501 fprintf(fp, "%s%ls%s", prefix ? prefix : "", str, suffix ? suffix : "");
1502 }
1503 }
1504
1505 /*
1506 * Output a header for this XHTML file.
1507 */
1508 static void xhtml_doheader(FILE * fp, word * title)
1509 {
1510 const int xhtml = is_xhtml(), html5 = is_html5();
1511 const char *xhtmldoctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
1512 const char *html4doctype = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
1513 const char *xhtmlxmlns = xhtml ? " xmlns=\"http://www.w3.org/1999/xhtml\"" : 0;
1514 const char *voidend = xhtml ? " /" : "";
1515 const wchar_t *tmpwstr;
1516 const ustr_slist *pussl;
1517
1518 if (xhtml && html5) fatal(err_whatever, "indeterminate format");
1519 fputs(html5 ? "<!DOCTYPE html>\n" : xhtml ? xhtmldoctype : html4doctype, fp);
1520 fprintf(fp, "<html%s", xhtml ? xhtmlxmlns : "");
1521 //www.w3.org/International/questions/qa-html-language-declarations
1522 if (*(tmpwstr = ustrdef(conf.html_lang, L"")))
1523 fprintf(fp, "%s%ls%s lang=\"%ls\"", xhtml ? " xml:lang=\"" : "", xhtml ? tmpwstr : L"", xhtml ? "\"" : "", tmpwstr);
1524 fputs("><head>\n", fp);
1525 if (ustricmp(L"none", (tmpwstr = ustrdef(conf.meta_charset, L"UTF-8"))))
1526 fprintf(fp, (xhtml || !html5) ? "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%ls\"%s>" : "<meta charset=\"%ls\">\n", tmpwstr, voidend);
1527 printoptstr(fp, "", conf.head_start, "\n");
1528 fputs("<title>", fp);
1529 if (title == NULL)
1530 fputs("Documentation", fp);
1531 else
1532 xhtml_para(fp, title);
1533 fputs("</title>\n", fp);
1534 for (pussl = conf.meta_append; pussl; pussl = pussl->next)
1535 fprintf(fp, "<meta %ls%s>", pussl->string, voidend);
1536 printoptstr(fp, "", conf.head_middle, "\n");
1537 fprintf(fp, "<meta name=\"generator\" content=\"Halibut %s\"%s>\n", version, voidend);
1538 if (conf.author)
1539 fprintf(fp, "<meta name=\"author\" content=\"%ls\"%s>\n", conf.author, voidend);
1540 if (conf.description)
1541 fprintf(fp, "<meta name=\"description\" content=\"%ls\"%s>\n", conf.description, voidend);
1542 printoptstr(fp, "", conf.head_end, "\n");
1543 fputs("</head>\n", fp);
1544 fprintf(fp, "%ls\n", conf.body ? conf.body : L"<body>");
1545 if (conf.body_start)
1546 fprintf(fp, "%ls\n", conf.body_start);
1547 }
1548
1549 static void chm_doheader(FILE * fp, word * title)
1550 {
1551 fputs("<HTML><BODY><UL><LI><OBJECT type=\"text/sitemap\"><param name=\"Name\" value=\"", fp);
1552 xhtml_para(fp, title);
1553 fputs("\"><param name=\"Local\" value=\"Contents.html\"></OBJECT></li>\n", fp);
1554 }
1555
1556 /*
1557 * Output a footer for this XHTML file.
1558 */
1559 static void xhtml_dofooter(FILE * fp)
1560 {
1561 int hr = conf.body_end || !conf.suppress_address;
1562
1563 if (hr)
1564 fprintf(fp, "\n%s\n\n", gettagtxt_hr());
1565
1566 if (conf.body_end)
1567 fprintf(fp, "%ls\n", conf.body_end);
1568 if (!conf.suppress_address)
1569 {
1570 fputs("<address>\n", fp);
1571 if (conf.address_start)
1572 fprintf(fp, "%ls\n", conf.address_start);
1573 /* Do the version ID */
1574 if (conf.include_version_id)
1575 {
1576 paragraph *p;
1577 int started = 0;
1578 for (p = sourceparas; p; p = p->next)
1579 if (p->type == para_VersionID)
1580 {
1581 xhtml_versionid(fp, p->words, started);
1582 started = 1;
1583 }
1584 }
1585 if (conf.address_end)
1586 fprintf(fp, "%ls\n", conf.address_end);
1587 fputs("</address>\n", fp);
1588 }
1589 fputs("</body></html>\n", fp);
1590 }
1591 static void chm_dofooter(FILE * fp)
1592 {
1593 fputs("</ul></BODY></HTML>\n", fp);
1594 }
1595
1596 /*
1597 * Output the versionid paragraph. Typically this is a version control
1598 * ID string (such as $Id...$ in RCS).
1599 */
1600 static void xhtml_versionid(FILE * fp, word * text, int started)
1601 {
1602 rdstringc t = { 0, 0, NULL };
1603
1604 rdaddc(&t, '['); /* FIXME: configurability */
1605 xhtml_rdaddwc(&t, text, NULL);
1606 rdaddc(&t, ']'); /* FIXME: configurability */
1607
1608 if (started)
1609 fprintf(fp, "%s\n", gettagtxt_br());
1610 fprintf(fp, "%s\n", t.text);
1611 sfree(t.text);
1612 }
1613
1614 /* Is this an XHTML reserved character? */
1615 static int xhtml_reservedchar(int c)
1616 {
1617 if (c == '&' || c == '<' || c == '>' || c == '"')
1618 return TRUE;
1619 else
1620 return FALSE;
1621 }
1622
1623 /*
1624 * Convert a wide string into valid XHTML: Anything outside ASCII will
1625 * be fixed up as an entity. Currently we don't worry about constraining the
1626 * encoded character set, which we should probably do at some point (we can
1627 * still fix up and return FALSE - see the last comment here). We also don't
1628 * currently
1629 *
1630 * Because this is only used for words, spaces are HARD spaces (any other
1631 * spaces will be word_Whitespace not word_Normal). So they become &nbsp;
1632 * Unless hard_spaces is FALSE, of course (code paragraphs break the above
1633 * rule).
1634 *
1635 * If `result' is non-NULL, mallocs the resulting string and stores a pointer to
1636 * it in `*result'. If `result' is NULL, merely checks whether all
1637 * characters in the string are feasible.
1638 *
1639 * Return is nonzero if all characters are OK. If not all
1640 * characters are OK but `result' is non-NULL, a result _will_
1641 * still be generated!
1642 */
1643 static int xhtml_convert(wchar_t * s, char **result, int hard_spaces)
1644 {
1645 int doing = (result != 0);
1646 int ok = TRUE;
1647 char *p = NULL;
1648 int plen = 0, psize = 0;
1649
1650 for (; *s; s++)
1651 {
1652 wchar_t c = *s;
1653
1654 #define ensure_size(i) if (i>=psize) { psize = i+256; p = resize(p, psize); }
1655
1656 if (((c == 32 && !hard_spaces)
1657 || (c > 32 && c <= 126 && !xhtml_reservedchar(c))))
1658 {
1659 /* Char is OK. */
1660 if (doing)
1661 {
1662 ensure_size(plen);
1663 p[plen++] = (char) c;
1664 }
1665 } else
1666 {
1667 /* Char needs fixing up. */
1668 /* ok = FALSE; -- currently we never return FALSE; we
1669 * might want to when considering a character set for the
1670 * encoded document.
1671 */
1672 if (doing)
1673 {
1674 if (c == 32)
1675 { /* a space in a word is a hard space */
1676 ensure_size(plen + 7); /* includes space for the NUL, which is subsequently stomped on */
1677 sprintf(p + plen, "&nbsp;");
1678 plen += 6;
1679 } else
1680 {
1681 switch (c)
1682 {
1683 case '&':
1684 ensure_size(plen + 6); /* includes space for the NUL, which is subsequently stomped on */
1685 plen += sprintf(p + plen, "&amp;");
1686 break;
1687 case '"':
1688 ensure_size(plen + 7); /* includes space for the NUL, which is subsequently stomped on */
1689 plen += sprintf(p + plen, "&quot;");
1690 break;
1691 case '<':
1692 if (plen > 1 && *(s - 1) == '\\' && *(s - 2) == '\\')
1693 {
1694 ensure_size(--plen);
1695 p[plen - 1] = (char) c;
1696 p[plen] = 0;
1697 } else
1698 {
1699 ensure_size(plen + 5); /* includes space for the NUL, which is subsequently stomped on */
1700 plen += sprintf(p + plen, "&lt;");
1701 }
1702 break;
1703 case '>':
1704 if (plen > 1 && *(s - 1) == '\\' && *(s - 2) == '\\')
1705 {
1706 ensure_size(--plen);
1707 p[plen - 1] = (char) c;
1708 p[plen] = 0;
1709 } else
1710 {
1711 ensure_size(plen + 5); /* includes space for the NUL, which is subsequently stomped on */
1712 plen += sprintf(p + plen, "&gt;");
1713 }
1714 break;
1715 default:
1716 ensure_size(plen + 8); /* includes space for the NUL, which is subsequently stomped on */
1717 plen += sprintf(p + plen, "&#%04i;", (int) c);
1718 break;
1719 }
1720 }
1721 }
1722 }
1723 }
1724 if (doing)
1725 {
1726 p = resize(p, plen + 1);
1727 p[plen] = '\0';
1728 *result = p;
1729 }
1730
1731 return ok;
1732 }
1733
1734 /*
1735 * This formats the given words as XHTML.
1736 */
1737 static void xhtml_rdaddwc(rdstringc * rs, word * text, word * end)
1738 {
1739 char *c;
1740 keyword *kwl;
1741 xhtmlsection *sect;
1742 indextag *itag;
1743 int ti, style, type;
1744 wchar_t *s;
1745
1746 for (; text && text != end; text = text->next)
1747 {
1748 switch (text->type)
1749 {
1750 case word_HyperLink:
1751 xhtml_utostr(text->text, &c);
1752 rdaddsc(rs, "<a href=\"");
1753 if(chm_toc && *c == '.' && *(c+1) == '.')
1754 rdaddsc(rs, c + 1);
1755 else
1756 rdaddsc(rs, c);
1757 rdaddsc(rs, "\">");
1758 sfree(c);
1759 break;
1760
1761 case word_LocalHyperLink:
1762 xhtml_utostr(text->text, &c);
1763 rdaddsc(rs, "<a href=\"");
1764 if (conf.rlink_prefix)
1765 {
1766 char *c2;
1767 xhtml_utostr(conf.rlink_prefix, &c2);
1768 rdaddsc(rs, c2);
1769 sfree(c2);
1770 }
1771 rdaddsc(rs, c);
1772 if (conf.rlink_suffix)
1773 {
1774 char *c2;
1775 xhtml_utostr(conf.rlink_suffix, &c2);
1776 rdaddsc(rs, c2);
1777 sfree(c2);
1778 }
1779 rdaddsc(rs, "\">");
1780 sfree(c);
1781 break;
1782
1783 case word_UpperXref:
1784 case word_LowerXref:
1785 case word_FreeTextXref:
1786 kwl = kw_lookup(keywords, text->text);
1787 if (kwl)
1788 {
1789 sect = xhtml_find_section(kwl->para);
1790 if (sect)
1791 {
1792 char fragmentbuf[NAMEDFRAGMENT_MAXLEN];
1793 rdaddsc(rs, "<a href=\"");
1794 rdaddsc(rs, conf.leaf_level ? sect->file->filename : "");
1795 rdaddc(rs, '#');
1796 rdaddsc(rs, xhtml_get_fragmentname(sect, fragmentbuf));
1797 rdaddsc(rs, "\">");
1798 } else
1799 {
1800 rdaddsc(rs,
1801 "<a href=\"Apologies.html\"><!-- probably a bibliography cross reference -->");
1802 error(err_whatever,
1803 "Couldn't locate cross-reference! (Probably a bibliography entry.)");
1804 }
1805 } else
1806 {
1807 rdaddsc(rs,
1808 "<a href=\"Apologies.html\"><!-- unknown cross-reference -->");
1809 error(err_whatever,
1810 "Couldn't locate cross-reference! (Wasn't in source file.)");
1811 }
1812 break;
1813
1814 case word_IndexRef: /* in theory we could make an index target here */
1815 /* rdaddsc(rs, "<a name=\"idx-");
1816 xhtml_utostr(text->text, &c);
1817 rdaddsc(rs, c);
1818 sfree(c);
1819 rdaddsc(rs, "\"></a>");*/
1820 /* what we _do_ need to do is to fix up the backend data
1821 * for any indexentry this points to.
1822 */
1823 for (ti = 0;
1824 (itag = (indextag *) index234(idx->tags, ti)) != NULL; ti++)
1825 {
1826 /* FIXME: really ustricmp() and not ustrcmp()? */
1827 if (ustricmp(itag->name, text->text) == 0)
1828 {
1829 break;
1830 }
1831 }
1832 if (itag != NULL)
1833 {
1834 if (itag->refs != NULL)
1835 {
1836 int i;
1837 for (i = 0; i < itag->nrefs; i++)
1838 {
1839 xhtmlindex *idx_ref;
1840 indexentry *ientry;
1841
1842 ientry = itag->refs[i];
1843 if (ientry->backend_data == NULL)
1844 {
1845 idx_ref = (xhtmlindex *) smalloc(sizeof(xhtmlindex));
1846 if (idx_ref == NULL)
1847 fatal(err_nomemory);
1848 idx_ref->nsection = 0;
1849 idx_ref->size = 4;
1850 idx_ref->sections =
1851 (xhtmlsection **) smalloc(idx_ref->size *
1852 sizeof(xhtmlsection *));
1853 if (idx_ref->sections == NULL)
1854 fatal(err_nomemory);
1855 ientry->backend_data = idx_ref;
1856 } else
1857 {
1858 idx_ref = ientry->backend_data;
1859 if (idx_ref->nsection + 1 > idx_ref->size)
1860 {
1861 int new_size = idx_ref->size * 2;
1862 idx_ref->sections =
1863 srealloc(idx_ref->sections,
1864 new_size * sizeof(xhtmlsection));
1865 if (idx_ref->sections == NULL)
1866 {
1867 fatal(err_nomemory);
1868 }
1869 idx_ref->size = new_size;
1870 }
1871 }
1872 idx_ref->sections[idx_ref->nsection++] = currentsection;
1873 #if 0
1874 #endif
1875 }
1876 } else
1877 {
1878 fatal(err_whatever, "Index tag had no entries!");
1879 }
1880 } else
1881 {
1882 fprintf(stderr, "Looking for index entry '%ls'\n", text->text);
1883 fatal(err_whatever,
1884 "Couldn't locate index entry! (Wasn't in index.)");
1885 }
1886 break;
1887
1888 case word_HyperEnd:
1889 case word_XrefEnd:
1890 rdaddsc(rs, "</a>");
1891 break;
1892
1893 case word_Html:
1894 xhtml_utostr(text->text, &c);
1895 rdaddsc(rs, c);
1896 sfree(c);
1897 break;
1898
1899 case word_Normal:
1900 case word_Emph:
1901 case word_Strong:
1902 case word_Bold:
1903 case word_Code:
1904 case word_WeakCode:
1905 case word_WhiteSpace:
1906 case word_EmphSpace:
1907 case word_StrongSpace:
1908 case word_BoldSpace:
1909 case word_CodeSpace:
1910 case word_WkCodeSpace:
1911 case word_Quote:
1912 case word_EmphQuote:
1913 case word_StrongQuote:
1914 case word_BoldQuote:
1915 case word_CodeQuote:
1916 case word_WkCodeQuote:
1917 assert(text->type != word_CodeQuote &&
1918 text->type != word_WkCodeQuote);
1919 style = towordstyle(text->type);
1920 type = removeattr(text->type);
1921
1922 switch((attraux(text->aux) == attr_First || attraux(text->aux) == attr_Only) ? style : word_NotWordType)
1923 {
1924 case word_Emph: element_open(rs, "em"); break;
1925 case word_Strong: element_open(rs, "strong"); break;
1926 case word_Bold: element_open(rs, "b"); break;
1927 case word_Code: element_open(rs, "code"); break;
1928 case word_WeakCode: element_open(rs, "code"); break;
1929 }
1930
1931 if (removeattr(text->type) == word_Normal)
1932 {
1933 static int dont_convert = 0;
1934 if (dont_convert)
1935 {
1936 char buf[2] = " ";
1937 dont_convert = 0;
1938 s = text->text;
1939 for (; *s; s++)
1940 {
1941 buf[0] = (char) *s;
1942 rdaddsc(rs, buf);
1943 }
1944 buf[0] = 0;
1945 rdaddsc(rs, buf);
1946 } else
1947 {
1948 if (*text->text == '\\' && text->next
1949 && text->next->text && (*text->next->text == '&'
1950 || *text->next->text == '<'
1951 || *text->next->text == '>'
1952 || *text->next->text == '"'))
1953 dont_convert = 1;
1954 else
1955 {
1956 if (xhtml_convert(text->text, &c, TRUE)) /* spaces in the word are hard */
1957 rdaddsc(rs, c);
1958 else
1959 xhtml_rdaddwc(rs, text->alt, NULL);
1960 sfree(c);
1961 }
1962 }
1963 } else if (removeattr(text->type) == word_WhiteSpace)
1964 {
1965 rdaddc(rs, ' ');
1966 } else if (removeattr(text->type) == word_Quote)
1967 {
1968 rdaddsc(rs, "&quot;");
1969 }
1970
1971 switch((attraux(text->aux) == attr_Last || attraux(text->aux) == attr_Only) ? style : word_NotWordType)
1972 {
1973 case word_Emph: element_close(rs, "em"); break;
1974 case word_Strong: element_close(rs, "strong"); break;
1975 case word_Bold: element_close(rs, "b"); break;
1976 case word_Code: element_close(rs, "code"); break;
1977 case word_WeakCode: element_close(rs, "code"); break;
1978 }
1979 break;
1980 }
1981 }
1982 }
1983
1984 /* Output a heading, formatted as XHTML.
1985 */
1986 static void xhtml_heading(FILE * fp, paragraph * p)
1987 {
1988 rdstringc t = { 0, 0, NULL };
1989 word *tprefix = p->kwtext;
1990 word *nprefix = p->kwtext2;
1991 word *text = p->words;
1992 int level = xhtml_para_level(p);
1993 xhtmlsection *sect = xhtml_find_section(p);
1994 xhtmlheadfmt *fmt;
1995 char fragmentbuf[NAMEDFRAGMENT_MAXLEN], *fragment;
1996 if (sect)
1997 {
1998 fragment = xhtml_get_fragmentname(sect, fragmentbuf);
1999 } else
2000 {
2001 if (p->type == para_Title)
2002 fragment = "title";
2003 else
2004 {
2005 fragment = ""; /* FIXME: what else can we do? */
2006 error(err_whatever, "Couldn't locate heading cross-reference!");
2007 }
2008 }
2009
2010 if (p->type == para_Title)
2011 fmt = NULL;
2012 else if (level == 1)
2013 fmt = &conf.fchapter;
2014 else if (level - 1 < conf.nfsect)
2015 fmt = &conf.fsect[level - 1];
2016 else
2017 fmt = &conf.fsect[conf.nfsect - 1];
2018
2019 if (fmt && fmt->just_numbers && nprefix)
2020 {
2021 xhtml_rdaddwc(&t, nprefix, NULL);
2022 if (fmt)
2023 {
2024 char *c;
2025 if (xhtml_convert(fmt->number_suffix, &c, FALSE))
2026 {
2027 rdaddsc(&t, c);
2028 sfree(c);
2029 }
2030 }
2031 } else if (fmt && !fmt->just_numbers && tprefix)
2032 {
2033 xhtml_rdaddwc(&t, tprefix, NULL);
2034 if (fmt)
2035 {
2036 char *c;
2037 if (xhtml_convert(fmt->number_suffix, &c, FALSE))
2038 {
2039 rdaddsc(&t, c);
2040 sfree(c);
2041 }
2042 }
2043 }
2044 xhtml_rdaddwc(&t, text, NULL);
2045 /*
2046 * If we're outputting in single-file mode, we need to lower
2047 * the level of each heading by one, because the overall
2048 * document title will be sitting right at the top as an <h1>
2049 * and so chapters and sections should start at <h2>.
2050 *
2051 * Even if not, the document title will come back from
2052 * xhtml_para_level() as level zero, so we must increment that
2053 * no matter what leaf_level is set to.
2054 */
2055 if (conf.leaf_level == 0 || level == 0)
2056 level++;
2057 fprintf(fp, "<a name=\"%s\"></a><h%i>%s</h%i>\n", fragment, level,
2058 t.text, level);
2059 sfree(t.text);
2060 }
2061
2062 /* Output a paragraph. Styles are handled by xhtml_rdaddwc().
2063 * This looks pretty simple; I may have missed something ...
2064 */
2065 static void xhtml_para(FILE * fp, word * text)
2066 {
2067 rdstringc out = { 0, 0, NULL };
2068 xhtml_rdaddwc(&out, text, NULL);
2069 fprintf(fp, "%s", out.text);
2070 sfree(out.text);
2071 }
2072
2073 /* Output a code paragraph. I'm treating this as preformatted, which
2074 * may not be entirely correct. See xhtml_para() for my worries about
2075 * this being overly-simple; however I think that most of the complexity
2076 * of the text backend came entirely out of word wrapping anyway.
2077 */
2078 static void xhtml_codepara(FILE * fp, word * text)
2079 {
2080 fputs("<pre>", fp);
2081 for (; text; text = text->next)
2082 if (text->type == word_WeakCode)
2083 {
2084 char *c;
2085 xhtml_convert(text->text, &c, FALSE);
2086 fprintf(fp, "%s\n", c);
2087 sfree(c);
2088 }
2089 fputs("</pre>\n", fp);
2090 }