Rename Buttress to Halibut. I _think_ I've caught everything in this pass.
[sgt/halibut] / bk_xhtml.c
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 <assert.h>
32 #include "halibut.h"
33
34 struct xhtmlsection_Struct {
35 struct xhtmlsection_Struct *next; /* next sibling (NULL if split across files) */
36 struct xhtmlsection_Struct *child; /* NULL if split across files */
37 struct xhtmlsection_Struct *parent; /* NULL if split across files */
38 struct xhtmlsection_Struct *chain; /* single structure independent of weird trees */
39 paragraph *para;
40 struct xhtmlfile_Struct *file; /* which file is this a part of? */
41 char *fragment; /* fragment id within the file */
42 int level;
43 };
44
45 struct xhtmlfile_Struct {
46 struct xhtmlfile_Struct *next;
47 struct xhtmlfile_Struct *child;
48 struct xhtmlfile_Struct *parent;
49 char *filename;
50 struct xhtmlsection_Struct *sections; /* sections within this file (only one for non-leaf) */
51 int is_leaf; /* is this file a leaf file, ie does it not have any children? */
52 };
53
54 typedef struct xhtmlsection_Struct xhtmlsection;
55 typedef struct xhtmlfile_Struct xhtmlfile;
56 typedef struct xhtmlindex_Struct xhtmlindex;
57
58 struct xhtmlindex_Struct {
59 int nsection;
60 int size;
61 xhtmlsection **sections;
62 };
63
64 typedef struct {
65 int contents_depth[6];
66 int leaf_contains_contents;
67 int leaf_level;
68 int leaf_smallest_contents;
69 int include_version_id;
70 wchar_t *author, *description;
71 wchar_t *head_end, *body, *body_start, *body_end, *address_start, *address_end, *nav_attrs;
72 int suppress_address;
73 } xhtmlconfig;
74
75 /*static void xhtml_level(paragraph *, int);
76 static void xhtml_level_0(paragraph *);
77 static void xhtml_docontents(FILE *, paragraph *, int);
78 static void xhtml_dosections(FILE *, paragraph *, int);
79 static void xhtml_dobody(FILE *, paragraph *, int);*/
80
81 static void xhtml_doheader(FILE *, word *);
82 static void xhtml_dofooter(FILE *);
83 static void xhtml_versionid(FILE *, word *, int);
84
85 static void xhtml_utostr(wchar_t *, char **);
86 static int xhtml_para_level(paragraph *);
87 static int xhtml_reservedchar(int);
88
89 static int xhtml_convert(wchar_t *, char **, int);
90 static void xhtml_rdaddwc(rdstringc *, word *, word *);
91 static void xhtml_para(FILE *, word *);
92 static void xhtml_codepara(FILE *, word *);
93 static void xhtml_heading(FILE *, paragraph *);
94
95 /* File-global variables are much easier than passing these things
96 * all over the place. Evil, but easier. We can replace this with a single
97 * structure at some point.
98 */
99 static xhtmlconfig conf;
100 static keywordlist *keywords;
101 static indexdata *idx;
102 static xhtmlfile *topfile;
103 static xhtmlsection *topsection;
104 static paragraph *sourceparas;
105 static xhtmlfile *lastfile;
106 static xhtmlfile *xhtml_last_file = NULL;
107 static int last_level=-1;
108 static xhtmlsection *currentsection;
109
110 static xhtmlconfig xhtml_configure(paragraph *source)
111 {
112 xhtmlconfig ret;
113
114 /*
115 * Defaults.
116 */
117 ret.contents_depth[0] = 2;
118 ret.contents_depth[1] = 3;
119 ret.contents_depth[2] = 4;
120 ret.contents_depth[3] = 5;
121 ret.contents_depth[4] = 6;
122 ret.contents_depth[5] = 7;
123 ret.leaf_level = 2;
124 ret.leaf_smallest_contents = 4;
125 ret.leaf_contains_contents = FALSE;
126 ret.include_version_id = TRUE;
127 ret.author = NULL;
128 ret.description = NULL;
129 ret.head_end = NULL;
130 ret.body = NULL;
131 ret.body_start = NULL;
132 ret.body_end = NULL;
133 ret.address_start = NULL;
134 ret.address_end = NULL;
135 ret.nav_attrs = NULL;
136 ret.suppress_address = FALSE;
137
138 for (; source; source = source->next)
139 {
140 if (source->type == para_Config)
141 {
142 if (!ustricmp(source->keyword, L"xhtml-contents-depth-0")) {
143 ret.contents_depth[0] = utoi(uadv(source->keyword));
144 } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-1")) {
145 ret.contents_depth[1] = utoi(uadv(source->keyword));
146 } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-2")) {
147 ret.contents_depth[2] = utoi(uadv(source->keyword));
148 } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-3")) {
149 ret.contents_depth[3] = utoi(uadv(source->keyword));
150 } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-4")) {
151 ret.contents_depth[4] = utoi(uadv(source->keyword));
152 } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-5")) {
153 ret.contents_depth[5] = utoi(uadv(source->keyword));
154 } else if (!ustricmp(source->keyword, L"xhtml-leaf-level")) {
155 ret.leaf_level = utoi(uadv(source->keyword));
156 if (ret.leaf_level==0) {
157 fatal(err_whatever, "xhtml-leaf-level cannot be zero");
158 }
159 } else if (!ustricmp(source->keyword, L"xhtml-leaf-smallest-contents")) {
160 ret.leaf_smallest_contents = utoi(uadv(source->keyword));
161 } else if (!ustricmp(source->keyword, L"xhtml-versionid")) {
162 ret.include_version_id = utob(uadv(source->keyword));
163 } else if (!ustricmp(source->keyword, L"xhtml-leaf-contains-contents")) {
164 ret.leaf_contains_contents = utob(uadv(source->keyword));
165 } else if (!ustricmp(source->keyword, L"xhtml-suppress-address")) {
166 ret.suppress_address = utob(uadv(source->keyword));
167 } else if (!ustricmp(source->keyword, L"xhtml-author")) {
168 ret.author = uadv(source->keyword);
169 } else if (!ustricmp(source->keyword, L"xhtml-description")) {
170 ret.description = uadv(source->keyword);
171 } else if (!ustricmp(source->keyword, L"xhtml-head-end")) {
172 ret.head_end = uadv(source->keyword);
173 } else if (!ustricmp(source->keyword, L"xhtml-body-start")) {
174 ret.body_start = uadv(source->keyword);
175 } else if (!ustricmp(source->keyword, L"xhtml-body-tag")) {
176 ret.body = uadv(source->keyword);
177 } else if (!ustricmp(source->keyword, L"xhtml-body-end")) {
178 ret.body_end = uadv(source->keyword);
179 } else if (!ustricmp(source->keyword, L"xhtml-address-start")) {
180 ret.address_start = uadv(source->keyword);
181 } else if (!ustricmp(source->keyword, L"xhtml-address-end")) {
182 ret.address_end = uadv(source->keyword);
183 } else if (!ustricmp(source->keyword, L"xhtml-navigation-attributes")) {
184 ret.nav_attrs = uadv(source->keyword);
185 }
186 }
187 }
188
189 /* printf(" !!! leaf_level = %i\n", ret.leaf_level);
190 printf(" !!! contentdepth-0 = %i\n", ret.contents_depth[0]);
191 printf(" !!! contentdepth-1 = %i\n", ret.contents_depth[1]);
192 printf(" !!! contentdepth-2 = %i\n", ret.contents_depth[2]);
193 printf(" !!! contentdepth-3 = %i\n", ret.contents_depth[3]);
194 printf(" !!! contentdepth-4 = %i\n", ret.contents_depth[4]);
195 printf(" !!! contentdepth-5 = %i\n", ret.contents_depth[5]);
196 printf(" !!! leaf_contains_contents = %i\n", ret.leaf_contains_contents);*/
197 return ret;
198 }
199
200 static xhtmlsection *xhtml_new_section(xhtmlsection *last)
201 {
202 xhtmlsection *ret = mknew(xhtmlsection);
203 ret->next=NULL;
204 ret->child=NULL;
205 ret->parent=NULL;
206 ret->chain=last;
207 ret->para=NULL;
208 ret->file=NULL;
209 ret->fragment=NULL;
210 ret->level=-1; /* marker: end of chain */
211 return ret;
212 }
213
214 /* Returns NULL or the section that marks that paragraph */
215 static xhtmlsection *xhtml_find_section(paragraph *p)
216 {
217 xhtmlsection *ret = topsection;
218 if (xhtml_para_level(p)==-1) { /* first, we back-track to a section paragraph */
219 paragraph *p2 = sourceparas;
220 paragraph *p3 = NULL;
221 while (p2 && p2!=p) {
222 if (xhtml_para_level(p2)!=-1) {
223 p3 = p2;
224 }
225 p2=p2->next;
226 }
227 if (p3==NULL) { /* for some reason, we couldn't find a section before this paragraph ... ? */
228 /* Note that this can happen, if you have a cross-reference to before the first chapter starts.
229 * So don't do that, then.
230 */
231 return NULL;
232 }
233 p=p3;
234 }
235 while (ret && ret->para != p) {
236 /* printf(" xhtml_find_section(): checking %s for para @ %p\n", ret->fragment, p);*/
237 ret=ret->chain;
238 }
239 return ret;
240 }
241
242 static xhtmlfile *xhtml_new_file(xhtmlsection *sect)
243 {
244 xhtmlfile *ret = mknew(xhtmlfile);
245
246 ret->next=NULL;
247 ret->child=NULL;
248 ret->parent=NULL;
249 ret->filename=NULL;
250 ret->sections=sect;
251 ret->is_leaf=(sect!=NULL && sect->level==conf.leaf_level);
252 if (sect==NULL) {
253 if (conf.leaf_level==0) { /* currently unused */
254 #define FILENAME_MANUAL "Manual.html"
255 #define FILENAME_CONTENTS "Contents.html"
256 ret->filename = smalloc(strlen(FILENAME_MANUAL)+1);
257 sprintf(ret->filename, FILENAME_MANUAL);
258 } else {
259 ret->filename = smalloc(strlen(FILENAME_CONTENTS)+1);
260 sprintf(ret->filename, FILENAME_CONTENTS);
261 }
262 } else {
263 paragraph *p = sect->para;
264 rdstringc fname_c = { 0, 0, NULL };
265 char *c;
266 word *w;
267 for (w=(p->kwtext)?(p->kwtext):(p->words); w; w=w->next)
268 {
269 switch (removeattr(w->type))
270 {
271 case word_Normal:
272 /*case word_Emph:
273 case word_Code:
274 case word_WeakCode:*/
275 xhtml_utostr(w->text, &c);
276 rdaddsc(&fname_c,c);
277 sfree(c);
278 break;
279 }
280 }
281 rdaddsc(&fname_c, ".html");
282 ret->filename = rdtrimc(&fname_c);
283 }
284 /* printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false"));*/
285 return ret;
286 }
287
288 /*
289 * Walk the tree fixing up files which are actually leaf (ie
290 * have no children) but aren't at leaf level, so they have the
291 * leaf flag set.
292 */
293 void xhtml_fixup_layout(xhtmlfile* file)
294 {
295 if (file->child==NULL) {
296 file->is_leaf = TRUE;
297 } else {
298 xhtml_fixup_layout(file->child);
299 }
300 if (file->next)
301 xhtml_fixup_layout(file->next);
302 }
303
304 /*
305 * Create the tree structure so we know where everything goes.
306 * Method:
307 *
308 * Ignoring file splitting, we have three choices with each new section:
309 *
310 * +-----------------+-----------------+
311 * | | |
312 * X +----X----+ (1)
313 * | |
314 * Y (3)
315 * |
316 * (3)
317 *
318 * Y is the last section we added (currentsect).
319 * If sect is the section we want to add, then:
320 *
321 * (1) if sect->level < currentsect->level
322 * (2) if sect->level == currentsect->level
323 * (3) if sect->level > currentsect->level
324 *
325 * This requires the constraint that you never skip section numbers
326 * (so you can't have a.b.c.d without all of a, a.b and a.b.c existing).
327 *
328 * Note that you _can_ have 1.1.1.1 followed by 1.2 - you can change
329 * more than one level at a time. Lots of asserts, and probably part of
330 * the algorithm here, rely on this being true. (It currently isn't
331 * enforced by halibut, however.)
332 *
333 * File splitting makes this harder. For instance, say we added at (3)
334 * above and now need to add another section. We are splitting at level
335 * 2, ie the level of Y. Z is the last section we added:
336 *
337 * +-----------------+-----------------+
338 * | | |
339 * X +----X----+ (1)
340 * | |
341 * +----Y----+ (1)
342 * | |
343 * Z (2)
344 * |
345 * (3)
346 *
347 * The (1) case is now split; we need to search upwards to find where
348 * to actually link in. The other two cases remain the same (and will
349 * always be like this).
350 *
351 * File splitting makes this harder, however. The decision of whether
352 * to split to a new file is always on the same condition, however (is
353 * the level of this section higher than the leaf_level configuration
354 * value or not).
355 *
356 * Treating the cases backwards:
357 *
358 * (3) same file if sect->level > conf.leaf_level, otherwise new file
359 *
360 * if in the same file, currentsect->child points to sect
361 * otherwise the linking is done through the file tree (which works
362 * in more or less the same way, ie currentfile->child points to
363 * the new file)
364 *
365 * (2) same file if sect->level > conf.leaf_level, otherwise new file
366 *
367 * if in the same file, currentsect->next points to sect
368 * otherwise file linking and currentfile->next points to the new
369 * file (we know that Z must have caused a new file to be created)
370 *
371 * (1) same file if sect->level > conf.leaf_level, otherwise new file
372 *
373 * this is actually effectively the same case as (2) here,
374 * except that we first have to travel up the sections to figure
375 * out which section this new one will be a sibling of. In doing
376 * so, we may disappear off the top of a file and have to go up
377 * to its parent in the file tree.
378 *
379 */
380 static void xhtml_ponder_layout(paragraph *p)
381 {
382 xhtmlsection *lastsection;
383 xhtmlsection *currentsect;
384 xhtmlfile *currentfile;
385
386 lastfile = NULL;
387 topsection = xhtml_new_section(NULL);
388 topfile = xhtml_new_file(NULL);
389 lastsection = topsection;
390 currentfile = topfile;
391 currentsect = topsection;
392
393 for (; p; p=p->next)
394 {
395 int level = xhtml_para_level(p);
396 if (level>0) /* actually a section */
397 {
398 xhtmlsection *sect;
399 word *w;
400 char *c;
401 rdstringc fname_c = { 0, 0, NULL };
402
403 sect = xhtml_new_section(lastsection);
404 lastsection = sect;
405 sect->para = p;
406 for (w=(p->kwtext2)?(p->kwtext2):(p->words); w; w=w->next) /* kwtext2 because we want numbers only! */
407 {
408 switch (removeattr(w->type))
409 {
410 case word_Normal:
411 /*case word_Emph:
412 case word_Code:
413 case word_WeakCode:*/
414 xhtml_utostr(w->text, &c);
415 rdaddsc(&fname_c,c);
416 sfree(c);
417 break;
418 }
419 }
420 /* rdaddsc(&fname_c, ".html");*/
421 sect->fragment = rdtrimc(&fname_c);
422 sect->level = level;
423 /* printf(" ! adding para @ %p as sect %s, level %i\n", sect->para, sect->fragment, level);*/
424
425 if (level>currentsect->level) { /* case (3) */
426 if (level>conf.leaf_level) { /* same file */
427 assert(currentfile->is_leaf);
428 currentsect->child = sect;
429 sect->parent=currentsect;
430 sect->file=currentfile;
431 /* printf("connected '%s' to existing file '%s' [I]\n", sect->fragment, currentfile->filename);*/
432 currentsect=sect;
433 } else { /* new file */
434 xhtmlfile *file = xhtml_new_file(sect);
435 assert(!currentfile->is_leaf);
436 currentfile->child=file;
437 sect->file=file;
438 file->parent=currentfile;
439 /* printf("connected '%s' to new file '%s' [I]\n", sect->fragment, file->filename);*/
440 currentfile=file;
441 currentsect=sect;
442 }
443 } else if (level >= currentsect->file->sections->level) {
444 /* Case (1) or (2) *AND* still under the section that starts
445 * the current file.
446 *
447 * I'm not convinced that this couldn't be rolled in with the
448 * final else {} leg further down. It seems a lot of effort
449 * this way.
450 */
451 if (level>conf.leaf_level) { /* stick within the same file */
452 assert(currentfile->is_leaf);
453 sect->file = currentfile;
454 while (currentsect && currentsect->level > level &&
455 currentsect->file==currentsect->parent->file) {
456 currentsect = currentsect->parent;
457 }
458 assert(currentsect);
459 currentsect->next = sect;
460 assert(currentsect->level == sect->level);
461 sect->parent = currentsect->parent;
462 currentsect = sect;
463 /* printf("connected '%s' to existing file '%s' [II]\n", sect->fragment, currentfile->filename);*/
464 } else { /* new file */
465 xhtmlfile *file = xhtml_new_file(sect);
466 sect->file=file;
467 currentfile->next=file;
468 file->parent=currentfile->parent;
469 file->is_leaf=(level==conf.leaf_level);
470 file->sections=sect;
471 /* printf("connected '%s' to new file '%s' [II]\n", sect->fragment, file->filename);*/
472 currentfile=file;
473 currentsect=sect;
474 }
475 } else { /* Case (1) or (2) and we must move up the file tree first */
476 /* this loop is now probably irrelevant - we know we can't connect
477 * to anything in the current file */
478 while (currentsect && level<currentsect->level) {
479 currentsect=currentsect->parent;
480 if (currentsect) {
481 /* printf(" * up one level to '%s'\n", currentsect->fragment);*/
482 } else {
483 /* printf(" * up one level (off top of current file)\n");*/
484 }
485 }
486 if (currentsect) {
487 /* I'm pretty sure this can now never fire */
488 assert(currentfile->is_leaf);
489 /* printf("connected '%s' to existing file '%s' [III]\n", sect->fragment, currentfile->filename);*/
490 sect->file = currentfile;
491 currentsect->next=sect;
492 currentsect=sect;
493 } else { /* find a file we can attach to */
494 while (currentfile && currentfile->sections && level<currentfile->sections->level) {
495 currentfile=currentfile->parent;
496 if (currentfile) {
497 /* printf(" * up one file level to '%s'\n", currentfile->filename);*/
498 } else {
499 /* printf(" * up one file level (off top of tree)\n");*/
500 }
501 }
502 if (currentfile) { /* new file (we had to skip up a file to
503 get here, so we must be dealing with a
504 level no lower than the configured
505 leaf_level */
506 xhtmlfile *file = xhtml_new_file(sect);
507 currentfile->next=file;
508 sect->file=file;
509 file->parent=currentfile->parent;
510 file->is_leaf=(level==conf.leaf_level);
511 file->sections=sect;
512 /* printf("connected '%s' to new file '%s' [III]\n", sect->fragment, file->filename);*/
513 currentfile=file;
514 currentsect=sect;
515 } else {
516 fatal(err_whatever, "Ran off the top trying to connect sibling: strange document.");
517 }
518 }
519 }
520 }
521 }
522 topsection = lastsection; /* get correct end of the chain */
523 xhtml_fixup_layout(topfile); /* leaf files not at leaf level marked as such */
524 }
525
526 static void xhtml_do_index();
527 static void xhtml_do_file(xhtmlfile *file);
528 static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform);
529 static void xhtml_do_paras(FILE *fp, paragraph *p);
530 static int xhtml_do_contents_limit(FILE *fp, xhtmlfile *file, int limit);
531 static int xhtml_do_contents_section_limit(FILE *fp, xhtmlsection *section, int limit);
532 static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit);
533 static int xhtml_do_contents(FILE *fp, xhtmlfile *file);
534 static int xhtml_do_naked_contents(FILE *fp, xhtmlfile *file);
535 static void xhtml_do_sections(FILE *fp, xhtmlsection *sections);
536
537 /*
538 * Do all the files in this structure.
539 */
540 static void xhtml_do_files(xhtmlfile *file)
541 {
542 xhtml_do_file(file);
543 if (file->child)
544 xhtml_do_files(file->child);
545 if (file->next)
546 xhtml_do_files(file->next);
547 }
548
549 /*
550 * Free up all memory used by the file tree from 'xfile' downwards
551 */
552 static void xhtml_free_file(xhtmlfile* xfile)
553 {
554 if (xfile==NULL) {
555 return;
556 }
557
558 if (xfile->filename) {
559 sfree(xfile->filename);
560 }
561 xhtml_free_file(xfile->child);
562 xhtml_free_file(xfile->next);
563 sfree(xfile);
564 }
565
566 /*
567 * Main function.
568 */
569 void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords,
570 indexdata *in_idx)
571 {
572 /* int i;*/
573 indexentry *ientry;
574 int ti;
575 xhtmlsection *xsect;
576
577 sourceparas = sourceform;
578 conf = xhtml_configure(sourceform);
579 keywords = in_keywords;
580 idx = in_idx;
581
582 /* Clear up the index entries backend data pointers */
583 for (ti=0; (ientry = (indexentry *)index234(idx->entries, ti))!=NULL; ti++) {
584 ientry->backend_data=NULL;
585 }
586
587 xhtml_ponder_layout(sourceform);
588
589 /* old system ... (writes to *.alt, but gets some stuff wrong and is ugly) */
590 /* xhtml_level_0(sourceform);
591 for (i=1; i<=conf.leaf_level; i++)
592 {
593 xhtml_level(sourceform, i);
594 }*/
595
596 /* new system ... (writes to *.html, but isn't fully trusted) */
597 xhtml_do_top_file(topfile, sourceform);
598 assert(!topfile->next); /* shouldn't have a sibling at all */
599 xhtml_do_files(topfile->child);
600 xhtml_do_index();
601
602 /* release file, section, index data structures */
603 xsect = topsection;
604 while (xsect) {
605 xhtmlsection *tmp = xsect->chain;
606 if (xsect->fragment) {
607 sfree(xsect->fragment);
608 }
609 sfree(xsect);
610 xsect = tmp;
611 }
612 xhtml_free_file(topfile);
613 for (ti = 0; (ientry=(indexentry *)index234(idx->entries, ti))!=NULL; ti++) {
614 if (ientry->backend_data!=NULL) {
615 xhtmlindex *xi = (xhtmlindex*) ientry->backend_data;
616 if (xi->sections!=NULL) {
617 sfree(xi->sections);
618 }
619 sfree(xi);
620 }
621 ientry->backend_data = NULL;
622 }
623 }
624
625 static int xhtml_para_level(paragraph *p)
626 {
627 switch (p->type)
628 {
629 case para_UnnumberedChapter:
630 case para_Chapter:
631 case para_Appendix:
632 return 1;
633 break;
634 /* case para_BiblioCited:
635 return 2;
636 break;*/
637 case para_Heading:
638 case para_Subsect:
639 return p->aux+2;
640 break;
641 default:
642 return -1;
643 break;
644 }
645 }
646
647 static char* xhtml_index_filename = "IndexPage.html";
648
649 /* Output the nav links for the current file.
650 * file == NULL means we're doing the index
651 */
652 static void xhtml_donavlinks(FILE *fp, xhtmlfile *file)
653 {
654 xhtmlfile *xhtml_next_file = NULL;
655 fprintf(fp, "<p");
656 if (conf.nav_attrs!=NULL) {
657 fprintf(fp, " %ls>", conf.nav_attrs);
658 } else {
659 fprintf(fp, ">");
660 }
661 if (xhtml_last_file==NULL) {
662 fprintf(fp, "Previous | ");
663 } else {
664 fprintf(fp, "<a href='%s'>Previous</a> | ", xhtml_last_file->filename);
665 }
666 fprintf(fp, "<a href='Contents.html'>Contents</a> | ");
667 if (file != NULL) { /* otherwise we're doing nav links for the index */
668 if (xhtml_next_file==NULL)
669 xhtml_next_file = file->child;
670 if (xhtml_next_file==NULL)
671 xhtml_next_file = file->next;
672 if (xhtml_next_file==NULL)
673 xhtml_next_file = file->parent->next;
674 }
675 if (xhtml_next_file==NULL) {
676 if (file==NULL) { /* index, so no next file */
677 fprintf(fp, "Next ");
678 } else {
679 fprintf(fp, "<a href='%s'>Next</a>", xhtml_index_filename);
680 }
681 } else {
682 fprintf(fp, "<a href='%s'>Next</a>", xhtml_next_file->filename);
683 }
684 fprintf(fp, "</p>\n");
685 }
686
687 /* Write out the index file */
688 static void xhtml_do_index()
689 {
690 word temp_word = { NULL, NULL, word_Normal, 0, 0, L"Index", { NULL, 0, 0} };
691 indexentry *y;
692 int ti;
693 FILE *fp = fopen(xhtml_index_filename, "w");
694
695 if (fp==NULL)
696 fatal(err_cantopenw, xhtml_index_filename);
697 xhtml_doheader(fp, &temp_word);
698 xhtml_donavlinks(fp, NULL);
699
700 fprintf(fp, "<dl>\n");
701 /* iterate over idx->entries using the tree functions and display everything */
702 for (ti = 0; (y = (indexentry *)index234(idx->entries, ti)) != NULL; ti++) {
703 if (y->backend_data) {
704 int i;
705 xhtmlindex *xi;
706
707 fprintf(fp, "<dt>");
708 xhtml_para(fp, y->text);
709 fprintf(fp, "</dt>\n<dd>");
710
711 xi = (xhtmlindex*) y->backend_data;
712 for (i=0; i<xi->nsection; i++) {
713 xhtmlsection *sect = xi->sections[i];
714 if (sect) {
715 fprintf(fp, "<a href='%s#%s'>", sect->file->filename, sect->fragment);
716 if (sect->para->kwtext) {
717 xhtml_para(fp, sect->para->kwtext);
718 } else if (sect->para->words) {
719 xhtml_para(fp, sect->para->words);
720 }
721 fprintf(fp, "</a>");
722 if (i+1<xi->nsection) {
723 fprintf(fp, ", ");
724 }
725 }
726 }
727 fprintf(fp, "</dd>\n");
728 }
729 }
730 fprintf(fp, "</dl>\n");
731
732 xhtml_donavlinks(fp, NULL);
733 xhtml_dofooter(fp);
734 fclose(fp);
735 }
736
737 /* Output the given file. This includes whatever contents at beginning and end, etc. etc. */
738 static void xhtml_do_file(xhtmlfile *file)
739 {
740 FILE *fp = fopen(file->filename, "w");
741 if (fp==NULL)
742 fatal(err_cantopenw, file->filename);
743
744 if (file->sections->para->words) {
745 xhtml_doheader(fp, file->sections->para->words);
746 } else if (file->sections->para->kwtext) {
747 xhtml_doheader(fp, file->sections->para->kwtext);
748 } else {
749 xhtml_doheader(fp, NULL);
750 }
751
752 xhtml_donavlinks(fp, file);
753
754 if (file->is_leaf && conf.leaf_contains_contents && xhtml_do_contents(NULL, file)>=conf.leaf_smallest_contents)
755 xhtml_do_contents(fp, file);
756 xhtml_do_sections(fp, file->sections);
757 if (!file->is_leaf)
758 xhtml_do_naked_contents(fp, file);
759
760 xhtml_donavlinks(fp, file);
761
762 xhtml_dofooter(fp);
763 fclose(fp);
764
765 xhtml_last_file = file;
766 }
767
768 /* Output the top-level file. */
769 static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform)
770 {
771 paragraph *p;
772 int done=FALSE;
773 FILE *fp = fopen(file->filename, "w");
774 if (fp==NULL)
775 fatal(err_cantopenw, file->filename);
776
777 /* Do the title -- only one allowed */
778 for (p = sourceform; p && !done; p = p->next)
779 {
780 if (p->type == para_Title)
781 {
782 xhtml_doheader(fp, p->words);
783 done=TRUE;
784 }
785 }
786 if (!done)
787 xhtml_doheader(fp, NULL /* Eek! */);
788
789 /* Do the preamble and copyright */
790 for (p = sourceform; p; p = p->next)
791 {
792 if (p->type == para_Preamble)
793 {
794 fprintf(fp, "<p>");
795 xhtml_para(fp, p->words);
796 fprintf(fp, "</p>\n");
797 }
798 }
799 for (p = sourceform; p; p = p->next)
800 {
801 if (p->type == para_Copyright)
802 {
803 fprintf(fp, "<p>");
804 xhtml_para(fp, p->words);
805 fprintf(fp, "</p>\n");
806 }
807 }
808
809 xhtml_do_contents(fp, file);
810 xhtml_do_sections(fp, file->sections);
811 xhtml_dofooter(fp);
812 fclose(fp);
813 }
814
815 /* Convert a Unicode string to an ASCII one. '?' is
816 * used for unmappable characters.
817 */
818 static void xhtml_utostr(wchar_t *in, char **out)
819 {
820 int l = ustrlen(in);
821 int i;
822 *out = smalloc(l+1);
823 for (i=0; i<l; i++)
824 {
825 if (in[i]>=32 && in[i]<=126)
826 (*out)[i]=(char)in[i];
827 else
828 (*out)[i]='?';
829 }
830 (*out)[i]=0;
831 }
832
833 /*
834 * Write contents for the given file, and subfiles, down to
835 * the appropriate contents depth. Returns the number of
836 * entries written.
837 */
838 static int xhtml_do_contents(FILE *fp, xhtmlfile *file)
839 {
840 int level, limit, start_level, count = 0;
841 if (!file)
842 return 0;
843
844 level = (file->sections)?(file->sections->level):(0);
845 limit = conf.contents_depth[(level>5)?(5):(level)];
846 start_level = (file->is_leaf) ? (level-1) : (level);
847 last_level = start_level;
848
849 count += xhtml_do_contents_section_limit(fp, file->sections, limit);
850 count += xhtml_do_contents_limit(fp, file->child, limit);
851 if (fp!=NULL) {
852 while (last_level > start_level) {
853 last_level--;
854 fprintf(fp, "</ul>\n");
855 }
856 }
857 return count;
858 }
859
860 /* As above, but doesn't do anything in the current file */
861 static int xhtml_do_naked_contents(FILE *fp, xhtmlfile *file)
862 {
863 int level, limit, start_level, count = 0;
864 if (!file)
865 return 0;
866
867 level = (file->sections)?(file->sections->level):(0);
868 limit = conf.contents_depth[(level>5)?(5):(level)];
869 start_level = (file->is_leaf) ? (level-1) : (level);
870 last_level = start_level;
871
872 count = xhtml_do_contents_limit(fp, file->child, limit);
873 if (fp!=NULL) {
874 while (last_level > start_level) {
875 last_level--;
876 fprintf(fp, "</ul>\n");
877 }
878 }
879 return count;
880 }
881
882 /*
883 * Write contents for the given file, children, and siblings, down to
884 * given limit contents depth.
885 */
886 static int xhtml_do_contents_limit(FILE *fp, xhtmlfile *file, int limit)
887 {
888 int count = 0;
889 while (file) {
890 count += xhtml_do_contents_section_limit(fp, file->sections, limit);
891 count += xhtml_do_contents_limit(fp, file->child, limit);
892 file = file->next;
893 }
894 return count;
895 }
896
897 /*
898 * Write contents entries for the given section tree, down to the
899 * limit contents depth.
900 */
901 static int xhtml_do_contents_section_deep_limit(FILE *fp, xhtmlsection *section, int limit)
902 {
903 int count = 0;
904 while (section) {
905 if (!xhtml_add_contents_entry(fp, section, limit))
906 return 0;
907 else
908 count++;
909 count += xhtml_do_contents_section_deep_limit(fp, section->child, limit);
910 section = section->next;
911 }
912 return count;
913 }
914
915 /*
916 * Write contents entries for the given section tree, down to the
917 * limit contents depth.
918 */
919 static int xhtml_do_contents_section_limit(FILE *fp, xhtmlsection *section, int limit)
920 {
921 int count = 0;
922 if (!section)
923 return 0;
924 xhtml_add_contents_entry(fp, section, limit);
925 count=1;
926 count += xhtml_do_contents_section_deep_limit(fp, section->child, limit);
927 /* section=section->child;
928 while (section && xhtml_add_contents_entry(fp, section, limit)) {
929 section = section->next;
930 }*/
931 return count;
932 }
933
934 /*
935 * Add a section entry, unless we're exceeding the limit, in which
936 * case return FALSE (otherwise return TRUE).
937 */
938 static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit)
939 {
940 if (!section || section->level > limit)
941 return FALSE;
942 if (fp==NULL)
943 return TRUE;
944 while (last_level > section->level) {
945 last_level--;
946 fprintf(fp, "</ul>\n");
947 }
948 while (last_level < section->level) {
949 last_level++;
950 fprintf(fp, "<ul>\n");
951 }
952 fprintf(fp, "<li><a href=\"%s#%s\">", section->file->filename, section->fragment);
953 if (section->para->kwtext) {
954 xhtml_para(fp, section->para->kwtext);
955 if (section->para->words) {
956 fprintf(fp, ": ");
957 }
958 }
959 if (section->para->words) {
960 xhtml_para(fp, section->para->words);
961 }
962 fprintf(fp, "</a></li>\n");
963 return TRUE;
964 }
965
966 /*
967 * Write all the sections in this file. Do all paragraphs in this section, then all
968 * children (recursively), then go on to the next one (tail recursively).
969 */
970 static void xhtml_do_sections(FILE *fp, xhtmlsection *sections)
971 {
972 while (sections) {
973 currentsection = sections;
974 xhtml_do_paras(fp, sections->para);
975 xhtml_do_sections(fp, sections->child);
976 sections = sections->next;
977 }
978 }
979
980 /* Write this list of paragraphs. Close off all lists at the end. */
981 static void xhtml_do_paras(FILE *fp, paragraph *p)
982 {
983 int last_type = -1, first=TRUE;
984 if (!p)
985 return;
986
987 /* for (; p && (xhtml_para_level(p)>limit || xhtml_para_level(p)==-1 || first); p=p->next) {*/
988 for (; p && (xhtml_para_level(p)==-1 || first); p=p->next) {
989 first=FALSE;
990 switch (p->type)
991 {
992 /*
993 * Things we ignore because we've already processed them or
994 * aren't going to touch them in this pass.
995 */
996 case para_IM:
997 case para_BR:
998 case para_Biblio: /* only touch BiblioCited */
999 case para_VersionID:
1000 case para_Copyright:
1001 case para_Preamble:
1002 case para_NoCite:
1003 case para_Title:
1004 break;
1005
1006 /*
1007 * Chapter titles.
1008 */
1009 case para_Chapter:
1010 case para_Appendix:
1011 case para_UnnumberedChapter:
1012 xhtml_heading(fp, p);
1013 break;
1014
1015 case para_Heading:
1016 case para_Subsect:
1017 xhtml_heading(fp, p);
1018 break;
1019
1020 case para_Rule:
1021 fprintf(fp, "\n<hr />\n");
1022 break;
1023
1024 case para_Normal:
1025 fprintf(fp, "\n<p>");
1026 xhtml_para(fp, p->words);
1027 fprintf(fp, "</p>\n");
1028 break;
1029
1030 case para_Bullet:
1031 case para_NumberedList:
1032 case para_BiblioCited:
1033 if (last_type!=p->type) {
1034 /* start up list if necessary */
1035 if (p->type == para_Bullet) {
1036 fprintf(fp, "<ul>\n");
1037 } else if (p->type == para_NumberedList) {
1038 fprintf(fp, "<ol>\n");
1039 } else if (p->type == para_BiblioCited) {
1040 fprintf(fp, "<dl>\n");
1041 }
1042 }
1043 if (p->type == para_Bullet || p->type == para_NumberedList)
1044 fprintf(fp, "<li>");
1045 else if (p->type == para_BiblioCited) {
1046 fprintf(fp, "<dt>");
1047 xhtml_para(fp, p->kwtext);
1048 fprintf(fp, "</dt>\n<dd>");
1049 }
1050 xhtml_para(fp, p->words);
1051 if (p->type == para_BiblioCited) {
1052 fprintf(fp, "</dd>\n");
1053 } else if (p->type == para_Bullet || p->type == para_NumberedList) {
1054 fprintf(fp, "</li>");
1055 }
1056 if (p->type == para_Bullet || p->type == para_NumberedList || p->type == para_BiblioCited)
1057 /* close off list if necessary */
1058 {
1059 paragraph *p2 = p->next;
1060 int close_off=FALSE;
1061 /* if (p2 && (xhtml_para_level(p2)>limit || xhtml_para_level(p2)==-1)) {*/
1062 if (p2 && xhtml_para_level(p2)==-1) {
1063 if (p2->type != p->type)
1064 close_off=TRUE;
1065 } else {
1066 close_off=TRUE;
1067 }
1068 if (close_off) {
1069 if (p->type == para_Bullet) {
1070 fprintf(fp, "</ul>\n");
1071 } else if (p->type == para_NumberedList) {
1072 fprintf(fp, "</ol>\n");
1073 } else if (p->type == para_BiblioCited) {
1074 fprintf(fp, "</dl>\n");
1075 }
1076 }
1077 }
1078 break;
1079
1080 case para_Code:
1081 xhtml_codepara(fp, p->words);
1082 break;
1083 }
1084 last_type = p->type;
1085 }
1086 }
1087
1088 /*
1089 * Output a header for this XHTML file.
1090 */
1091 static void xhtml_doheader(FILE *fp, word *title)
1092 {
1093 fprintf(fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
1094 fprintf(fp, "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
1095 fprintf(fp, "<html xmlns='http://www.w3.org/1999/xhtml'>\n\n<head>\n<title>");
1096 if (title==NULL)
1097 fprintf(fp, "The thing with no name!");
1098 else
1099 xhtml_para(fp, title);
1100 fprintf(fp, "</title>\n");
1101 fprintf(fp, "<meta name=\"generator\" content=\"Halibut %s xhtml-backend\" />\n", version);
1102 if (conf.author)
1103 fprintf(fp, "<meta name=\"author\" content=\"%ls\" />\n", conf.author);
1104 if (conf.description)
1105 fprintf(fp, "<meta name=\"description\" content=\"%ls\" />\n", conf.description);
1106 if (conf.head_end)
1107 fprintf(fp, "%ls\n", conf.head_end);
1108 fprintf(fp, "</head>\n\n");
1109 if (conf.body)
1110 fprintf(fp, "%ls\n", conf.body);
1111 else
1112 fprintf(fp, "<body>\n");
1113 if (conf.body_start)
1114 fprintf(fp, "%ls\n", conf.body_start);
1115 }
1116
1117 /*
1118 * Output a footer for this XHTML file.
1119 */
1120 static void xhtml_dofooter(FILE *fp)
1121 {
1122 fprintf(fp, "\n<hr />\n\n");
1123 if (conf.body_end)
1124 fprintf(fp, "%ls\n", conf.body_end);
1125 if (!conf.suppress_address) {
1126 fprintf(fp,"<address>\n");
1127 if (conf.address_start)
1128 fprintf(fp, "%ls\n", conf.address_start);
1129 /* Do the version ID */
1130 if (conf.include_version_id) {
1131 paragraph *p;
1132 int started = 0;
1133 for (p = sourceparas; p; p = p->next)
1134 if (p->type == para_VersionID) {
1135 xhtml_versionid(fp, p->words, started);
1136 started = 1;
1137 }
1138 }
1139 if (conf.address_end)
1140 fprintf(fp, "%ls\n", conf.address_end);
1141 fprintf(fp, "</address>\n");
1142 }
1143 fprintf(fp, "</body>\n\n</html>\n");
1144 }
1145
1146 /*
1147 * Output the versionid paragraph. Typically this is a version control
1148 * ID string (such as $Id...$ in RCS).
1149 */
1150 static void xhtml_versionid(FILE *fp, word *text, int started)
1151 {
1152 rdstringc t = { 0, 0, NULL };
1153
1154 rdaddc(&t, '['); /* FIXME: configurability */
1155 xhtml_rdaddwc(&t, text, NULL);
1156 rdaddc(&t, ']'); /* FIXME: configurability */
1157
1158 if (started)
1159 fprintf(fp, "<br>\n");
1160 fprintf(fp, "%s\n", t.text);
1161 sfree(t.text);
1162 }
1163
1164 /* Is this an XHTML reserved character? */
1165 static int xhtml_reservedchar(int c)
1166 {
1167 if (c=='&' || c=='<' || c=='>' || c=='"')
1168 return TRUE;
1169 else
1170 return FALSE;
1171 }
1172
1173 /*
1174 * Convert a wide string into valid XHTML: Anything outside ASCII will
1175 * be fixed up as an entity. Currently we don't worry about constraining the
1176 * encoded character set, which we should probably do at some point (we can
1177 * still fix up and return FALSE - see the last comment here). We also don't
1178 * currently
1179 *
1180 * Because this is only used for words, spaces are HARD spaces (any other
1181 * spaces will be word_Whitespace not word_Normal). So they become &nbsp;
1182 * Unless hard_spaces is FALSE, of course (code paragraphs break the above
1183 * rule).
1184 *
1185 * If `result' is non-NULL, mallocs the resulting string and stores a pointer to
1186 * it in `*result'. If `result' is NULL, merely checks whether all
1187 * characters in the string are feasible.
1188 *
1189 * Return is nonzero if all characters are OK. If not all
1190 * characters are OK but `result' is non-NULL, a result _will_
1191 * still be generated!
1192 */
1193 static int xhtml_convert(wchar_t *s, char **result, int hard_spaces) {
1194 int doing = (result != 0);
1195 int ok = TRUE;
1196 char *p = NULL;
1197 int plen = 0, psize = 0;
1198
1199 for (; *s; s++) {
1200 wchar_t c = *s;
1201
1202 #define ensure_size(i) if (i>=psize) { psize = i+256; p = resize(p, psize); }
1203
1204 if (((c == 32 && !hard_spaces) || (c > 32 && c <= 126 && !xhtml_reservedchar(c)))) {
1205 /* Char is OK. */
1206 if (doing)
1207 {
1208 ensure_size(plen);
1209 p[plen++] = (char)c;
1210 }
1211 } else {
1212 /* Char needs fixing up. */
1213 /* ok = FALSE; -- currently we never return FALSE; we
1214 * might want to when considering a character set for the
1215 * encoded document.
1216 */
1217 if (doing)
1218 {
1219 if (c==32) { /* a space in a word is a hard space */
1220 ensure_size(plen+6); /* includes space for the NUL, which is subsequently stomped on */
1221 sprintf(p+plen, "&nbsp;");
1222 plen+=6;
1223 } else {
1224 /* FIXME: entity names! */
1225 ensure_size(plen+8); /* includes space for the NUL, which is subsequently stomped on */
1226 plen+=sprintf(p+plen, "&#%04i;", (int)c);
1227 }
1228 }
1229 }
1230 }
1231 if (doing) {
1232 p = resize(p, plen+1);
1233 p[plen] = '\0';
1234 *result = p;
1235 }
1236 return ok;
1237 }
1238
1239 /*
1240 * This formats the given words as XHTML.
1241 */
1242 static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end) {
1243 char *c;
1244 keyword *kwl;
1245 xhtmlsection *sect;
1246 indextag *itag;
1247 int ti;
1248
1249 for (; text && text != end; text = text->next) {
1250 switch (text->type) {
1251 case word_HyperLink:
1252 xhtml_utostr(text->text, &c);
1253 rdaddsc(rs, "<a href=\"");
1254 rdaddsc(rs, c);
1255 rdaddsc(rs, "\">");
1256 sfree(c);
1257 break;
1258
1259 case word_UpperXref:
1260 case word_LowerXref:
1261 kwl = kw_lookup(keywords, text->text);
1262 if (kwl) {
1263 sect=xhtml_find_section(kwl->para);
1264 if (sect) {
1265 rdaddsc(rs, "<a href=\"");
1266 rdaddsc(rs, sect->file->filename);
1267 rdaddc(rs, '#');
1268 rdaddsc(rs, sect->fragment);
1269 rdaddsc(rs, "\">");
1270 } else {
1271 rdaddsc(rs, "<a href=\"Apologies.html\"><!-- probably a bibliography cross reference -->");
1272 error(err_whatever, "Couldn't locate cross-reference! (Probably a bibliography entry.)");
1273 }
1274 } else {
1275 rdaddsc(rs, "<a href=\"Apologies.html\"><!-- unknown cross-reference -->");
1276 error(err_whatever, "Couldn't locate cross-reference! (Wasn't in source file.)");
1277 }
1278 break;
1279
1280 case word_IndexRef: /* in theory we could make an index target here */
1281 /* rdaddsc(rs, "<a name=\"idx-");
1282 xhtml_utostr(text->text, &c);
1283 rdaddsc(rs, c);
1284 sfree(c);
1285 rdaddsc(rs, "\"></a>");*/
1286 /* what we _do_ need to do is to fix up the backend data
1287 * for any indexentry this points to.
1288 */
1289 for (ti=0; (itag = (indextag *)index234(idx->tags, ti))!=NULL; ti++) {
1290 /* FIXME: really ustricmp() and not ustrcmp()? */
1291 if (ustricmp(itag->name, text->text)==0) {
1292 break;
1293 }
1294 }
1295 if (itag!=NULL) {
1296 if (itag->refs!=NULL) {
1297 int i;
1298 for (i=0; i<itag->nrefs; i++) {
1299 xhtmlindex *idx_ref;
1300 indexentry *ientry;
1301
1302 ientry = itag->refs[i];
1303 if (ientry->backend_data==NULL) {
1304 idx_ref = (xhtmlindex*) smalloc(sizeof(xhtmlindex));
1305 if (idx_ref==NULL)
1306 fatal(err_nomemory);
1307 idx_ref->nsection = 0;
1308 idx_ref->size = 4;
1309 idx_ref->sections = (xhtmlsection**) smalloc(idx_ref->size * sizeof(xhtmlsection*));
1310 if (idx_ref->sections==NULL)
1311 fatal(err_nomemory);
1312 ientry->backend_data = idx_ref;
1313 } else {
1314 idx_ref = ientry->backend_data;
1315 if (idx_ref->nsection+1 > idx_ref->size) {
1316 int new_size = idx_ref->size * 2;
1317 idx_ref->sections = srealloc(idx_ref->sections, new_size * sizeof(xhtmlsection));
1318 if (idx_ref->sections==NULL) {
1319 fatal(err_nomemory);
1320 }
1321 idx_ref->size = new_size;
1322 }
1323 }
1324 idx_ref->sections[idx_ref->nsection++] = currentsection;
1325 #if 0
1326 #endif
1327 }
1328 } else {
1329 fatal(err_whatever, "Index tag had no entries!");
1330 }
1331 } else {
1332 fprintf(stderr, "Looking for index entry '%ls'\n", text->text);
1333 fatal(err_whatever, "Couldn't locate index entry! (Wasn't in index.)");
1334 }
1335 break;
1336
1337 case word_HyperEnd:
1338 case word_XrefEnd:
1339 rdaddsc(rs, "</a>");
1340 break;
1341
1342 case word_Normal:
1343 case word_Emph:
1344 case word_Code:
1345 case word_WeakCode:
1346 case word_WhiteSpace:
1347 case word_EmphSpace:
1348 case word_CodeSpace:
1349 case word_WkCodeSpace:
1350 case word_Quote:
1351 case word_EmphQuote:
1352 case word_CodeQuote:
1353 case word_WkCodeQuote:
1354 assert(text->type != word_CodeQuote &&
1355 text->type != word_WkCodeQuote);
1356 if (towordstyle(text->type) == word_Emph &&
1357 (attraux(text->aux) == attr_First ||
1358 attraux(text->aux) == attr_Only))
1359 rdaddsc(rs, "<em>");
1360 else if ((towordstyle(text->type) == word_Code || towordstyle(text->type) == word_WeakCode) &&
1361 (attraux(text->aux) == attr_First ||
1362 attraux(text->aux) == attr_Only))
1363 rdaddsc(rs, "<code>");
1364
1365 if (removeattr(text->type) == word_Normal) {
1366 if (xhtml_convert(text->text, &c, TRUE)) /* spaces in the word are hard */
1367 rdaddsc(rs, c);
1368 else
1369 xhtml_rdaddwc(rs, text->alt, NULL);
1370 sfree(c);
1371 } else if (removeattr(text->type) == word_WhiteSpace) {
1372 rdaddc(rs, ' ');
1373 } else if (removeattr(text->type) == word_Quote) {
1374 rdaddsc(rs, "&quot;");
1375 }
1376
1377 if (towordstyle(text->type) == word_Emph &&
1378 (attraux(text->aux) == attr_Last ||
1379 attraux(text->aux) == attr_Only))
1380 rdaddsc(rs, "</em>");
1381 else if ((towordstyle(text->type) == word_Code || towordstyle(text->type) == word_WeakCode) &&
1382 (attraux(text->aux) == attr_Last ||
1383 attraux(text->aux) == attr_Only))
1384 rdaddsc(rs, "</code>");
1385 break;
1386 }
1387 }
1388 }
1389
1390 /* Output a heading, formatted as XHTML.
1391 */
1392 static void xhtml_heading(FILE *fp, paragraph *p)
1393 {
1394 rdstringc t = { 0, 0, NULL };
1395 word *tprefix = p->kwtext;
1396 word *nprefix = p->kwtext2;
1397 word *text = p->words;
1398 int level = xhtml_para_level(p);
1399 xhtmlsection *sect = xhtml_find_section(p);
1400 char *fragment;
1401 if (sect) {
1402 fragment = sect->fragment;
1403 } else {
1404 fragment = ""; /* FIXME: what else can we do? */
1405 error(err_whatever, "Couldn't locate heading cross-reference!");
1406 }
1407
1408 if (level>2 && nprefix) { /* FIXME: configurability on the level thing */
1409 xhtml_rdaddwc(&t, nprefix, NULL);
1410 rdaddc(&t, ' '); /* FIXME: as below */
1411 } else if (tprefix) {
1412 xhtml_rdaddwc(&t, tprefix, NULL);
1413 rdaddsc(&t, ": "); /* FIXME: configurability */
1414 }
1415 xhtml_rdaddwc(&t, text, NULL);
1416 fprintf(fp, "<a name=\"%s\"></a><h%i>%s</h%i>\n", fragment, level, t.text, level);
1417 sfree(t.text);
1418 }
1419
1420 /* Output a paragraph. Styles are handled by xhtml_rdaddwc().
1421 * This looks pretty simple; I may have missed something ...
1422 */
1423 static void xhtml_para(FILE *fp, word *text)
1424 {
1425 rdstringc out = { 0, 0, NULL };
1426 xhtml_rdaddwc(&out, text, NULL);
1427 fprintf(fp, "%s", out.text);
1428 sfree(out.text);
1429 }
1430
1431 /* Output a code paragraph. I'm treating this as preformatted, which
1432 * may not be entirely correct. See xhtml_para() for my worries about
1433 * this being overly-simple; however I think that most of the complexity
1434 * of the text backend came entirely out of word wrapping anyway.
1435 */
1436 static void xhtml_codepara(FILE *fp, word *text)
1437 {
1438 fprintf(fp, "<pre>");
1439 for (; text; text = text->next) if (text->type == word_WeakCode) {
1440 char *c;
1441 xhtml_convert(text->text, &c, FALSE);
1442 fprintf(fp, "%s\n", c);
1443 sfree(c);
1444 }
1445 fprintf(fp, "</pre>\n");
1446 }