| 1 | /* |
| 2 | * HTML backend for Halibut |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * TODO: |
| 7 | * |
| 8 | * - I'm never entirely convinced that having a fragment link to |
| 9 | * come in at the start of the real text in the file is |
| 10 | * sensible. Perhaps for the topmost section in the file, no |
| 11 | * fragment should be used? (Though it should probably still be |
| 12 | * _there_ even if unused.) |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include "halibut.h" |
| 20 | |
| 21 | #define is_heading_type(type) ( (type) == para_Title || \ |
| 22 | (type) == para_Chapter || \ |
| 23 | (type) == para_Appendix || \ |
| 24 | (type) == para_UnnumberedChapter || \ |
| 25 | (type) == para_Heading || \ |
| 26 | (type) == para_Subsect) |
| 27 | |
| 28 | #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \ |
| 29 | (p)->type == para_Heading ? 1 : \ |
| 30 | (p)->type == para_Title ? -1 : 0 ) |
| 31 | |
| 32 | typedef struct { |
| 33 | int just_numbers; |
| 34 | wchar_t *number_suffix; |
| 35 | } sectlevel; |
| 36 | |
| 37 | typedef struct { |
| 38 | int nasect; |
| 39 | sectlevel achapter, *asect; |
| 40 | int *contents_depths; /* 0=main, 1=chapter, 2=sect etc */ |
| 41 | int ncdepths; |
| 42 | int address_section, visible_version_id; |
| 43 | int leaf_contains_contents, leaf_smallest_contents; |
| 44 | char *contents_filename; |
| 45 | char *index_filename; |
| 46 | char *template_filename; |
| 47 | char *single_filename; |
| 48 | char *template_fragment; |
| 49 | char *head_end, *body_start, *body_end, *addr_start, *addr_end; |
| 50 | char *body_tag, *nav_attr; |
| 51 | wchar_t *author, *description; |
| 52 | wchar_t *index_text, *contents_text, *preamble_text, *title_separator; |
| 53 | wchar_t *nav_prev_text, *nav_next_text, *nav_separator; |
| 54 | wchar_t *index_main_sep, *index_multi_sep; |
| 55 | wchar_t *pre_versionid, *post_versionid; |
| 56 | int restrict_charset, output_charset; |
| 57 | enum { |
| 58 | HTML_3_2, HTML_4, ISO_HTML, |
| 59 | XHTML_1_0_TRANSITIONAL, XHTML_1_0_STRICT |
| 60 | } htmlver; |
| 61 | wchar_t *lquote, *rquote; |
| 62 | int leaf_level; |
| 63 | } htmlconfig; |
| 64 | |
| 65 | #define contents_depth(conf, level) \ |
| 66 | ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 ) |
| 67 | |
| 68 | #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL) |
| 69 | |
| 70 | typedef struct htmlfile htmlfile; |
| 71 | typedef struct htmlsect htmlsect; |
| 72 | |
| 73 | struct htmlfile { |
| 74 | htmlfile *next; |
| 75 | char *filename; |
| 76 | int last_fragment_number; |
| 77 | int min_heading_depth; |
| 78 | htmlsect *first, *last; /* first/last highest-level sections */ |
| 79 | }; |
| 80 | |
| 81 | struct htmlsect { |
| 82 | htmlsect *next, *parent; |
| 83 | htmlfile *file; |
| 84 | paragraph *title, *text; |
| 85 | enum { NORMAL, TOP, INDEX } type; |
| 86 | int contents_depth; |
| 87 | char *fragment; |
| 88 | }; |
| 89 | |
| 90 | typedef struct { |
| 91 | htmlfile *head, *tail; |
| 92 | htmlfile *single, *index; |
| 93 | tree234 *frags; |
| 94 | } htmlfilelist; |
| 95 | |
| 96 | typedef struct { |
| 97 | htmlsect *head, *tail; |
| 98 | } htmlsectlist; |
| 99 | |
| 100 | typedef struct { |
| 101 | htmlfile *file; |
| 102 | char *fragment; |
| 103 | } htmlfragment; |
| 104 | |
| 105 | typedef struct { |
| 106 | int nrefs, refsize; |
| 107 | word **refs; |
| 108 | } htmlindex; |
| 109 | |
| 110 | typedef struct { |
| 111 | htmlsect *section; |
| 112 | char *fragment; |
| 113 | int generated, referenced; |
| 114 | } htmlindexref; |
| 115 | |
| 116 | typedef struct { |
| 117 | /* |
| 118 | * This level deals with charset conversion, starting and |
| 119 | * ending tags, and writing to the file. It's the lexical |
| 120 | * level. |
| 121 | */ |
| 122 | FILE *fp; |
| 123 | int charset, restrict_charset; |
| 124 | charset_state cstate; |
| 125 | int ver; |
| 126 | enum { |
| 127 | HO_NEUTRAL, HO_IN_TAG, HO_IN_EMPTY_TAG, HO_IN_TEXT |
| 128 | } state; |
| 129 | /* |
| 130 | * Stuff beyond here deals with the higher syntactic level: it |
| 131 | * tracks how many levels of <ul> are currently open when |
| 132 | * producing a contents list, for example. |
| 133 | */ |
| 134 | int contents_level; |
| 135 | } htmloutput; |
| 136 | |
| 137 | static int html_fragment_compare(void *av, void *bv) |
| 138 | { |
| 139 | htmlfragment *a = (htmlfragment *)av; |
| 140 | htmlfragment *b = (htmlfragment *)bv; |
| 141 | int cmp; |
| 142 | |
| 143 | if ((cmp = strcmp(a->file->filename, b->file->filename)) != 0) |
| 144 | return cmp; |
| 145 | else |
| 146 | return strcmp(a->fragment, b->fragment); |
| 147 | } |
| 148 | |
| 149 | static void html_file_section(htmlconfig *cfg, htmlfilelist *files, |
| 150 | htmlsect *sect, int depth); |
| 151 | |
| 152 | static htmlfile *html_new_file(htmlfilelist *list, char *filename); |
| 153 | static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title); |
| 154 | |
| 155 | /* Flags for html_words() flags parameter */ |
| 156 | #define NOTHING 0x00 |
| 157 | #define MARKUP 0x01 |
| 158 | #define LINKS 0x02 |
| 159 | #define INDEXENTS 0x04 |
| 160 | #define ALL 0x07 |
| 161 | static void html_words(htmloutput *ho, word *words, int flags, |
| 162 | htmlfile *file, keywordlist *keywords, htmlconfig *cfg); |
| 163 | static void html_codepara(htmloutput *ho, word *words); |
| 164 | |
| 165 | static void element_open(htmloutput *ho, char const *name); |
| 166 | static void element_close(htmloutput *ho, char const *name); |
| 167 | static void element_empty(htmloutput *ho, char const *name); |
| 168 | static void element_attr(htmloutput *ho, char const *name, char const *value); |
| 169 | static void element_attr_w(htmloutput *ho, char const *name, |
| 170 | wchar_t const *value); |
| 171 | static void html_text(htmloutput *ho, wchar_t const *str); |
| 172 | static void html_text_nbsp(htmloutput *ho, wchar_t const *str); |
| 173 | static void html_text_limit(htmloutput *ho, wchar_t const *str, int maxlen); |
| 174 | static void html_text_limit_internal(htmloutput *ho, wchar_t const *text, |
| 175 | int maxlen, int quote_quotes, int nbsp); |
| 176 | static void html_nl(htmloutput *ho); |
| 177 | static void html_raw(htmloutput *ho, char *text); |
| 178 | static void html_raw_as_attr(htmloutput *ho, char *text); |
| 179 | static void cleanup(htmloutput *ho); |
| 180 | |
| 181 | static void html_href(htmloutput *ho, htmlfile *thisfile, |
| 182 | htmlfile *targetfile, char *targetfrag); |
| 183 | static void html_fragment(htmloutput *ho, char const *fragment); |
| 184 | |
| 185 | static char *html_format(paragraph *p, char *template_string); |
| 186 | static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file, |
| 187 | char *text); |
| 188 | |
| 189 | static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s, |
| 190 | htmlfile *thisfile, keywordlist *keywords, |
| 191 | htmlconfig *cfg); |
| 192 | static void html_section_title(htmloutput *ho, htmlsect *s, |
| 193 | htmlfile *thisfile, keywordlist *keywords, |
| 194 | htmlconfig *cfg, int real); |
| 195 | |
| 196 | static htmlconfig html_configure(paragraph *source) { |
| 197 | htmlconfig ret; |
| 198 | paragraph *p; |
| 199 | |
| 200 | /* |
| 201 | * Defaults. |
| 202 | */ |
| 203 | ret.leaf_level = 2; |
| 204 | ret.achapter.just_numbers = FALSE; |
| 205 | ret.achapter.number_suffix = L": "; |
| 206 | ret.nasect = 1; |
| 207 | ret.asect = snewn(ret.nasect, sectlevel); |
| 208 | ret.asect[0].just_numbers = TRUE; |
| 209 | ret.asect[0].number_suffix = L" "; |
| 210 | ret.ncdepths = 0; |
| 211 | ret.contents_depths = 0; |
| 212 | ret.visible_version_id = TRUE; |
| 213 | ret.address_section = TRUE; |
| 214 | ret.leaf_contains_contents = FALSE; |
| 215 | ret.leaf_smallest_contents = 4; |
| 216 | ret.single_filename = dupstr("Manual.html"); |
| 217 | ret.contents_filename = dupstr("Contents.html"); |
| 218 | ret.index_filename = dupstr("IndexPage.html"); |
| 219 | ret.template_filename = dupstr("%n.html"); |
| 220 | ret.template_fragment = dupstr("%b"); |
| 221 | ret.head_end = ret.body_tag = ret.body_start = ret.body_end = |
| 222 | ret.addr_start = ret.addr_end = ret.nav_attr = NULL; |
| 223 | ret.author = ret.description = NULL; |
| 224 | ret.restrict_charset = CS_UTF8; |
| 225 | ret.output_charset = CS_ASCII; |
| 226 | ret.htmlver = HTML_4; |
| 227 | ret.index_text = L"Index"; |
| 228 | ret.contents_text = L"Contents"; |
| 229 | ret.preamble_text = L"Preamble"; |
| 230 | ret.title_separator = L" - "; |
| 231 | ret.nav_prev_text = L"Previous"; |
| 232 | ret.nav_next_text = L"Next"; |
| 233 | ret.nav_separator = L" | "; |
| 234 | ret.index_main_sep = L": "; |
| 235 | ret.index_multi_sep = L", "; |
| 236 | ret.pre_versionid = L"["; |
| 237 | ret.post_versionid = L"]"; |
| 238 | /* |
| 239 | * Default quote characters are Unicode matched single quotes, |
| 240 | * falling back to ordinary ASCII ". |
| 241 | */ |
| 242 | ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0"; |
| 243 | ret.rquote = uadv(ret.lquote); |
| 244 | |
| 245 | /* |
| 246 | * Two-pass configuration so that we can pick up global config |
| 247 | * (e.g. `quotes') before having it overridden by specific |
| 248 | * config (`html-quotes'), irrespective of the order in which |
| 249 | * they occur. |
| 250 | */ |
| 251 | for (p = source; p; p = p->next) { |
| 252 | if (p->type == para_Config) { |
| 253 | if (!ustricmp(p->keyword, L"quotes")) { |
| 254 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
| 255 | ret.lquote = uadv(p->keyword); |
| 256 | ret.rquote = uadv(ret.lquote); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | for (p = source; p; p = p->next) { |
| 263 | if (p->type == para_Config) { |
| 264 | wchar_t *k = p->keyword; |
| 265 | |
| 266 | if (!ustrnicmp(k, L"xhtml-", 6)) |
| 267 | k++; /* treat `xhtml-' and `html-' the same */ |
| 268 | |
| 269 | if (!ustricmp(k, L"html-restrict-charset")) { |
| 270 | ret.restrict_charset = charset_from_ustr(&p->fpos, uadv(k)); |
| 271 | } else if (!ustricmp(k, L"html-output-charset")) { |
| 272 | ret.output_charset = charset_from_ustr(&p->fpos, uadv(k)); |
| 273 | } else if (!ustricmp(k, L"html-version")) { |
| 274 | wchar_t *vername = uadv(k); |
| 275 | static const struct { |
| 276 | const wchar_t *name; |
| 277 | int ver; |
| 278 | } versions[] = { |
| 279 | {L"html3.2", HTML_3_2}, |
| 280 | {L"html4", HTML_4}, |
| 281 | {L"iso-html", ISO_HTML}, |
| 282 | {L"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL}, |
| 283 | {L"xhtml1.0strict", XHTML_1_0_STRICT} |
| 284 | }; |
| 285 | int i; |
| 286 | |
| 287 | for (i = 0; i < (int)lenof(versions); i++) |
| 288 | if (!ustricmp(versions[i].name, vername)) |
| 289 | break; |
| 290 | |
| 291 | if (i == lenof(versions)) |
| 292 | error(err_htmlver, &p->fpos, vername); |
| 293 | else |
| 294 | ret.htmlver = versions[i].ver; |
| 295 | } else if (!ustricmp(k, L"html-single-filename")) { |
| 296 | sfree(ret.single_filename); |
| 297 | ret.single_filename = dupstr(adv(p->origkeyword)); |
| 298 | } else if (!ustricmp(k, L"html-contents-filename")) { |
| 299 | sfree(ret.contents_filename); |
| 300 | ret.contents_filename = dupstr(adv(p->origkeyword)); |
| 301 | } else if (!ustricmp(k, L"html-index-filename")) { |
| 302 | sfree(ret.index_filename); |
| 303 | ret.index_filename = dupstr(adv(p->origkeyword)); |
| 304 | } else if (!ustricmp(k, L"html-template-filename")) { |
| 305 | sfree(ret.template_filename); |
| 306 | ret.template_filename = dupstr(adv(p->origkeyword)); |
| 307 | } else if (!ustricmp(k, L"html-template-fragment")) { |
| 308 | sfree(ret.template_fragment); |
| 309 | ret.template_fragment = dupstr(adv(p->origkeyword)); |
| 310 | } else if (!ustricmp(k, L"html-chapter-numeric")) { |
| 311 | ret.achapter.just_numbers = utob(uadv(k)); |
| 312 | } else if (!ustricmp(k, L"html-chapter-suffix")) { |
| 313 | ret.achapter.number_suffix = uadv(k); |
| 314 | } else if (!ustricmp(k, L"html-leaf-level")) { |
| 315 | ret.leaf_level = utoi(uadv(k)); |
| 316 | } else if (!ustricmp(k, L"html-section-numeric")) { |
| 317 | wchar_t *q = uadv(k); |
| 318 | int n = 0; |
| 319 | if (uisdigit(*q)) { |
| 320 | n = utoi(q); |
| 321 | q = uadv(q); |
| 322 | } |
| 323 | if (n >= ret.nasect) { |
| 324 | int i; |
| 325 | ret.asect = sresize(ret.asect, n+1, sectlevel); |
| 326 | for (i = ret.nasect; i <= n; i++) |
| 327 | ret.asect[i] = ret.asect[ret.nasect-1]; |
| 328 | ret.nasect = n+1; |
| 329 | } |
| 330 | ret.asect[n].just_numbers = utob(q); |
| 331 | } else if (!ustricmp(k, L"html-section-suffix")) { |
| 332 | wchar_t *q = uadv(k); |
| 333 | int n = 0; |
| 334 | if (uisdigit(*q)) { |
| 335 | n = utoi(q); |
| 336 | q = uadv(q); |
| 337 | } |
| 338 | if (n >= ret.nasect) { |
| 339 | int i; |
| 340 | ret.asect = sresize(ret.asect, n+1, sectlevel); |
| 341 | for (i = ret.nasect; i <= n; i++) { |
| 342 | ret.asect[i] = ret.asect[ret.nasect-1]; |
| 343 | } |
| 344 | ret.nasect = n+1; |
| 345 | } |
| 346 | ret.asect[n].number_suffix = q; |
| 347 | } else if (!ustricmp(k, L"html-contents-depth") || |
| 348 | !ustrnicmp(k, L"html-contents-depth-", 20)) { |
| 349 | /* |
| 350 | * Relic of old implementation: this directive used |
| 351 | * to be written as \cfg{html-contents-depth-3}{2} |
| 352 | * rather than the usual Halibut convention of |
| 353 | * \cfg{html-contents-depth}{3}{2}. We therefore |
| 354 | * support both. |
| 355 | */ |
| 356 | wchar_t *q = k[19] ? k+20 : uadv(k); |
| 357 | int n = 0; |
| 358 | if (uisdigit(*q)) { |
| 359 | n = utoi(q); |
| 360 | q = uadv(q); |
| 361 | } |
| 362 | if (n >= ret.ncdepths) { |
| 363 | int i; |
| 364 | ret.contents_depths = |
| 365 | sresize(ret.contents_depths, n+1, int); |
| 366 | for (i = ret.ncdepths; i <= n; i++) { |
| 367 | ret.contents_depths[i] = i+2; |
| 368 | } |
| 369 | ret.ncdepths = n+1; |
| 370 | } |
| 371 | ret.contents_depths[n] = utoi(q); |
| 372 | } else if (!ustricmp(k, L"html-head-end")) { |
| 373 | ret.head_end = adv(p->origkeyword); |
| 374 | } else if (!ustricmp(k, L"html-body-tag")) { |
| 375 | ret.body_tag = adv(p->origkeyword); |
| 376 | } else if (!ustricmp(k, L"html-body-start")) { |
| 377 | ret.body_start = adv(p->origkeyword); |
| 378 | } else if (!ustricmp(k, L"html-body-end")) { |
| 379 | ret.body_end = adv(p->origkeyword); |
| 380 | } else if (!ustricmp(k, L"html-address-start")) { |
| 381 | ret.addr_start = adv(p->origkeyword); |
| 382 | } else if (!ustricmp(k, L"html-address-end")) { |
| 383 | ret.addr_end = adv(p->origkeyword); |
| 384 | } else if (!ustricmp(k, L"html-navigation-attributes")) { |
| 385 | ret.nav_attr = adv(p->origkeyword); |
| 386 | } else if (!ustricmp(k, L"html-author")) { |
| 387 | ret.author = uadv(k); |
| 388 | } else if (!ustricmp(k, L"html-description")) { |
| 389 | ret.description = uadv(k); |
| 390 | } else if (!ustricmp(k, L"html-suppress-address")) { |
| 391 | ret.address_section = !utob(uadv(k)); |
| 392 | } else if (!ustricmp(k, L"html-versionid")) { |
| 393 | ret.visible_version_id = utob(uadv(k)); |
| 394 | } else if (!ustricmp(k, L"html-quotes")) { |
| 395 | if (*uadv(k) && *uadv(uadv(k))) { |
| 396 | ret.lquote = uadv(k); |
| 397 | ret.rquote = uadv(ret.lquote); |
| 398 | } |
| 399 | } else if (!ustricmp(k, L"html-leaf-contains-contents")) { |
| 400 | ret.leaf_contains_contents = utob(uadv(k)); |
| 401 | } else if (!ustricmp(k, L"html-leaf-smallest-contents")) { |
| 402 | ret.leaf_smallest_contents = utoi(uadv(k)); |
| 403 | } else if (!ustricmp(k, L"html-index-text")) { |
| 404 | ret.index_text = uadv(k); |
| 405 | } else if (!ustricmp(k, L"html-contents-text")) { |
| 406 | ret.contents_text = uadv(k); |
| 407 | } else if (!ustricmp(k, L"html-preamble-text")) { |
| 408 | ret.preamble_text = uadv(k); |
| 409 | } else if (!ustricmp(k, L"html-title-separator")) { |
| 410 | ret.title_separator = uadv(k); |
| 411 | } else if (!ustricmp(k, L"html-nav-prev-text")) { |
| 412 | ret.nav_prev_text = uadv(k); |
| 413 | } else if (!ustricmp(k, L"html-nav-next-text")) { |
| 414 | ret.nav_next_text = uadv(k); |
| 415 | } else if (!ustricmp(k, L"html-nav-separator")) { |
| 416 | ret.nav_separator = uadv(k); |
| 417 | } else if (!ustricmp(k, L"html-index-main-separator")) { |
| 418 | ret.index_main_sep = uadv(k); |
| 419 | } else if (!ustricmp(k, L"html-index-multiple-separator")) { |
| 420 | ret.index_multi_sep = uadv(k); |
| 421 | } else if (!ustricmp(k, L"html-pre-versionid")) { |
| 422 | ret.pre_versionid = uadv(k); |
| 423 | } else if (!ustricmp(k, L"html-post-versionid")) { |
| 424 | ret.post_versionid = uadv(k); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Now process fallbacks on quote characters. |
| 431 | */ |
| 432 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) && |
| 433 | (!cvt_ok(ret.restrict_charset, ret.lquote) || |
| 434 | !cvt_ok(ret.restrict_charset, ret.rquote))) { |
| 435 | ret.lquote = uadv(ret.rquote); |
| 436 | ret.rquote = uadv(ret.lquote); |
| 437 | } |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | paragraph *html_config_filename(char *filename) |
| 443 | { |
| 444 | /* |
| 445 | * If the user passes in a single filename as a parameter to |
| 446 | * the `--html' command-line option, then we should assume it |
| 447 | * to imply _two_ config directives: |
| 448 | * \cfg{html-single-filename}{whatever} and |
| 449 | * \cfg{html-leaf-level}{0}; the rationale being that the user |
| 450 | * wants their output _in that file_. |
| 451 | */ |
| 452 | paragraph *p, *q; |
| 453 | |
| 454 | p = cmdline_cfg_simple("html-single-filename", filename, NULL); |
| 455 | q = cmdline_cfg_simple("html-leaf-level", "0", NULL); |
| 456 | p->next = q; |
| 457 | return p; |
| 458 | } |
| 459 | |
| 460 | void html_backend(paragraph *sourceform, keywordlist *keywords, |
| 461 | indexdata *idx, void *unused) |
| 462 | { |
| 463 | paragraph *p; |
| 464 | htmlconfig conf; |
| 465 | htmlfilelist files = { NULL, NULL, NULL, NULL, NULL }; |
| 466 | htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL }; |
| 467 | int has_index; |
| 468 | |
| 469 | IGNORE(unused); |
| 470 | |
| 471 | conf = html_configure(sourceform); |
| 472 | |
| 473 | /* |
| 474 | * We're going to make heavy use of paragraphs' private data |
| 475 | * fields in the forthcoming code. Clear them first, so we can |
| 476 | * reliably tell whether we have auxiliary data for a |
| 477 | * particular paragraph. |
| 478 | */ |
| 479 | for (p = sourceform; p; p = p->next) |
| 480 | p->private_data = NULL; |
| 481 | |
| 482 | files.frags = newtree234(html_fragment_compare); |
| 483 | |
| 484 | /* |
| 485 | * Start by figuring out into which file each piece of the |
| 486 | * document should be put. We'll do this by inventing an |
| 487 | * `htmlsect' structure and stashing it in the private_data |
| 488 | * field of each section paragraph; we also need one additional |
| 489 | * htmlsect for the document index, which won't show up in the |
| 490 | * source form but needs to be consistently mentioned in |
| 491 | * contents links. |
| 492 | * |
| 493 | * While we're here, we'll also invent the HTML fragment name |
| 494 | * for each section. |
| 495 | */ |
| 496 | { |
| 497 | htmlsect *topsect, *sect; |
| 498 | int d; |
| 499 | |
| 500 | topsect = html_new_sect(§s, NULL); |
| 501 | topsect->type = TOP; |
| 502 | topsect->title = NULL; |
| 503 | topsect->text = sourceform; |
| 504 | topsect->contents_depth = contents_depth(conf, 0); |
| 505 | html_file_section(&conf, &files, topsect, -1); |
| 506 | topsect->fragment = NULL; |
| 507 | |
| 508 | for (p = sourceform; p; p = p->next) |
| 509 | if (is_heading_type(p->type)) { |
| 510 | d = heading_depth(p); |
| 511 | |
| 512 | if (p->type == para_Title) { |
| 513 | topsect->title = p; |
| 514 | continue; |
| 515 | } |
| 516 | |
| 517 | sect = html_new_sect(§s, p); |
| 518 | sect->text = p->next; |
| 519 | |
| 520 | sect->contents_depth = contents_depth(conf, d+1) - (d+1); |
| 521 | |
| 522 | if (p->parent) { |
| 523 | sect->parent = (htmlsect *)p->parent->private_data; |
| 524 | assert(sect->parent != NULL); |
| 525 | } else |
| 526 | sect->parent = topsect; |
| 527 | p->private_data = sect; |
| 528 | |
| 529 | html_file_section(&conf, &files, sect, d); |
| 530 | |
| 531 | sect->fragment = html_format(p, conf.template_fragment); |
| 532 | sect->fragment = html_sanitise_fragment(&files, sect->file, |
| 533 | sect->fragment); |
| 534 | } |
| 535 | |
| 536 | /* And the index, if we have one. */ |
| 537 | has_index = (count234(idx->entries) > 0); |
| 538 | if (has_index) { |
| 539 | sect = html_new_sect(§s, NULL); |
| 540 | sect->text = NULL; |
| 541 | sect->type = INDEX; |
| 542 | sect->parent = topsect; |
| 543 | sect->contents_depth = 0; |
| 544 | html_file_section(&conf, &files, sect, 0); /* peer of chapters */ |
| 545 | sect->fragment = utoa_dup(conf.index_text, CS_ASCII); |
| 546 | sect->fragment = html_sanitise_fragment(&files, sect->file, |
| 547 | sect->fragment); |
| 548 | files.index = sect->file; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Go through the keyword list and sort out fragment IDs for |
| 554 | * all the potentially referenced paragraphs which _aren't_ |
| 555 | * headings. |
| 556 | */ |
| 557 | { |
| 558 | int i; |
| 559 | keyword *kw; |
| 560 | htmlsect *sect; |
| 561 | |
| 562 | for (i = 0; (kw = index234(keywords->keys, i)) != NULL; i++) { |
| 563 | paragraph *q, *p = kw->para; |
| 564 | |
| 565 | if (!is_heading_type(p->type)) { |
| 566 | htmlsect *parent; |
| 567 | |
| 568 | /* |
| 569 | * Find the paragraph's parent htmlsect, to |
| 570 | * determine which file it will end up in. |
| 571 | */ |
| 572 | q = p->parent; |
| 573 | if (!q) { |
| 574 | /* |
| 575 | * Preamble paragraphs have no parent. So if we |
| 576 | * have a non-heading with no parent, it must |
| 577 | * be preamble, and therefore its parent |
| 578 | * htmlsect must be the preamble one. |
| 579 | */ |
| 580 | assert(sects.head && |
| 581 | sects.head->type == TOP); |
| 582 | parent = sects.head; |
| 583 | } else |
| 584 | parent = (htmlsect *)q->private_data; |
| 585 | |
| 586 | /* |
| 587 | * Now we can construct an htmlsect for this |
| 588 | * paragraph itself, taking care to put it in the |
| 589 | * list of non-sections rather than the list of |
| 590 | * sections (so that traverses of the `sects' list |
| 591 | * won't attempt to add it to the contents or |
| 592 | * anything weird like that). |
| 593 | */ |
| 594 | sect = html_new_sect(&nonsects, p); |
| 595 | sect->file = parent->file; |
| 596 | sect->parent = parent; |
| 597 | p->private_data = sect; |
| 598 | |
| 599 | /* |
| 600 | * Fragment IDs for these paragraphs will simply be |
| 601 | * `p' followed by an integer. |
| 602 | */ |
| 603 | sect->fragment = snewn(40, char); |
| 604 | sprintf(sect->fragment, "p%d", |
| 605 | sect->file->last_fragment_number++); |
| 606 | sect->fragment = html_sanitise_fragment(&files, sect->file, |
| 607 | sect->fragment); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Reset the fragment numbers in each file. I've just used them |
| 614 | * to generate `p' fragment IDs for non-section paragraphs |
| 615 | * (numbered list elements, bibliocited), and now I want to use |
| 616 | * them for `i' fragment IDs for index entries. |
| 617 | */ |
| 618 | { |
| 619 | htmlfile *file; |
| 620 | for (file = files.head; file; file = file->next) |
| 621 | file->last_fragment_number = 0; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Now sort out the index. This involves: |
| 626 | * |
| 627 | * - For each index term, we set up an htmlindex structure to |
| 628 | * store all the references to that term. |
| 629 | * |
| 630 | * - Then we make a pass over the actual document, finding |
| 631 | * every word_IndexRef; for each one, we actually figure out |
| 632 | * the HTML filename/fragment pair we will use to reference |
| 633 | * it, store that information in the private data field of |
| 634 | * the word_IndexRef itself (so we can recreate it when the |
| 635 | * time comes to output our HTML), and add a reference to it |
| 636 | * to the index term in question. |
| 637 | */ |
| 638 | { |
| 639 | int i; |
| 640 | indexentry *entry; |
| 641 | htmlsect *lastsect; |
| 642 | word *w; |
| 643 | |
| 644 | /* |
| 645 | * Set up the htmlindex structures. |
| 646 | */ |
| 647 | |
| 648 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
| 649 | htmlindex *hi = snew(htmlindex); |
| 650 | |
| 651 | hi->nrefs = hi->refsize = 0; |
| 652 | hi->refs = NULL; |
| 653 | |
| 654 | entry->backend_data = hi; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Run over the document inventing fragments. Each fragment |
| 659 | * is of the form `i' followed by an integer. |
| 660 | */ |
| 661 | lastsect = sects.head; /* this is always the top section */ |
| 662 | for (p = sourceform; p; p = p->next) { |
| 663 | if (is_heading_type(p->type) && p->type != para_Title) |
| 664 | lastsect = (htmlsect *)p->private_data; |
| 665 | |
| 666 | for (w = p->words; w; w = w->next) |
| 667 | if (w->type == word_IndexRef) { |
| 668 | htmlindexref *hr = snew(htmlindexref); |
| 669 | indextag *tag; |
| 670 | int i; |
| 671 | |
| 672 | hr->referenced = hr->generated = FALSE; |
| 673 | hr->section = lastsect; |
| 674 | { |
| 675 | char buf[40]; |
| 676 | sprintf(buf, "i%d", |
| 677 | lastsect->file->last_fragment_number++); |
| 678 | hr->fragment = dupstr(buf); |
| 679 | hr->fragment = |
| 680 | html_sanitise_fragment(&files, hr->section->file, |
| 681 | hr->fragment); |
| 682 | } |
| 683 | w->private_data = hr; |
| 684 | |
| 685 | tag = index_findtag(idx, w->text); |
| 686 | if (!tag) |
| 687 | break; |
| 688 | |
| 689 | for (i = 0; i < tag->nrefs; i++) { |
| 690 | indexentry *entry = tag->refs[i]; |
| 691 | htmlindex *hi = (htmlindex *)entry->backend_data; |
| 692 | |
| 693 | if (hi->nrefs >= hi->refsize) { |
| 694 | hi->refsize += 32; |
| 695 | hi->refs = sresize(hi->refs, hi->refsize, word *); |
| 696 | } |
| 697 | |
| 698 | hi->refs[hi->nrefs++] = w; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Now we're ready to write out the actual HTML files. |
| 706 | * |
| 707 | * For each file: |
| 708 | * |
| 709 | * - we open that file and write its header |
| 710 | * - we run down the list of sections |
| 711 | * - for each section directly contained within that file, we |
| 712 | * output the section text |
| 713 | * - for each section which is not in the file but which has a |
| 714 | * parent that is, we output a contents entry for the |
| 715 | * section if appropriate |
| 716 | * - finally, we output the file trailer and close the file. |
| 717 | */ |
| 718 | { |
| 719 | htmlfile *f, *prevf; |
| 720 | htmlsect *s; |
| 721 | paragraph *p; |
| 722 | |
| 723 | prevf = NULL; |
| 724 | |
| 725 | for (f = files.head; f; f = f->next) { |
| 726 | htmloutput ho; |
| 727 | int displaying; |
| 728 | enum LISTTYPE { NOLIST, UL, OL, DL }; |
| 729 | enum ITEMTYPE { NOITEM, LI, DT, DD }; |
| 730 | struct stackelement { |
| 731 | struct stackelement *next; |
| 732 | enum LISTTYPE listtype; |
| 733 | enum ITEMTYPE itemtype; |
| 734 | } *stackhead; |
| 735 | |
| 736 | #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" ) |
| 737 | #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" ) |
| 738 | |
| 739 | ho.fp = fopen(f->filename, "w"); |
| 740 | ho.charset = conf.output_charset; |
| 741 | ho.restrict_charset = conf.restrict_charset; |
| 742 | ho.cstate = charset_init_state; |
| 743 | ho.ver = conf.htmlver; |
| 744 | ho.state = HO_NEUTRAL; |
| 745 | ho.contents_level = 0; |
| 746 | |
| 747 | /* <!DOCTYPE>. */ |
| 748 | switch (conf.htmlver) { |
| 749 | case HTML_3_2: |
| 750 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " |
| 751 | "HTML 3.2 Final//EN\">\n"); |
| 752 | break; |
| 753 | case HTML_4: |
| 754 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML" |
| 755 | " 4.01//EN\"\n\"http://www.w3.org/TR/html4/" |
| 756 | "strict.dtd\">\n"); |
| 757 | break; |
| 758 | case ISO_HTML: |
| 759 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"ISO/IEC " |
| 760 | "15445:2000//DTD HTML//EN\">\n"); |
| 761 | break; |
| 762 | case XHTML_1_0_TRANSITIONAL: |
| 763 | fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", |
| 764 | charset_to_mimeenc(conf.output_charset)); |
| 765 | fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML" |
| 766 | " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/" |
| 767 | "xhtml1/DTD/xhtml1-transitional.dtd\">\n"); |
| 768 | break; |
| 769 | case XHTML_1_0_STRICT: |
| 770 | fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", |
| 771 | charset_to_mimeenc(conf.output_charset)); |
| 772 | fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML" |
| 773 | " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/" |
| 774 | "DTD/xhtml1-strict.dtd\">\n"); |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | element_open(&ho, "html"); |
| 779 | if (is_xhtml(conf.htmlver)) { |
| 780 | element_attr(&ho, "xmlns", "http://www.w3.org/1999/xhtml"); |
| 781 | } |
| 782 | html_nl(&ho); |
| 783 | |
| 784 | element_open(&ho, "head"); |
| 785 | html_nl(&ho); |
| 786 | |
| 787 | element_empty(&ho, "meta"); |
| 788 | element_attr(&ho, "http-equiv", "content-type"); |
| 789 | { |
| 790 | char buf[200]; |
| 791 | sprintf(buf, "text/html; charset=%.150s", |
| 792 | charset_to_mimeenc(conf.output_charset)); |
| 793 | element_attr(&ho, "content", buf); |
| 794 | } |
| 795 | html_nl(&ho); |
| 796 | |
| 797 | if (conf.author) { |
| 798 | element_empty(&ho, "meta"); |
| 799 | element_attr(&ho, "name", "author"); |
| 800 | element_attr_w(&ho, "content", conf.author); |
| 801 | html_nl(&ho); |
| 802 | } |
| 803 | |
| 804 | if (conf.description) { |
| 805 | element_empty(&ho, "meta"); |
| 806 | element_attr(&ho, "name", "description"); |
| 807 | element_attr_w(&ho, "content", conf.description); |
| 808 | html_nl(&ho); |
| 809 | } |
| 810 | |
| 811 | element_open(&ho, "title"); |
| 812 | if (f->first && f->first->title) { |
| 813 | html_words(&ho, f->first->title->words, NOTHING, |
| 814 | f, keywords, &conf); |
| 815 | |
| 816 | assert(f->last); |
| 817 | if (f->last != f->first && f->last->title) { |
| 818 | html_text(&ho, conf.title_separator); |
| 819 | html_words(&ho, f->last->title->words, NOTHING, |
| 820 | f, keywords, &conf); |
| 821 | } |
| 822 | } |
| 823 | element_close(&ho, "title"); |
| 824 | html_nl(&ho); |
| 825 | |
| 826 | if (conf.head_end) |
| 827 | html_raw(&ho, conf.head_end); |
| 828 | |
| 829 | /* |
| 830 | * Add any <head> data defined in specific sections |
| 831 | * that go in this file. (This is mostly to allow <meta |
| 832 | * name="AppleTitle"> tags for Mac online help.) |
| 833 | */ |
| 834 | for (s = sects.head; s; s = s->next) { |
| 835 | if (s->file == f && s->text) { |
| 836 | for (p = s->text; |
| 837 | p && (p == s->text || p->type == para_Title || |
| 838 | !is_heading_type(p->type)); |
| 839 | p = p->next) { |
| 840 | if (p->type == para_Config) { |
| 841 | if (!ustricmp(p->keyword, L"html-local-head")) { |
| 842 | html_raw(&ho, adv(p->origkeyword)); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | element_close(&ho, "head"); |
| 850 | html_nl(&ho); |
| 851 | |
| 852 | if (conf.body_tag) |
| 853 | html_raw(&ho, conf.body_tag); |
| 854 | else |
| 855 | element_open(&ho, "body"); |
| 856 | html_nl(&ho); |
| 857 | |
| 858 | if (conf.body_start) |
| 859 | html_raw(&ho, conf.body_start); |
| 860 | |
| 861 | /* |
| 862 | * Write out a nav bar. Special case: we don't do this |
| 863 | * if there is only one file. |
| 864 | */ |
| 865 | if (files.head != files.tail) { |
| 866 | element_open(&ho, "p"); |
| 867 | if (conf.nav_attr) |
| 868 | html_raw_as_attr(&ho, conf.nav_attr); |
| 869 | |
| 870 | if (prevf) { |
| 871 | element_open(&ho, "a"); |
| 872 | element_attr(&ho, "href", prevf->filename); |
| 873 | } |
| 874 | html_text(&ho, conf.nav_prev_text); |
| 875 | if (prevf) |
| 876 | element_close(&ho, "a"); |
| 877 | |
| 878 | html_text(&ho, conf.nav_separator); |
| 879 | |
| 880 | if (f != files.head) { |
| 881 | element_open(&ho, "a"); |
| 882 | element_attr(&ho, "href", files.head->filename); |
| 883 | } |
| 884 | html_text(&ho, conf.contents_text); |
| 885 | if (f != files.head) |
| 886 | element_close(&ho, "a"); |
| 887 | |
| 888 | html_text(&ho, conf.nav_separator); |
| 889 | |
| 890 | if (has_index) { |
| 891 | if (f != files.index) { |
| 892 | element_open(&ho, "a"); |
| 893 | element_attr(&ho, "href", files.index->filename); |
| 894 | } |
| 895 | html_text(&ho, conf.index_text); |
| 896 | if (f != files.index) |
| 897 | element_close(&ho, "a"); |
| 898 | } |
| 899 | |
| 900 | html_text(&ho, conf.nav_separator); |
| 901 | |
| 902 | if (f->next) { |
| 903 | element_open(&ho, "a"); |
| 904 | element_attr(&ho, "href", f->next->filename); |
| 905 | } |
| 906 | html_text(&ho, conf.nav_next_text); |
| 907 | if (f->next) |
| 908 | element_close(&ho, "a"); |
| 909 | |
| 910 | element_close(&ho, "p"); |
| 911 | html_nl(&ho); |
| 912 | } |
| 913 | prevf = f; |
| 914 | |
| 915 | /* |
| 916 | * Write out a prefix TOC for the file. |
| 917 | * |
| 918 | * We start by going through the section list and |
| 919 | * collecting the sections which need to be added to |
| 920 | * the contents. On the way, we also test to see if |
| 921 | * this file is a leaf file (defined as one which |
| 922 | * contains all descendants of any section it |
| 923 | * contains), because this will play a part in our |
| 924 | * decision on whether or not to _output_ the TOC. |
| 925 | * |
| 926 | * Special case: we absolutely do not do this if we're |
| 927 | * in single-file mode. |
| 928 | */ |
| 929 | if (files.head != files.tail) { |
| 930 | int ntoc = 0, tocsize = 0; |
| 931 | htmlsect **toc = NULL; |
| 932 | int leaf = TRUE; |
| 933 | |
| 934 | for (s = sects.head; s; s = s->next) { |
| 935 | htmlsect *a, *ac; |
| 936 | int depth, adepth; |
| 937 | |
| 938 | /* |
| 939 | * Search up from this section until we find |
| 940 | * the highest-level one which belongs in this |
| 941 | * file. |
| 942 | */ |
| 943 | depth = adepth = 0; |
| 944 | a = NULL; |
| 945 | for (ac = s; ac; ac = ac->parent) { |
| 946 | if (ac->file == f) { |
| 947 | a = ac; |
| 948 | adepth = depth; |
| 949 | } |
| 950 | depth++; |
| 951 | } |
| 952 | |
| 953 | if (s->file != f && a != NULL) |
| 954 | leaf = FALSE; |
| 955 | |
| 956 | if (a) { |
| 957 | if (adepth <= a->contents_depth) { |
| 958 | if (ntoc >= tocsize) { |
| 959 | tocsize += 64; |
| 960 | toc = sresize(toc, tocsize, htmlsect *); |
| 961 | } |
| 962 | toc[ntoc++] = s; |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if (leaf && conf.leaf_contains_contents && |
| 968 | ntoc >= conf.leaf_smallest_contents) { |
| 969 | int i; |
| 970 | |
| 971 | for (i = 0; i < ntoc; i++) { |
| 972 | htmlsect *s = toc[i]; |
| 973 | int hlevel = (s->type == TOP ? -1 : |
| 974 | s->type == INDEX ? 0 : |
| 975 | heading_depth(s->title)) |
| 976 | - f->min_heading_depth + 1; |
| 977 | |
| 978 | assert(hlevel >= 1); |
| 979 | html_contents_entry(&ho, hlevel, s, |
| 980 | f, keywords, &conf); |
| 981 | } |
| 982 | html_contents_entry(&ho, 0, NULL, f, keywords, &conf); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * Now go through the document and output some real |
| 988 | * text. |
| 989 | */ |
| 990 | displaying = FALSE; |
| 991 | for (s = sects.head; s; s = s->next) { |
| 992 | if (s->file == f) { |
| 993 | /* |
| 994 | * This section belongs in this file. |
| 995 | * Display it. |
| 996 | */ |
| 997 | displaying = TRUE; |
| 998 | } else { |
| 999 | htmlsect *a, *ac; |
| 1000 | int depth, adepth; |
| 1001 | |
| 1002 | displaying = FALSE; |
| 1003 | |
| 1004 | /* |
| 1005 | * Search up from this section until we find |
| 1006 | * the highest-level one which belongs in this |
| 1007 | * file. |
| 1008 | */ |
| 1009 | depth = adepth = 0; |
| 1010 | a = NULL; |
| 1011 | for (ac = s; ac; ac = ac->parent) { |
| 1012 | if (ac->file == f) { |
| 1013 | a = ac; |
| 1014 | adepth = depth; |
| 1015 | } |
| 1016 | depth++; |
| 1017 | } |
| 1018 | |
| 1019 | if (a != NULL) { |
| 1020 | /* |
| 1021 | * This section does not belong in this |
| 1022 | * file, but an ancestor of it does. Write |
| 1023 | * out a contents table entry, if the depth |
| 1024 | * doesn't exceed the maximum contents |
| 1025 | * depth for the ancestor section. |
| 1026 | */ |
| 1027 | if (adepth <= a->contents_depth) { |
| 1028 | html_contents_entry(&ho, adepth, s, |
| 1029 | f, keywords, &conf); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (displaying) { |
| 1035 | int hlevel; |
| 1036 | char htag[3]; |
| 1037 | |
| 1038 | html_contents_entry(&ho, 0, NULL, f, keywords, &conf); |
| 1039 | |
| 1040 | /* |
| 1041 | * Display the section heading. |
| 1042 | */ |
| 1043 | |
| 1044 | hlevel = (s->type == TOP ? -1 : |
| 1045 | s->type == INDEX ? 0 : |
| 1046 | heading_depth(s->title)) |
| 1047 | - f->min_heading_depth + 1; |
| 1048 | assert(hlevel >= 1); |
| 1049 | /* HTML headings only go up to <h6> */ |
| 1050 | if (hlevel > 6) |
| 1051 | hlevel = 6; |
| 1052 | htag[0] = 'h'; |
| 1053 | htag[1] = '0' + hlevel; |
| 1054 | htag[2] = '\0'; |
| 1055 | element_open(&ho, htag); |
| 1056 | |
| 1057 | /* |
| 1058 | * Provide anchor for cross-links to target. |
| 1059 | * |
| 1060 | * (Also we'll have to do this separately in |
| 1061 | * other paragraph types - NumberedList and |
| 1062 | * BiblioCited.) |
| 1063 | */ |
| 1064 | if (s->fragment) |
| 1065 | html_fragment(&ho, s->fragment); |
| 1066 | |
| 1067 | html_section_title(&ho, s, f, keywords, &conf, TRUE); |
| 1068 | |
| 1069 | element_close(&ho, htag); |
| 1070 | |
| 1071 | /* |
| 1072 | * Now display the section text. |
| 1073 | */ |
| 1074 | if (s->text) { |
| 1075 | stackhead = snew(struct stackelement); |
| 1076 | stackhead->next = NULL; |
| 1077 | stackhead->listtype = NOLIST; |
| 1078 | stackhead->itemtype = NOITEM; |
| 1079 | |
| 1080 | for (p = s->text;; p = p->next) { |
| 1081 | enum LISTTYPE listtype; |
| 1082 | struct stackelement *se; |
| 1083 | |
| 1084 | /* |
| 1085 | * Preliminary switch to figure out what |
| 1086 | * sort of list we expect to be inside at |
| 1087 | * this stage. |
| 1088 | * |
| 1089 | * Since p may still be NULL at this point, |
| 1090 | * I invent a harmless paragraph type for |
| 1091 | * it if it is. |
| 1092 | */ |
| 1093 | switch (p ? p->type : para_Normal) { |
| 1094 | case para_Rule: |
| 1095 | case para_Normal: |
| 1096 | case para_Copyright: |
| 1097 | case para_BiblioCited: |
| 1098 | case para_Code: |
| 1099 | case para_QuotePush: |
| 1100 | case para_QuotePop: |
| 1101 | case para_Chapter: |
| 1102 | case para_Appendix: |
| 1103 | case para_UnnumberedChapter: |
| 1104 | case para_Heading: |
| 1105 | case para_Subsect: |
| 1106 | case para_LcontPop: |
| 1107 | listtype = NOLIST; |
| 1108 | break; |
| 1109 | |
| 1110 | case para_Bullet: |
| 1111 | listtype = UL; |
| 1112 | break; |
| 1113 | |
| 1114 | case para_NumberedList: |
| 1115 | listtype = OL; |
| 1116 | break; |
| 1117 | |
| 1118 | case para_DescribedThing: |
| 1119 | case para_Description: |
| 1120 | listtype = DL; |
| 1121 | break; |
| 1122 | |
| 1123 | case para_LcontPush: |
| 1124 | se = snew(struct stackelement); |
| 1125 | se->next = stackhead; |
| 1126 | se->listtype = NOLIST; |
| 1127 | se->itemtype = NOITEM; |
| 1128 | stackhead = se; |
| 1129 | continue; |
| 1130 | |
| 1131 | default: /* some totally non-printing para */ |
| 1132 | continue; |
| 1133 | } |
| 1134 | |
| 1135 | html_nl(&ho); |
| 1136 | |
| 1137 | /* |
| 1138 | * Terminate the most recent list item, if |
| 1139 | * any. (We left this until after |
| 1140 | * processing LcontPush, since in that case |
| 1141 | * the list item won't want to be |
| 1142 | * terminated until after the corresponding |
| 1143 | * LcontPop.) |
| 1144 | */ |
| 1145 | if (stackhead->itemtype != NOITEM) { |
| 1146 | element_close(&ho, itemname(stackhead->itemtype)); |
| 1147 | html_nl(&ho); |
| 1148 | } |
| 1149 | stackhead->itemtype = NOITEM; |
| 1150 | |
| 1151 | /* |
| 1152 | * Terminate the current list, if it's not |
| 1153 | * the one we want to be in. |
| 1154 | */ |
| 1155 | if (listtype != stackhead->listtype && |
| 1156 | stackhead->listtype != NOLIST) { |
| 1157 | element_close(&ho, listname(stackhead->listtype)); |
| 1158 | html_nl(&ho); |
| 1159 | } |
| 1160 | |
| 1161 | /* |
| 1162 | * Leave the loop if our time has come. |
| 1163 | */ |
| 1164 | if (!p || (is_heading_type(p->type) && |
| 1165 | p->type != para_Title)) |
| 1166 | break; /* end of section text */ |
| 1167 | |
| 1168 | /* |
| 1169 | * Start a fresh list if necessary. |
| 1170 | */ |
| 1171 | if (listtype != stackhead->listtype && |
| 1172 | listtype != NOLIST) |
| 1173 | element_open(&ho, listname(listtype)); |
| 1174 | |
| 1175 | stackhead->listtype = listtype; |
| 1176 | |
| 1177 | switch (p->type) { |
| 1178 | case para_Rule: |
| 1179 | element_empty(&ho, "hr"); |
| 1180 | break; |
| 1181 | case para_Code: |
| 1182 | html_codepara(&ho, p->words); |
| 1183 | break; |
| 1184 | case para_Normal: |
| 1185 | case para_Copyright: |
| 1186 | element_open(&ho, "p"); |
| 1187 | html_nl(&ho); |
| 1188 | html_words(&ho, p->words, ALL, |
| 1189 | f, keywords, &conf); |
| 1190 | html_nl(&ho); |
| 1191 | element_close(&ho, "p"); |
| 1192 | break; |
| 1193 | case para_BiblioCited: |
| 1194 | element_open(&ho, "p"); |
| 1195 | if (p->private_data) { |
| 1196 | htmlsect *s = (htmlsect *)p->private_data; |
| 1197 | html_fragment(&ho, s->fragment); |
| 1198 | } |
| 1199 | html_nl(&ho); |
| 1200 | html_words(&ho, p->kwtext, ALL, |
| 1201 | f, keywords, &conf); |
| 1202 | html_text(&ho, L" "); |
| 1203 | html_words(&ho, p->words, ALL, |
| 1204 | f, keywords, &conf); |
| 1205 | html_nl(&ho); |
| 1206 | element_close(&ho, "p"); |
| 1207 | break; |
| 1208 | case para_Bullet: |
| 1209 | case para_NumberedList: |
| 1210 | element_open(&ho, "li"); |
| 1211 | if (p->private_data) { |
| 1212 | htmlsect *s = (htmlsect *)p->private_data; |
| 1213 | html_fragment(&ho, s->fragment); |
| 1214 | } |
| 1215 | html_nl(&ho); |
| 1216 | stackhead->itemtype = LI; |
| 1217 | html_words(&ho, p->words, ALL, |
| 1218 | f, keywords, &conf); |
| 1219 | break; |
| 1220 | case para_DescribedThing: |
| 1221 | element_open(&ho, "dt"); |
| 1222 | html_nl(&ho); |
| 1223 | stackhead->itemtype = DT; |
| 1224 | html_words(&ho, p->words, ALL, |
| 1225 | f, keywords, &conf); |
| 1226 | break; |
| 1227 | case para_Description: |
| 1228 | element_open(&ho, "dd"); |
| 1229 | html_nl(&ho); |
| 1230 | stackhead->itemtype = DD; |
| 1231 | html_words(&ho, p->words, ALL, |
| 1232 | f, keywords, &conf); |
| 1233 | break; |
| 1234 | |
| 1235 | case para_QuotePush: |
| 1236 | element_open(&ho, "blockquote"); |
| 1237 | break; |
| 1238 | case para_QuotePop: |
| 1239 | element_close(&ho, "blockquote"); |
| 1240 | break; |
| 1241 | |
| 1242 | case para_LcontPop: |
| 1243 | se = stackhead; |
| 1244 | stackhead = stackhead->next; |
| 1245 | assert(stackhead); |
| 1246 | sfree(se); |
| 1247 | break; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | assert(stackhead && !stackhead->next); |
| 1252 | sfree(stackhead); |
| 1253 | } |
| 1254 | |
| 1255 | if (s->type == INDEX) { |
| 1256 | indexentry *entry; |
| 1257 | int i; |
| 1258 | |
| 1259 | /* |
| 1260 | * This section is the index. I'll just |
| 1261 | * render it as a single paragraph, with a |
| 1262 | * colon between the index term and the |
| 1263 | * references, and <br> in between each |
| 1264 | * entry. |
| 1265 | */ |
| 1266 | element_open(&ho, "p"); |
| 1267 | |
| 1268 | for (i = 0; (entry = |
| 1269 | index234(idx->entries, i)) != NULL; i++) { |
| 1270 | htmlindex *hi = (htmlindex *)entry->backend_data; |
| 1271 | int j; |
| 1272 | |
| 1273 | if (i > 0) |
| 1274 | element_empty(&ho, "br"); |
| 1275 | html_nl(&ho); |
| 1276 | |
| 1277 | html_words(&ho, entry->text, MARKUP|LINKS, |
| 1278 | f, keywords, &conf); |
| 1279 | |
| 1280 | html_text(&ho, conf.index_main_sep); |
| 1281 | |
| 1282 | for (j = 0; j < hi->nrefs; j++) { |
| 1283 | htmlindexref *hr = |
| 1284 | (htmlindexref *)hi->refs[j]->private_data; |
| 1285 | paragraph *p = hr->section->title; |
| 1286 | |
| 1287 | if (j > 0) |
| 1288 | html_text(&ho, conf.index_multi_sep); |
| 1289 | |
| 1290 | html_href(&ho, f, hr->section->file, |
| 1291 | hr->fragment); |
| 1292 | hr->referenced = TRUE; |
| 1293 | if (p && p->kwtext) |
| 1294 | html_words(&ho, p->kwtext, MARKUP|LINKS, |
| 1295 | f, keywords, &conf); |
| 1296 | else if (p && p->words) |
| 1297 | html_words(&ho, p->words, MARKUP|LINKS, |
| 1298 | f, keywords, &conf); |
| 1299 | else { |
| 1300 | /* |
| 1301 | * If there is no title at all, |
| 1302 | * this must be because our |
| 1303 | * target section is the |
| 1304 | * preamble section and there |
| 1305 | * is no title. So we use the |
| 1306 | * preamble_text. |
| 1307 | */ |
| 1308 | html_text(&ho, conf.preamble_text); |
| 1309 | } |
| 1310 | element_close(&ho, "a"); |
| 1311 | } |
| 1312 | } |
| 1313 | element_close(&ho, "p"); |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | html_contents_entry(&ho, 0, NULL, f, keywords, &conf); |
| 1319 | html_nl(&ho); |
| 1320 | |
| 1321 | { |
| 1322 | /* |
| 1323 | * Footer. |
| 1324 | */ |
| 1325 | int done_version_ids = FALSE; |
| 1326 | |
| 1327 | element_empty(&ho, "hr"); |
| 1328 | |
| 1329 | if (conf.body_end) |
| 1330 | html_raw(&ho, conf.body_end); |
| 1331 | |
| 1332 | if (conf.address_section) { |
| 1333 | int started = FALSE; |
| 1334 | if (conf.htmlver == ISO_HTML) { |
| 1335 | /* |
| 1336 | * The ISO-HTML validator complains if |
| 1337 | * there isn't a <div> tag surrounding the |
| 1338 | * <address> tag. I'm uncertain of why this |
| 1339 | * should be - there appears to be no |
| 1340 | * mention of this in the ISO-HTML spec, |
| 1341 | * suggesting that it doesn't represent a |
| 1342 | * change from HTML 4, but nonetheless the |
| 1343 | * HTML 4 validator doesn't seem to mind. |
| 1344 | */ |
| 1345 | element_open(&ho, "div"); |
| 1346 | } |
| 1347 | element_open(&ho, "address"); |
| 1348 | if (conf.addr_start) { |
| 1349 | html_raw(&ho, conf.addr_start); |
| 1350 | html_nl(&ho); |
| 1351 | started = TRUE; |
| 1352 | } |
| 1353 | if (conf.visible_version_id) { |
| 1354 | for (p = sourceform; p; p = p->next) |
| 1355 | if (p->type == para_VersionID) { |
| 1356 | if (started) |
| 1357 | element_empty(&ho, "br"); |
| 1358 | html_nl(&ho); |
| 1359 | html_text(&ho, conf.pre_versionid); |
| 1360 | html_words(&ho, p->words, NOTHING, |
| 1361 | f, keywords, &conf); |
| 1362 | html_text(&ho, conf.post_versionid); |
| 1363 | started = TRUE; |
| 1364 | } |
| 1365 | done_version_ids = TRUE; |
| 1366 | } |
| 1367 | if (conf.addr_end) { |
| 1368 | if (started) |
| 1369 | element_empty(&ho, "br"); |
| 1370 | html_raw(&ho, conf.addr_end); |
| 1371 | } |
| 1372 | element_close(&ho, "address"); |
| 1373 | if (conf.htmlver == ISO_HTML) |
| 1374 | element_close(&ho, "div"); |
| 1375 | } |
| 1376 | |
| 1377 | if (!done_version_ids) { |
| 1378 | /* |
| 1379 | * If the user didn't want the version IDs |
| 1380 | * visible, I think we still have a duty to put |
| 1381 | * them in an HTML comment. |
| 1382 | */ |
| 1383 | int started = FALSE; |
| 1384 | for (p = sourceform; p; p = p->next) |
| 1385 | if (p->type == para_VersionID) { |
| 1386 | if (!started) { |
| 1387 | html_raw(&ho, "<!-- version IDs:\n"); |
| 1388 | started = TRUE; |
| 1389 | } |
| 1390 | html_words(&ho, p->words, NOTHING, |
| 1391 | f, keywords, &conf); |
| 1392 | html_nl(&ho); |
| 1393 | } |
| 1394 | if (started) |
| 1395 | html_raw(&ho, "-->\n"); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | element_close(&ho, "body"); |
| 1400 | html_nl(&ho); |
| 1401 | element_close(&ho, "html"); |
| 1402 | html_nl(&ho); |
| 1403 | cleanup(&ho); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * Go through and check that no index fragments were referenced |
| 1409 | * without being generated, or indeed vice versa. |
| 1410 | * |
| 1411 | * (When I actually get round to freeing everything, this can |
| 1412 | * probably be the freeing loop as well.) |
| 1413 | */ |
| 1414 | for (p = sourceform; p; p = p->next) { |
| 1415 | word *w; |
| 1416 | for (w = p->words; w; w = w->next) |
| 1417 | if (w->type == word_IndexRef) { |
| 1418 | htmlindexref *hr = (htmlindexref *)w->private_data; |
| 1419 | |
| 1420 | assert(!hr->referenced == !hr->generated); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Free all the working data. |
| 1426 | */ |
| 1427 | sfree(conf.asect); |
| 1428 | sfree(conf.single_filename); |
| 1429 | sfree(conf.contents_filename); |
| 1430 | sfree(conf.index_filename); |
| 1431 | sfree(conf.template_filename); |
| 1432 | sfree(conf.template_fragment); |
| 1433 | { |
| 1434 | htmlfragment *frag; |
| 1435 | while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) { |
| 1436 | /* |
| 1437 | * frag->fragment is dynamically allocated, but will be |
| 1438 | * freed when we process the htmlsect structure which |
| 1439 | * it is attached to. |
| 1440 | */ |
| 1441 | sfree(frag); |
| 1442 | } |
| 1443 | freetree234(files.frags); |
| 1444 | } |
| 1445 | { |
| 1446 | htmlsect *sect, *tmp; |
| 1447 | sect = sects.head; |
| 1448 | while (sect) { |
| 1449 | tmp = sect->next; |
| 1450 | sfree(sect->fragment); |
| 1451 | sfree(sect); |
| 1452 | sect = tmp; |
| 1453 | } |
| 1454 | sect = nonsects.head; |
| 1455 | while (sect) { |
| 1456 | tmp = sect->next; |
| 1457 | sfree(sect->fragment); |
| 1458 | sfree(sect); |
| 1459 | sect = tmp; |
| 1460 | } |
| 1461 | } |
| 1462 | { |
| 1463 | htmlfile *file, *tmp; |
| 1464 | file = files.head; |
| 1465 | while (file) { |
| 1466 | tmp = file->next; |
| 1467 | sfree(file->filename); |
| 1468 | sfree(file); |
| 1469 | file = tmp; |
| 1470 | } |
| 1471 | } |
| 1472 | { |
| 1473 | int i; |
| 1474 | indexentry *entry; |
| 1475 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
| 1476 | htmlindex *hi = (htmlindex *)entry->backend_data; |
| 1477 | sfree(hi); |
| 1478 | } |
| 1479 | } |
| 1480 | { |
| 1481 | paragraph *p; |
| 1482 | word *w; |
| 1483 | for (p = sourceform; p; p = p->next) |
| 1484 | for (w = p->words; w; w = w->next) |
| 1485 | if (w->type == word_IndexRef) { |
| 1486 | htmlindexref *hr = (htmlindexref *)w->private_data; |
| 1487 | assert(hr != NULL); |
| 1488 | sfree(hr->fragment); |
| 1489 | sfree(hr); |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | static void html_file_section(htmlconfig *cfg, htmlfilelist *files, |
| 1495 | htmlsect *sect, int depth) |
| 1496 | { |
| 1497 | htmlfile *file; |
| 1498 | int ldepth; |
| 1499 | |
| 1500 | /* |
| 1501 | * `depth' is derived from the heading_depth() macro at the top |
| 1502 | * of this file, which counts title as -1, chapter as 0, |
| 1503 | * heading as 1 and subsection as 2. However, the semantics of |
| 1504 | * cfg->leaf_level are defined to count chapter as 1, heading |
| 1505 | * as 2 etc. So first I increment depth :-( |
| 1506 | */ |
| 1507 | ldepth = depth + 1; |
| 1508 | |
| 1509 | if (cfg->leaf_level == 0) { |
| 1510 | /* |
| 1511 | * leaf_level==0 is a special case, in which everything is |
| 1512 | * put into a single file. |
| 1513 | */ |
| 1514 | if (!files->single) |
| 1515 | files->single = html_new_file(files, cfg->single_filename); |
| 1516 | |
| 1517 | file = files->single; |
| 1518 | } else { |
| 1519 | /* |
| 1520 | * If the depth of this section is at or above leaf_level, |
| 1521 | * we invent a fresh file and put this section at its head. |
| 1522 | * Otherwise, we put it in the same file as its parent |
| 1523 | * section. |
| 1524 | */ |
| 1525 | if (ldepth > cfg->leaf_level) { |
| 1526 | /* |
| 1527 | * We know that sect->parent cannot be NULL. The only |
| 1528 | * circumstance in which it can be is if sect is at |
| 1529 | * chapter or appendix level, i.e. ldepth==1; and if |
| 1530 | * that's the case, then we cannot have entered this |
| 1531 | * branch unless cfg->leaf_level==0, in which case we |
| 1532 | * would be in the single-file case above and not here |
| 1533 | * at all. |
| 1534 | */ |
| 1535 | assert(sect->parent); |
| 1536 | |
| 1537 | file = sect->parent->file; |
| 1538 | } else { |
| 1539 | if (sect->type == TOP) { |
| 1540 | file = html_new_file(files, cfg->contents_filename); |
| 1541 | } else if (sect->type == INDEX) { |
| 1542 | file = html_new_file(files, cfg->index_filename); |
| 1543 | } else { |
| 1544 | char *title; |
| 1545 | |
| 1546 | assert(ldepth > 0 && sect->title); |
| 1547 | title = html_format(sect->title, cfg->template_filename); |
| 1548 | file = html_new_file(files, title); |
| 1549 | sfree(title); |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | sect->file = file; |
| 1555 | |
| 1556 | if (file->min_heading_depth > depth) { |
| 1557 | /* |
| 1558 | * This heading is at a higher level than any heading we |
| 1559 | * have so far placed in this file; so we set the `first' |
| 1560 | * pointer. |
| 1561 | */ |
| 1562 | file->min_heading_depth = depth; |
| 1563 | file->first = sect; |
| 1564 | } |
| 1565 | |
| 1566 | if (file->min_heading_depth == depth) |
| 1567 | file->last = sect; |
| 1568 | } |
| 1569 | |
| 1570 | static htmlfile *html_new_file(htmlfilelist *list, char *filename) |
| 1571 | { |
| 1572 | htmlfile *ret = snew(htmlfile); |
| 1573 | |
| 1574 | ret->next = NULL; |
| 1575 | if (list->tail) |
| 1576 | list->tail->next = ret; |
| 1577 | else |
| 1578 | list->head = ret; |
| 1579 | list->tail = ret; |
| 1580 | |
| 1581 | ret->filename = dupstr(filename); |
| 1582 | ret->last_fragment_number = 0; |
| 1583 | ret->min_heading_depth = INT_MAX; |
| 1584 | ret->first = ret->last = NULL; |
| 1585 | |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
| 1589 | static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title) |
| 1590 | { |
| 1591 | htmlsect *ret = snew(htmlsect); |
| 1592 | |
| 1593 | ret->next = NULL; |
| 1594 | if (list->tail) |
| 1595 | list->tail->next = ret; |
| 1596 | else |
| 1597 | list->head = ret; |
| 1598 | list->tail = ret; |
| 1599 | |
| 1600 | ret->title = title; |
| 1601 | ret->file = NULL; |
| 1602 | ret->parent = NULL; |
| 1603 | ret->type = NORMAL; |
| 1604 | |
| 1605 | return ret; |
| 1606 | } |
| 1607 | |
| 1608 | static void html_words(htmloutput *ho, word *words, int flags, |
| 1609 | htmlfile *file, keywordlist *keywords, htmlconfig *cfg) |
| 1610 | { |
| 1611 | word *w; |
| 1612 | char *c; |
| 1613 | int style, type; |
| 1614 | |
| 1615 | for (w = words; w; w = w->next) switch (w->type) { |
| 1616 | case word_HyperLink: |
| 1617 | if (flags & LINKS) { |
| 1618 | element_open(ho, "a"); |
| 1619 | c = utoa_dup(w->text, CS_ASCII); |
| 1620 | element_attr(ho, "href", c); |
| 1621 | sfree(c); |
| 1622 | } |
| 1623 | break; |
| 1624 | case word_UpperXref: |
| 1625 | case word_LowerXref: |
| 1626 | if (flags & LINKS) { |
| 1627 | keyword *kwl = kw_lookup(keywords, w->text); |
| 1628 | paragraph *p; |
| 1629 | htmlsect *s; |
| 1630 | |
| 1631 | assert(kwl); |
| 1632 | p = kwl->para; |
| 1633 | s = (htmlsect *)p->private_data; |
| 1634 | |
| 1635 | assert(s); |
| 1636 | |
| 1637 | html_href(ho, file, s->file, s->fragment); |
| 1638 | } |
| 1639 | break; |
| 1640 | case word_HyperEnd: |
| 1641 | case word_XrefEnd: |
| 1642 | if (flags & LINKS) |
| 1643 | element_close(ho, "a"); |
| 1644 | break; |
| 1645 | case word_IndexRef: |
| 1646 | if (flags & INDEXENTS) { |
| 1647 | htmlindexref *hr = (htmlindexref *)w->private_data; |
| 1648 | html_fragment(ho, hr->fragment); |
| 1649 | hr->generated = TRUE; |
| 1650 | } |
| 1651 | break; |
| 1652 | case word_Normal: |
| 1653 | case word_Emph: |
| 1654 | case word_Code: |
| 1655 | case word_WeakCode: |
| 1656 | case word_WhiteSpace: |
| 1657 | case word_EmphSpace: |
| 1658 | case word_CodeSpace: |
| 1659 | case word_WkCodeSpace: |
| 1660 | case word_Quote: |
| 1661 | case word_EmphQuote: |
| 1662 | case word_CodeQuote: |
| 1663 | case word_WkCodeQuote: |
| 1664 | style = towordstyle(w->type); |
| 1665 | type = removeattr(w->type); |
| 1666 | if (style == word_Emph && |
| 1667 | (attraux(w->aux) == attr_First || |
| 1668 | attraux(w->aux) == attr_Only) && |
| 1669 | (flags & MARKUP)) |
| 1670 | element_open(ho, "em"); |
| 1671 | else if ((style == word_Code || style == word_WeakCode) && |
| 1672 | (attraux(w->aux) == attr_First || |
| 1673 | attraux(w->aux) == attr_Only) && |
| 1674 | (flags & MARKUP)) |
| 1675 | element_open(ho, "code"); |
| 1676 | |
| 1677 | if (type == word_WhiteSpace) |
| 1678 | html_text(ho, L" "); |
| 1679 | else if (type == word_Quote) { |
| 1680 | if (quoteaux(w->aux) == quote_Open) |
| 1681 | html_text(ho, cfg->lquote); |
| 1682 | else |
| 1683 | html_text(ho, cfg->rquote); |
| 1684 | } else { |
| 1685 | if (!w->alt || cvt_ok(ho->restrict_charset, w->text)) |
| 1686 | html_text_nbsp(ho, w->text); |
| 1687 | else |
| 1688 | html_words(ho, w->alt, flags, file, keywords, cfg); |
| 1689 | } |
| 1690 | |
| 1691 | if (style == word_Emph && |
| 1692 | (attraux(w->aux) == attr_Last || |
| 1693 | attraux(w->aux) == attr_Only) && |
| 1694 | (flags & MARKUP)) |
| 1695 | element_close(ho, "em"); |
| 1696 | else if ((style == word_Code || style == word_WeakCode) && |
| 1697 | (attraux(w->aux) == attr_Last || |
| 1698 | attraux(w->aux) == attr_Only) && |
| 1699 | (flags & MARKUP)) |
| 1700 | element_close(ho, "code"); |
| 1701 | |
| 1702 | break; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | static void html_codepara(htmloutput *ho, word *words) |
| 1707 | { |
| 1708 | element_open(ho, "pre"); |
| 1709 | element_open(ho, "code"); |
| 1710 | for (; words; words = words->next) if (words->type == word_WeakCode) { |
| 1711 | char *open_tag; |
| 1712 | wchar_t *t, *e; |
| 1713 | |
| 1714 | t = words->text; |
| 1715 | if (words->next && words->next->type == word_Emph) { |
| 1716 | e = words->next->text; |
| 1717 | words = words->next; |
| 1718 | } else |
| 1719 | e = NULL; |
| 1720 | |
| 1721 | while (e && *e && *t) { |
| 1722 | int n; |
| 1723 | int ec = *e; |
| 1724 | |
| 1725 | for (n = 0; t[n] && e[n] && e[n] == ec; n++); |
| 1726 | |
| 1727 | open_tag = NULL; |
| 1728 | if (ec == 'i') |
| 1729 | open_tag = "em"; |
| 1730 | else if (ec == 'b') |
| 1731 | open_tag = "b"; |
| 1732 | if (open_tag) |
| 1733 | element_open(ho, open_tag); |
| 1734 | |
| 1735 | html_text_limit(ho, t, n); |
| 1736 | |
| 1737 | if (open_tag) |
| 1738 | element_close(ho, open_tag); |
| 1739 | |
| 1740 | t += n; |
| 1741 | e += n; |
| 1742 | } |
| 1743 | html_text(ho, t); |
| 1744 | html_nl(ho); |
| 1745 | } |
| 1746 | element_close(ho, "code"); |
| 1747 | element_close(ho, "pre"); |
| 1748 | } |
| 1749 | |
| 1750 | static void html_charset_cleanup(htmloutput *ho) |
| 1751 | { |
| 1752 | char outbuf[256]; |
| 1753 | int bytes; |
| 1754 | |
| 1755 | bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf), |
| 1756 | ho->charset, &ho->cstate, NULL); |
| 1757 | if (bytes > 0) |
| 1758 | fwrite(outbuf, 1, bytes, ho->fp); |
| 1759 | } |
| 1760 | |
| 1761 | static void return_mostly_to_neutral(htmloutput *ho) |
| 1762 | { |
| 1763 | if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) { |
| 1764 | fprintf(ho->fp, " />"); |
| 1765 | } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) { |
| 1766 | fprintf(ho->fp, ">"); |
| 1767 | } |
| 1768 | |
| 1769 | ho->state = HO_NEUTRAL; |
| 1770 | } |
| 1771 | |
| 1772 | static void return_to_neutral(htmloutput *ho) |
| 1773 | { |
| 1774 | if (ho->state == HO_IN_TEXT) { |
| 1775 | html_charset_cleanup(ho); |
| 1776 | } |
| 1777 | |
| 1778 | return_mostly_to_neutral(ho); |
| 1779 | } |
| 1780 | |
| 1781 | static void element_open(htmloutput *ho, char const *name) |
| 1782 | { |
| 1783 | return_to_neutral(ho); |
| 1784 | fprintf(ho->fp, "<%s", name); |
| 1785 | ho->state = HO_IN_TAG; |
| 1786 | } |
| 1787 | |
| 1788 | static void element_close(htmloutput *ho, char const *name) |
| 1789 | { |
| 1790 | return_to_neutral(ho); |
| 1791 | fprintf(ho->fp, "</%s>", name); |
| 1792 | ho->state = HO_NEUTRAL; |
| 1793 | } |
| 1794 | |
| 1795 | static void element_empty(htmloutput *ho, char const *name) |
| 1796 | { |
| 1797 | return_to_neutral(ho); |
| 1798 | fprintf(ho->fp, "<%s", name); |
| 1799 | ho->state = HO_IN_EMPTY_TAG; |
| 1800 | } |
| 1801 | |
| 1802 | static void html_nl(htmloutput *ho) |
| 1803 | { |
| 1804 | return_to_neutral(ho); |
| 1805 | fputc('\n', ho->fp); |
| 1806 | } |
| 1807 | |
| 1808 | static void html_raw(htmloutput *ho, char *text) |
| 1809 | { |
| 1810 | return_to_neutral(ho); |
| 1811 | fputs(text, ho->fp); |
| 1812 | } |
| 1813 | |
| 1814 | static void html_raw_as_attr(htmloutput *ho, char *text) |
| 1815 | { |
| 1816 | assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG); |
| 1817 | fputc(' ', ho->fp); |
| 1818 | fputs(text, ho->fp); |
| 1819 | } |
| 1820 | |
| 1821 | static void element_attr(htmloutput *ho, char const *name, char const *value) |
| 1822 | { |
| 1823 | html_charset_cleanup(ho); |
| 1824 | assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG); |
| 1825 | fprintf(ho->fp, " %s=\"%s\"", name, value); |
| 1826 | } |
| 1827 | |
| 1828 | static void element_attr_w(htmloutput *ho, char const *name, |
| 1829 | wchar_t const *value) |
| 1830 | { |
| 1831 | html_charset_cleanup(ho); |
| 1832 | fprintf(ho->fp, " %s=\"", name); |
| 1833 | html_text_limit_internal(ho, value, 0, TRUE, FALSE); |
| 1834 | html_charset_cleanup(ho); |
| 1835 | fputc('"', ho->fp); |
| 1836 | } |
| 1837 | |
| 1838 | static void html_text(htmloutput *ho, wchar_t const *text) |
| 1839 | { |
| 1840 | return_mostly_to_neutral(ho); |
| 1841 | html_text_limit_internal(ho, text, 0, FALSE, FALSE); |
| 1842 | } |
| 1843 | |
| 1844 | static void html_text_nbsp(htmloutput *ho, wchar_t const *text) |
| 1845 | { |
| 1846 | return_mostly_to_neutral(ho); |
| 1847 | html_text_limit_internal(ho, text, 0, FALSE, TRUE); |
| 1848 | } |
| 1849 | |
| 1850 | static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen) |
| 1851 | { |
| 1852 | return_mostly_to_neutral(ho); |
| 1853 | html_text_limit_internal(ho, text, maxlen, FALSE, FALSE); |
| 1854 | } |
| 1855 | |
| 1856 | static void html_text_limit_internal(htmloutput *ho, wchar_t const *text, |
| 1857 | int maxlen, int quote_quotes, int nbsp) |
| 1858 | { |
| 1859 | int textlen = ustrlen(text); |
| 1860 | char outbuf[256]; |
| 1861 | int bytes, err; |
| 1862 | |
| 1863 | if (maxlen > 0 && textlen > maxlen) |
| 1864 | textlen = maxlen; |
| 1865 | |
| 1866 | while (textlen > 0) { |
| 1867 | /* Scan ahead for characters we really can't display in HTML. */ |
| 1868 | int lenbefore, lenafter; |
| 1869 | for (lenbefore = 0; lenbefore < textlen; lenbefore++) |
| 1870 | if (text[lenbefore] == L'<' || |
| 1871 | text[lenbefore] == L'>' || |
| 1872 | text[lenbefore] == L'&' || |
| 1873 | (text[lenbefore] == L'"' && quote_quotes) || |
| 1874 | (text[lenbefore] == L' ' && nbsp)) |
| 1875 | break; |
| 1876 | lenafter = lenbefore; |
| 1877 | bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf), |
| 1878 | ho->charset, &ho->cstate, &err); |
| 1879 | textlen -= (lenbefore - lenafter); |
| 1880 | if (bytes > 0) |
| 1881 | fwrite(outbuf, 1, bytes, ho->fp); |
| 1882 | if (err) { |
| 1883 | /* |
| 1884 | * We have encountered a character that cannot be |
| 1885 | * displayed in the selected output charset. Therefore, |
| 1886 | * we use an HTML numeric entity reference. |
| 1887 | */ |
| 1888 | assert(textlen > 0); |
| 1889 | fprintf(ho->fp, "&#%ld;", (long int)*text); |
| 1890 | text++, textlen--; |
| 1891 | } else if (lenafter == 0 && textlen > 0) { |
| 1892 | /* |
| 1893 | * We have encountered a character which is special to |
| 1894 | * HTML. |
| 1895 | */ |
| 1896 | if (*text == L'<') |
| 1897 | fprintf(ho->fp, "<"); |
| 1898 | else if (*text == L'>') |
| 1899 | fprintf(ho->fp, ">"); |
| 1900 | else if (*text == L'&') |
| 1901 | fprintf(ho->fp, "&"); |
| 1902 | else if (*text == L'"') |
| 1903 | fprintf(ho->fp, """); |
| 1904 | else if (*text == L' ') { |
| 1905 | assert(nbsp); |
| 1906 | fprintf(ho->fp, " "); |
| 1907 | } else |
| 1908 | assert(!"Can't happen"); |
| 1909 | text++, textlen--; |
| 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | static void cleanup(htmloutput *ho) |
| 1915 | { |
| 1916 | return_to_neutral(ho); |
| 1917 | fclose(ho->fp); |
| 1918 | } |
| 1919 | |
| 1920 | static void html_href(htmloutput *ho, htmlfile *thisfile, |
| 1921 | htmlfile *targetfile, char *targetfrag) |
| 1922 | { |
| 1923 | rdstringc rs = { 0, 0, NULL }; |
| 1924 | char *url; |
| 1925 | |
| 1926 | if (targetfile != thisfile) |
| 1927 | rdaddsc(&rs, targetfile->filename); |
| 1928 | if (targetfrag) { |
| 1929 | rdaddc(&rs, '#'); |
| 1930 | rdaddsc(&rs, targetfrag); |
| 1931 | } |
| 1932 | url = rs.text; |
| 1933 | |
| 1934 | element_open(ho, "a"); |
| 1935 | element_attr(ho, "href", url); |
| 1936 | sfree(url); |
| 1937 | } |
| 1938 | |
| 1939 | static void html_fragment(htmloutput *ho, char const *fragment) |
| 1940 | { |
| 1941 | element_open(ho, "a"); |
| 1942 | element_attr(ho, "name", fragment); |
| 1943 | if (is_xhtml(ho->ver)) |
| 1944 | element_attr(ho, "id", fragment); |
| 1945 | element_close(ho, "a"); |
| 1946 | } |
| 1947 | |
| 1948 | static char *html_format(paragraph *p, char *template_string) |
| 1949 | { |
| 1950 | char *c, *t; |
| 1951 | word *w; |
| 1952 | wchar_t *ws, wsbuf[2]; |
| 1953 | rdstringc rs = { 0, 0, NULL }; |
| 1954 | |
| 1955 | t = template_string; |
| 1956 | while (*t) { |
| 1957 | if (*t == '%' && t[1]) { |
| 1958 | int fmt; |
| 1959 | |
| 1960 | t++; |
| 1961 | fmt = *t++; |
| 1962 | |
| 1963 | if (fmt == '%') { |
| 1964 | rdaddc(&rs, fmt); |
| 1965 | continue; |
| 1966 | } |
| 1967 | |
| 1968 | w = NULL; |
| 1969 | ws = NULL; |
| 1970 | |
| 1971 | if (p->kwtext && fmt == 'n') |
| 1972 | w = p->kwtext; |
| 1973 | else if (p->kwtext2 && fmt == 'b') { |
| 1974 | /* |
| 1975 | * HTML fragment names must start with a letter, so |
| 1976 | * simply `1.2.3' is not adequate. In this case I'm |
| 1977 | * going to cheat slightly by prepending the first |
| 1978 | * character of the first word of kwtext, so that |
| 1979 | * we get `C1' for chapter 1, `S2.3' for section |
| 1980 | * 2.3 etc. |
| 1981 | */ |
| 1982 | if (p->kwtext && p->kwtext->text[0]) { |
| 1983 | ws = wsbuf; |
| 1984 | wsbuf[1] = '\0'; |
| 1985 | wsbuf[0] = p->kwtext->text[0]; |
| 1986 | } |
| 1987 | w = p->kwtext2; |
| 1988 | } else if (p->keyword && *p->keyword && fmt == 'k') |
| 1989 | ws = p->keyword; |
| 1990 | else |
| 1991 | w = p->words; |
| 1992 | |
| 1993 | if (ws) { |
| 1994 | c = utoa_dup(ws, CS_ASCII); |
| 1995 | rdaddsc(&rs,c); |
| 1996 | sfree(c); |
| 1997 | } |
| 1998 | |
| 1999 | while (w) { |
| 2000 | if (removeattr(w->type) == word_Normal) { |
| 2001 | c = utoa_dup(w->text, CS_ASCII); |
| 2002 | rdaddsc(&rs,c); |
| 2003 | sfree(c); |
| 2004 | } |
| 2005 | w = w->next; |
| 2006 | } |
| 2007 | } else { |
| 2008 | rdaddc(&rs, *t++); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | return rdtrimc(&rs); |
| 2013 | } |
| 2014 | |
| 2015 | static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file, |
| 2016 | char *text) |
| 2017 | { |
| 2018 | /* |
| 2019 | * The HTML 4 spec's strictest definition of fragment names (<a |
| 2020 | * name> and "id" attributes) says that they `must begin with a |
| 2021 | * letter and may be followed by any number of letters, digits, |
| 2022 | * hyphens, underscores, colons, and periods'. |
| 2023 | * |
| 2024 | * So here we unceremoniously rip out any characters not |
| 2025 | * conforming to this limitation. |
| 2026 | */ |
| 2027 | char *p = text, *q = text; |
| 2028 | |
| 2029 | while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z'))) |
| 2030 | p++; |
| 2031 | if ((*q++ = *p++) != '\0') { |
| 2032 | while (*p) { |
| 2033 | if ((*p>='A' && *p<='Z') || |
| 2034 | (*p>='a' && *p<='z') || |
| 2035 | (*p>='0' && *p<='9') || |
| 2036 | *p=='-' || *p=='_' || *p==':' || *p=='.') |
| 2037 | *q++ = *p; |
| 2038 | p++; |
| 2039 | } |
| 2040 | |
| 2041 | *q = '\0'; |
| 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * Now we check for clashes with other fragment names, and |
| 2046 | * adjust this one if necessary by appending a hyphen followed |
| 2047 | * by a number. |
| 2048 | */ |
| 2049 | { |
| 2050 | htmlfragment *frag = snew(htmlfragment); |
| 2051 | int len = 0; /* >0 indicates we have resized */ |
| 2052 | int suffix = 1; |
| 2053 | |
| 2054 | frag->file = file; |
| 2055 | frag->fragment = text; |
| 2056 | |
| 2057 | while (add234(files->frags, frag) != frag) { |
| 2058 | if (!len) { |
| 2059 | len = strlen(text); |
| 2060 | frag->fragment = text = sresize(text, len+20, char); |
| 2061 | } |
| 2062 | |
| 2063 | sprintf(text + len, "-%d", ++suffix); |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | return text; |
| 2068 | } |
| 2069 | |
| 2070 | static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s, |
| 2071 | htmlfile *thisfile, keywordlist *keywords, |
| 2072 | htmlconfig *cfg) |
| 2073 | { |
| 2074 | while (ho->contents_level > depth) { |
| 2075 | element_close(ho, "ul"); |
| 2076 | ho->contents_level--; |
| 2077 | } |
| 2078 | |
| 2079 | while (ho->contents_level < depth) { |
| 2080 | element_open(ho, "ul"); |
| 2081 | ho->contents_level++; |
| 2082 | } |
| 2083 | |
| 2084 | if (!s) |
| 2085 | return; |
| 2086 | |
| 2087 | element_open(ho, "li"); |
| 2088 | html_href(ho, thisfile, s->file, s->fragment); |
| 2089 | html_section_title(ho, s, thisfile, keywords, cfg, FALSE); |
| 2090 | element_close(ho, "a"); |
| 2091 | element_close(ho, "li"); |
| 2092 | } |
| 2093 | |
| 2094 | static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile, |
| 2095 | keywordlist *keywords, htmlconfig *cfg, |
| 2096 | int real) |
| 2097 | { |
| 2098 | if (s->title) { |
| 2099 | sectlevel *sl; |
| 2100 | word *number; |
| 2101 | int depth = heading_depth(s->title); |
| 2102 | |
| 2103 | if (depth < 0) |
| 2104 | sl = NULL; |
| 2105 | else if (depth == 0) |
| 2106 | sl = &cfg->achapter; |
| 2107 | else if (depth <= cfg->nasect) |
| 2108 | sl = &cfg->asect[depth-1]; |
| 2109 | else |
| 2110 | sl = &cfg->asect[cfg->nasect-1]; |
| 2111 | |
| 2112 | if (!sl) |
| 2113 | number = NULL; |
| 2114 | else if (sl->just_numbers) |
| 2115 | number = s->title->kwtext2; |
| 2116 | else |
| 2117 | number = s->title->kwtext; |
| 2118 | |
| 2119 | if (number) { |
| 2120 | html_words(ho, number, MARKUP, |
| 2121 | thisfile, keywords, cfg); |
| 2122 | html_text(ho, sl->number_suffix); |
| 2123 | } |
| 2124 | |
| 2125 | html_words(ho, s->title->words, real ? ALL : MARKUP, |
| 2126 | thisfile, keywords, cfg); |
| 2127 | } else { |
| 2128 | assert(s->type != NORMAL); |
| 2129 | /* |
| 2130 | * If we're printing the full document title for _real_ and |
| 2131 | * there isn't one, we don't want to print `Preamble' at |
| 2132 | * the top of what ought to just be some text. If we need |
| 2133 | * it in any other context such as TOCs, we need to print |
| 2134 | * `Preamble'. |
| 2135 | */ |
| 2136 | if (s->type == TOP && !real) |
| 2137 | html_text(ho, cfg->preamble_text); |
| 2138 | else if (s->type == INDEX) |
| 2139 | html_text(ho, cfg->index_text); |
| 2140 | } |
| 2141 | } |