2 * xhtml backend for Halibut
3 * (initial implementation by James Aylett)
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(),
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.
35 struct xhtmlsection_Struct
{
36 struct xhtmlsection_Struct
*next
; /* next sibling (NULL if split across files) */
37 struct xhtmlsection_Struct
*child
; /* NULL if split across files */
38 struct xhtmlsection_Struct
*parent
; /* NULL if split across files */
39 struct xhtmlsection_Struct
*chain
; /* single structure independent of weird trees */
41 struct xhtmlfile_Struct
*file
; /* which file is this a part of? */
42 char *fragment
; /* fragment id within the file */
46 struct xhtmlfile_Struct
{
47 struct xhtmlfile_Struct
*next
;
48 struct xhtmlfile_Struct
*child
;
49 struct xhtmlfile_Struct
*parent
;
51 struct xhtmlsection_Struct
*sections
; /* sections within this file (only one for non-leaf) */
52 int is_leaf
; /* is this file a leaf file, ie does it not have any children? */
55 typedef struct xhtmlsection_Struct xhtmlsection
;
56 typedef struct xhtmlfile_Struct xhtmlfile
;
57 typedef struct xhtmlindex_Struct xhtmlindex
;
59 struct xhtmlindex_Struct
{
62 xhtmlsection
**sections
;
67 wchar_t *number_suffix
;
71 int contents_depth
[6];
72 int leaf_contains_contents
;
74 int leaf_smallest_contents
;
75 int include_version_id
;
76 wchar_t *author
, *description
;
77 wchar_t *head_end
, *body
, *body_start
, *body_end
, *address_start
, *address_end
, *nav_attrs
;
79 xhtmlheadfmt fchapter
, *fsect
;
83 /*static void xhtml_level(paragraph *, int);
84 static void xhtml_level_0(paragraph *);
85 static void xhtml_docontents(FILE *, paragraph *, int);
86 static void xhtml_dosections(FILE *, paragraph *, int);
87 static void xhtml_dobody(FILE *, paragraph *, int);*/
89 static void xhtml_doheader(FILE *, word
*);
90 static void xhtml_dofooter(FILE *);
91 static void xhtml_versionid(FILE *, word
*, int);
93 static void xhtml_utostr(wchar_t *, char **);
94 static int xhtml_para_level(paragraph
*);
95 static int xhtml_reservedchar(int);
97 static int xhtml_convert(wchar_t *, int, char **, int);
98 static void xhtml_rdaddwc(rdstringc
*, word
*, word
*, int);
99 static void xhtml_para(FILE *, word
*, int);
100 static void xhtml_codepara(FILE *, word
*);
101 static void xhtml_heading(FILE *, paragraph
*, int);
103 /* File-global variables are much easier than passing these things
104 * all over the place. Evil, but easier. We can replace this with a single
105 * structure at some point.
107 static xhtmlconfig conf
;
108 static keywordlist
*keywords
;
109 static indexdata
*idx
;
110 static xhtmlfile
*topfile
;
111 static xhtmlsection
*topsection
;
112 static paragraph
*sourceparas
;
113 static xhtmlfile
*lastfile
;
114 static xhtmlfile
*xhtml_last_file
= NULL
;
115 static int last_level
=-1;
116 static xhtmlsection
*currentsection
;
118 static xhtmlconfig
xhtml_configure(paragraph
*source
)
125 ret
.contents_depth
[0] = 2;
126 ret
.contents_depth
[1] = 3;
127 ret
.contents_depth
[2] = 4;
128 ret
.contents_depth
[3] = 5;
129 ret
.contents_depth
[4] = 6;
130 ret
.contents_depth
[5] = 7;
132 ret
.leaf_smallest_contents
= 4;
133 ret
.leaf_contains_contents
= FALSE
;
134 ret
.include_version_id
= TRUE
;
136 ret
.description
= NULL
;
139 ret
.body_start
= NULL
;
141 ret
.address_start
= NULL
;
142 ret
.address_end
= NULL
;
143 ret
.nav_attrs
= NULL
;
144 ret
.suppress_address
= FALSE
;
146 ret
.fchapter
.just_numbers
= FALSE
;
147 ret
.fchapter
.number_suffix
= ustrdup(L
": ");
149 ret
.fsect
= mknewa(xhtmlheadfmt
, ret
.nfsect
);
150 ret
.fsect
[0].just_numbers
= FALSE
;
151 ret
.fsect
[0].number_suffix
= ustrdup(L
": ");
152 ret
.fsect
[1].just_numbers
= TRUE
;
153 ret
.fsect
[1].number_suffix
= ustrdup(L
" ");
155 for (; source
; source
= source
->next
)
157 if (source
->type
== para_Config
)
159 if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-0")) {
160 ret
.contents_depth
[0] = utoi(uadv(source
->keyword
));
161 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-1")) {
162 ret
.contents_depth
[1] = utoi(uadv(source
->keyword
));
163 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-2")) {
164 ret
.contents_depth
[2] = utoi(uadv(source
->keyword
));
165 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-3")) {
166 ret
.contents_depth
[3] = utoi(uadv(source
->keyword
));
167 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-4")) {
168 ret
.contents_depth
[4] = utoi(uadv(source
->keyword
));
169 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-5")) {
170 ret
.contents_depth
[5] = utoi(uadv(source
->keyword
));
171 } else if (!ustricmp(source
->keyword
, L
"xhtml-leaf-level")) {
172 ret
.leaf_level
= utoi(uadv(source
->keyword
));
173 } else if (!ustricmp(source
->keyword
, L
"xhtml-leaf-smallest-contents")) {
174 ret
.leaf_smallest_contents
= utoi(uadv(source
->keyword
));
175 } else if (!ustricmp(source
->keyword
, L
"xhtml-versionid")) {
176 ret
.include_version_id
= utob(uadv(source
->keyword
));
177 } else if (!ustricmp(source
->keyword
, L
"xhtml-leaf-contains-contents")) {
178 ret
.leaf_contains_contents
= utob(uadv(source
->keyword
));
179 } else if (!ustricmp(source
->keyword
, L
"xhtml-suppress-address")) {
180 ret
.suppress_address
= utob(uadv(source
->keyword
));
181 } else if (!ustricmp(source
->keyword
, L
"xhtml-author")) {
182 ret
.author
= uadv(source
->keyword
);
183 } else if (!ustricmp(source
->keyword
, L
"xhtml-description")) {
184 ret
.description
= uadv(source
->keyword
);
185 } else if (!ustricmp(source
->keyword
, L
"xhtml-head-end")) {
186 ret
.head_end
= uadv(source
->keyword
);
187 } else if (!ustricmp(source
->keyword
, L
"xhtml-body-start")) {
188 ret
.body_start
= uadv(source
->keyword
);
189 } else if (!ustricmp(source
->keyword
, L
"xhtml-body-tag")) {
190 ret
.body
= uadv(source
->keyword
);
191 } else if (!ustricmp(source
->keyword
, L
"xhtml-body-end")) {
192 ret
.body_end
= uadv(source
->keyword
);
193 } else if (!ustricmp(source
->keyword
, L
"xhtml-address-start")) {
194 ret
.address_start
= uadv(source
->keyword
);
195 } else if (!ustricmp(source
->keyword
, L
"xhtml-address-end")) {
196 ret
.address_end
= uadv(source
->keyword
);
197 } else if (!ustricmp(source
->keyword
, L
"xhtml-navigation-attributes")) {
198 ret
.nav_attrs
= uadv(source
->keyword
);
199 } else if (!ustricmp(source
->keyword
, L
"xhtml-chapter-numeric")) {
200 ret
.fchapter
.just_numbers
= utob(uadv(source
->keyword
));
201 } else if (!ustricmp(source
->keyword
, L
"xhtml-chapter-suffix")) {
202 ret
.fchapter
.number_suffix
= ustrdup(uadv(source
->keyword
));
203 } else if (!ustricmp(source
->keyword
, L
"xhtml-section-numeric")) {
204 wchar_t *p
= uadv(source
->keyword
);
210 if (n
>= ret
.nfsect
) {
212 ret
.fsect
= resize(ret
.fsect
, n
+1);
213 for (i
= ret
.nfsect
; i
<= n
; i
++)
214 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
217 ret
.fsect
[n
].just_numbers
= utob(p
);
218 } else if (!ustricmp(source
->keyword
, L
"xhtml-section-suffix")) {
219 wchar_t *p
= uadv(source
->keyword
);
225 if (n
>= ret
.nfsect
) {
227 ret
.fsect
= resize(ret
.fsect
, n
+1);
228 for (i
= ret
.nfsect
; i
<= n
; i
++)
229 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
232 ret
.fsect
[n
].number_suffix
= ustrdup(p
);
237 /* printf(" !!! leaf_level = %i\n", ret.leaf_level);
238 printf(" !!! contentdepth-0 = %i\n", ret.contents_depth[0]);
239 printf(" !!! contentdepth-1 = %i\n", ret.contents_depth[1]);
240 printf(" !!! contentdepth-2 = %i\n", ret.contents_depth[2]);
241 printf(" !!! contentdepth-3 = %i\n", ret.contents_depth[3]);
242 printf(" !!! contentdepth-4 = %i\n", ret.contents_depth[4]);
243 printf(" !!! contentdepth-5 = %i\n", ret.contents_depth[5]);
244 printf(" !!! leaf_contains_contents = %i\n", ret.leaf_contains_contents);*/
248 static xhtmlsection
*xhtml_new_section(xhtmlsection
*last
)
250 xhtmlsection
*ret
= mknew(xhtmlsection
);
258 ret
->level
=-1; /* marker: end of chain */
262 /* Returns NULL or the section that marks that paragraph */
263 static xhtmlsection
*xhtml_find_section(paragraph
*p
)
265 xhtmlsection
*ret
= topsection
;
266 if (xhtml_para_level(p
)==-1) { /* first, we back-track to a section paragraph */
267 paragraph
*p2
= sourceparas
;
268 paragraph
*p3
= NULL
;
269 while (p2
&& p2
!=p
) {
270 if (xhtml_para_level(p2
)!=-1) {
275 if (p3
==NULL
) { /* for some reason, we couldn't find a section before this paragraph ... ? */
276 /* Note that this can happen, if you have a cross-reference to before the first chapter starts.
277 * So don't do that, then.
283 while (ret
&& ret
->para
!= p
) {
284 /* printf(" xhtml_find_section(): checking %s for para @ %p\n", ret->fragment, p);*/
290 static xhtmlfile
*xhtml_new_file(xhtmlsection
*sect
)
292 xhtmlfile
*ret
= mknew(xhtmlfile
);
299 ret
->is_leaf
=(sect
!=NULL
&& sect
->level
==conf
.leaf_level
);
301 if (conf
.leaf_level
==0) { /* currently unused */
302 #define FILENAME_MANUAL "Manual.html"
303 #define FILENAME_CONTENTS "Contents.html"
304 ret
->filename
= smalloc(strlen(FILENAME_MANUAL
)+1);
305 sprintf(ret
->filename
, FILENAME_MANUAL
);
307 ret
->filename
= smalloc(strlen(FILENAME_CONTENTS
)+1);
308 sprintf(ret
->filename
, FILENAME_CONTENTS
);
311 paragraph
*p
= sect
->para
;
312 rdstringc fname_c
= { 0, 0, NULL
};
315 for (w
=(p
->kwtext
)?
(p
->kwtext
):(p
->words
); w
; w
=w
->next
)
317 switch (removeattr(w
->type
))
322 case word_WeakCode:*/
323 xhtml_utostr(w
->text
, &c
);
329 rdaddsc(&fname_c
, ".html");
330 ret
->filename
= rdtrimc(&fname_c
);
332 /* printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false"));*/
337 * Walk the tree fixing up files which are actually leaf (ie
338 * have no children) but aren't at leaf level, so they have the
341 void xhtml_fixup_layout(xhtmlfile
* file
)
343 if (file
->child
==NULL
) {
344 file
->is_leaf
= TRUE
;
346 xhtml_fixup_layout(file
->child
);
349 xhtml_fixup_layout(file
->next
);
353 * Create the tree structure so we know where everything goes.
356 * Ignoring file splitting, we have three choices with each new section:
358 * +-----------------+-----------------+
366 * Y is the last section we added (currentsect).
367 * If sect is the section we want to add, then:
369 * (1) if sect->level < currentsect->level
370 * (2) if sect->level == currentsect->level
371 * (3) if sect->level > currentsect->level
373 * This requires the constraint that you never skip section numbers
374 * (so you can't have a.b.c.d without all of a, a.b and a.b.c existing).
376 * Note that you _can_ have 1.1.1.1 followed by 1.2 - you can change
377 * more than one level at a time. Lots of asserts, and probably part of
378 * the algorithm here, rely on this being true. (It currently isn't
379 * enforced by halibut, however.)
381 * File splitting makes this harder. For instance, say we added at (3)
382 * above and now need to add another section. We are splitting at level
383 * 2, ie the level of Y. Z is the last section we added:
385 * +-----------------+-----------------+
395 * The (1) case is now split; we need to search upwards to find where
396 * to actually link in. The other two cases remain the same (and will
397 * always be like this).
399 * File splitting makes this harder, however. The decision of whether
400 * to split to a new file is always on the same condition, however (is
401 * the level of this section higher than the leaf_level configuration
404 * Treating the cases backwards:
406 * (3) same file if sect->level > conf.leaf_level, otherwise new file
408 * if in the same file, currentsect->child points to sect
409 * otherwise the linking is done through the file tree (which works
410 * in more or less the same way, ie currentfile->child points to
413 * (2) same file if sect->level > conf.leaf_level, otherwise new file
415 * if in the same file, currentsect->next points to sect
416 * otherwise file linking and currentfile->next points to the new
417 * file (we know that Z must have caused a new file to be created)
419 * (1) same file if sect->level > conf.leaf_level, otherwise new file
421 * this is actually effectively the same case as (2) here,
422 * except that we first have to travel up the sections to figure
423 * out which section this new one will be a sibling of. In doing
424 * so, we may disappear off the top of a file and have to go up
425 * to its parent in the file tree.
428 static void xhtml_ponder_layout(paragraph
*p
)
430 xhtmlsection
*lastsection
;
431 xhtmlsection
*currentsect
;
432 xhtmlfile
*currentfile
;
435 topsection
= xhtml_new_section(NULL
);
436 topfile
= xhtml_new_file(NULL
);
437 lastsection
= topsection
;
438 currentfile
= topfile
;
439 currentsect
= topsection
;
441 if (conf
.leaf_level
== 0) {
442 topfile
->is_leaf
= 1;
443 topfile
->sections
= topsection
;
444 topsection
->file
= topfile
;
449 int level
= xhtml_para_level(p
);
450 if (level
>0) /* actually a section */
455 rdstringc fname_c
= { 0, 0, NULL
};
457 sect
= xhtml_new_section(lastsection
);
460 for (w
=(p
->kwtext2
)?
(p
->kwtext2
):(p
->words
); w
; w
=w
->next
) /* kwtext2 because we want numbers only! */
462 switch (removeattr(w
->type
))
467 case word_WeakCode:*/
468 xhtml_utostr(w
->text
, &c
);
474 /* rdaddsc(&fname_c, ".html");*/
475 sect
->fragment
= rdtrimc(&fname_c
);
477 /* printf(" ! adding para @ %p as sect %s, level %i\n", sect->para, sect->fragment, level);*/
479 if (level
>currentsect
->level
) { /* case (3) */
480 if (level
>conf
.leaf_level
) { /* same file */
481 assert(currentfile
->is_leaf
);
482 currentsect
->child
= sect
;
483 sect
->parent
=currentsect
;
484 sect
->file
=currentfile
;
485 /* printf("connected '%s' to existing file '%s' [I]\n", sect->fragment, currentfile->filename);*/
487 } else { /* new file */
488 xhtmlfile
*file
= xhtml_new_file(sect
);
489 assert(!currentfile
->is_leaf
);
490 currentfile
->child
=file
;
492 file
->parent
=currentfile
;
493 /* printf("connected '%s' to new file '%s' [I]\n", sect->fragment, file->filename);*/
497 } else if (level
>= currentsect
->file
->sections
->level
) {
498 /* Case (1) or (2) *AND* still under the section that starts
501 * I'm not convinced that this couldn't be rolled in with the
502 * final else {} leg further down. It seems a lot of effort
505 if (level
>conf
.leaf_level
) { /* stick within the same file */
506 assert(currentfile
->is_leaf
);
507 sect
->file
= currentfile
;
508 while (currentsect
&& currentsect
->level
> level
&&
509 currentsect
->file
==currentsect
->parent
->file
) {
510 currentsect
= currentsect
->parent
;
513 currentsect
->next
= sect
;
514 assert(currentsect
->level
== sect
->level
);
515 sect
->parent
= currentsect
->parent
;
517 /* printf("connected '%s' to existing file '%s' [II]\n", sect->fragment, currentfile->filename);*/
518 } else { /* new file */
519 xhtmlfile
*file
= xhtml_new_file(sect
);
521 currentfile
->next
=file
;
522 file
->parent
=currentfile
->parent
;
523 file
->is_leaf
=(level
==conf
.leaf_level
);
525 /* printf("connected '%s' to new file '%s' [II]\n", sect->fragment, file->filename);*/
529 } else { /* Case (1) or (2) and we must move up the file tree first */
530 /* this loop is now probably irrelevant - we know we can't connect
531 * to anything in the current file */
532 while (currentsect
&& level
<currentsect
->level
) {
533 currentsect
=currentsect
->parent
;
535 /* printf(" * up one level to '%s'\n", currentsect->fragment);*/
537 /* printf(" * up one level (off top of current file)\n");*/
541 /* I'm pretty sure this can now never fire */
542 assert(currentfile
->is_leaf
);
543 /* printf("connected '%s' to existing file '%s' [III]\n", sect->fragment, currentfile->filename);*/
544 sect
->file
= currentfile
;
545 currentsect
->next
=sect
;
547 } else { /* find a file we can attach to */
548 while (currentfile
&& currentfile
->sections
&& level
<currentfile
->sections
->level
) {
549 currentfile
=currentfile
->parent
;
551 /* printf(" * up one file level to '%s'\n", currentfile->filename);*/
553 /* printf(" * up one file level (off top of tree)\n");*/
556 if (currentfile
) { /* new file (we had to skip up a file to
557 get here, so we must be dealing with a
558 level no lower than the configured
560 xhtmlfile
*file
= xhtml_new_file(sect
);
561 currentfile
->next
=file
;
563 file
->parent
=currentfile
->parent
;
564 file
->is_leaf
=(level
==conf
.leaf_level
);
566 /* printf("connected '%s' to new file '%s' [III]\n", sect->fragment, file->filename);*/
570 fatal(err_whatever
, "Ran off the top trying to connect sibling: strange document.");
576 topsection
= lastsection
; /* get correct end of the chain */
577 xhtml_fixup_layout(topfile
); /* leaf files not at leaf level marked as such */
580 static void xhtml_do_index();
581 static void xhtml_do_file(xhtmlfile
*file
);
582 static void xhtml_do_top_file(xhtmlfile
*file
, paragraph
*sourceform
);
583 static void xhtml_do_paras(FILE *fp
, paragraph
*p
, paragraph
*end
, int indexable
);
584 static int xhtml_do_contents_limit(FILE *fp
, xhtmlfile
*file
, int limit
);
585 static int xhtml_do_contents_section_limit(FILE *fp
, xhtmlsection
*section
, int limit
);
586 static int xhtml_add_contents_entry(FILE *fp
, xhtmlsection
*section
, int limit
);
587 static int xhtml_do_contents(FILE *fp
, xhtmlfile
*file
);
588 static int xhtml_do_naked_contents(FILE *fp
, xhtmlfile
*file
);
589 static void xhtml_do_sections(FILE *fp
, xhtmlsection
*sections
);
592 * Do all the files in this structure.
594 static void xhtml_do_files(xhtmlfile
*file
)
598 xhtml_do_files(file
->child
);
600 xhtml_do_files(file
->next
);
604 * Free up all memory used by the file tree from 'xfile' downwards
606 static void xhtml_free_file(xhtmlfile
* xfile
)
612 if (xfile
->filename
) {
613 sfree(xfile
->filename
);
615 xhtml_free_file(xfile
->child
);
616 xhtml_free_file(xfile
->next
);
623 void xhtml_backend(paragraph
*sourceform
, keywordlist
*in_keywords
,
631 sourceparas
= sourceform
;
632 conf
= xhtml_configure(sourceform
);
633 keywords
= in_keywords
;
636 /* Clear up the index entries backend data pointers */
637 for (ti
=0; (ientry
= (indexentry
*)index234(idx
->entries
, ti
))!=NULL
; ti
++) {
638 ientry
->backend_data
=NULL
;
641 xhtml_ponder_layout(sourceform
);
643 /* old system ... (writes to *.alt, but gets some stuff wrong and is ugly) */
644 /* xhtml_level_0(sourceform);
645 for (i=1; i<=conf.leaf_level; i++)
647 xhtml_level(sourceform, i);
650 /* new system ... (writes to *.html, but isn't fully trusted) */
651 xhtml_do_top_file(topfile
, sourceform
);
652 assert(!topfile
->next
); /* shouldn't have a sibling at all */
653 if (topfile
->child
) {
654 xhtml_do_files(topfile
->child
);
658 /* release file, section, index data structures */
661 xhtmlsection
*tmp
= xsect
->chain
;
662 if (xsect
->fragment
) {
663 sfree(xsect
->fragment
);
668 xhtml_free_file(topfile
);
669 for (ti
= 0; (ientry
=(indexentry
*)index234(idx
->entries
, ti
))!=NULL
; ti
++) {
670 if (ientry
->backend_data
!=NULL
) {
671 xhtmlindex
*xi
= (xhtmlindex
*) ientry
->backend_data
;
672 if (xi
->sections
!=NULL
) {
677 ientry
->backend_data
= NULL
;
681 sfree(conf
.fchapter
.number_suffix
);
682 for (i
= 0; i
< conf
.nfsect
; i
++)
683 sfree(conf
.fsect
[i
].number_suffix
);
688 static int xhtml_para_level(paragraph
*p
)
695 case para_UnnumberedChapter
:
700 /* case para_BiblioCited:
713 static char* xhtml_index_filename
= "IndexPage.html";
715 /* Output the nav links for the current file.
716 * file == NULL means we're doing the index
718 static void xhtml_donavlinks(FILE *fp
, xhtmlfile
*file
)
720 xhtmlfile
*xhtml_next_file
= NULL
;
722 if (conf
.nav_attrs
!=NULL
) {
723 fprintf(fp
, " %ls>", conf
.nav_attrs
);
727 if (xhtml_last_file
==NULL
) {
728 fprintf(fp
, "Previous | ");
730 fprintf(fp
, "<a href='%s'>Previous</a> | ", xhtml_last_file
->filename
);
732 fprintf(fp
, "<a href='Contents.html'>Contents</a> | ");
734 fprintf(fp
, "Index | ");
736 fprintf(fp
, "<a href='%s'>Index</a> | ", xhtml_index_filename
);
738 if (file
!= NULL
) { /* otherwise we're doing nav links for the index */
739 if (xhtml_next_file
==NULL
)
740 xhtml_next_file
= file
->child
;
741 if (xhtml_next_file
==NULL
)
742 xhtml_next_file
= file
->next
;
743 if (xhtml_next_file
==NULL
)
744 xhtml_next_file
= file
->parent
->next
;
746 if (xhtml_next_file
==NULL
) {
747 if (file
==NULL
) { /* index, so no next file */
748 fprintf(fp
, "Next ");
750 fprintf(fp
, "<a href='%s'>Next</a>", xhtml_index_filename
);
753 fprintf(fp
, "<a href='%s'>Next</a>", xhtml_next_file
->filename
);
755 fprintf(fp
, "</p>\n");
758 /* Write out the index file */
759 static void xhtml_do_index_body(FILE *fp
)
764 if (count234(idx
->entries
) == 0)
765 return; /* don't write anything at all */
767 fprintf(fp
, "<dl>\n");
768 /* iterate over idx->entries using the tree functions and display everything */
769 for (ti
= 0; (y
= (indexentry
*)index234(idx
->entries
, ti
)) != NULL
; ti
++) {
770 if (y
->backend_data
) {
775 xhtml_para(fp
, y
->text
, FALSE
);
776 fprintf(fp
, "</dt>\n<dd>");
778 xi
= (xhtmlindex
*) y
->backend_data
;
779 for (i
=0; i
<xi
->nsection
; i
++) {
780 xhtmlsection
*sect
= xi
->sections
[i
];
782 fprintf(fp
, "<a href='%s#%s'>", sect
->file
->filename
, sect
->fragment
);
783 if (sect
->para
->kwtext
) {
784 xhtml_para(fp
, sect
->para
->kwtext
, FALSE
);
785 } else if (sect
->para
->words
) {
786 xhtml_para(fp
, sect
->para
->words
, FALSE
);
789 if (i
+1<xi
->nsection
) {
794 fprintf(fp
, "</dd>\n");
797 fprintf(fp
, "</dl>\n");
799 static void xhtml_do_index()
801 word temp_word
= { NULL
, NULL
, word_Normal
, 0, 0, L
"Index", { NULL
, 0, 0} };
802 FILE *fp
= fopen(xhtml_index_filename
, "w");
805 fatal(err_cantopenw
, xhtml_index_filename
);
806 xhtml_doheader(fp
, &temp_word
);
807 xhtml_donavlinks(fp
, NULL
);
809 xhtml_do_index_body(fp
);
811 xhtml_donavlinks(fp
, NULL
);
816 /* Output the given file. This includes whatever contents at beginning and end, etc. etc. */
817 static void xhtml_do_file(xhtmlfile
*file
)
819 FILE *fp
= fopen(file
->filename
, "w");
821 fatal(err_cantopenw
, file
->filename
);
823 if (file
->sections
->para
->words
) {
824 xhtml_doheader(fp
, file
->sections
->para
->words
);
825 } else if (file
->sections
->para
->kwtext
) {
826 xhtml_doheader(fp
, file
->sections
->para
->kwtext
);
828 xhtml_doheader(fp
, NULL
);
831 xhtml_donavlinks(fp
, file
);
833 if (file
->is_leaf
&& conf
.leaf_contains_contents
&&
834 xhtml_do_contents(NULL
, file
)>=conf
.leaf_smallest_contents
)
835 xhtml_do_contents(fp
, file
);
836 xhtml_do_sections(fp
, file
->sections
);
838 xhtml_do_naked_contents(fp
, file
);
840 xhtml_donavlinks(fp
, file
);
845 xhtml_last_file
= file
;
848 /* Output the top-level file. */
849 static void xhtml_do_top_file(xhtmlfile
*file
, paragraph
*sourceform
)
853 FILE *fp
= fopen(file
->filename
, "w");
855 fatal(err_cantopenw
, file
->filename
);
857 /* Do the title -- only one allowed */
858 for (p
= sourceform
; p
&& !done
; p
= p
->next
)
860 if (p
->type
== para_Title
)
862 xhtml_doheader(fp
, p
->words
);
867 xhtml_doheader(fp
, NULL
/* Eek! */);
872 for (p
= sourceform
; p
; p
= p
->next
)
874 if (p
->type
== para_Title
) {
875 xhtml_heading(fp
, p
, FALSE
);
880 /* Do the preamble */
881 for (p
= sourceform
; p
; p
= p
->next
)
883 if (p
->type
== para_Chapter
|| p
->type
== para_Heading
||
884 p
->type
== para_Subsect
|| p
->type
== para_Appendix
||
885 p
->type
== para_UnnumberedChapter
) {
887 * We've found the end of the preamble. Do every normal
888 * paragraph up to there.
890 xhtml_do_paras(fp
, sourceform
, p
, FALSE
);
895 xhtml_do_contents(fp
, file
);
896 xhtml_do_sections(fp
, file
->sections
);
899 * Put the index in the top file if we're in single-file mode
902 if (conf
.leaf_level
== 0 && count234(idx
->entries
) > 0) {
903 fprintf(fp
, "<a name=\"index\"></a><h1>Index</h1>\n");
904 xhtml_do_index_body(fp
);
911 /* Convert a Unicode string to an ASCII one. '?' is
912 * used for unmappable characters.
914 static void xhtml_utostr(wchar_t *in
, char **out
)
921 if (in
[i
]>=32 && in
[i
]<=126)
922 (*out
)[i
]=(char)in
[i
];
930 * Write contents for the given file, and subfiles, down to
931 * the appropriate contents depth. Returns the number of
934 static int xhtml_do_contents(FILE *fp
, xhtmlfile
*file
)
936 int level
, limit
, start_level
, count
= 0;
940 level
= (file
->sections
)?
(file
->sections
->level
):(0);
941 limit
= conf
.contents_depth
[(level
>5)?
(5):(level
)];
942 start_level
= (file
->is_leaf
) ?
(level
-1) : (level
);
943 last_level
= start_level
;
945 count
+= xhtml_do_contents_section_limit(fp
, file
->sections
, limit
);
946 count
+= xhtml_do_contents_limit(fp
, file
->child
, limit
);
948 while (last_level
> start_level
) {
950 fprintf(fp
, "</ul>\n");
956 /* As above, but doesn't do anything in the current file */
957 static int xhtml_do_naked_contents(FILE *fp
, xhtmlfile
*file
)
959 int level
, limit
, start_level
, count
= 0;
963 level
= (file
->sections
)?
(file
->sections
->level
):(0);
964 limit
= conf
.contents_depth
[(level
>5)?
(5):(level
)];
965 start_level
= (file
->is_leaf
) ?
(level
-1) : (level
);
966 last_level
= start_level
;
968 count
= xhtml_do_contents_limit(fp
, file
->child
, limit
);
970 while (last_level
> start_level
) {
972 fprintf(fp
, "</ul>\n");
979 * Write contents for the given file, children, and siblings, down to
980 * given limit contents depth.
982 static int xhtml_do_contents_limit(FILE *fp
, xhtmlfile
*file
, int limit
)
986 count
+= xhtml_do_contents_section_limit(fp
, file
->sections
, limit
);
987 count
+= xhtml_do_contents_limit(fp
, file
->child
, limit
);
994 * Write contents entries for the given section tree, down to the
995 * limit contents depth.
997 static int xhtml_do_contents_section_deep_limit(FILE *fp
, xhtmlsection
*section
, int limit
)
1001 if (!xhtml_add_contents_entry(fp
, section
, limit
))
1005 count
+= xhtml_do_contents_section_deep_limit(fp
, section
->child
, limit
);
1006 section
= section
->next
;
1012 * Write contents entries for the given section tree, down to the
1013 * limit contents depth.
1015 static int xhtml_do_contents_section_limit(FILE *fp
, xhtmlsection
*section
, int limit
)
1020 xhtml_add_contents_entry(fp
, section
, limit
);
1022 count
+= xhtml_do_contents_section_deep_limit(fp
, section
->child
, limit
);
1023 /* section=section->child;
1024 while (section && xhtml_add_contents_entry(fp, section, limit)) {
1025 section = section->next;
1031 * Add a section entry, unless we're exceeding the limit, in which
1032 * case return FALSE (otherwise return TRUE).
1034 static int xhtml_add_contents_entry(FILE *fp
, xhtmlsection
*section
, int limit
)
1036 if (!section
|| section
->level
> limit
)
1038 if (fp
==NULL
|| section
->level
< 0)
1040 while (last_level
> section
->level
) {
1042 fprintf(fp
, "</ul>\n");
1044 while (last_level
< section
->level
) {
1046 fprintf(fp
, "<ul>\n");
1048 fprintf(fp
, "<li><a href=\"%s#%s\">", section
->file
->filename
, section
->fragment
);
1049 if (section
->para
->kwtext
) {
1050 xhtml_para(fp
, section
->para
->kwtext
, FALSE
);
1051 if (section
->para
->words
) {
1055 if (section
->para
->words
) {
1056 xhtml_para(fp
, section
->para
->words
, FALSE
);
1058 fprintf(fp
, "</a></li>\n");
1063 * Write all the sections in this file. Do all paragraphs in this section, then all
1064 * children (recursively), then go on to the next one (tail recursively).
1066 static void xhtml_do_sections(FILE *fp
, xhtmlsection
*sections
)
1069 currentsection
= sections
;
1070 xhtml_do_paras(fp
, sections
->para
, NULL
, TRUE
);
1071 xhtml_do_sections(fp
, sections
->child
);
1072 sections
= sections
->next
;
1076 /* Write this list of paragraphs. Close off all lists at the end. */
1077 static void xhtml_do_paras(FILE *fp
, paragraph
*p
, paragraph
*end
,
1080 int last_type
= -1, ptype
, first
=TRUE
;
1081 stack lcont_stack
= stk_new();
1085 /* for (; p && (xhtml_para_level(p)>limit || xhtml_para_level(p)==-1 || first); p=p->next) {*/
1086 for (; p
&& p
!= end
&& (xhtml_para_level(p
)==-1 || first
); p
=p
->next
) {
1088 switch (ptype
= p
->type
)
1091 * Things we ignore because we've already processed them or
1092 * aren't going to touch them in this pass.
1096 case para_Biblio
: /* only touch BiblioCited */
1097 case para_VersionID
:
1107 case para_UnnumberedChapter
:
1108 xhtml_heading(fp
, p
, indexable
);
1113 xhtml_heading(fp
, p
, indexable
);
1117 fprintf(fp
, "\n<hr />\n");
1121 case para_Copyright
:
1122 fprintf(fp
, "\n<p>");
1123 xhtml_para(fp
, p
->words
, indexable
);
1124 fprintf(fp
, "</p>\n");
1127 case para_LcontPush
:
1132 stk_push(lcont_stack
, p
);
1133 last_type
= para_Normal
;
1138 int *p
= stk_pop(lcont_stack
);
1140 ptype
= last_type
= *p
;
1142 goto closeofflist
; /* ick */
1145 case para_QuotePush
:
1146 fprintf(fp
, "<blockquote>\n");
1149 fprintf(fp
, "</blockquote>\n");
1153 case para_NumberedList
:
1154 case para_Description
:
1155 case para_DescribedThing
:
1156 case para_BiblioCited
:
1157 if (last_type
!=p
->type
) {
1158 /* start up list if necessary */
1159 if (p
->type
== para_Bullet
) {
1160 fprintf(fp
, "<ul>\n");
1161 } else if (p
->type
== para_NumberedList
) {
1162 fprintf(fp
, "<ol>\n");
1163 } else if (p
->type
== para_BiblioCited
||
1164 p
->type
== para_DescribedThing
||
1165 p
->type
== para_Description
) {
1166 fprintf(fp
, "<dl>\n");
1169 if (p
->type
== para_Bullet
|| p
->type
== para_NumberedList
) {
1170 fprintf(fp
, "<li>");
1171 } else if (p
->type
== para_DescribedThing
) {
1172 fprintf(fp
, "<dt>");
1173 } else if (p
->type
== para_Description
) {
1174 fprintf(fp
, "<dd>");
1175 } else if (p
->type
== para_BiblioCited
) {
1176 fprintf(fp
, "<dt>");
1177 xhtml_para(fp
, p
->kwtext
, indexable
);
1178 fprintf(fp
, "</dt>\n<dd>");
1180 xhtml_para(fp
, p
->words
, indexable
);
1182 paragraph
*p2
= p
->next
;
1183 if (p2
&& xhtml_para_level(p2
)==-1 && p2
->type
== para_LcontPush
)
1188 if (ptype
== para_BiblioCited
) {
1189 fprintf(fp
, "</dd>\n");
1190 } else if (p
->type
== para_DescribedThing
) {
1191 fprintf(fp
, "</dt>");
1192 } else if (p
->type
== para_Description
) {
1193 fprintf(fp
, "</dd>");
1194 } else if (ptype
== para_Bullet
|| ptype
== para_NumberedList
) {
1195 fprintf(fp
, "</li>");
1197 if (ptype
== para_Bullet
|| ptype
== para_NumberedList
||
1198 ptype
== para_BiblioCited
|| ptype
== para_Description
||
1199 ptype
== para_DescribedThing
)
1200 /* close off list if necessary */
1202 paragraph
*p2
= p
->next
;
1203 int close_off
=FALSE
;
1204 /* if (p2 && (xhtml_para_level(p2)>limit || xhtml_para_level(p2)==-1)) {*/
1205 if (p2
&& xhtml_para_level(p2
)==-1) {
1206 if (p2
->type
!= ptype
&& p2
->type
!= para_LcontPush
)
1212 if (ptype
== para_Bullet
) {
1213 fprintf(fp
, "</ul>\n");
1214 } else if (ptype
== para_NumberedList
) {
1215 fprintf(fp
, "</ol>\n");
1216 } else if (ptype
== para_BiblioCited
||
1217 ptype
== para_Description
||
1218 ptype
== para_DescribedThing
) {
1219 fprintf(fp
, "</dl>\n");
1226 xhtml_codepara(fp
, p
->words
);
1232 stk_free(lcont_stack
);
1236 * Output a header for this XHTML file.
1238 static void xhtml_doheader(FILE *fp
, word
*title
)
1240 fprintf(fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
1241 fprintf(fp
, "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
1242 fprintf(fp
, "<html xmlns='http://www.w3.org/1999/xhtml'>\n\n<head>\n<title>");
1244 fprintf(fp
, "The thing with no name!");
1246 xhtml_para(fp
, title
, FALSE
);
1247 fprintf(fp
, "</title>\n");
1248 fprintf(fp
, "<meta name=\"generator\" content=\"Halibut %s xhtml-backend\" />\n", version
);
1250 fprintf(fp
, "<meta name=\"author\" content=\"%ls\" />\n", conf
.author
);
1251 if (conf
.description
)
1252 fprintf(fp
, "<meta name=\"description\" content=\"%ls\" />\n", conf
.description
);
1254 fprintf(fp
, "%ls\n", conf
.head_end
);
1255 fprintf(fp
, "</head>\n\n");
1257 fprintf(fp
, "%ls\n", conf
.body
);
1259 fprintf(fp
, "<body>\n");
1260 if (conf
.body_start
)
1261 fprintf(fp
, "%ls\n", conf
.body_start
);
1265 * Output a footer for this XHTML file.
1267 static void xhtml_dofooter(FILE *fp
)
1269 fprintf(fp
, "\n<hr />\n\n");
1271 fprintf(fp
, "%ls\n", conf
.body_end
);
1272 if (!conf
.suppress_address
) {
1273 fprintf(fp
,"<address>\n");
1274 if (conf
.address_start
)
1275 fprintf(fp
, "%ls\n", conf
.address_start
);
1276 /* Do the version ID */
1277 if (conf
.include_version_id
) {
1280 for (p
= sourceparas
; p
; p
= p
->next
)
1281 if (p
->type
== para_VersionID
) {
1282 xhtml_versionid(fp
, p
->words
, started
);
1286 if (conf
.address_end
)
1287 fprintf(fp
, "%ls\n", conf
.address_end
);
1288 fprintf(fp
, "</address>\n");
1290 fprintf(fp
, "</body>\n\n</html>\n");
1294 * Output the versionid paragraph. Typically this is a version control
1295 * ID string (such as $Id...$ in RCS).
1297 static void xhtml_versionid(FILE *fp
, word
*text
, int started
)
1299 rdstringc t
= { 0, 0, NULL
};
1301 rdaddc(&t
, '['); /* FIXME: configurability */
1302 xhtml_rdaddwc(&t
, text
, NULL
, FALSE
);
1303 rdaddc(&t
, ']'); /* FIXME: configurability */
1306 fprintf(fp
, "<br>\n");
1307 fprintf(fp
, "%s\n", t
.text
);
1311 /* Is this an XHTML reserved character? */
1312 static int xhtml_reservedchar(int c
)
1314 if (c
=='&' || c
=='<' || c
=='>' || c
=='"')
1321 * Convert a wide string into valid XHTML: Anything outside ASCII will
1322 * be fixed up as an entity. Currently we don't worry about constraining the
1323 * encoded character set, which we should probably do at some point (we can
1324 * still fix up and return FALSE - see the last comment here). We also don't
1327 * Because this is only used for words, spaces are HARD spaces (any other
1328 * spaces will be word_Whitespace not word_Normal). So they become
1329 * Unless hard_spaces is FALSE, of course (code paragraphs break the above
1332 * If `result' is non-NULL, mallocs the resulting string and stores a pointer to
1333 * it in `*result'. If `result' is NULL, merely checks whether all
1334 * characters in the string are feasible.
1336 * Return is nonzero if all characters are OK. If not all
1337 * characters are OK but `result' is non-NULL, a result _will_
1338 * still be generated!
1340 static int xhtml_convert(wchar_t *s
, int maxlen
, char **result
,
1342 int doing
= (result
!= 0);
1345 int plen
= 0, psize
= 0;
1350 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
1353 #define ensure_size(i) if (i>=psize) { psize = i+256; p = resize(p, psize); }
1355 if (((c
== 32 && !hard_spaces
) || (c
> 32 && c
<= 126 && !xhtml_reservedchar(c
)))) {
1360 p
[plen
++] = (char)c
;
1363 /* Char needs fixing up. */
1364 /* ok = FALSE; -- currently we never return FALSE; we
1365 * might want to when considering a character set for the
1370 if (c
==32) { /* a space in a word is a hard space */
1371 ensure_size(plen
+6); /* includes space for the NUL, which is subsequently stomped on */
1372 sprintf(p
+plen
, " ");
1375 /* FIXME: entity names! */
1376 ensure_size(plen
+8); /* includes space for the NUL, which is subsequently stomped on */
1377 plen
+=sprintf(p
+plen
, "&#%04i;", (int)c
);
1383 p
= resize(p
, plen
+1);
1391 * This formats the given words as XHTML.
1393 * `indexable', if FALSE, prohibits adding any index references.
1394 * You might use this, for example, if an index reference occurred
1395 * in a section title, to prevent phony index references when the
1396 * section title is processed in strange places such as contents
1399 static void xhtml_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
, int indexable
) {
1406 for (; text
&& text
!= end
; text
= text
->next
) {
1407 switch (text
->type
) {
1408 case word_HyperLink
:
1409 xhtml_utostr(text
->text
, &c
);
1410 rdaddsc(rs
, "<a href=\"");
1416 case word_UpperXref
:
1417 case word_LowerXref
:
1418 kwl
= kw_lookup(keywords
, text
->text
);
1420 sect
=xhtml_find_section(kwl
->para
);
1422 rdaddsc(rs
, "<a href=\"");
1423 rdaddsc(rs
, sect
->file
->filename
);
1425 rdaddsc(rs
, sect
->fragment
);
1428 rdaddsc(rs
, "<a href=\"Apologies.html\"><!-- probably a bibliography cross reference -->");
1429 error(err_whatever
, "Couldn't locate cross-reference! (Probably a bibliography entry.)");
1432 rdaddsc(rs
, "<a href=\"Apologies.html\"><!-- unknown cross-reference -->");
1433 error(err_whatever
, "Couldn't locate cross-reference! (Wasn't in source file.)");
1437 case word_IndexRef
: /* in theory we could make an index target here */
1438 /* rdaddsc(rs, "<a name=\"idx-");
1439 xhtml_utostr(text->text, &c);
1442 rdaddsc(rs, "\"></a>");*/
1443 /* what we _do_ need to do is to fix up the backend data
1444 * for any indexentry this points to.
1449 for (ti
=0; (itag
= (indextag
*)index234(idx
->tags
, ti
))!=NULL
; ti
++) {
1450 /* FIXME: really ustricmp() and not ustrcmp()? */
1451 if (ustricmp(itag
->name
, text
->text
)==0) {
1456 if (itag
->refs
!=NULL
) {
1458 for (i
=0; i
<itag
->nrefs
; i
++) {
1459 xhtmlindex
*idx_ref
;
1462 ientry
= itag
->refs
[i
];
1463 if (ientry
->backend_data
==NULL
) {
1464 idx_ref
= (xhtmlindex
*) smalloc(sizeof(xhtmlindex
));
1466 fatal(err_nomemory
);
1467 idx_ref
->nsection
= 0;
1469 idx_ref
->sections
= (xhtmlsection
**) smalloc(idx_ref
->size
* sizeof(xhtmlsection
*));
1470 if (idx_ref
->sections
==NULL
)
1471 fatal(err_nomemory
);
1472 ientry
->backend_data
= idx_ref
;
1474 idx_ref
= ientry
->backend_data
;
1475 if (idx_ref
->nsection
+1 > idx_ref
->size
) {
1476 int new_size
= idx_ref
->size
* 2;
1477 idx_ref
->sections
= srealloc(idx_ref
->sections
, new_size
* sizeof(xhtmlsection
));
1478 if (idx_ref
->sections
==NULL
) {
1479 fatal(err_nomemory
);
1481 idx_ref
->size
= new_size
;
1484 idx_ref
->sections
[idx_ref
->nsection
++] = currentsection
;
1489 fatal(err_whatever
, "Index tag had no entries!");
1492 fprintf(stderr
, "Looking for index entry '%ls'\n", text
->text
);
1493 fatal(err_whatever
, "Couldn't locate index entry! (Wasn't in index.)");
1499 rdaddsc(rs
, "</a>");
1506 case word_WhiteSpace
:
1507 case word_EmphSpace
:
1508 case word_CodeSpace
:
1509 case word_WkCodeSpace
:
1511 case word_EmphQuote
:
1512 case word_CodeQuote
:
1513 case word_WkCodeQuote
:
1514 assert(text
->type
!= word_CodeQuote
&&
1515 text
->type
!= word_WkCodeQuote
);
1516 if (towordstyle(text
->type
) == word_Emph
&&
1517 (attraux(text
->aux
) == attr_First
||
1518 attraux(text
->aux
) == attr_Only
))
1519 rdaddsc(rs
, "<em>");
1520 else if ((towordstyle(text
->type
) == word_Code
|| towordstyle(text
->type
) == word_WeakCode
) &&
1521 (attraux(text
->aux
) == attr_First
||
1522 attraux(text
->aux
) == attr_Only
))
1523 rdaddsc(rs
, "<code>");
1525 if (removeattr(text
->type
) == word_Normal
) {
1526 if (xhtml_convert(text
->text
, 0, &c
, TRUE
)) /* spaces in the word are hard */
1529 xhtml_rdaddwc(rs
, text
->alt
, NULL
, indexable
);
1531 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
1533 } else if (removeattr(text
->type
) == word_Quote
) {
1534 rdaddsc(rs
, """);
1537 if (towordstyle(text
->type
) == word_Emph
&&
1538 (attraux(text
->aux
) == attr_Last
||
1539 attraux(text
->aux
) == attr_Only
))
1540 rdaddsc(rs
, "</em>");
1541 else if ((towordstyle(text
->type
) == word_Code
|| towordstyle(text
->type
) == word_WeakCode
) &&
1542 (attraux(text
->aux
) == attr_Last
||
1543 attraux(text
->aux
) == attr_Only
))
1544 rdaddsc(rs
, "</code>");
1550 /* Output a heading, formatted as XHTML.
1552 static void xhtml_heading(FILE *fp
, paragraph
*p
, int indexable
)
1554 rdstringc t
= { 0, 0, NULL
};
1555 word
*tprefix
= p
->kwtext
;
1556 word
*nprefix
= p
->kwtext2
;
1557 word
*text
= p
->words
;
1558 int level
= xhtml_para_level(p
);
1559 xhtmlsection
*sect
= xhtml_find_section(p
);
1563 fragment
= sect
->fragment
;
1565 if (p
->type
== para_Title
)
1568 fragment
= ""; /* FIXME: what else can we do? */
1569 error(err_whatever
, "Couldn't locate heading cross-reference!");
1573 if (p
->type
== para_Title
)
1575 else if (level
== 1)
1576 fmt
= &conf
.fchapter
;
1577 else if (level
-1 < conf
.nfsect
)
1578 fmt
= &conf
.fsect
[level
-1];
1580 fmt
= &conf
.fsect
[conf
.nfsect
-1];
1582 if (fmt
&& fmt
->just_numbers
&& nprefix
) {
1583 xhtml_rdaddwc(&t
, nprefix
, NULL
, indexable
);
1586 if (xhtml_convert(fmt
->number_suffix
, 0, &c
, FALSE
)) {
1591 } else if (fmt
&& !fmt
->just_numbers
&& tprefix
) {
1592 xhtml_rdaddwc(&t
, tprefix
, NULL
, indexable
);
1595 if (xhtml_convert(fmt
->number_suffix
, 0, &c
, FALSE
)) {
1601 xhtml_rdaddwc(&t
, text
, NULL
, indexable
);
1603 * If we're outputting in single-file mode, we need to lower
1604 * the level of each heading by one, because the overall
1605 * document title will be sitting right at the top as an <h1>
1606 * and so chapters and sections should start at <h2>.
1608 * Even if not, the document title will come back from
1609 * xhtml_para_level() as level zero, so we must increment that
1610 * no matter what leaf_level is set to.
1612 if (conf
.leaf_level
== 0 || level
== 0)
1614 fprintf(fp
, "<a name=\"%s\"></a><h%i>%s</h%i>\n", fragment
, level
, t
.text
, level
);
1618 /* Output a paragraph. Styles are handled by xhtml_rdaddwc().
1619 * This looks pretty simple; I may have missed something ...
1621 static void xhtml_para(FILE *fp
, word
*text
, int indexable
)
1623 rdstringc out
= { 0, 0, NULL
};
1624 xhtml_rdaddwc(&out
, text
, NULL
, indexable
);
1625 fprintf(fp
, "%s", out
.text
);
1629 /* Output a code paragraph. I'm treating this as preformatted, which
1630 * may not be entirely correct. See xhtml_para() for my worries about
1631 * this being overly-simple; however I think that most of the complexity
1632 * of the text backend came entirely out of word wrapping anyway.
1634 static void xhtml_codepara(FILE *fp
, word
*text
)
1636 fprintf(fp
, "<pre>");
1637 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
1642 * See if this WeakCode is followed by an Emph to indicate
1646 if (text
->next
&& text
->next
->type
== word_Emph
) {
1647 next
= text
= text
->next
;
1661 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1662 xhtml_convert(t
, n
, &c
, FALSE
);
1663 fprintf(fp
, "%s%s%s",
1664 (ec
== 'i' ?
"<em>" : ec
== 'b' ?
"<b>" : ""),
1666 (ec
== 'i' ?
"</em>" : ec
== 'b' ?
"</b>" : ""));
1673 xhtml_convert(t
, 0, &c
, FALSE
);
1674 fprintf(fp
, "%s\n", c
);
1677 xhtml_convert(here
->text
, 0, &c
, FALSE
);
1678 fprintf(fp
, "%s\n", c
);
1682 fprintf(fp
, "</pre>\n");