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