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