| 1 | /* |
| 2 | * xhtml backend for Halibut |
| 3 | * (initial implementation by James Aylett) |
| 4 | * |
| 5 | * Still to do: |
| 6 | * |
| 7 | * +++ doesn't handle non-breaking hyphens. Not sure how to yet. |
| 8 | * +++ entity names (from a file -- ideally supply normal SGML files) |
| 9 | * +++ configuration directive to file split where the current layout |
| 10 | * code wouldn't. Needs changes to _ponder_layout() and _do_paras(), |
| 11 | * perhaps others. |
| 12 | * |
| 13 | * Limitations: |
| 14 | * |
| 15 | * +++ biblio/index references target the nearest section marker, rather |
| 16 | * than having a dedicated target themselves. In large bibliographies |
| 17 | * this will cause problems. (The solution is to fake up a response |
| 18 | * from xhtml_find_section(), probably linking it into the sections |
| 19 | * chain just in case we need it again, and to make freeing it up |
| 20 | * easier.) docsrc.pl used to work as we do, however, and SGT agrees that |
| 21 | * this is acceptable for now. |
| 22 | * +++ can't cope with leaf-level == 0. It's all to do with the |
| 23 | * top-level file not being normal, probably not even having a valid |
| 24 | * section level, and stuff like that. I question whether this is an |
| 25 | * issue, frankly; small manuals that fit on one page should probably |
| 26 | * not be written in halibut at all. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <assert.h> |
| 33 | #include "halibut.h" |
| 34 | |
| 35 | 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 */ |
| 40 | paragraph *para; |
| 41 | struct xhtmlfile_Struct *file; /* which file is this a part of? */ |
| 42 | char *fragment; /* fragment id within the file */ |
| 43 | int level; |
| 44 | }; |
| 45 | |
| 46 | struct xhtmlfile_Struct { |
| 47 | struct xhtmlfile_Struct *next; |
| 48 | struct xhtmlfile_Struct *child; |
| 49 | struct xhtmlfile_Struct *parent; |
| 50 | char *filename; |
| 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? */ |
| 53 | }; |
| 54 | |
| 55 | typedef struct xhtmlsection_Struct xhtmlsection; |
| 56 | typedef struct xhtmlfile_Struct xhtmlfile; |
| 57 | typedef struct xhtmlindex_Struct xhtmlindex; |
| 58 | |
| 59 | struct xhtmlindex_Struct { |
| 60 | int nsection; |
| 61 | int size; |
| 62 | xhtmlsection **sections; |
| 63 | }; |
| 64 | |
| 65 | typedef struct { |
| 66 | int just_numbers; |
| 67 | wchar_t *number_suffix; |
| 68 | } xhtmlheadfmt; |
| 69 | |
| 70 | typedef struct { |
| 71 | int contents_depth[6]; |
| 72 | int leaf_contains_contents; |
| 73 | int leaf_level; |
| 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; |
| 78 | int suppress_address; |
| 79 | xhtmlheadfmt fchapter, *fsect; |
| 80 | int nfsect; |
| 81 | } xhtmlconfig; |
| 82 | |
| 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);*/ |
| 88 | |
| 89 | static void xhtml_doheader(FILE *, word *); |
| 90 | static void xhtml_dofooter(FILE *); |
| 91 | static void xhtml_versionid(FILE *, word *, int); |
| 92 | |
| 93 | static void xhtml_utostr(wchar_t *, char **); |
| 94 | static int xhtml_para_level(paragraph *); |
| 95 | static int xhtml_reservedchar(int); |
| 96 | |
| 97 | static int xhtml_convert(wchar_t *, int, char **, int); |
| 98 | static void xhtml_rdaddwc(rdstringc *, word *, word *); |
| 99 | static void xhtml_para(FILE *, word *); |
| 100 | static void xhtml_codepara(FILE *, word *); |
| 101 | static void xhtml_heading(FILE *, paragraph *); |
| 102 | |
| 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. |
| 106 | */ |
| 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; |
| 117 | |
| 118 | static xhtmlconfig xhtml_configure(paragraph *source) |
| 119 | { |
| 120 | xhtmlconfig ret; |
| 121 | |
| 122 | /* |
| 123 | * Defaults. |
| 124 | */ |
| 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; |
| 131 | ret.leaf_level = 2; |
| 132 | ret.leaf_smallest_contents = 4; |
| 133 | ret.leaf_contains_contents = FALSE; |
| 134 | ret.include_version_id = TRUE; |
| 135 | ret.author = NULL; |
| 136 | ret.description = NULL; |
| 137 | ret.head_end = NULL; |
| 138 | ret.body = NULL; |
| 139 | ret.body_start = NULL; |
| 140 | ret.body_end = NULL; |
| 141 | ret.address_start = NULL; |
| 142 | ret.address_end = NULL; |
| 143 | ret.nav_attrs = NULL; |
| 144 | ret.suppress_address = FALSE; |
| 145 | |
| 146 | ret.fchapter.just_numbers = FALSE; |
| 147 | ret.fchapter.number_suffix = ustrdup(L": "); |
| 148 | ret.nfsect = 2; |
| 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" "); |
| 154 | |
| 155 | for (; source; source = source->next) |
| 156 | { |
| 157 | if (source->type == para_Config) |
| 158 | { |
| 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); |
| 205 | int n = 0; |
| 206 | if (uisdigit(*p)) { |
| 207 | n = utoi(p); |
| 208 | p = uadv(p); |
| 209 | } |
| 210 | if (n >= ret.nfsect) { |
| 211 | int i; |
| 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]; |
| 215 | ret.nfsect = n+1; |
| 216 | } |
| 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); |
| 220 | int n = 0; |
| 221 | if (uisdigit(*p)) { |
| 222 | n = utoi(p); |
| 223 | p = uadv(p); |
| 224 | } |
| 225 | if (n >= ret.nfsect) { |
| 226 | int i; |
| 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]; |
| 230 | ret.nfsect = n+1; |
| 231 | } |
| 232 | ret.fsect[n].number_suffix = ustrdup(p); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 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);*/ |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static xhtmlsection *xhtml_new_section(xhtmlsection *last) |
| 249 | { |
| 250 | xhtmlsection *ret = mknew(xhtmlsection); |
| 251 | ret->next=NULL; |
| 252 | ret->child=NULL; |
| 253 | ret->parent=NULL; |
| 254 | ret->chain=last; |
| 255 | ret->para=NULL; |
| 256 | ret->file=NULL; |
| 257 | ret->fragment=NULL; |
| 258 | ret->level=-1; /* marker: end of chain */ |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | /* Returns NULL or the section that marks that paragraph */ |
| 263 | static xhtmlsection *xhtml_find_section(paragraph *p) |
| 264 | { |
| 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) { |
| 271 | p3 = p2; |
| 272 | } |
| 273 | p2=p2->next; |
| 274 | } |
| 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. |
| 278 | */ |
| 279 | return NULL; |
| 280 | } |
| 281 | p=p3; |
| 282 | } |
| 283 | while (ret && ret->para != p) { |
| 284 | /* printf(" xhtml_find_section(): checking %s for para @ %p\n", ret->fragment, p);*/ |
| 285 | ret=ret->chain; |
| 286 | } |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static xhtmlfile *xhtml_new_file(xhtmlsection *sect) |
| 291 | { |
| 292 | xhtmlfile *ret = mknew(xhtmlfile); |
| 293 | |
| 294 | ret->next=NULL; |
| 295 | ret->child=NULL; |
| 296 | ret->parent=NULL; |
| 297 | ret->filename=NULL; |
| 298 | ret->sections=sect; |
| 299 | ret->is_leaf=(sect!=NULL && sect->level==conf.leaf_level); |
| 300 | if (sect==NULL) { |
| 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); |
| 306 | } else { |
| 307 | ret->filename = smalloc(strlen(FILENAME_CONTENTS)+1); |
| 308 | sprintf(ret->filename, FILENAME_CONTENTS); |
| 309 | } |
| 310 | } else { |
| 311 | paragraph *p = sect->para; |
| 312 | rdstringc fname_c = { 0, 0, NULL }; |
| 313 | char *c; |
| 314 | word *w; |
| 315 | for (w=(p->kwtext)?(p->kwtext):(p->words); w; w=w->next) |
| 316 | { |
| 317 | switch (removeattr(w->type)) |
| 318 | { |
| 319 | case word_Normal: |
| 320 | /*case word_Emph: |
| 321 | case word_Code: |
| 322 | case word_WeakCode:*/ |
| 323 | xhtml_utostr(w->text, &c); |
| 324 | rdaddsc(&fname_c,c); |
| 325 | sfree(c); |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | rdaddsc(&fname_c, ".html"); |
| 330 | ret->filename = rdtrimc(&fname_c); |
| 331 | } |
| 332 | /* printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false"));*/ |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /* |
| 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 |
| 339 | * leaf flag set. |
| 340 | */ |
| 341 | void xhtml_fixup_layout(xhtmlfile* file) |
| 342 | { |
| 343 | if (file->child==NULL) { |
| 344 | file->is_leaf = TRUE; |
| 345 | } else { |
| 346 | xhtml_fixup_layout(file->child); |
| 347 | } |
| 348 | if (file->next) |
| 349 | xhtml_fixup_layout(file->next); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Create the tree structure so we know where everything goes. |
| 354 | * Method: |
| 355 | * |
| 356 | * Ignoring file splitting, we have three choices with each new section: |
| 357 | * |
| 358 | * +-----------------+-----------------+ |
| 359 | * | | | |
| 360 | * X +----X----+ (1) |
| 361 | * | | |
| 362 | * Y (2) |
| 363 | * | |
| 364 | * (3) |
| 365 | * |
| 366 | * Y is the last section we added (currentsect). |
| 367 | * If sect is the section we want to add, then: |
| 368 | * |
| 369 | * (1) if sect->level < currentsect->level |
| 370 | * (2) if sect->level == currentsect->level |
| 371 | * (3) if sect->level > currentsect->level |
| 372 | * |
| 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). |
| 375 | * |
| 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.) |
| 380 | * |
| 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: |
| 384 | * |
| 385 | * +-----------------+-----------------+ |
| 386 | * | | | |
| 387 | * X +----X----+ (1) |
| 388 | * | | |
| 389 | * +----Y----+ (1) |
| 390 | * | | |
| 391 | * Z (2) |
| 392 | * | |
| 393 | * (3) |
| 394 | * |
| 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). |
| 398 | * |
| 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 |
| 402 | * value or not). |
| 403 | * |
| 404 | * Treating the cases backwards: |
| 405 | * |
| 406 | * (3) same file if sect->level > conf.leaf_level, otherwise new file |
| 407 | * |
| 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 |
| 411 | * the new file) |
| 412 | * |
| 413 | * (2) same file if sect->level > conf.leaf_level, otherwise new file |
| 414 | * |
| 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) |
| 418 | * |
| 419 | * (1) same file if sect->level > conf.leaf_level, otherwise new file |
| 420 | * |
| 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. |
| 426 | * |
| 427 | */ |
| 428 | static void xhtml_ponder_layout(paragraph *p) |
| 429 | { |
| 430 | xhtmlsection *lastsection; |
| 431 | xhtmlsection *currentsect; |
| 432 | xhtmlfile *currentfile; |
| 433 | |
| 434 | lastfile = NULL; |
| 435 | topsection = xhtml_new_section(NULL); |
| 436 | topfile = xhtml_new_file(NULL); |
| 437 | lastsection = topsection; |
| 438 | currentfile = topfile; |
| 439 | currentsect = topsection; |
| 440 | |
| 441 | if (conf.leaf_level == 0) { |
| 442 | topfile->is_leaf = 1; |
| 443 | topfile->sections = topsection; |
| 444 | topsection->file = topfile; |
| 445 | } |
| 446 | |
| 447 | for (; p; p=p->next) |
| 448 | { |
| 449 | int level = xhtml_para_level(p); |
| 450 | if (level>0) /* actually a section */ |
| 451 | { |
| 452 | xhtmlsection *sect; |
| 453 | word *w; |
| 454 | char *c; |
| 455 | rdstringc fname_c = { 0, 0, NULL }; |
| 456 | |
| 457 | sect = xhtml_new_section(lastsection); |
| 458 | lastsection = sect; |
| 459 | sect->para = p; |
| 460 | for (w=(p->kwtext2)?(p->kwtext2):(p->words); w; w=w->next) /* kwtext2 because we want numbers only! */ |
| 461 | { |
| 462 | switch (removeattr(w->type)) |
| 463 | { |
| 464 | case word_Normal: |
| 465 | /*case word_Emph: |
| 466 | case word_Code: |
| 467 | case word_WeakCode:*/ |
| 468 | xhtml_utostr(w->text, &c); |
| 469 | rdaddsc(&fname_c,c); |
| 470 | sfree(c); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | /* rdaddsc(&fname_c, ".html");*/ |
| 475 | sect->fragment = rdtrimc(&fname_c); |
| 476 | sect->level = level; |
| 477 | /* printf(" ! adding para @ %p as sect %s, level %i\n", sect->para, sect->fragment, level);*/ |
| 478 | |
| 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);*/ |
| 486 | currentsect=sect; |
| 487 | } else { /* new file */ |
| 488 | xhtmlfile *file = xhtml_new_file(sect); |
| 489 | assert(!currentfile->is_leaf); |
| 490 | currentfile->child=file; |
| 491 | sect->file=file; |
| 492 | file->parent=currentfile; |
| 493 | /* printf("connected '%s' to new file '%s' [I]\n", sect->fragment, file->filename);*/ |
| 494 | currentfile=file; |
| 495 | currentsect=sect; |
| 496 | } |
| 497 | } else if (level >= currentsect->file->sections->level) { |
| 498 | /* Case (1) or (2) *AND* still under the section that starts |
| 499 | * the current file. |
| 500 | * |
| 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 |
| 503 | * this way. |
| 504 | */ |
| 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; |
| 511 | } |
| 512 | assert(currentsect); |
| 513 | currentsect->next = sect; |
| 514 | assert(currentsect->level == sect->level); |
| 515 | sect->parent = currentsect->parent; |
| 516 | currentsect = sect; |
| 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); |
| 520 | sect->file=file; |
| 521 | currentfile->next=file; |
| 522 | file->parent=currentfile->parent; |
| 523 | file->is_leaf=(level==conf.leaf_level); |
| 524 | file->sections=sect; |
| 525 | /* printf("connected '%s' to new file '%s' [II]\n", sect->fragment, file->filename);*/ |
| 526 | currentfile=file; |
| 527 | currentsect=sect; |
| 528 | } |
| 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; |
| 534 | if (currentsect) { |
| 535 | /* printf(" * up one level to '%s'\n", currentsect->fragment);*/ |
| 536 | } else { |
| 537 | /* printf(" * up one level (off top of current file)\n");*/ |
| 538 | } |
| 539 | } |
| 540 | if (currentsect) { |
| 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; |
| 546 | currentsect=sect; |
| 547 | } else { /* find a file we can attach to */ |
| 548 | while (currentfile && currentfile->sections && level<currentfile->sections->level) { |
| 549 | currentfile=currentfile->parent; |
| 550 | if (currentfile) { |
| 551 | /* printf(" * up one file level to '%s'\n", currentfile->filename);*/ |
| 552 | } else { |
| 553 | /* printf(" * up one file level (off top of tree)\n");*/ |
| 554 | } |
| 555 | } |
| 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 |
| 559 | leaf_level */ |
| 560 | xhtmlfile *file = xhtml_new_file(sect); |
| 561 | currentfile->next=file; |
| 562 | sect->file=file; |
| 563 | file->parent=currentfile->parent; |
| 564 | file->is_leaf=(level==conf.leaf_level); |
| 565 | file->sections=sect; |
| 566 | /* printf("connected '%s' to new file '%s' [III]\n", sect->fragment, file->filename);*/ |
| 567 | currentfile=file; |
| 568 | currentsect=sect; |
| 569 | } else { |
| 570 | fatal(err_whatever, "Ran off the top trying to connect sibling: strange document."); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | topsection = lastsection; /* get correct end of the chain */ |
| 577 | xhtml_fixup_layout(topfile); /* leaf files not at leaf level marked as such */ |
| 578 | } |
| 579 | |
| 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); |
| 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); |
| 590 | |
| 591 | /* |
| 592 | * Do all the files in this structure. |
| 593 | */ |
| 594 | static void xhtml_do_files(xhtmlfile *file) |
| 595 | { |
| 596 | xhtml_do_file(file); |
| 597 | if (file->child) |
| 598 | xhtml_do_files(file->child); |
| 599 | if (file->next) |
| 600 | xhtml_do_files(file->next); |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Free up all memory used by the file tree from 'xfile' downwards |
| 605 | */ |
| 606 | static void xhtml_free_file(xhtmlfile* xfile) |
| 607 | { |
| 608 | if (xfile==NULL) { |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | if (xfile->filename) { |
| 613 | sfree(xfile->filename); |
| 614 | } |
| 615 | xhtml_free_file(xfile->child); |
| 616 | xhtml_free_file(xfile->next); |
| 617 | sfree(xfile); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Main function. |
| 622 | */ |
| 623 | void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords, |
| 624 | indexdata *in_idx) |
| 625 | { |
| 626 | /* int i;*/ |
| 627 | indexentry *ientry; |
| 628 | int ti; |
| 629 | xhtmlsection *xsect; |
| 630 | |
| 631 | sourceparas = sourceform; |
| 632 | conf = xhtml_configure(sourceform); |
| 633 | keywords = in_keywords; |
| 634 | idx = in_idx; |
| 635 | |
| 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; |
| 639 | } |
| 640 | |
| 641 | xhtml_ponder_layout(sourceform); |
| 642 | |
| 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++) |
| 646 | { |
| 647 | xhtml_level(sourceform, i); |
| 648 | }*/ |
| 649 | |
| 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); |
| 655 | xhtml_do_index(); |
| 656 | } |
| 657 | |
| 658 | /* release file, section, index data structures */ |
| 659 | xsect = topsection; |
| 660 | while (xsect) { |
| 661 | xhtmlsection *tmp = xsect->chain; |
| 662 | if (xsect->fragment) { |
| 663 | sfree(xsect->fragment); |
| 664 | } |
| 665 | sfree(xsect); |
| 666 | xsect = tmp; |
| 667 | } |
| 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) { |
| 673 | sfree(xi->sections); |
| 674 | } |
| 675 | sfree(xi); |
| 676 | } |
| 677 | ientry->backend_data = NULL; |
| 678 | } |
| 679 | { |
| 680 | int i; |
| 681 | sfree(conf.fchapter.number_suffix); |
| 682 | for (i = 0; i < conf.nfsect; i++) |
| 683 | sfree(conf.fsect[i].number_suffix); |
| 684 | sfree(conf.fsect); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | static int xhtml_para_level(paragraph *p) |
| 689 | { |
| 690 | switch (p->type) |
| 691 | { |
| 692 | case para_Title: |
| 693 | return 0; |
| 694 | break; |
| 695 | case para_UnnumberedChapter: |
| 696 | case para_Chapter: |
| 697 | case para_Appendix: |
| 698 | return 1; |
| 699 | break; |
| 700 | /* case para_BiblioCited: |
| 701 | return 2; |
| 702 | break;*/ |
| 703 | case para_Heading: |
| 704 | case para_Subsect: |
| 705 | return p->aux+2; |
| 706 | break; |
| 707 | default: |
| 708 | return -1; |
| 709 | break; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | static char* xhtml_index_filename = "IndexPage.html"; |
| 714 | |
| 715 | /* Output the nav links for the current file. |
| 716 | * file == NULL means we're doing the index |
| 717 | */ |
| 718 | static void xhtml_donavlinks(FILE *fp, xhtmlfile *file) |
| 719 | { |
| 720 | xhtmlfile *xhtml_next_file = NULL; |
| 721 | fprintf(fp, "<p"); |
| 722 | if (conf.nav_attrs!=NULL) { |
| 723 | fprintf(fp, " %ls>", conf.nav_attrs); |
| 724 | } else { |
| 725 | fprintf(fp, ">"); |
| 726 | } |
| 727 | if (xhtml_last_file==NULL) { |
| 728 | fprintf(fp, "Previous | "); |
| 729 | } else { |
| 730 | fprintf(fp, "<a href='%s'>Previous</a> | ", xhtml_last_file->filename); |
| 731 | } |
| 732 | fprintf(fp, "<a href='Contents.html'>Contents</a> | "); |
| 733 | if (file != NULL) { /* otherwise we're doing nav links for the index */ |
| 734 | if (xhtml_next_file==NULL) |
| 735 | xhtml_next_file = file->child; |
| 736 | if (xhtml_next_file==NULL) |
| 737 | xhtml_next_file = file->next; |
| 738 | if (xhtml_next_file==NULL) |
| 739 | xhtml_next_file = file->parent->next; |
| 740 | } |
| 741 | if (xhtml_next_file==NULL) { |
| 742 | if (file==NULL) { /* index, so no next file */ |
| 743 | fprintf(fp, "Next "); |
| 744 | } else { |
| 745 | fprintf(fp, "<a href='%s'>Next</a>", xhtml_index_filename); |
| 746 | } |
| 747 | } else { |
| 748 | fprintf(fp, "<a href='%s'>Next</a>", xhtml_next_file->filename); |
| 749 | } |
| 750 | fprintf(fp, "</p>\n"); |
| 751 | } |
| 752 | |
| 753 | /* Write out the index file */ |
| 754 | static void xhtml_do_index_body(FILE *fp) |
| 755 | { |
| 756 | indexentry *y; |
| 757 | int ti; |
| 758 | |
| 759 | if (count234(idx->entries) == 0) |
| 760 | return; /* don't write anything at all */ |
| 761 | |
| 762 | fprintf(fp, "<dl>\n"); |
| 763 | /* iterate over idx->entries using the tree functions and display everything */ |
| 764 | for (ti = 0; (y = (indexentry *)index234(idx->entries, ti)) != NULL; ti++) { |
| 765 | if (y->backend_data) { |
| 766 | int i; |
| 767 | xhtmlindex *xi; |
| 768 | |
| 769 | fprintf(fp, "<dt>"); |
| 770 | xhtml_para(fp, y->text); |
| 771 | fprintf(fp, "</dt>\n<dd>"); |
| 772 | |
| 773 | xi = (xhtmlindex*) y->backend_data; |
| 774 | for (i=0; i<xi->nsection; i++) { |
| 775 | xhtmlsection *sect = xi->sections[i]; |
| 776 | if (sect) { |
| 777 | fprintf(fp, "<a href='%s#%s'>", sect->file->filename, sect->fragment); |
| 778 | if (sect->para->kwtext) { |
| 779 | xhtml_para(fp, sect->para->kwtext); |
| 780 | } else if (sect->para->words) { |
| 781 | xhtml_para(fp, sect->para->words); |
| 782 | } |
| 783 | fprintf(fp, "</a>"); |
| 784 | if (i+1<xi->nsection) { |
| 785 | fprintf(fp, ", "); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | fprintf(fp, "</dd>\n"); |
| 790 | } |
| 791 | } |
| 792 | fprintf(fp, "</dl>\n"); |
| 793 | } |
| 794 | static void xhtml_do_index() |
| 795 | { |
| 796 | word temp_word = { NULL, NULL, word_Normal, 0, 0, L"Index", { NULL, 0, 0} }; |
| 797 | FILE *fp = fopen(xhtml_index_filename, "w"); |
| 798 | |
| 799 | if (fp==NULL) |
| 800 | fatal(err_cantopenw, xhtml_index_filename); |
| 801 | xhtml_doheader(fp, &temp_word); |
| 802 | xhtml_donavlinks(fp, NULL); |
| 803 | |
| 804 | xhtml_do_index_body(fp); |
| 805 | |
| 806 | xhtml_donavlinks(fp, NULL); |
| 807 | xhtml_dofooter(fp); |
| 808 | fclose(fp); |
| 809 | } |
| 810 | |
| 811 | /* Output the given file. This includes whatever contents at beginning and end, etc. etc. */ |
| 812 | static void xhtml_do_file(xhtmlfile *file) |
| 813 | { |
| 814 | FILE *fp = fopen(file->filename, "w"); |
| 815 | if (fp==NULL) |
| 816 | fatal(err_cantopenw, file->filename); |
| 817 | |
| 818 | if (file->sections->para->words) { |
| 819 | xhtml_doheader(fp, file->sections->para->words); |
| 820 | } else if (file->sections->para->kwtext) { |
| 821 | xhtml_doheader(fp, file->sections->para->kwtext); |
| 822 | } else { |
| 823 | xhtml_doheader(fp, NULL); |
| 824 | } |
| 825 | |
| 826 | xhtml_donavlinks(fp, file); |
| 827 | |
| 828 | if (file->is_leaf && conf.leaf_contains_contents && |
| 829 | xhtml_do_contents(NULL, file)>=conf.leaf_smallest_contents) |
| 830 | xhtml_do_contents(fp, file); |
| 831 | xhtml_do_sections(fp, file->sections); |
| 832 | if (!file->is_leaf) |
| 833 | xhtml_do_naked_contents(fp, file); |
| 834 | |
| 835 | xhtml_donavlinks(fp, file); |
| 836 | |
| 837 | xhtml_dofooter(fp); |
| 838 | fclose(fp); |
| 839 | |
| 840 | xhtml_last_file = file; |
| 841 | } |
| 842 | |
| 843 | /* Output the top-level file. */ |
| 844 | static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform) |
| 845 | { |
| 846 | paragraph *p; |
| 847 | int done=FALSE; |
| 848 | FILE *fp = fopen(file->filename, "w"); |
| 849 | if (fp==NULL) |
| 850 | fatal(err_cantopenw, file->filename); |
| 851 | |
| 852 | /* Do the title -- only one allowed */ |
| 853 | for (p = sourceform; p && !done; p = p->next) |
| 854 | { |
| 855 | if (p->type == para_Title) |
| 856 | { |
| 857 | xhtml_doheader(fp, p->words); |
| 858 | done=TRUE; |
| 859 | } |
| 860 | } |
| 861 | if (!done) |
| 862 | xhtml_doheader(fp, NULL /* Eek! */); |
| 863 | |
| 864 | /* |
| 865 | * Display the title. |
| 866 | */ |
| 867 | for (p = sourceform; p; p = p->next) |
| 868 | { |
| 869 | if (p->type == para_Title) { |
| 870 | xhtml_heading(fp, p); |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /* Do the preamble and copyright */ |
| 876 | for (p = sourceform; p; p = p->next) |
| 877 | { |
| 878 | if (p->type == para_Chapter || p->type == para_Heading || |
| 879 | p->type == para_Subsect || p->type == para_Appendix || |
| 880 | p->type == para_UnnumberedChapter) { |
| 881 | /* |
| 882 | * We've found the end of the preamble. Do every normal |
| 883 | * paragraph up to there. |
| 884 | */ |
| 885 | xhtml_do_paras(fp, sourceform, p); |
| 886 | break; |
| 887 | } |
| 888 | } |
| 889 | for (p = sourceform; p; p = p->next) |
| 890 | { |
| 891 | if (p->type == para_Copyright) |
| 892 | { |
| 893 | fprintf(fp, "<p>"); |
| 894 | xhtml_para(fp, p->words); |
| 895 | fprintf(fp, "</p>\n"); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | xhtml_do_contents(fp, file); |
| 900 | xhtml_do_sections(fp, file->sections); |
| 901 | |
| 902 | /* |
| 903 | * Put the index in the top file if we're in single-file mode |
| 904 | * (leaf-level 0). |
| 905 | */ |
| 906 | if (conf.leaf_level == 0 && count234(idx->entries) > 0) { |
| 907 | fprintf(fp, "<a name=\"index\"></a><h1>Index</h1>\n"); |
| 908 | xhtml_do_index_body(fp); |
| 909 | } |
| 910 | |
| 911 | xhtml_dofooter(fp); |
| 912 | fclose(fp); |
| 913 | } |
| 914 | |
| 915 | /* Convert a Unicode string to an ASCII one. '?' is |
| 916 | * used for unmappable characters. |
| 917 | */ |
| 918 | static void xhtml_utostr(wchar_t *in, char **out) |
| 919 | { |
| 920 | int l = ustrlen(in); |
| 921 | int i; |
| 922 | *out = smalloc(l+1); |
| 923 | for (i=0; i<l; i++) |
| 924 | { |
| 925 | if (in[i]>=32 && in[i]<=126) |
| 926 | (*out)[i]=(char)in[i]; |
| 927 | else |
| 928 | (*out)[i]='?'; |
| 929 | } |
| 930 | (*out)[i]=0; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * Write contents for the given file, and subfiles, down to |
| 935 | * the appropriate contents depth. Returns the number of |
| 936 | * entries written. |
| 937 | */ |
| 938 | static int xhtml_do_contents(FILE *fp, xhtmlfile *file) |
| 939 | { |
| 940 | int level, limit, start_level, count = 0; |
| 941 | if (!file) |
| 942 | return 0; |
| 943 | |
| 944 | level = (file->sections)?(file->sections->level):(0); |
| 945 | limit = conf.contents_depth[(level>5)?(5):(level)]; |
| 946 | start_level = (file->is_leaf) ? (level-1) : (level); |
| 947 | last_level = start_level; |
| 948 | |
| 949 | count += xhtml_do_contents_section_limit(fp, file->sections, limit); |
| 950 | count += xhtml_do_contents_limit(fp, file->child, limit); |
| 951 | if (fp!=NULL) { |
| 952 | while (last_level > start_level) { |
| 953 | last_level--; |
| 954 | fprintf(fp, "</ul>\n"); |
| 955 | } |
| 956 | } |
| 957 | return count; |
| 958 | } |
| 959 | |
| 960 | /* As above, but doesn't do anything in the current file */ |
| 961 | static int xhtml_do_naked_contents(FILE *fp, xhtmlfile *file) |
| 962 | { |
| 963 | int level, limit, start_level, count = 0; |
| 964 | if (!file) |
| 965 | return 0; |
| 966 | |
| 967 | level = (file->sections)?(file->sections->level):(0); |
| 968 | limit = conf.contents_depth[(level>5)?(5):(level)]; |
| 969 | start_level = (file->is_leaf) ? (level-1) : (level); |
| 970 | last_level = start_level; |
| 971 | |
| 972 | count = xhtml_do_contents_limit(fp, file->child, limit); |
| 973 | if (fp!=NULL) { |
| 974 | while (last_level > start_level) { |
| 975 | last_level--; |
| 976 | fprintf(fp, "</ul>\n"); |
| 977 | } |
| 978 | } |
| 979 | return count; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Write contents for the given file, children, and siblings, down to |
| 984 | * given limit contents depth. |
| 985 | */ |
| 986 | static int xhtml_do_contents_limit(FILE *fp, xhtmlfile *file, int limit) |
| 987 | { |
| 988 | int count = 0; |
| 989 | while (file) { |
| 990 | count += xhtml_do_contents_section_limit(fp, file->sections, limit); |
| 991 | count += xhtml_do_contents_limit(fp, file->child, limit); |
| 992 | file = file->next; |
| 993 | } |
| 994 | return count; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Write contents entries for the given section tree, down to the |
| 999 | * limit contents depth. |
| 1000 | */ |
| 1001 | static int xhtml_do_contents_section_deep_limit(FILE *fp, xhtmlsection *section, int limit) |
| 1002 | { |
| 1003 | int count = 0; |
| 1004 | while (section) { |
| 1005 | if (!xhtml_add_contents_entry(fp, section, limit)) |
| 1006 | return 0; |
| 1007 | else |
| 1008 | count++; |
| 1009 | count += xhtml_do_contents_section_deep_limit(fp, section->child, limit); |
| 1010 | section = section->next; |
| 1011 | } |
| 1012 | return count; |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * Write contents entries for the given section tree, down to the |
| 1017 | * limit contents depth. |
| 1018 | */ |
| 1019 | static int xhtml_do_contents_section_limit(FILE *fp, xhtmlsection *section, int limit) |
| 1020 | { |
| 1021 | int count = 0; |
| 1022 | if (!section) |
| 1023 | return 0; |
| 1024 | xhtml_add_contents_entry(fp, section, limit); |
| 1025 | count=1; |
| 1026 | count += xhtml_do_contents_section_deep_limit(fp, section->child, limit); |
| 1027 | /* section=section->child; |
| 1028 | while (section && xhtml_add_contents_entry(fp, section, limit)) { |
| 1029 | section = section->next; |
| 1030 | }*/ |
| 1031 | return count; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Add a section entry, unless we're exceeding the limit, in which |
| 1036 | * case return FALSE (otherwise return TRUE). |
| 1037 | */ |
| 1038 | static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit) |
| 1039 | { |
| 1040 | if (!section || section->level > limit) |
| 1041 | return FALSE; |
| 1042 | if (fp==NULL || section->level < 0) |
| 1043 | return TRUE; |
| 1044 | while (last_level > section->level) { |
| 1045 | last_level--; |
| 1046 | fprintf(fp, "</ul>\n"); |
| 1047 | } |
| 1048 | while (last_level < section->level) { |
| 1049 | last_level++; |
| 1050 | fprintf(fp, "<ul>\n"); |
| 1051 | } |
| 1052 | fprintf(fp, "<li><a href=\"%s#%s\">", section->file->filename, section->fragment); |
| 1053 | if (section->para->kwtext) { |
| 1054 | xhtml_para(fp, section->para->kwtext); |
| 1055 | if (section->para->words) { |
| 1056 | fprintf(fp, ": "); |
| 1057 | } |
| 1058 | } |
| 1059 | if (section->para->words) { |
| 1060 | xhtml_para(fp, section->para->words); |
| 1061 | } |
| 1062 | fprintf(fp, "</a></li>\n"); |
| 1063 | return TRUE; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Write all the sections in this file. Do all paragraphs in this section, then all |
| 1068 | * children (recursively), then go on to the next one (tail recursively). |
| 1069 | */ |
| 1070 | static void xhtml_do_sections(FILE *fp, xhtmlsection *sections) |
| 1071 | { |
| 1072 | while (sections) { |
| 1073 | currentsection = sections; |
| 1074 | xhtml_do_paras(fp, sections->para, NULL); |
| 1075 | xhtml_do_sections(fp, sections->child); |
| 1076 | sections = sections->next; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* Write this list of paragraphs. Close off all lists at the end. */ |
| 1081 | static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end) |
| 1082 | { |
| 1083 | int last_type = -1, ptype, first=TRUE; |
| 1084 | stack lcont_stack = stk_new(); |
| 1085 | if (!p) |
| 1086 | return; |
| 1087 | |
| 1088 | /* for (; p && (xhtml_para_level(p)>limit || xhtml_para_level(p)==-1 || first); p=p->next) {*/ |
| 1089 | for (; p && p != end && (xhtml_para_level(p)==-1 || first); p=p->next) { |
| 1090 | first=FALSE; |
| 1091 | switch (ptype = p->type) |
| 1092 | { |
| 1093 | /* |
| 1094 | * Things we ignore because we've already processed them or |
| 1095 | * aren't going to touch them in this pass. |
| 1096 | */ |
| 1097 | case para_IM: |
| 1098 | case para_BR: |
| 1099 | case para_Biblio: /* only touch BiblioCited */ |
| 1100 | case para_VersionID: |
| 1101 | case para_Copyright: |
| 1102 | case para_NoCite: |
| 1103 | case para_Title: |
| 1104 | break; |
| 1105 | |
| 1106 | /* |
| 1107 | * Chapter titles. |
| 1108 | */ |
| 1109 | case para_Chapter: |
| 1110 | case para_Appendix: |
| 1111 | case para_UnnumberedChapter: |
| 1112 | xhtml_heading(fp, p); |
| 1113 | break; |
| 1114 | |
| 1115 | case para_Heading: |
| 1116 | case para_Subsect: |
| 1117 | xhtml_heading(fp, p); |
| 1118 | break; |
| 1119 | |
| 1120 | case para_Rule: |
| 1121 | fprintf(fp, "\n<hr />\n"); |
| 1122 | break; |
| 1123 | |
| 1124 | case para_Normal: |
| 1125 | fprintf(fp, "\n<p>"); |
| 1126 | xhtml_para(fp, p->words); |
| 1127 | fprintf(fp, "</p>\n"); |
| 1128 | break; |
| 1129 | |
| 1130 | case para_LcontPush: |
| 1131 | { |
| 1132 | int *p; |
| 1133 | p = mknew(int); |
| 1134 | *p = last_type; |
| 1135 | stk_push(lcont_stack, p); |
| 1136 | last_type = para_Normal; |
| 1137 | } |
| 1138 | break; |
| 1139 | case para_LcontPop: |
| 1140 | { |
| 1141 | int *p = stk_pop(lcont_stack); |
| 1142 | assert(p); |
| 1143 | ptype = last_type = *p; |
| 1144 | sfree(p); |
| 1145 | goto closeofflist; /* ick */ |
| 1146 | } |
| 1147 | break; |
| 1148 | case para_QuotePush: |
| 1149 | fprintf(fp, "<blockquote>\n"); |
| 1150 | break; |
| 1151 | case para_QuotePop: |
| 1152 | fprintf(fp, "</blockquote>\n"); |
| 1153 | break; |
| 1154 | |
| 1155 | case para_Bullet: |
| 1156 | case para_NumberedList: |
| 1157 | case para_Description: |
| 1158 | case para_DescribedThing: |
| 1159 | case para_BiblioCited: |
| 1160 | if (last_type!=p->type) { |
| 1161 | /* start up list if necessary */ |
| 1162 | if (p->type == para_Bullet) { |
| 1163 | fprintf(fp, "<ul>\n"); |
| 1164 | } else if (p->type == para_NumberedList) { |
| 1165 | fprintf(fp, "<ol>\n"); |
| 1166 | } else if (p->type == para_BiblioCited || |
| 1167 | p->type == para_DescribedThing || |
| 1168 | p->type == para_Description) { |
| 1169 | fprintf(fp, "<dl>\n"); |
| 1170 | } |
| 1171 | } |
| 1172 | if (p->type == para_Bullet || p->type == para_NumberedList) { |
| 1173 | fprintf(fp, "<li>"); |
| 1174 | } else if (p->type == para_DescribedThing) { |
| 1175 | fprintf(fp, "<dt>"); |
| 1176 | } else if (p->type == para_Description) { |
| 1177 | fprintf(fp, "<dd>"); |
| 1178 | } else if (p->type == para_BiblioCited) { |
| 1179 | fprintf(fp, "<dt>"); |
| 1180 | xhtml_para(fp, p->kwtext); |
| 1181 | fprintf(fp, "</dt>\n<dd>"); |
| 1182 | } |
| 1183 | xhtml_para(fp, p->words); |
| 1184 | { |
| 1185 | paragraph *p2 = p->next; |
| 1186 | if (p2 && xhtml_para_level(p2)==-1 && p2->type == para_LcontPush) |
| 1187 | break; |
| 1188 | } |
| 1189 | |
| 1190 | closeofflist: |
| 1191 | if (ptype == para_BiblioCited) { |
| 1192 | fprintf(fp, "</dd>\n"); |
| 1193 | } else if (p->type == para_DescribedThing) { |
| 1194 | fprintf(fp, "</dt>"); |
| 1195 | } else if (p->type == para_Description) { |
| 1196 | fprintf(fp, "</dd>"); |
| 1197 | } else if (ptype == para_Bullet || ptype == para_NumberedList) { |
| 1198 | fprintf(fp, "</li>"); |
| 1199 | } |
| 1200 | if (ptype == para_Bullet || ptype == para_NumberedList || |
| 1201 | ptype == para_BiblioCited || ptype == para_Description || |
| 1202 | ptype == para_DescribedThing) |
| 1203 | /* close off list if necessary */ |
| 1204 | { |
| 1205 | paragraph *p2 = p->next; |
| 1206 | int close_off=FALSE; |
| 1207 | /* if (p2 && (xhtml_para_level(p2)>limit || xhtml_para_level(p2)==-1)) {*/ |
| 1208 | if (p2 && xhtml_para_level(p2)==-1) { |
| 1209 | if (p2->type != ptype && p2->type != para_LcontPush) |
| 1210 | close_off=TRUE; |
| 1211 | } else { |
| 1212 | close_off=TRUE; |
| 1213 | } |
| 1214 | if (close_off) { |
| 1215 | if (ptype == para_Bullet) { |
| 1216 | fprintf(fp, "</ul>\n"); |
| 1217 | } else if (ptype == para_NumberedList) { |
| 1218 | fprintf(fp, "</ol>\n"); |
| 1219 | } else if (ptype == para_BiblioCited || |
| 1220 | ptype == para_Description || |
| 1221 | ptype == para_DescribedThing) { |
| 1222 | fprintf(fp, "</dl>\n"); |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | break; |
| 1227 | |
| 1228 | case para_Code: |
| 1229 | xhtml_codepara(fp, p->words); |
| 1230 | break; |
| 1231 | } |
| 1232 | last_type = ptype; |
| 1233 | } |
| 1234 | |
| 1235 | stk_free(lcont_stack); |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * Output a header for this XHTML file. |
| 1240 | */ |
| 1241 | static void xhtml_doheader(FILE *fp, word *title) |
| 1242 | { |
| 1243 | fprintf(fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"); |
| 1244 | fprintf(fp, "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"); |
| 1245 | fprintf(fp, "<html xmlns='http://www.w3.org/1999/xhtml'>\n\n<head>\n<title>"); |
| 1246 | if (title==NULL) |
| 1247 | fprintf(fp, "The thing with no name!"); |
| 1248 | else |
| 1249 | xhtml_para(fp, title); |
| 1250 | fprintf(fp, "</title>\n"); |
| 1251 | fprintf(fp, "<meta name=\"generator\" content=\"Halibut %s xhtml-backend\" />\n", version); |
| 1252 | if (conf.author) |
| 1253 | fprintf(fp, "<meta name=\"author\" content=\"%ls\" />\n", conf.author); |
| 1254 | if (conf.description) |
| 1255 | fprintf(fp, "<meta name=\"description\" content=\"%ls\" />\n", conf.description); |
| 1256 | if (conf.head_end) |
| 1257 | fprintf(fp, "%ls\n", conf.head_end); |
| 1258 | fprintf(fp, "</head>\n\n"); |
| 1259 | if (conf.body) |
| 1260 | fprintf(fp, "%ls\n", conf.body); |
| 1261 | else |
| 1262 | fprintf(fp, "<body>\n"); |
| 1263 | if (conf.body_start) |
| 1264 | fprintf(fp, "%ls\n", conf.body_start); |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Output a footer for this XHTML file. |
| 1269 | */ |
| 1270 | static void xhtml_dofooter(FILE *fp) |
| 1271 | { |
| 1272 | fprintf(fp, "\n<hr />\n\n"); |
| 1273 | if (conf.body_end) |
| 1274 | fprintf(fp, "%ls\n", conf.body_end); |
| 1275 | if (!conf.suppress_address) { |
| 1276 | fprintf(fp,"<address>\n"); |
| 1277 | if (conf.address_start) |
| 1278 | fprintf(fp, "%ls\n", conf.address_start); |
| 1279 | /* Do the version ID */ |
| 1280 | if (conf.include_version_id) { |
| 1281 | paragraph *p; |
| 1282 | int started = 0; |
| 1283 | for (p = sourceparas; p; p = p->next) |
| 1284 | if (p->type == para_VersionID) { |
| 1285 | xhtml_versionid(fp, p->words, started); |
| 1286 | started = 1; |
| 1287 | } |
| 1288 | } |
| 1289 | if (conf.address_end) |
| 1290 | fprintf(fp, "%ls\n", conf.address_end); |
| 1291 | fprintf(fp, "</address>\n"); |
| 1292 | } |
| 1293 | fprintf(fp, "</body>\n\n</html>\n"); |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * Output the versionid paragraph. Typically this is a version control |
| 1298 | * ID string (such as $Id...$ in RCS). |
| 1299 | */ |
| 1300 | static void xhtml_versionid(FILE *fp, word *text, int started) |
| 1301 | { |
| 1302 | rdstringc t = { 0, 0, NULL }; |
| 1303 | |
| 1304 | rdaddc(&t, '['); /* FIXME: configurability */ |
| 1305 | xhtml_rdaddwc(&t, text, NULL); |
| 1306 | rdaddc(&t, ']'); /* FIXME: configurability */ |
| 1307 | |
| 1308 | if (started) |
| 1309 | fprintf(fp, "<br>\n"); |
| 1310 | fprintf(fp, "%s\n", t.text); |
| 1311 | sfree(t.text); |
| 1312 | } |
| 1313 | |
| 1314 | /* Is this an XHTML reserved character? */ |
| 1315 | static int xhtml_reservedchar(int c) |
| 1316 | { |
| 1317 | if (c=='&' || c=='<' || c=='>' || c=='"') |
| 1318 | return TRUE; |
| 1319 | else |
| 1320 | return FALSE; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * Convert a wide string into valid XHTML: Anything outside ASCII will |
| 1325 | * be fixed up as an entity. Currently we don't worry about constraining the |
| 1326 | * encoded character set, which we should probably do at some point (we can |
| 1327 | * still fix up and return FALSE - see the last comment here). We also don't |
| 1328 | * currently |
| 1329 | * |
| 1330 | * Because this is only used for words, spaces are HARD spaces (any other |
| 1331 | * spaces will be word_Whitespace not word_Normal). So they become |
| 1332 | * Unless hard_spaces is FALSE, of course (code paragraphs break the above |
| 1333 | * rule). |
| 1334 | * |
| 1335 | * If `result' is non-NULL, mallocs the resulting string and stores a pointer to |
| 1336 | * it in `*result'. If `result' is NULL, merely checks whether all |
| 1337 | * characters in the string are feasible. |
| 1338 | * |
| 1339 | * Return is nonzero if all characters are OK. If not all |
| 1340 | * characters are OK but `result' is non-NULL, a result _will_ |
| 1341 | * still be generated! |
| 1342 | */ |
| 1343 | static int xhtml_convert(wchar_t *s, int maxlen, char **result, |
| 1344 | int hard_spaces) { |
| 1345 | int doing = (result != 0); |
| 1346 | int ok = TRUE; |
| 1347 | char *p = NULL; |
| 1348 | int plen = 0, psize = 0; |
| 1349 | |
| 1350 | if (maxlen <= 0) |
| 1351 | maxlen = -1; |
| 1352 | |
| 1353 | for (; *s && maxlen != 0; s++, maxlen--) { |
| 1354 | wchar_t c = *s; |
| 1355 | |
| 1356 | #define ensure_size(i) if (i>=psize) { psize = i+256; p = resize(p, psize); } |
| 1357 | |
| 1358 | if (((c == 32 && !hard_spaces) || (c > 32 && c <= 126 && !xhtml_reservedchar(c)))) { |
| 1359 | /* Char is OK. */ |
| 1360 | if (doing) |
| 1361 | { |
| 1362 | ensure_size(plen); |
| 1363 | p[plen++] = (char)c; |
| 1364 | } |
| 1365 | } else { |
| 1366 | /* Char needs fixing up. */ |
| 1367 | /* ok = FALSE; -- currently we never return FALSE; we |
| 1368 | * might want to when considering a character set for the |
| 1369 | * encoded document. |
| 1370 | */ |
| 1371 | if (doing) |
| 1372 | { |
| 1373 | if (c==32) { /* a space in a word is a hard space */ |
| 1374 | ensure_size(plen+6); /* includes space for the NUL, which is subsequently stomped on */ |
| 1375 | sprintf(p+plen, " "); |
| 1376 | plen+=6; |
| 1377 | } else { |
| 1378 | /* FIXME: entity names! */ |
| 1379 | ensure_size(plen+8); /* includes space for the NUL, which is subsequently stomped on */ |
| 1380 | plen+=sprintf(p+plen, "&#%04i;", (int)c); |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | if (doing) { |
| 1386 | p = resize(p, plen+1); |
| 1387 | p[plen] = '\0'; |
| 1388 | *result = p; |
| 1389 | } |
| 1390 | return ok; |
| 1391 | } |
| 1392 | |
| 1393 | /* |
| 1394 | * This formats the given words as XHTML. |
| 1395 | */ |
| 1396 | static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end) { |
| 1397 | char *c; |
| 1398 | keyword *kwl; |
| 1399 | xhtmlsection *sect; |
| 1400 | indextag *itag; |
| 1401 | int ti; |
| 1402 | |
| 1403 | for (; text && text != end; text = text->next) { |
| 1404 | switch (text->type) { |
| 1405 | case word_HyperLink: |
| 1406 | xhtml_utostr(text->text, &c); |
| 1407 | rdaddsc(rs, "<a href=\""); |
| 1408 | rdaddsc(rs, c); |
| 1409 | rdaddsc(rs, "\">"); |
| 1410 | sfree(c); |
| 1411 | break; |
| 1412 | |
| 1413 | case word_UpperXref: |
| 1414 | case word_LowerXref: |
| 1415 | kwl = kw_lookup(keywords, text->text); |
| 1416 | if (kwl) { |
| 1417 | sect=xhtml_find_section(kwl->para); |
| 1418 | if (sect) { |
| 1419 | rdaddsc(rs, "<a href=\""); |
| 1420 | rdaddsc(rs, sect->file->filename); |
| 1421 | rdaddc(rs, '#'); |
| 1422 | rdaddsc(rs, sect->fragment); |
| 1423 | rdaddsc(rs, "\">"); |
| 1424 | } else { |
| 1425 | rdaddsc(rs, "<a href=\"Apologies.html\"><!-- probably a bibliography cross reference -->"); |
| 1426 | error(err_whatever, "Couldn't locate cross-reference! (Probably a bibliography entry.)"); |
| 1427 | } |
| 1428 | } else { |
| 1429 | rdaddsc(rs, "<a href=\"Apologies.html\"><!-- unknown cross-reference -->"); |
| 1430 | error(err_whatever, "Couldn't locate cross-reference! (Wasn't in source file.)"); |
| 1431 | } |
| 1432 | break; |
| 1433 | |
| 1434 | case word_IndexRef: /* in theory we could make an index target here */ |
| 1435 | /* rdaddsc(rs, "<a name=\"idx-"); |
| 1436 | xhtml_utostr(text->text, &c); |
| 1437 | rdaddsc(rs, c); |
| 1438 | sfree(c); |
| 1439 | rdaddsc(rs, "\"></a>");*/ |
| 1440 | /* what we _do_ need to do is to fix up the backend data |
| 1441 | * for any indexentry this points to. |
| 1442 | */ |
| 1443 | for (ti=0; (itag = (indextag *)index234(idx->tags, ti))!=NULL; ti++) { |
| 1444 | /* FIXME: really ustricmp() and not ustrcmp()? */ |
| 1445 | if (ustricmp(itag->name, text->text)==0) { |
| 1446 | break; |
| 1447 | } |
| 1448 | } |
| 1449 | if (itag!=NULL) { |
| 1450 | if (itag->refs!=NULL) { |
| 1451 | int i; |
| 1452 | for (i=0; i<itag->nrefs; i++) { |
| 1453 | xhtmlindex *idx_ref; |
| 1454 | indexentry *ientry; |
| 1455 | |
| 1456 | ientry = itag->refs[i]; |
| 1457 | if (ientry->backend_data==NULL) { |
| 1458 | idx_ref = (xhtmlindex*) smalloc(sizeof(xhtmlindex)); |
| 1459 | if (idx_ref==NULL) |
| 1460 | fatal(err_nomemory); |
| 1461 | idx_ref->nsection = 0; |
| 1462 | idx_ref->size = 4; |
| 1463 | idx_ref->sections = (xhtmlsection**) smalloc(idx_ref->size * sizeof(xhtmlsection*)); |
| 1464 | if (idx_ref->sections==NULL) |
| 1465 | fatal(err_nomemory); |
| 1466 | ientry->backend_data = idx_ref; |
| 1467 | } else { |
| 1468 | idx_ref = ientry->backend_data; |
| 1469 | if (idx_ref->nsection+1 > idx_ref->size) { |
| 1470 | int new_size = idx_ref->size * 2; |
| 1471 | idx_ref->sections = srealloc(idx_ref->sections, new_size * sizeof(xhtmlsection)); |
| 1472 | if (idx_ref->sections==NULL) { |
| 1473 | fatal(err_nomemory); |
| 1474 | } |
| 1475 | idx_ref->size = new_size; |
| 1476 | } |
| 1477 | } |
| 1478 | idx_ref->sections[idx_ref->nsection++] = currentsection; |
| 1479 | #if 0 |
| 1480 | #endif |
| 1481 | } |
| 1482 | } else { |
| 1483 | fatal(err_whatever, "Index tag had no entries!"); |
| 1484 | } |
| 1485 | } else { |
| 1486 | fprintf(stderr, "Looking for index entry '%ls'\n", text->text); |
| 1487 | fatal(err_whatever, "Couldn't locate index entry! (Wasn't in index.)"); |
| 1488 | } |
| 1489 | break; |
| 1490 | |
| 1491 | case word_HyperEnd: |
| 1492 | case word_XrefEnd: |
| 1493 | rdaddsc(rs, "</a>"); |
| 1494 | break; |
| 1495 | |
| 1496 | case word_Normal: |
| 1497 | case word_Emph: |
| 1498 | case word_Code: |
| 1499 | case word_WeakCode: |
| 1500 | case word_WhiteSpace: |
| 1501 | case word_EmphSpace: |
| 1502 | case word_CodeSpace: |
| 1503 | case word_WkCodeSpace: |
| 1504 | case word_Quote: |
| 1505 | case word_EmphQuote: |
| 1506 | case word_CodeQuote: |
| 1507 | case word_WkCodeQuote: |
| 1508 | assert(text->type != word_CodeQuote && |
| 1509 | text->type != word_WkCodeQuote); |
| 1510 | if (towordstyle(text->type) == word_Emph && |
| 1511 | (attraux(text->aux) == attr_First || |
| 1512 | attraux(text->aux) == attr_Only)) |
| 1513 | rdaddsc(rs, "<em>"); |
| 1514 | else if ((towordstyle(text->type) == word_Code || towordstyle(text->type) == word_WeakCode) && |
| 1515 | (attraux(text->aux) == attr_First || |
| 1516 | attraux(text->aux) == attr_Only)) |
| 1517 | rdaddsc(rs, "<code>"); |
| 1518 | |
| 1519 | if (removeattr(text->type) == word_Normal) { |
| 1520 | if (xhtml_convert(text->text, 0, &c, TRUE)) /* spaces in the word are hard */ |
| 1521 | rdaddsc(rs, c); |
| 1522 | else |
| 1523 | xhtml_rdaddwc(rs, text->alt, NULL); |
| 1524 | sfree(c); |
| 1525 | } else if (removeattr(text->type) == word_WhiteSpace) { |
| 1526 | rdaddc(rs, ' '); |
| 1527 | } else if (removeattr(text->type) == word_Quote) { |
| 1528 | rdaddsc(rs, """); |
| 1529 | } |
| 1530 | |
| 1531 | if (towordstyle(text->type) == word_Emph && |
| 1532 | (attraux(text->aux) == attr_Last || |
| 1533 | attraux(text->aux) == attr_Only)) |
| 1534 | rdaddsc(rs, "</em>"); |
| 1535 | else if ((towordstyle(text->type) == word_Code || towordstyle(text->type) == word_WeakCode) && |
| 1536 | (attraux(text->aux) == attr_Last || |
| 1537 | attraux(text->aux) == attr_Only)) |
| 1538 | rdaddsc(rs, "</code>"); |
| 1539 | break; |
| 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | /* Output a heading, formatted as XHTML. |
| 1545 | */ |
| 1546 | static void xhtml_heading(FILE *fp, paragraph *p) |
| 1547 | { |
| 1548 | rdstringc t = { 0, 0, NULL }; |
| 1549 | word *tprefix = p->kwtext; |
| 1550 | word *nprefix = p->kwtext2; |
| 1551 | word *text = p->words; |
| 1552 | int level = xhtml_para_level(p); |
| 1553 | xhtmlsection *sect = xhtml_find_section(p); |
| 1554 | xhtmlheadfmt *fmt; |
| 1555 | char *fragment; |
| 1556 | if (sect) { |
| 1557 | fragment = sect->fragment; |
| 1558 | } else { |
| 1559 | if (p->type == para_Title) |
| 1560 | fragment = "title"; |
| 1561 | else { |
| 1562 | fragment = ""; /* FIXME: what else can we do? */ |
| 1563 | error(err_whatever, "Couldn't locate heading cross-reference!"); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | if (p->type == para_Title) |
| 1568 | fmt = NULL; |
| 1569 | else if (level == 1) |
| 1570 | fmt = &conf.fchapter; |
| 1571 | else if (level-1 < conf.nfsect) |
| 1572 | fmt = &conf.fsect[level-1]; |
| 1573 | else |
| 1574 | fmt = &conf.fsect[conf.nfsect-1]; |
| 1575 | |
| 1576 | if (fmt && fmt->just_numbers && nprefix) { |
| 1577 | xhtml_rdaddwc(&t, nprefix, NULL); |
| 1578 | if (fmt) { |
| 1579 | char *c; |
| 1580 | if (xhtml_convert(fmt->number_suffix, 0, &c, FALSE)) { |
| 1581 | rdaddsc(&t, c); |
| 1582 | sfree(c); |
| 1583 | } |
| 1584 | } |
| 1585 | } else if (fmt && !fmt->just_numbers && tprefix) { |
| 1586 | xhtml_rdaddwc(&t, tprefix, NULL); |
| 1587 | if (fmt) { |
| 1588 | char *c; |
| 1589 | if (xhtml_convert(fmt->number_suffix, 0, &c, FALSE)) { |
| 1590 | rdaddsc(&t, c); |
| 1591 | sfree(c); |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | xhtml_rdaddwc(&t, text, NULL); |
| 1596 | /* |
| 1597 | * If we're outputting in single-file mode, we need to lower |
| 1598 | * the level of each heading by one, because the overall |
| 1599 | * document title will be sitting right at the top as an <h1> |
| 1600 | * and so chapters and sections should start at <h2>. |
| 1601 | * |
| 1602 | * Even if not, the document title will come back from |
| 1603 | * xhtml_para_level() as level zero, so we must increment that |
| 1604 | * no matter what leaf_level is set to. |
| 1605 | */ |
| 1606 | if (conf.leaf_level == 0 || level == 0) |
| 1607 | level++; |
| 1608 | fprintf(fp, "<a name=\"%s\"></a><h%i>%s</h%i>\n", fragment, level, t.text, level); |
| 1609 | sfree(t.text); |
| 1610 | } |
| 1611 | |
| 1612 | /* Output a paragraph. Styles are handled by xhtml_rdaddwc(). |
| 1613 | * This looks pretty simple; I may have missed something ... |
| 1614 | */ |
| 1615 | static void xhtml_para(FILE *fp, word *text) |
| 1616 | { |
| 1617 | rdstringc out = { 0, 0, NULL }; |
| 1618 | xhtml_rdaddwc(&out, text, NULL); |
| 1619 | fprintf(fp, "%s", out.text); |
| 1620 | sfree(out.text); |
| 1621 | } |
| 1622 | |
| 1623 | /* Output a code paragraph. I'm treating this as preformatted, which |
| 1624 | * may not be entirely correct. See xhtml_para() for my worries about |
| 1625 | * this being overly-simple; however I think that most of the complexity |
| 1626 | * of the text backend came entirely out of word wrapping anyway. |
| 1627 | */ |
| 1628 | static void xhtml_codepara(FILE *fp, word *text) |
| 1629 | { |
| 1630 | fprintf(fp, "<pre>"); |
| 1631 | for (; text; text = text->next) if (text->type == word_WeakCode) { |
| 1632 | word *here, *next; |
| 1633 | char *c; |
| 1634 | |
| 1635 | /* |
| 1636 | * See if this WeakCode is followed by an Emph to indicate |
| 1637 | * emphasis. |
| 1638 | */ |
| 1639 | here = text; |
| 1640 | if (text->next && text->next->type == word_Emph) { |
| 1641 | next = text = text->next; |
| 1642 | } else |
| 1643 | next = NULL; |
| 1644 | |
| 1645 | if (next) { |
| 1646 | wchar_t *t, *e; |
| 1647 | int n; |
| 1648 | |
| 1649 | t = here->text; |
| 1650 | e = next->text; |
| 1651 | |
| 1652 | while (*e) { |
| 1653 | int ec = *e; |
| 1654 | |
| 1655 | for (n = 0; t[n] && e[n] && e[n] == ec; n++); |
| 1656 | xhtml_convert(t, n, &c, FALSE); |
| 1657 | fprintf(fp, "%s%s%s", |
| 1658 | (ec == 'i' ? "<em>" : ec == 'b' ? "<b>" : ""), |
| 1659 | c, |
| 1660 | (ec == 'i' ? "</em>" : ec == 'b' ? "</b>" : "")); |
| 1661 | sfree(c); |
| 1662 | |
| 1663 | t += n; |
| 1664 | e += n; |
| 1665 | } |
| 1666 | |
| 1667 | xhtml_convert(t, 0, &c, FALSE); |
| 1668 | fprintf(fp, "%s\n", c); |
| 1669 | sfree(c); |
| 1670 | } else { |
| 1671 | xhtml_convert(here->text, 0, &c, FALSE); |
| 1672 | fprintf(fp, "%s\n", c); |
| 1673 | sfree(c); |
| 1674 | } |
| 1675 | } |
| 1676 | fprintf(fp, "</pre>\n"); |
| 1677 | } |