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.
36 * FILENAME_TEMPLATE (overridable in config of course) allows you
37 * to choose the general form for your HTML file names. It is
38 * slightly printf-styled (% followed by a single character is a
39 * formatting directive, %% is a literal %). Formatting directives
42 * - %n is the section type-plus-number, minus whitespace (`Chapter1.2').
43 * - %b is the section number on its own (`1.2').
44 * - %k is the section's _internal_ keyword.
45 * - %N is the section's visible title in the output, again minus
48 * %n, %b and %k will all default to %N if the section is
49 * unnumbered (`Bibliography' is often a good example).
51 * FRAGMENT_TEMPLATE is the same, but defines the <a name="foo">
52 * markers used to cross-reference to particular subsections of a
56 #define FILENAME_SINGLE "Manual.html"
57 #define FILENAME_CONTENTS "Contents.html"
58 #define FILENAME_INDEX "IndexPage.html"
59 #define FILENAME_TEMPLATE "%n.html"
60 #define FRAGMENT_TEMPLATE "%b"
62 struct xhtmlsection_Struct
{
63 struct xhtmlsection_Struct
*next
; /* next sibling (NULL if split across files) */
64 struct xhtmlsection_Struct
*child
; /* NULL if split across files */
65 struct xhtmlsection_Struct
*parent
; /* NULL if split across files */
66 struct xhtmlsection_Struct
*chain
; /* single structure independent of weird trees */
68 struct xhtmlfile_Struct
*file
; /* which file is this a part of? */
69 char *fragment
; /* fragment id within the file */
73 struct xhtmlfile_Struct
{
74 struct xhtmlfile_Struct
*next
;
75 struct xhtmlfile_Struct
*child
;
76 struct xhtmlfile_Struct
*parent
;
78 struct xhtmlsection_Struct
*sections
; /* sections within this file (only one for non-leaf) */
79 int is_leaf
; /* is this file a leaf file, ie does it not have any children? */
82 typedef struct xhtmlsection_Struct xhtmlsection
;
83 typedef struct xhtmlfile_Struct xhtmlfile
;
84 typedef struct xhtmlindex_Struct xhtmlindex
;
86 struct xhtmlindex_Struct
{
89 xhtmlsection
**sections
;
94 wchar_t *number_suffix
;
98 int contents_depth
[6];
99 int leaf_contains_contents
;
101 int leaf_smallest_contents
;
102 int include_version_id
;
103 wchar_t *author
, *description
;
104 wchar_t *head_end
, *body
, *body_start
, *body_end
, *address_start
, *address_end
, *nav_attrs
;
105 int suppress_address
;
106 xhtmlheadfmt fchapter
, *fsect
;
108 char *contents_filename
, *index_filename
;
109 char *single_filename
, *template_filename
, *template_fragment
;
112 /*static void xhtml_level(paragraph *, int);
113 static void xhtml_level_0(paragraph *);
114 static void xhtml_docontents(FILE *, paragraph *, int);
115 static void xhtml_dosections(FILE *, paragraph *, int);
116 static void xhtml_dobody(FILE *, paragraph *, int);*/
118 static void xhtml_doheader(FILE *, word
*);
119 static void xhtml_dofooter(FILE *);
120 static void xhtml_versionid(FILE *, word
*, int);
122 static void xhtml_utostr(wchar_t *, char **);
123 static int xhtml_para_level(paragraph
*);
124 static int xhtml_reservedchar(int);
126 static int xhtml_convert(wchar_t *, int, char **, int);
127 static void xhtml_rdaddwc(rdstringc
*, word
*, word
*, int);
128 static void xhtml_para(FILE *, word
*, int);
129 static void xhtml_codepara(FILE *, word
*);
130 static void xhtml_heading(FILE *, paragraph
*, int);
132 /* File-global variables are much easier than passing these things
133 * all over the place. Evil, but easier. We can replace this with a single
134 * structure at some point.
136 static xhtmlconfig conf
;
137 static keywordlist
*keywords
;
138 static indexdata
*idx
;
139 static xhtmlfile
*topfile
;
140 static xhtmlsection
*topsection
;
141 static paragraph
*sourceparas
;
142 static xhtmlfile
*lastfile
;
143 static xhtmlfile
*xhtml_last_file
= NULL
;
144 static int last_level
=-1, start_level
;
145 static xhtmlsection
*currentsection
;
147 static xhtmlconfig
xhtml_configure(paragraph
*source
)
154 ret
.contents_depth
[0] = 2;
155 ret
.contents_depth
[1] = 3;
156 ret
.contents_depth
[2] = 4;
157 ret
.contents_depth
[3] = 5;
158 ret
.contents_depth
[4] = 6;
159 ret
.contents_depth
[5] = 7;
161 ret
.leaf_smallest_contents
= 4;
162 ret
.leaf_contains_contents
= FALSE
;
163 ret
.include_version_id
= TRUE
;
165 ret
.description
= NULL
;
168 ret
.body_start
= NULL
;
170 ret
.address_start
= NULL
;
171 ret
.address_end
= NULL
;
172 ret
.nav_attrs
= NULL
;
173 ret
.suppress_address
= FALSE
;
175 ret
.fchapter
.just_numbers
= FALSE
;
176 ret
.fchapter
.number_suffix
= L
": ";
178 ret
.fsect
= mknewa(xhtmlheadfmt
, ret
.nfsect
);
179 ret
.fsect
[0].just_numbers
= FALSE
;
180 ret
.fsect
[0].number_suffix
= L
": ";
181 ret
.fsect
[1].just_numbers
= TRUE
;
182 ret
.fsect
[1].number_suffix
= L
" ";
183 ret
.contents_filename
= strdup(FILENAME_CONTENTS
);
184 ret
.single_filename
= strdup(FILENAME_SINGLE
);
185 ret
.index_filename
= strdup(FILENAME_INDEX
);
186 ret
.template_filename
= strdup(FILENAME_TEMPLATE
);
187 ret
.template_fragment
= strdup(FRAGMENT_TEMPLATE
);
189 for (; source
; source
= source
->next
)
191 if (source
->type
== para_Config
)
193 if (!ustricmp(source
->keyword
, L
"xhtml-contents-filename")) {
194 sfree(ret
.contents_filename
);
195 ret
.contents_filename
= utoa_dup(uadv(source
->keyword
));
196 } else if (!ustricmp(source
->keyword
, L
"xhtml-single-filename")) {
197 sfree(ret
.single_filename
);
198 ret
.single_filename
= utoa_dup(uadv(source
->keyword
));
199 } else if (!ustricmp(source
->keyword
, L
"xhtml-index-filename")) {
200 sfree(ret
.index_filename
);
201 ret
.index_filename
= utoa_dup(uadv(source
->keyword
));
202 } else if (!ustricmp(source
->keyword
, L
"xhtml-template-filename")) {
203 sfree(ret
.template_filename
);
204 ret
.template_filename
= utoa_dup(uadv(source
->keyword
));
205 } else if (!ustricmp(source
->keyword
, L
"xhtml-template-fragment")) {
206 sfree(ret
.template_fragment
);
207 ret
.template_fragment
= utoa_dup(uadv(source
->keyword
));
208 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-0")) {
209 ret
.contents_depth
[0] = utoi(uadv(source
->keyword
));
210 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-1")) {
211 ret
.contents_depth
[1] = utoi(uadv(source
->keyword
));
212 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-2")) {
213 ret
.contents_depth
[2] = utoi(uadv(source
->keyword
));
214 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-3")) {
215 ret
.contents_depth
[3] = utoi(uadv(source
->keyword
));
216 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-4")) {
217 ret
.contents_depth
[4] = utoi(uadv(source
->keyword
));
218 } else if (!ustricmp(source
->keyword
, L
"xhtml-contents-depth-5")) {
219 ret
.contents_depth
[5] = utoi(uadv(source
->keyword
));
220 } else if (!ustricmp(source
->keyword
, L
"xhtml-leaf-level")) {
221 ret
.leaf_level
= utoi(uadv(source
->keyword
));
222 } else if (!ustricmp(source
->keyword
, L
"xhtml-leaf-smallest-contents")) {
223 ret
.leaf_smallest_contents
= utoi(uadv(source
->keyword
));
224 } else if (!ustricmp(source
->keyword
, L
"xhtml-versionid")) {
225 ret
.include_version_id
= utob(uadv(source
->keyword
));
226 } else if (!ustricmp(source
->keyword
, L
"xhtml-leaf-contains-contents")) {
227 ret
.leaf_contains_contents
= utob(uadv(source
->keyword
));
228 } else if (!ustricmp(source
->keyword
, L
"xhtml-suppress-address")) {
229 ret
.suppress_address
= utob(uadv(source
->keyword
));
230 } else if (!ustricmp(source
->keyword
, L
"xhtml-author")) {
231 ret
.author
= uadv(source
->keyword
);
232 } else if (!ustricmp(source
->keyword
, L
"xhtml-description")) {
233 ret
.description
= uadv(source
->keyword
);
234 } else if (!ustricmp(source
->keyword
, L
"xhtml-head-end")) {
235 ret
.head_end
= uadv(source
->keyword
);
236 } else if (!ustricmp(source
->keyword
, L
"xhtml-body-start")) {
237 ret
.body_start
= uadv(source
->keyword
);
238 } else if (!ustricmp(source
->keyword
, L
"xhtml-body-tag")) {
239 ret
.body
= uadv(source
->keyword
);
240 } else if (!ustricmp(source
->keyword
, L
"xhtml-body-end")) {
241 ret
.body_end
= uadv(source
->keyword
);
242 } else if (!ustricmp(source
->keyword
, L
"xhtml-address-start")) {
243 ret
.address_start
= uadv(source
->keyword
);
244 } else if (!ustricmp(source
->keyword
, L
"xhtml-address-end")) {
245 ret
.address_end
= uadv(source
->keyword
);
246 } else if (!ustricmp(source
->keyword
, L
"xhtml-navigation-attributes")) {
247 ret
.nav_attrs
= uadv(source
->keyword
);
248 } else if (!ustricmp(source
->keyword
, L
"xhtml-chapter-numeric")) {
249 ret
.fchapter
.just_numbers
= utob(uadv(source
->keyword
));
250 } else if (!ustricmp(source
->keyword
, L
"xhtml-chapter-suffix")) {
251 ret
.fchapter
.number_suffix
= uadv(source
->keyword
);
252 } else if (!ustricmp(source
->keyword
, L
"xhtml-section-numeric")) {
253 wchar_t *p
= uadv(source
->keyword
);
259 if (n
>= ret
.nfsect
) {
261 ret
.fsect
= resize(ret
.fsect
, n
+1);
262 for (i
= ret
.nfsect
; i
<= n
; i
++)
263 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
266 ret
.fsect
[n
].just_numbers
= utob(p
);
267 } else if (!ustricmp(source
->keyword
, L
"xhtml-section-suffix")) {
268 wchar_t *p
= uadv(source
->keyword
);
274 if (n
>= ret
.nfsect
) {
276 ret
.fsect
= resize(ret
.fsect
, n
+1);
277 for (i
= ret
.nfsect
; i
<= n
; i
++)
278 ret
.fsect
[i
] = ret
.fsect
[ret
.nfsect
-1];
281 ret
.fsect
[n
].number_suffix
= p
;
286 /* printf(" !!! leaf_level = %i\n", ret.leaf_level);
287 printf(" !!! contentdepth-0 = %i\n", ret.contents_depth[0]);
288 printf(" !!! contentdepth-1 = %i\n", ret.contents_depth[1]);
289 printf(" !!! contentdepth-2 = %i\n", ret.contents_depth[2]);
290 printf(" !!! contentdepth-3 = %i\n", ret.contents_depth[3]);
291 printf(" !!! contentdepth-4 = %i\n", ret.contents_depth[4]);
292 printf(" !!! contentdepth-5 = %i\n", ret.contents_depth[5]);
293 printf(" !!! leaf_contains_contents = %i\n", ret.leaf_contains_contents);*/
297 paragraph
*xhtml_config_filename(char *filename
)
300 * If the user passes in a single filename as a parameter to
301 * the `--html' command-line option, then we should assume it
302 * to imply _two_ config directives:
303 * \cfg{xhtml-single-filename}{whatever} and
304 * \cfg{xhtml-leaf-level}{0}; the rationale being that the user
305 * wants their output _in that file_.
310 wchar_t *ufilename
, *up
;
312 for (i
= 0; i
< 2; i
++) {
313 p
[i
] = mknew(paragraph
);
314 memset(p
[i
], 0, sizeof(*p
[i
]));
315 p
[i
]->type
= para_Config
;
317 p
[i
]->fpos
.filename
= "<command line>";
318 p
[i
]->fpos
.line
= p
[i
]->fpos
.col
= -1;
321 ufilename
= ufroma_dup(filename
);
322 len
= ustrlen(ufilename
) + 2 + lenof(L
"xhtml-single-filename");
323 p
[0]->keyword
= mknewa(wchar_t, len
);
325 ustrcpy(up
, L
"xhtml-single-filename");
327 ustrcpy(up
, ufilename
);
330 assert(up
- p
[0]->keyword
< len
);
333 len
= lenof(L
"xhtml-leaf-level") + lenof(L
"0") + 1;
334 p
[1]->keyword
= mknewa(wchar_t, len
);
336 ustrcpy(up
, L
"xhtml-leaf-level");
341 assert(up
- p
[1]->keyword
< len
);
348 static xhtmlsection
*xhtml_new_section(xhtmlsection
*last
)
350 xhtmlsection
*ret
= mknew(xhtmlsection
);
358 ret
->level
=-1; /* marker: end of chain */
362 /* Returns NULL or the section that marks that paragraph */
363 static xhtmlsection
*xhtml_find_section(paragraph
*p
)
365 xhtmlsection
*ret
= topsection
;
366 if (xhtml_para_level(p
)==-1) { /* first, we back-track to a section paragraph */
367 paragraph
*p2
= sourceparas
;
368 paragraph
*p3
= NULL
;
369 while (p2
&& p2
!=p
) {
370 if (xhtml_para_level(p2
)!=-1) {
375 if (p3
==NULL
) { /* for some reason, we couldn't find a section before this paragraph ... ? */
376 /* Note that this can happen, if you have a cross-reference to before the first chapter starts.
377 * So don't do that, then.
383 while (ret
&& ret
->para
!= p
) {
384 /* printf(" xhtml_find_section(): checking %s for para @ %p\n", ret->fragment, p);*/
390 static void xhtml_format(paragraph
*p
, char *template_string
, rdstringc
*r
)
398 if (*t
== '%' && t
[1]) {
412 if (p
->kwtext
&& fmt
== 'n')
414 else if (p
->kwtext2
&& fmt
== 'b')
416 else if (p
->keyword
&& *p
->keyword
&& fmt
== 'k')
422 switch (removeattr(w
->type
))
427 case word_WeakCode:*/
428 xhtml_utostr(w
->text
, &c
);
436 xhtml_utostr(ws
, &c
);
446 static xhtmlfile
*xhtml_new_file(xhtmlsection
*sect
)
448 xhtmlfile
*ret
= mknew(xhtmlfile
);
455 ret
->is_leaf
=(sect
!=NULL
&& sect
->level
==conf
.leaf_level
);
457 if (conf
.leaf_level
==0) { /* currently unused */
458 ret
->filename
= smalloc(strlen(conf
.single_filename
)+1);
459 sprintf(ret
->filename
, conf
.single_filename
);
461 ret
->filename
= smalloc(strlen(conf
.contents_filename
)+1);
462 sprintf(ret
->filename
, conf
.contents_filename
);
465 paragraph
*p
= sect
->para
;
466 rdstringc fname_c
= { 0, 0, NULL
};
467 xhtml_format(p
, conf
.template_filename
, &fname_c
);
468 ret
->filename
= rdtrimc(&fname_c
);
470 /* printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false"));*/
475 * Walk the tree fixing up files which are actually leaf (ie
476 * have no children) but aren't at leaf level, so they have the
479 void xhtml_fixup_layout(xhtmlfile
* file
)
481 if (file
->child
==NULL
) {
482 file
->is_leaf
= TRUE
;
484 xhtml_fixup_layout(file
->child
);
487 xhtml_fixup_layout(file
->next
);
491 * Create the tree structure so we know where everything goes.
494 * Ignoring file splitting, we have three choices with each new section:
496 * +-----------------+-----------------+
504 * Y is the last section we added (currentsect).
505 * If sect is the section we want to add, then:
507 * (1) if sect->level < currentsect->level
508 * (2) if sect->level == currentsect->level
509 * (3) if sect->level > currentsect->level
511 * This requires the constraint that you never skip section numbers
512 * (so you can't have a.b.c.d without all of a, a.b and a.b.c existing).
514 * Note that you _can_ have 1.1.1.1 followed by 1.2 - you can change
515 * more than one level at a time. Lots of asserts, and probably part of
516 * the algorithm here, rely on this being true. (It currently isn't
517 * enforced by halibut, however.)
519 * File splitting makes this harder. For instance, say we added at (3)
520 * above and now need to add another section. We are splitting at level
521 * 2, ie the level of Y. Z is the last section we added:
523 * +-----------------+-----------------+
533 * The (1) case is now split; we need to search upwards to find where
534 * to actually link in. The other two cases remain the same (and will
535 * always be like this).
537 * File splitting makes this harder, however. The decision of whether
538 * to split to a new file is always on the same condition, however (is
539 * the level of this section higher than the leaf_level configuration
542 * Treating the cases backwards:
544 * (3) same file if sect->level > conf.leaf_level, otherwise new file
546 * if in the same file, currentsect->child points to sect
547 * otherwise the linking is done through the file tree (which works
548 * in more or less the same way, ie currentfile->child points to
551 * (2) same file if sect->level > conf.leaf_level, otherwise new file
553 * if in the same file, currentsect->next points to sect
554 * otherwise file linking and currentfile->next points to the new
555 * file (we know that Z must have caused a new file to be created)
557 * (1) same file if sect->level > conf.leaf_level, otherwise new file
559 * this is actually effectively the same case as (2) here,
560 * except that we first have to travel up the sections to figure
561 * out which section this new one will be a sibling of. In doing
562 * so, we may disappear off the top of a file and have to go up
563 * to its parent in the file tree.
566 static void xhtml_ponder_layout(paragraph
*p
)
568 xhtmlsection
*lastsection
;
569 xhtmlsection
*currentsect
;
570 xhtmlfile
*currentfile
;
573 topsection
= xhtml_new_section(NULL
);
574 topfile
= xhtml_new_file(NULL
);
575 lastsection
= topsection
;
576 currentfile
= topfile
;
577 currentsect
= topsection
;
579 if (conf
.leaf_level
== 0) {
580 topfile
->is_leaf
= 1;
581 topfile
->sections
= topsection
;
582 topsection
->file
= topfile
;
587 int level
= xhtml_para_level(p
);
588 if (level
>0) /* actually a section */
591 rdstringc frag_c
= { 0, 0, NULL
};
593 sect
= xhtml_new_section(lastsection
);
597 xhtml_format(p
, conf
.template_fragment
, &frag_c
);
598 sect
->fragment
= rdtrimc(&frag_c
);
600 /* printf(" ! adding para @ %p as sect %s, level %i\n", sect->para, sect->fragment, level);*/
602 if (level
>currentsect
->level
) { /* case (3) */
603 if (level
>conf
.leaf_level
) { /* same file */
604 assert(currentfile
->is_leaf
);
605 currentsect
->child
= sect
;
606 sect
->parent
=currentsect
;
607 sect
->file
=currentfile
;
608 /* printf("connected '%s' to existing file '%s' [I]\n", sect->fragment, currentfile->filename);*/
610 } else { /* new file */
611 xhtmlfile
*file
= xhtml_new_file(sect
);
612 assert(!currentfile
->is_leaf
);
613 currentfile
->child
=file
;
615 file
->parent
=currentfile
;
616 /* printf("connected '%s' to new file '%s' [I]\n", sect->fragment, file->filename);*/
620 } else if (level
>= currentsect
->file
->sections
->level
) {
621 /* Case (1) or (2) *AND* still under the section that starts
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
628 if (level
>conf
.leaf_level
) { /* stick within the same file */
629 assert(currentfile
->is_leaf
);
630 sect
->file
= currentfile
;
631 while (currentsect
&& currentsect
->level
> level
&&
632 currentsect
->file
==currentsect
->parent
->file
) {
633 currentsect
= currentsect
->parent
;
636 currentsect
->next
= sect
;
637 assert(currentsect
->level
== sect
->level
);
638 sect
->parent
= currentsect
->parent
;
640 /* printf("connected '%s' to existing file '%s' [II]\n", sect->fragment, currentfile->filename);*/
641 } else { /* new file */
642 xhtmlfile
*file
= xhtml_new_file(sect
);
644 currentfile
->next
=file
;
645 file
->parent
=currentfile
->parent
;
646 file
->is_leaf
=(level
==conf
.leaf_level
);
648 /* printf("connected '%s' to new file '%s' [II]\n", sect->fragment, file->filename);*/
652 } else { /* Case (1) or (2) and we must move up the file tree first */
653 /* this loop is now probably irrelevant - we know we can't connect
654 * to anything in the current file */
655 while (currentsect
&& level
<currentsect
->level
) {
656 currentsect
=currentsect
->parent
;
658 /* printf(" * up one level to '%s'\n", currentsect->fragment);*/
660 /* printf(" * up one level (off top of current file)\n");*/
664 /* I'm pretty sure this can now never fire */
665 assert(currentfile
->is_leaf
);
666 /* printf("connected '%s' to existing file '%s' [III]\n", sect->fragment, currentfile->filename);*/
667 sect
->file
= currentfile
;
668 currentsect
->next
=sect
;
670 } else { /* find a file we can attach to */
671 while (currentfile
&& currentfile
->sections
&& level
<currentfile
->sections
->level
) {
672 currentfile
=currentfile
->parent
;
674 /* printf(" * up one file level to '%s'\n", currentfile->filename);*/
676 /* printf(" * up one file level (off top of tree)\n");*/
679 if (currentfile
) { /* new file (we had to skip up a file to
680 get here, so we must be dealing with a
681 level no lower than the configured
683 xhtmlfile
*file
= xhtml_new_file(sect
);
684 currentfile
->next
=file
;
686 file
->parent
=currentfile
->parent
;
687 file
->is_leaf
=(level
==conf
.leaf_level
);
689 /* printf("connected '%s' to new file '%s' [III]\n", sect->fragment, file->filename);*/
693 fatal(err_whatever
, "Ran off the top trying to connect sibling: strange document.");
699 topsection
= lastsection
; /* get correct end of the chain */
700 xhtml_fixup_layout(topfile
); /* leaf files not at leaf level marked as such */
703 static void xhtml_do_index();
704 static void xhtml_do_file(xhtmlfile
*file
);
705 static void xhtml_do_top_file(xhtmlfile
*file
, paragraph
*sourceform
);
706 static void xhtml_do_paras(FILE *fp
, paragraph
*p
, paragraph
*end
, int indexable
);
707 static int xhtml_do_contents_limit(FILE *fp
, xhtmlfile
*file
, int limit
);
708 static int xhtml_do_contents_section_limit(FILE *fp
, xhtmlsection
*section
, int limit
);
709 static int xhtml_add_contents_entry(FILE *fp
, xhtmlsection
*section
, int limit
);
710 static int xhtml_do_contents(FILE *fp
, xhtmlfile
*file
);
711 static int xhtml_do_naked_contents(FILE *fp
, xhtmlfile
*file
);
712 static void xhtml_do_sections(FILE *fp
, xhtmlsection
*sections
);
715 * Do all the files in this structure.
717 static void xhtml_do_files(xhtmlfile
*file
)
721 xhtml_do_files(file
->child
);
723 xhtml_do_files(file
->next
);
727 * Free up all memory used by the file tree from 'xfile' downwards
729 static void xhtml_free_file(xhtmlfile
* xfile
)
735 if (xfile
->filename
) {
736 sfree(xfile
->filename
);
738 xhtml_free_file(xfile
->child
);
739 xhtml_free_file(xfile
->next
);
746 void xhtml_backend(paragraph
*sourceform
, keywordlist
*in_keywords
,
747 indexdata
*in_idx
, void *unused
)
756 sourceparas
= sourceform
;
757 conf
= xhtml_configure(sourceform
);
758 keywords
= in_keywords
;
761 /* Clear up the index entries backend data pointers */
762 for (ti
=0; (ientry
= (indexentry
*)index234(idx
->entries
, ti
))!=NULL
; ti
++) {
763 ientry
->backend_data
=NULL
;
766 xhtml_ponder_layout(sourceform
);
768 /* old system ... (writes to *.alt, but gets some stuff wrong and is ugly) */
769 /* xhtml_level_0(sourceform);
770 for (i=1; i<=conf.leaf_level; i++)
772 xhtml_level(sourceform, i);
775 /* new system ... (writes to *.html, but isn't fully trusted) */
776 xhtml_do_top_file(topfile
, sourceform
);
777 assert(!topfile
->next
); /* shouldn't have a sibling at all */
778 if (topfile
->child
) {
779 xhtml_do_files(topfile
->child
);
783 /* release file, section, index data structures */
786 xhtmlsection
*tmp
= xsect
->chain
;
787 if (xsect
->fragment
) {
788 sfree(xsect
->fragment
);
793 xhtml_free_file(topfile
);
794 for (ti
= 0; (ientry
=(indexentry
*)index234(idx
->entries
, ti
))!=NULL
; ti
++) {
795 if (ientry
->backend_data
!=NULL
) {
796 xhtmlindex
*xi
= (xhtmlindex
*) ientry
->backend_data
;
797 if (xi
->sections
!=NULL
) {
802 ientry
->backend_data
= NULL
;
807 static int xhtml_para_level(paragraph
*p
)
814 case para_UnnumberedChapter
:
819 /* case para_BiblioCited:
832 /* Output the nav links for the current file.
833 * file == NULL means we're doing the index
835 static void xhtml_donavlinks(FILE *fp
, xhtmlfile
*file
)
837 xhtmlfile
*xhtml_next_file
= NULL
;
839 if (conf
.nav_attrs
!=NULL
) {
840 fprintf(fp
, " %ls>", conf
.nav_attrs
);
844 if (xhtml_last_file
==NULL
) {
845 fprintf(fp
, "Previous | ");
847 fprintf(fp
, "<a href=\"%s\">Previous</a> | ", xhtml_last_file
->filename
);
849 fprintf(fp
, "<a href=\"%s\">Contents</a> | ", conf
.contents_filename
);
851 fprintf(fp
, "Index | ");
853 fprintf(fp
, "<a href=\"%s\">Index</a> | ", conf
.index_filename
);
855 if (file
!= NULL
) { /* otherwise we're doing nav links for the index */
856 if (xhtml_next_file
==NULL
)
857 xhtml_next_file
= file
->child
;
858 if (xhtml_next_file
==NULL
)
859 xhtml_next_file
= file
->next
;
860 if (xhtml_next_file
==NULL
)
861 xhtml_next_file
= file
->parent
->next
;
863 if (xhtml_next_file
==NULL
) {
864 if (file
==NULL
) { /* index, so no next file */
865 fprintf(fp
, "Next ");
867 fprintf(fp
, "<a href=\"%s\">Next</a>", conf
.index_filename
);
870 fprintf(fp
, "<a href=\"%s\">Next</a>", xhtml_next_file
->filename
);
872 fprintf(fp
, "</p>\n");
875 /* Write out the index file */
876 static void xhtml_do_index_body(FILE *fp
)
881 if (count234(idx
->entries
) == 0)
882 return; /* don't write anything at all */
884 fprintf(fp
, "<dl>\n");
885 /* iterate over idx->entries using the tree functions and display everything */
886 for (ti
= 0; (y
= (indexentry
*)index234(idx
->entries
, ti
)) != NULL
; ti
++) {
887 if (y
->backend_data
) {
892 xhtml_para(fp
, y
->text
, FALSE
);
893 fprintf(fp
, "</dt>\n<dd>");
895 xi
= (xhtmlindex
*) y
->backend_data
;
896 for (i
=0; i
<xi
->nsection
; i
++) {
897 xhtmlsection
*sect
= xi
->sections
[i
];
899 fprintf(fp
, "<a href=\"%s#%s\">", sect
->file
->filename
, sect
->fragment
);
900 if (sect
->para
->kwtext
) {
901 xhtml_para(fp
, sect
->para
->kwtext
, FALSE
);
902 } else if (sect
->para
->words
) {
903 xhtml_para(fp
, sect
->para
->words
, FALSE
);
906 if (i
+1<xi
->nsection
) {
911 fprintf(fp
, "</dd>\n");
914 fprintf(fp
, "</dl>\n");
916 static void xhtml_do_index()
918 word temp_word
= { NULL
, NULL
, word_Normal
, 0, 0, L
"Index",
919 { NULL
, 0, 0}, NULL
};
920 FILE *fp
= fopen(conf
.index_filename
, "w");
923 fatal(err_cantopenw
, conf
.index_filename
);
924 xhtml_doheader(fp
, &temp_word
);
925 xhtml_donavlinks(fp
, NULL
);
927 xhtml_do_index_body(fp
);
929 xhtml_donavlinks(fp
, NULL
);
934 /* Output the given file. This includes whatever contents at beginning and end, etc. etc. */
935 static void xhtml_do_file(xhtmlfile
*file
)
937 FILE *fp
= fopen(file
->filename
, "w");
939 fatal(err_cantopenw
, file
->filename
);
941 if (file
->sections
->para
->words
) {
942 xhtml_doheader(fp
, file
->sections
->para
->words
);
943 } else if (file
->sections
->para
->kwtext
) {
944 xhtml_doheader(fp
, file
->sections
->para
->kwtext
);
946 xhtml_doheader(fp
, NULL
);
949 xhtml_donavlinks(fp
, file
);
951 if (file
->is_leaf
&& conf
.leaf_contains_contents
&&
952 xhtml_do_contents(NULL
, file
)>=conf
.leaf_smallest_contents
)
953 xhtml_do_contents(fp
, file
);
954 xhtml_do_sections(fp
, file
->sections
);
956 xhtml_do_naked_contents(fp
, file
);
958 xhtml_donavlinks(fp
, file
);
963 xhtml_last_file
= file
;
966 /* Output the top-level file. */
967 static void xhtml_do_top_file(xhtmlfile
*file
, paragraph
*sourceform
)
971 FILE *fp
= fopen(file
->filename
, "w");
973 fatal(err_cantopenw
, file
->filename
);
975 /* Do the title -- only one allowed */
976 for (p
= sourceform
; p
&& !done
; p
= p
->next
)
978 if (p
->type
== para_Title
)
980 xhtml_doheader(fp
, p
->words
);
985 xhtml_doheader(fp
, NULL
/* Eek! */);
990 for (p
= sourceform
; p
; p
= p
->next
)
992 if (p
->type
== para_Title
) {
993 xhtml_heading(fp
, p
, FALSE
);
998 /* Do the preamble */
999 for (p
= sourceform
; p
; p
= p
->next
)
1001 if (p
->type
== para_Chapter
|| p
->type
== para_Heading
||
1002 p
->type
== para_Subsect
|| p
->type
== para_Appendix
||
1003 p
->type
== para_UnnumberedChapter
) {
1005 * We've found the end of the preamble. Do every normal
1006 * paragraph up to there.
1008 xhtml_do_paras(fp
, sourceform
, p
, FALSE
);
1013 xhtml_do_contents(fp
, file
);
1014 xhtml_do_sections(fp
, file
->sections
);
1017 * Put the index in the top file if we're in single-file mode
1020 if (conf
.leaf_level
== 0 && count234(idx
->entries
) > 0) {
1021 fprintf(fp
, "<a name=\"index\"></a><h1>Index</h1>\n");
1022 xhtml_do_index_body(fp
);
1029 /* Convert a Unicode string to an ASCII one. '?' is
1030 * used for unmappable characters.
1032 static void xhtml_utostr(wchar_t *in
, char **out
)
1034 int l
= ustrlen(in
);
1036 *out
= smalloc(l
+1);
1039 if (in
[i
]>=32 && in
[i
]<=126)
1040 (*out
)[i
]=(char)in
[i
];
1048 * Write contents for the given file, and subfiles, down to
1049 * the appropriate contents depth. Returns the number of
1052 static int xhtml_do_contents(FILE *fp
, xhtmlfile
*file
)
1054 int level
, limit
, count
= 0;
1058 level
= (file
->sections
)?
(file
->sections
->level
):(0);
1059 limit
= conf
.contents_depth
[(level
>5)?
(5):(level
)];
1060 start_level
= (file
->is_leaf
) ?
(level
-1) : (level
);
1061 last_level
= start_level
;
1063 count
+= xhtml_do_contents_section_limit(fp
, file
->sections
, limit
);
1064 count
+= xhtml_do_contents_limit(fp
, file
->child
, limit
);
1066 while (last_level
> start_level
) {
1068 fprintf(fp
, "</li></ul>\n");
1074 /* As above, but doesn't do anything in the current file */
1075 static int xhtml_do_naked_contents(FILE *fp
, xhtmlfile
*file
)
1077 int level
, limit
, start_level
, count
= 0;
1081 level
= (file
->sections
)?
(file
->sections
->level
):(0);
1082 limit
= conf
.contents_depth
[(level
>5)?
(5):(level
)];
1083 start_level
= (file
->is_leaf
) ?
(level
-1) : (level
);
1084 last_level
= start_level
;
1086 count
= xhtml_do_contents_limit(fp
, file
->child
, limit
);
1088 while (last_level
> start_level
) {
1090 fprintf(fp
, "</li></ul>\n");
1097 * Write contents for the given file, children, and siblings, down to
1098 * given limit contents depth.
1100 static int xhtml_do_contents_limit(FILE *fp
, xhtmlfile
*file
, int limit
)
1104 count
+= xhtml_do_contents_section_limit(fp
, file
->sections
, limit
);
1105 count
+= xhtml_do_contents_limit(fp
, file
->child
, limit
);
1112 * Write contents entries for the given section tree, down to the
1113 * limit contents depth.
1115 static int xhtml_do_contents_section_deep_limit(FILE *fp
, xhtmlsection
*section
, int limit
)
1119 if (!xhtml_add_contents_entry(fp
, section
, limit
))
1123 count
+= xhtml_do_contents_section_deep_limit(fp
, section
->child
, limit
);
1124 section
= section
->next
;
1130 * Write contents entries for the given section tree, down to the
1131 * limit contents depth.
1133 static int xhtml_do_contents_section_limit(FILE *fp
, xhtmlsection
*section
, int limit
)
1138 xhtml_add_contents_entry(fp
, section
, limit
);
1140 count
+= xhtml_do_contents_section_deep_limit(fp
, section
->child
, limit
);
1141 /* section=section->child;
1142 while (section && xhtml_add_contents_entry(fp, section, limit)) {
1143 section = section->next;
1149 * Add a section entry, unless we're exceeding the limit, in which
1150 * case return FALSE (otherwise return TRUE).
1152 static int xhtml_add_contents_entry(FILE *fp
, xhtmlsection
*section
, int limit
)
1154 if (!section
|| section
->level
> limit
)
1156 if (fp
==NULL
|| section
->level
< 0)
1158 if (last_level
> section
->level
) {
1159 while (last_level
> section
->level
) {
1161 fprintf(fp
, "</li></ul>\n");
1163 fprintf(fp
, "</li>\n");
1164 } else if (last_level
< section
->level
) {
1165 assert(last_level
== section
->level
- 1);
1167 fprintf(fp
, "<ul>\n");
1169 fprintf(fp
, "</li>\n");
1171 fprintf(fp
, "<li><a href=\"%s#%s\">", section
->file
->filename
, section
->fragment
);
1172 if (section
->para
->kwtext
) {
1173 xhtml_para(fp
, section
->para
->kwtext
, FALSE
);
1174 if (section
->para
->words
) {
1178 if (section
->para
->words
) {
1179 xhtml_para(fp
, section
->para
->words
, FALSE
);
1181 fprintf(fp
, "</a>\n");
1186 * Write all the sections in this file. Do all paragraphs in this section, then all
1187 * children (recursively), then go on to the next one (tail recursively).
1189 static void xhtml_do_sections(FILE *fp
, xhtmlsection
*sections
)
1192 currentsection
= sections
;
1193 xhtml_do_paras(fp
, sections
->para
, NULL
, TRUE
);
1194 xhtml_do_sections(fp
, sections
->child
);
1195 sections
= sections
->next
;
1199 /* Write this list of paragraphs. Close off all lists at the end. */
1200 static void xhtml_do_paras(FILE *fp
, paragraph
*p
, paragraph
*end
,
1203 int last_type
= -1, ptype
, first
=TRUE
;
1204 stack lcont_stack
= stk_new();
1208 /* for (; p && (xhtml_para_level(p)>limit || xhtml_para_level(p)==-1 || first); p=p->next) {*/
1209 for (; p
&& p
!= end
&& (xhtml_para_level(p
)==-1 || first
); p
=p
->next
) {
1211 switch (ptype
= p
->type
)
1214 * Things we ignore because we've already processed them or
1215 * aren't going to touch them in this pass.
1219 case para_Biblio
: /* only touch BiblioCited */
1220 case para_VersionID
:
1230 case para_UnnumberedChapter
:
1231 xhtml_heading(fp
, p
, indexable
);
1236 xhtml_heading(fp
, p
, indexable
);
1240 fprintf(fp
, "\n<hr />\n");
1244 case para_Copyright
:
1245 fprintf(fp
, "\n<p>");
1246 xhtml_para(fp
, p
->words
, indexable
);
1247 fprintf(fp
, "</p>\n");
1250 case para_LcontPush
:
1255 stk_push(lcont_stack
, p
);
1256 last_type
= para_Normal
;
1261 int *p
= stk_pop(lcont_stack
);
1263 ptype
= last_type
= *p
;
1265 goto closeofflist
; /* ick */
1268 case para_QuotePush
:
1269 fprintf(fp
, "<blockquote>\n");
1272 fprintf(fp
, "</blockquote>\n");
1276 case para_NumberedList
:
1277 case para_Description
:
1278 case para_DescribedThing
:
1279 case para_BiblioCited
:
1280 if (last_type
!=p
->type
&&
1281 !(last_type
==para_DescribedThing
&& p
->type
==para_Description
) &&
1282 !(last_type
==para_Description
&& p
->type
==para_DescribedThing
)) {
1283 /* start up list if necessary */
1284 if (p
->type
== para_Bullet
) {
1285 fprintf(fp
, "<ul>\n");
1286 } else if (p
->type
== para_NumberedList
) {
1287 fprintf(fp
, "<ol>\n");
1288 } else if (p
->type
== para_BiblioCited
||
1289 p
->type
== para_DescribedThing
||
1290 p
->type
== para_Description
) {
1291 fprintf(fp
, "<dl>\n");
1294 if (p
->type
== para_Bullet
|| p
->type
== para_NumberedList
) {
1295 fprintf(fp
, "<li>");
1296 } else if (p
->type
== para_DescribedThing
) {
1297 fprintf(fp
, "<dt>");
1298 } else if (p
->type
== para_Description
) {
1299 fprintf(fp
, "<dd>");
1300 } else if (p
->type
== para_BiblioCited
) {
1301 fprintf(fp
, "<dt>");
1302 xhtml_para(fp
, p
->kwtext
, indexable
);
1303 fprintf(fp
, "</dt>\n<dd>");
1305 xhtml_para(fp
, p
->words
, indexable
);
1307 paragraph
*p2
= p
->next
;
1308 if (p2
&& xhtml_para_level(p2
)==-1 && p2
->type
== para_LcontPush
)
1313 if (ptype
== para_BiblioCited
) {
1314 fprintf(fp
, "</dd>\n");
1315 } else if (ptype
== para_DescribedThing
) {
1316 fprintf(fp
, "</dt>");
1317 } else if (ptype
== para_Description
) {
1318 fprintf(fp
, "</dd>");
1319 } else if (ptype
== para_Bullet
|| ptype
== para_NumberedList
) {
1320 fprintf(fp
, "</li>");
1322 if (ptype
== para_Bullet
|| ptype
== para_NumberedList
||
1323 ptype
== para_BiblioCited
|| ptype
== para_Description
||
1324 ptype
== para_DescribedThing
)
1325 /* close off list if necessary */
1327 paragraph
*p2
= p
->next
;
1328 int close_off
=FALSE
;
1329 /* if (p2 && (xhtml_para_level(p2)>limit || xhtml_para_level(p2)==-1)) {*/
1330 if (p2
&& xhtml_para_level(p2
)==-1) {
1331 if (p2
->type
!= ptype
&&
1332 !(p2
->type
==para_DescribedThing
&& ptype
==para_Description
) &&
1333 !(p2
->type
==para_Description
&& ptype
==para_DescribedThing
) &&
1334 p2
->type
!= para_LcontPush
)
1340 if (ptype
== para_Bullet
) {
1341 fprintf(fp
, "</ul>\n");
1342 } else if (ptype
== para_NumberedList
) {
1343 fprintf(fp
, "</ol>\n");
1344 } else if (ptype
== para_BiblioCited
||
1345 ptype
== para_Description
||
1346 ptype
== para_DescribedThing
) {
1347 fprintf(fp
, "</dl>\n");
1354 xhtml_codepara(fp
, p
->words
);
1360 stk_free(lcont_stack
);
1364 * Output a header for this XHTML file.
1366 static void xhtml_doheader(FILE *fp
, word
*title
)
1368 fprintf(fp
, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
1369 fprintf(fp
, "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
1370 fprintf(fp
, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\n<head>\n<title>");
1372 fprintf(fp
, "The thing with no name!");
1374 xhtml_para(fp
, title
, FALSE
);
1375 fprintf(fp
, "</title>\n");
1376 fprintf(fp
, "<meta name=\"generator\" content=\"Halibut %s xhtml-backend\" />\n", version
);
1378 fprintf(fp
, "<meta name=\"author\" content=\"%ls\" />\n", conf
.author
);
1379 if (conf
.description
)
1380 fprintf(fp
, "<meta name=\"description\" content=\"%ls\" />\n", conf
.description
);
1382 fprintf(fp
, "%ls\n", conf
.head_end
);
1383 fprintf(fp
, "</head>\n\n");
1385 fprintf(fp
, "%ls\n", conf
.body
);
1387 fprintf(fp
, "<body>\n");
1388 if (conf
.body_start
)
1389 fprintf(fp
, "%ls\n", conf
.body_start
);
1393 * Output a footer for this XHTML file.
1395 static void xhtml_dofooter(FILE *fp
)
1397 fprintf(fp
, "\n<hr />\n\n");
1399 fprintf(fp
, "%ls\n", conf
.body_end
);
1400 if (!conf
.suppress_address
) {
1401 fprintf(fp
,"<address>\n");
1402 if (conf
.address_start
)
1403 fprintf(fp
, "%ls\n", conf
.address_start
);
1404 /* Do the version ID */
1405 if (conf
.include_version_id
) {
1408 for (p
= sourceparas
; p
; p
= p
->next
)
1409 if (p
->type
== para_VersionID
) {
1410 xhtml_versionid(fp
, p
->words
, started
);
1414 if (conf
.address_end
)
1415 fprintf(fp
, "%ls\n", conf
.address_end
);
1416 fprintf(fp
, "</address>\n");
1418 fprintf(fp
, "</body>\n\n</html>\n");
1422 * Output the versionid paragraph. Typically this is a version control
1423 * ID string (such as $Id...$ in RCS).
1425 static void xhtml_versionid(FILE *fp
, word
*text
, int started
)
1427 rdstringc t
= { 0, 0, NULL
};
1429 rdaddc(&t
, '['); /* FIXME: configurability */
1430 xhtml_rdaddwc(&t
, text
, NULL
, FALSE
);
1431 rdaddc(&t
, ']'); /* FIXME: configurability */
1434 fprintf(fp
, "<br />\n");
1435 fprintf(fp
, "%s\n", t
.text
);
1439 /* Is this an XHTML reserved character? */
1440 static int xhtml_reservedchar(int c
)
1442 if (c
=='&' || c
=='<' || c
=='>' || c
=='"')
1449 * Convert a wide string into valid XHTML: Anything outside ASCII will
1450 * be fixed up as an entity. Currently we don't worry about constraining the
1451 * encoded character set, which we should probably do at some point (we can
1452 * still fix up and return FALSE - see the last comment here). We also don't
1455 * Because this is only used for words, spaces are HARD spaces (any other
1456 * spaces will be word_Whitespace not word_Normal). So they become
1457 * Unless hard_spaces is FALSE, of course (code paragraphs break the above
1460 * If `result' is non-NULL, mallocs the resulting string and stores a pointer to
1461 * it in `*result'. If `result' is NULL, merely checks whether all
1462 * characters in the string are feasible.
1464 * Return is nonzero if all characters are OK. If not all
1465 * characters are OK but `result' is non-NULL, a result _will_
1466 * still be generated!
1468 static int xhtml_convert(wchar_t *s
, int maxlen
, char **result
,
1470 int doing
= (result
!= 0);
1473 int plen
= 0, psize
= 0;
1478 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
1481 #define ensure_size(i) if (i>=psize) { psize = i+256; p = resize(p, psize); }
1483 if (((c
== 32 && !hard_spaces
) || (c
> 32 && c
<= 126 && !xhtml_reservedchar(c
)))) {
1488 p
[plen
++] = (char)c
;
1491 /* Char needs fixing up. */
1492 /* ok = FALSE; -- currently we never return FALSE; we
1493 * might want to when considering a character set for the
1498 if (c
==32) { /* a space in a word is a hard space */
1499 ensure_size(plen
+6); /* includes space for the NUL, which is subsequently stomped on */
1500 sprintf(p
+plen
, " ");
1503 /* FIXME: entity names! */
1504 ensure_size(plen
+8); /* includes space for the NUL, which is subsequently stomped on */
1505 plen
+=sprintf(p
+plen
, "&#%04i;", (int)c
);
1511 p
= resize(p
, plen
+1);
1519 * This formats the given words as XHTML.
1521 * `indexable', if FALSE, prohibits adding any index references.
1522 * You might use this, for example, if an index reference occurred
1523 * in a section title, to prevent phony index references when the
1524 * section title is processed in strange places such as contents
1527 static void xhtml_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
, int indexable
) {
1534 for (; text
&& text
!= end
; text
= text
->next
) {
1535 switch (text
->type
) {
1536 case word_HyperLink
:
1537 xhtml_utostr(text
->text
, &c
);
1538 rdaddsc(rs
, "<a href=\"");
1544 case word_UpperXref
:
1545 case word_LowerXref
:
1546 kwl
= kw_lookup(keywords
, text
->text
);
1548 sect
=xhtml_find_section(kwl
->para
);
1550 rdaddsc(rs
, "<a href=\"");
1551 rdaddsc(rs
, sect
->file
->filename
);
1553 rdaddsc(rs
, sect
->fragment
);
1556 rdaddsc(rs
, "<a href=\"Apologies.html\"><!-- probably a bibliography cross reference -->");
1557 error(err_whatever
, "Couldn't locate cross-reference! (Probably a bibliography entry.)");
1560 rdaddsc(rs
, "<a href=\"Apologies.html\"><!-- unknown cross-reference -->");
1561 error(err_whatever
, "Couldn't locate cross-reference! (Wasn't in source file.)");
1565 case word_IndexRef
: /* in theory we could make an index target here */
1566 /* rdaddsc(rs, "<a name=\"idx-");
1567 xhtml_utostr(text->text, &c);
1570 rdaddsc(rs, "\"></a>");*/
1571 /* what we _do_ need to do is to fix up the backend data
1572 * for any indexentry this points to.
1577 for (ti
=0; (itag
= (indextag
*)index234(idx
->tags
, ti
))!=NULL
; ti
++) {
1578 /* FIXME: really ustricmp() and not ustrcmp()? */
1579 if (ustricmp(itag
->name
, text
->text
)==0) {
1584 if (itag
->refs
!=NULL
) {
1586 for (i
=0; i
<itag
->nrefs
; i
++) {
1587 xhtmlindex
*idx_ref
;
1590 ientry
= itag
->refs
[i
];
1591 if (ientry
->backend_data
==NULL
) {
1592 idx_ref
= (xhtmlindex
*) smalloc(sizeof(xhtmlindex
));
1594 fatal(err_nomemory
);
1595 idx_ref
->nsection
= 0;
1597 idx_ref
->sections
= (xhtmlsection
**) smalloc(idx_ref
->size
* sizeof(xhtmlsection
*));
1598 if (idx_ref
->sections
==NULL
)
1599 fatal(err_nomemory
);
1600 ientry
->backend_data
= idx_ref
;
1602 idx_ref
= ientry
->backend_data
;
1603 if (idx_ref
->nsection
+1 > idx_ref
->size
) {
1604 int new_size
= idx_ref
->size
* 2;
1605 idx_ref
->sections
= srealloc(idx_ref
->sections
, new_size
* sizeof(xhtmlsection
));
1606 if (idx_ref
->sections
==NULL
) {
1607 fatal(err_nomemory
);
1609 idx_ref
->size
= new_size
;
1612 idx_ref
->sections
[idx_ref
->nsection
++] = currentsection
;
1617 fatal(err_whatever
, "Index tag had no entries!");
1620 fprintf(stderr
, "Looking for index entry '%ls'\n", text
->text
);
1621 fatal(err_whatever
, "Couldn't locate index entry! (Wasn't in index.)");
1627 rdaddsc(rs
, "</a>");
1634 case word_WhiteSpace
:
1635 case word_EmphSpace
:
1636 case word_CodeSpace
:
1637 case word_WkCodeSpace
:
1639 case word_EmphQuote
:
1640 case word_CodeQuote
:
1641 case word_WkCodeQuote
:
1642 assert(text
->type
!= word_CodeQuote
&&
1643 text
->type
!= word_WkCodeQuote
);
1644 if (towordstyle(text
->type
) == word_Emph
&&
1645 (attraux(text
->aux
) == attr_First
||
1646 attraux(text
->aux
) == attr_Only
))
1647 rdaddsc(rs
, "<em>");
1648 else if ((towordstyle(text
->type
) == word_Code
|| towordstyle(text
->type
) == word_WeakCode
) &&
1649 (attraux(text
->aux
) == attr_First
||
1650 attraux(text
->aux
) == attr_Only
))
1651 rdaddsc(rs
, "<code>");
1653 if (removeattr(text
->type
) == word_Normal
) {
1654 if (xhtml_convert(text
->text
, 0, &c
, TRUE
)) /* spaces in the word are hard */
1657 xhtml_rdaddwc(rs
, text
->alt
, NULL
, indexable
);
1659 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
1661 } else if (removeattr(text
->type
) == word_Quote
) {
1662 rdaddsc(rs
, """);
1665 if (towordstyle(text
->type
) == word_Emph
&&
1666 (attraux(text
->aux
) == attr_Last
||
1667 attraux(text
->aux
) == attr_Only
))
1668 rdaddsc(rs
, "</em>");
1669 else if ((towordstyle(text
->type
) == word_Code
|| towordstyle(text
->type
) == word_WeakCode
) &&
1670 (attraux(text
->aux
) == attr_Last
||
1671 attraux(text
->aux
) == attr_Only
))
1672 rdaddsc(rs
, "</code>");
1678 /* Output a heading, formatted as XHTML.
1680 static void xhtml_heading(FILE *fp
, paragraph
*p
, int indexable
)
1682 rdstringc t
= { 0, 0, NULL
};
1683 word
*tprefix
= p
->kwtext
;
1684 word
*nprefix
= p
->kwtext2
;
1685 word
*text
= p
->words
;
1686 int level
= xhtml_para_level(p
);
1687 xhtmlsection
*sect
= xhtml_find_section(p
);
1691 fragment
= sect
->fragment
;
1693 if (p
->type
== para_Title
)
1696 fragment
= ""; /* FIXME: what else can we do? */
1697 error(err_whatever
, "Couldn't locate heading cross-reference!");
1701 if (p
->type
== para_Title
)
1703 else if (level
== 1)
1704 fmt
= &conf
.fchapter
;
1705 else if (level
-1 < conf
.nfsect
)
1706 fmt
= &conf
.fsect
[level
-1];
1708 fmt
= &conf
.fsect
[conf
.nfsect
-1];
1710 if (fmt
&& fmt
->just_numbers
&& nprefix
) {
1711 xhtml_rdaddwc(&t
, nprefix
, NULL
, indexable
);
1714 if (xhtml_convert(fmt
->number_suffix
, 0, &c
, FALSE
)) {
1719 } else if (fmt
&& !fmt
->just_numbers
&& tprefix
) {
1720 xhtml_rdaddwc(&t
, tprefix
, NULL
, indexable
);
1723 if (xhtml_convert(fmt
->number_suffix
, 0, &c
, FALSE
)) {
1729 xhtml_rdaddwc(&t
, text
, NULL
, indexable
);
1731 * If we're outputting in single-file mode, we need to lower
1732 * the level of each heading by one, because the overall
1733 * document title will be sitting right at the top as an <h1>
1734 * and so chapters and sections should start at <h2>.
1736 * Even if not, the document title will come back from
1737 * xhtml_para_level() as level zero, so we must increment that
1738 * no matter what leaf_level is set to.
1740 if (conf
.leaf_level
== 0 || level
== 0)
1742 fprintf(fp
, "<a name=\"%s\"></a><h%i>%s</h%i>\n", fragment
, level
, t
.text
, level
);
1746 /* Output a paragraph. Styles are handled by xhtml_rdaddwc().
1747 * This looks pretty simple; I may have missed something ...
1749 static void xhtml_para(FILE *fp
, word
*text
, int indexable
)
1751 rdstringc out
= { 0, 0, NULL
};
1752 xhtml_rdaddwc(&out
, text
, NULL
, indexable
);
1753 fprintf(fp
, "%s", out
.text
);
1757 /* Output a code paragraph. I'm treating this as preformatted, which
1758 * may not be entirely correct. See xhtml_para() for my worries about
1759 * this being overly-simple; however I think that most of the complexity
1760 * of the text backend came entirely out of word wrapping anyway.
1762 static void xhtml_codepara(FILE *fp
, word
*text
)
1764 fprintf(fp
, "<pre>");
1765 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
1770 * See if this WeakCode is followed by an Emph to indicate
1774 if (text
->next
&& text
->next
->type
== word_Emph
) {
1775 next
= text
= text
->next
;
1789 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
1790 xhtml_convert(t
, n
, &c
, FALSE
);
1791 fprintf(fp
, "%s%s%s",
1792 (ec
== 'i' ?
"<em>" : ec
== 'b' ?
"<b>" : ""),
1794 (ec
== 'i' ?
"</em>" : ec
== 'b' ?
"</b>" : ""));
1801 xhtml_convert(t
, 0, &c
, FALSE
);
1802 fprintf(fp
, "%s\n", c
);
1805 xhtml_convert(here
->text
, 0, &c
, FALSE
);
1806 fprintf(fp
, "%s\n", c
);
1810 fprintf(fp
, "</pre>\n");