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