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