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.) |
f2ef00b5 |
13 | * |
14 | * - In HHK index mode: subsidiary hhk entries (as in replacing |
15 | * `foo, bar' with `foo\n\tbar') can be done by embedding |
16 | * sub-<UL>s in the hhk file. This requires me getting round to |
17 | * supporting that idiom in the rest of Halibut, but I thought |
18 | * I'd record how it's done here in case I turn out to have |
19 | * forgotten when I get there. |
78c73085 |
20 | */ |
21 | |
22 | #include <stdio.h> |
23 | #include <stdlib.h> |
24 | #include <assert.h> |
25 | #include <limits.h> |
26 | #include "halibut.h" |
27 | |
28 | #define is_heading_type(type) ( (type) == para_Title || \ |
29 | (type) == para_Chapter || \ |
30 | (type) == para_Appendix || \ |
31 | (type) == para_UnnumberedChapter || \ |
32 | (type) == para_Heading || \ |
33 | (type) == para_Subsect) |
34 | |
35 | #define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \ |
36 | (p)->type == para_Heading ? 1 : \ |
37 | (p)->type == para_Title ? -1 : 0 ) |
38 | |
39 | typedef struct { |
40 | int just_numbers; |
41 | wchar_t *number_suffix; |
42 | } sectlevel; |
43 | |
44 | typedef struct { |
45 | int nasect; |
46 | sectlevel achapter, *asect; |
47 | int *contents_depths; /* 0=main, 1=chapter, 2=sect etc */ |
48 | int ncdepths; |
49 | int address_section, visible_version_id; |
50 | int leaf_contains_contents, leaf_smallest_contents; |
f2ef00b5 |
51 | int navlinks; |
1aed5cf5 |
52 | int rellinks; |
78c73085 |
53 | char *contents_filename; |
54 | char *index_filename; |
55 | char *template_filename; |
56 | char *single_filename; |
f2ef00b5 |
57 | char *chm_filename, *hhp_filename, *hhc_filename, *hhk_filename; |
12f0ee84 |
58 | char **template_fragments; |
59 | int ntfragments; |
78c73085 |
60 | char *head_end, *body_start, *body_end, *addr_start, *addr_end; |
61 | char *body_tag, *nav_attr; |
62 | wchar_t *author, *description; |
56a99eb6 |
63 | wchar_t *index_text, *contents_text, *preamble_text, *title_separator; |
64 | wchar_t *nav_prev_text, *nav_next_text, *nav_separator; |
65 | wchar_t *index_main_sep, *index_multi_sep; |
66 | wchar_t *pre_versionid, *post_versionid; |
78c73085 |
67 | int restrict_charset, output_charset; |
68 | enum { |
27bdc5ab |
69 | HTML_3_2, HTML_4, ISO_HTML, |
78c73085 |
70 | XHTML_1_0_TRANSITIONAL, XHTML_1_0_STRICT |
71 | } htmlver; |
72 | wchar_t *lquote, *rquote; |
73 | int leaf_level; |
74 | } htmlconfig; |
75 | |
76 | #define contents_depth(conf, level) \ |
77 | ( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 ) |
78 | |
79 | #define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL) |
80 | |
81 | typedef struct htmlfile htmlfile; |
82 | typedef struct htmlsect htmlsect; |
83 | |
84 | struct htmlfile { |
85 | htmlfile *next; |
86 | char *filename; |
87 | int last_fragment_number; |
88 | int min_heading_depth; |
89 | htmlsect *first, *last; /* first/last highest-level sections */ |
f2ef00b5 |
90 | /* |
91 | * The `temp' field is available for use in individual passes |
92 | * over the file list. For example, the HHK index generation |
93 | * uses it to ensure no index term references the same file |
94 | * more than once. |
95 | */ |
96 | int temp; |
78c73085 |
97 | }; |
98 | |
99 | struct htmlsect { |
100 | htmlsect *next, *parent; |
101 | htmlfile *file; |
102 | paragraph *title, *text; |
103 | enum { NORMAL, TOP, INDEX } type; |
104 | int contents_depth; |
12f0ee84 |
105 | char **fragments; |
78c73085 |
106 | }; |
107 | |
108 | typedef struct { |
109 | htmlfile *head, *tail; |
110 | htmlfile *single, *index; |
3e82de8f |
111 | tree234 *frags; |
f2ef00b5 |
112 | tree234 *files; |
78c73085 |
113 | } htmlfilelist; |
114 | |
115 | typedef struct { |
116 | htmlsect *head, *tail; |
117 | } htmlsectlist; |
118 | |
119 | typedef struct { |
3e82de8f |
120 | htmlfile *file; |
121 | char *fragment; |
122 | } htmlfragment; |
123 | |
124 | typedef struct { |
78c73085 |
125 | int nrefs, refsize; |
126 | word **refs; |
127 | } htmlindex; |
128 | |
129 | typedef struct { |
130 | htmlsect *section; |
131 | char *fragment; |
1b7bf715 |
132 | int generated, referenced; |
78c73085 |
133 | } htmlindexref; |
134 | |
135 | typedef struct { |
136 | /* |
137 | * This level deals with charset conversion, starting and |
138 | * ending tags, and writing to the file. It's the lexical |
139 | * level. |
140 | */ |
141 | FILE *fp; |
b7309494 |
142 | int charset, restrict_charset; |
78c73085 |
143 | charset_state cstate; |
144 | int ver; |
145 | enum { |
146 | HO_NEUTRAL, HO_IN_TAG, HO_IN_EMPTY_TAG, HO_IN_TEXT |
147 | } state; |
f2ef00b5 |
148 | int hackflags; /* used for icky .HH* stuff */ |
149 | int hacklimit; /* text size limit, again for .HH* */ |
78c73085 |
150 | /* |
151 | * Stuff beyond here deals with the higher syntactic level: it |
152 | * tracks how many levels of <ul> are currently open when |
153 | * producing a contents list, for example. |
154 | */ |
155 | int contents_level; |
156 | } htmloutput; |
157 | |
f2ef00b5 |
158 | /* |
159 | * Nasty hacks that modify the behaviour of htmloutput files. All |
160 | * of these are flag bits set in ho.hackflags. HO_HACK_QUOTEQUOTES |
161 | * has the same effect as the `quote_quotes' parameter to |
162 | * html_text_limit_internal, except that it's set globally on an |
163 | * entire htmloutput structure; HO_HACK_QUOTENOTHING suppresses |
164 | * quoting of any HTML special characters (for .HHP files); |
165 | * HO_HACK_OMITQUOTES completely suppresses the generation of |
166 | * double quotes at all (turning them into single quotes, for want |
167 | * of a better idea). |
168 | */ |
169 | #define HO_HACK_QUOTEQUOTES 1 |
170 | #define HO_HACK_QUOTENOTHING 2 |
171 | #define HO_HACK_OMITQUOTES 4 |
172 | |
3e82de8f |
173 | static int html_fragment_compare(void *av, void *bv) |
174 | { |
175 | htmlfragment *a = (htmlfragment *)av; |
176 | htmlfragment *b = (htmlfragment *)bv; |
177 | int cmp; |
178 | |
179 | if ((cmp = strcmp(a->file->filename, b->file->filename)) != 0) |
180 | return cmp; |
181 | else |
182 | return strcmp(a->fragment, b->fragment); |
183 | } |
184 | |
f2ef00b5 |
185 | static int html_filename_compare(void *av, void *bv) |
186 | { |
187 | char *a = (char *)av; |
188 | char *b = (char *)bv; |
189 | |
190 | return strcmp(a, b); |
191 | } |
192 | |
78c73085 |
193 | static void html_file_section(htmlconfig *cfg, htmlfilelist *files, |
194 | htmlsect *sect, int depth); |
195 | |
196 | static htmlfile *html_new_file(htmlfilelist *list, char *filename); |
12f0ee84 |
197 | static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title, |
198 | htmlconfig *cfg); |
78c73085 |
199 | |
200 | /* Flags for html_words() flags parameter */ |
201 | #define NOTHING 0x00 |
202 | #define MARKUP 0x01 |
203 | #define LINKS 0x02 |
204 | #define INDEXENTS 0x04 |
205 | #define ALL 0x07 |
206 | static void html_words(htmloutput *ho, word *words, int flags, |
207 | htmlfile *file, keywordlist *keywords, htmlconfig *cfg); |
208 | static void html_codepara(htmloutput *ho, word *words); |
209 | |
210 | static void element_open(htmloutput *ho, char const *name); |
211 | static void element_close(htmloutput *ho, char const *name); |
212 | static void element_empty(htmloutput *ho, char const *name); |
213 | static void element_attr(htmloutput *ho, char const *name, char const *value); |
214 | static void element_attr_w(htmloutput *ho, char const *name, |
215 | wchar_t const *value); |
216 | static void html_text(htmloutput *ho, wchar_t const *str); |
35b123a0 |
217 | static void html_text_nbsp(htmloutput *ho, wchar_t const *str); |
78c73085 |
218 | static void html_text_limit(htmloutput *ho, wchar_t const *str, int maxlen); |
219 | static void html_text_limit_internal(htmloutput *ho, wchar_t const *text, |
35b123a0 |
220 | int maxlen, int quote_quotes, int nbsp); |
78c73085 |
221 | static void html_nl(htmloutput *ho); |
222 | static void html_raw(htmloutput *ho, char *text); |
223 | static void html_raw_as_attr(htmloutput *ho, char *text); |
224 | static void cleanup(htmloutput *ho); |
225 | |
226 | static void html_href(htmloutput *ho, htmlfile *thisfile, |
227 | htmlfile *targetfile, char *targetfrag); |
27bdc5ab |
228 | static void html_fragment(htmloutput *ho, char const *fragment); |
78c73085 |
229 | |
230 | static char *html_format(paragraph *p, char *template_string); |
3e82de8f |
231 | static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file, |
232 | char *text); |
f2ef00b5 |
233 | static char *html_sanitise_filename(htmlfilelist *files, char *text); |
78c73085 |
234 | |
235 | static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s, |
236 | htmlfile *thisfile, keywordlist *keywords, |
237 | htmlconfig *cfg); |
238 | static void html_section_title(htmloutput *ho, htmlsect *s, |
239 | htmlfile *thisfile, keywordlist *keywords, |
23c9bbc2 |
240 | htmlconfig *cfg, int real); |
78c73085 |
241 | |
242 | static htmlconfig html_configure(paragraph *source) { |
243 | htmlconfig ret; |
244 | paragraph *p; |
245 | |
246 | /* |
247 | * Defaults. |
248 | */ |
249 | ret.leaf_level = 2; |
250 | ret.achapter.just_numbers = FALSE; |
251 | ret.achapter.number_suffix = L": "; |
252 | ret.nasect = 1; |
f1530049 |
253 | ret.asect = snewn(ret.nasect, sectlevel); |
78c73085 |
254 | ret.asect[0].just_numbers = TRUE; |
255 | ret.asect[0].number_suffix = L" "; |
256 | ret.ncdepths = 0; |
257 | ret.contents_depths = 0; |
258 | ret.visible_version_id = TRUE; |
259 | ret.address_section = TRUE; |
260 | ret.leaf_contains_contents = FALSE; |
261 | ret.leaf_smallest_contents = 4; |
f2ef00b5 |
262 | ret.navlinks = TRUE; |
2e032f92 |
263 | ret.rellinks = TRUE; |
78c73085 |
264 | ret.single_filename = dupstr("Manual.html"); |
265 | ret.contents_filename = dupstr("Contents.html"); |
266 | ret.index_filename = dupstr("IndexPage.html"); |
267 | ret.template_filename = dupstr("%n.html"); |
f2ef00b5 |
268 | ret.chm_filename = ret.hhp_filename = NULL; |
269 | ret.hhc_filename = ret.hhk_filename = NULL; |
12f0ee84 |
270 | ret.ntfragments = 1; |
271 | ret.template_fragments = snewn(ret.ntfragments, char *); |
272 | ret.template_fragments[0] = dupstr("%b"); |
78c73085 |
273 | ret.head_end = ret.body_tag = ret.body_start = ret.body_end = |
274 | ret.addr_start = ret.addr_end = ret.nav_attr = NULL; |
275 | ret.author = ret.description = NULL; |
b7309494 |
276 | ret.restrict_charset = CS_UTF8; |
78c73085 |
277 | ret.output_charset = CS_ASCII; |
278 | ret.htmlver = HTML_4; |
56a99eb6 |
279 | ret.index_text = L"Index"; |
280 | ret.contents_text = L"Contents"; |
281 | ret.preamble_text = L"Preamble"; |
282 | ret.title_separator = L" - "; |
283 | ret.nav_prev_text = L"Previous"; |
284 | ret.nav_next_text = L"Next"; |
285 | ret.nav_separator = L" | "; |
286 | ret.index_main_sep = L": "; |
287 | ret.index_multi_sep = L", "; |
288 | ret.pre_versionid = L"["; |
289 | ret.post_versionid = L"]"; |
78c73085 |
290 | /* |
291 | * Default quote characters are Unicode matched single quotes, |
292 | * falling back to ordinary ASCII ". |
293 | */ |
294 | ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0"; |
295 | ret.rquote = uadv(ret.lquote); |
296 | |
297 | /* |
298 | * Two-pass configuration so that we can pick up global config |
299 | * (e.g. `quotes') before having it overridden by specific |
300 | * config (`html-quotes'), irrespective of the order in which |
301 | * they occur. |
302 | */ |
303 | for (p = source; p; p = p->next) { |
304 | if (p->type == para_Config) { |
305 | if (!ustricmp(p->keyword, L"quotes")) { |
306 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
307 | ret.lquote = uadv(p->keyword); |
308 | ret.rquote = uadv(ret.lquote); |
309 | } |
f6220253 |
310 | } else if (!ustricmp(p->keyword, L"index")) { |
311 | ret.index_text = uadv(p->keyword); |
312 | } else if (!ustricmp(p->keyword, L"contents")) { |
313 | ret.contents_text = uadv(p->keyword); |
78c73085 |
314 | } |
315 | } |
316 | } |
317 | |
318 | for (p = source; p; p = p->next) { |
319 | if (p->type == para_Config) { |
320 | wchar_t *k = p->keyword; |
321 | |
322 | if (!ustrnicmp(k, L"xhtml-", 6)) |
323 | k++; /* treat `xhtml-' and `html-' the same */ |
324 | |
b7309494 |
325 | if (!ustricmp(k, L"html-restrict-charset")) { |
0960a3d8 |
326 | ret.restrict_charset = charset_from_ustr(&p->fpos, uadv(k)); |
b7309494 |
327 | } else if (!ustricmp(k, L"html-output-charset")) { |
0960a3d8 |
328 | ret.output_charset = charset_from_ustr(&p->fpos, uadv(k)); |
27bdc5ab |
329 | } else if (!ustricmp(k, L"html-version")) { |
330 | wchar_t *vername = uadv(k); |
331 | static const struct { |
332 | const wchar_t *name; |
333 | int ver; |
334 | } versions[] = { |
335 | {L"html3.2", HTML_3_2}, |
336 | {L"html4", HTML_4}, |
337 | {L"iso-html", ISO_HTML}, |
338 | {L"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL}, |
339 | {L"xhtml1.0strict", XHTML_1_0_STRICT} |
340 | }; |
341 | int i; |
342 | |
343 | for (i = 0; i < (int)lenof(versions); i++) |
344 | if (!ustricmp(versions[i].name, vername)) |
345 | break; |
346 | |
347 | if (i == lenof(versions)) |
348 | error(err_htmlver, &p->fpos, vername); |
349 | else |
350 | ret.htmlver = versions[i].ver; |
78c73085 |
351 | } else if (!ustricmp(k, L"html-single-filename")) { |
352 | sfree(ret.single_filename); |
353 | ret.single_filename = dupstr(adv(p->origkeyword)); |
354 | } else if (!ustricmp(k, L"html-contents-filename")) { |
355 | sfree(ret.contents_filename); |
356 | ret.contents_filename = dupstr(adv(p->origkeyword)); |
357 | } else if (!ustricmp(k, L"html-index-filename")) { |
358 | sfree(ret.index_filename); |
359 | ret.index_filename = dupstr(adv(p->origkeyword)); |
360 | } else if (!ustricmp(k, L"html-template-filename")) { |
361 | sfree(ret.template_filename); |
362 | ret.template_filename = dupstr(adv(p->origkeyword)); |
363 | } else if (!ustricmp(k, L"html-template-fragment")) { |
12f0ee84 |
364 | char *frag = adv(p->origkeyword); |
365 | if (*frag) { |
366 | while (ret.ntfragments--) |
367 | sfree(ret.template_fragments[ret.ntfragments]); |
368 | sfree(ret.template_fragments); |
369 | ret.template_fragments = NULL; |
370 | ret.ntfragments = 0; |
371 | while (*frag) { |
372 | ret.ntfragments++; |
373 | ret.template_fragments = |
374 | sresize(ret.template_fragments, |
375 | ret.ntfragments, char *); |
376 | ret.template_fragments[ret.ntfragments-1] = |
377 | dupstr(frag); |
378 | frag = adv(frag); |
379 | } |
380 | } else |
381 | error(err_cfginsufarg, &p->fpos, p->origkeyword, 1); |
78c73085 |
382 | } else if (!ustricmp(k, L"html-chapter-numeric")) { |
383 | ret.achapter.just_numbers = utob(uadv(k)); |
f2ef00b5 |
384 | } else if (!ustricmp(k, L"html-suppress-navlinks")) { |
385 | ret.navlinks = !utob(uadv(k)); |
1aed5cf5 |
386 | } else if (!ustricmp(k, L"html-rellinks")) { |
387 | ret.rellinks = utob(uadv(k)); |
78c73085 |
388 | } else if (!ustricmp(k, L"html-chapter-suffix")) { |
389 | ret.achapter.number_suffix = uadv(k); |
390 | } else if (!ustricmp(k, L"html-leaf-level")) { |
f2ef00b5 |
391 | wchar_t *u = uadv(k); |
392 | if (!ustricmp(u, L"infinite") || |
393 | !ustricmp(u, L"infinity") || |
394 | !ustricmp(u, L"inf")) |
395 | ret.leaf_level = -1; /* represents infinity */ |
396 | else |
397 | ret.leaf_level = utoi(u); |
78c73085 |
398 | } else if (!ustricmp(k, L"html-section-numeric")) { |
399 | wchar_t *q = uadv(k); |
400 | int n = 0; |
401 | if (uisdigit(*q)) { |
402 | n = utoi(q); |
403 | q = uadv(q); |
404 | } |
405 | if (n >= ret.nasect) { |
406 | int i; |
f1530049 |
407 | ret.asect = sresize(ret.asect, n+1, sectlevel); |
78c73085 |
408 | for (i = ret.nasect; i <= n; i++) |
409 | ret.asect[i] = ret.asect[ret.nasect-1]; |
410 | ret.nasect = n+1; |
411 | } |
412 | ret.asect[n].just_numbers = utob(q); |
413 | } else if (!ustricmp(k, L"html-section-suffix")) { |
414 | wchar_t *q = uadv(k); |
415 | int n = 0; |
416 | if (uisdigit(*q)) { |
417 | n = utoi(q); |
418 | q = uadv(q); |
419 | } |
420 | if (n >= ret.nasect) { |
421 | int i; |
f1530049 |
422 | ret.asect = sresize(ret.asect, n+1, sectlevel); |
78c73085 |
423 | for (i = ret.nasect; i <= n; i++) { |
424 | ret.asect[i] = ret.asect[ret.nasect-1]; |
425 | } |
426 | ret.nasect = n+1; |
427 | } |
428 | ret.asect[n].number_suffix = q; |
429 | } else if (!ustricmp(k, L"html-contents-depth") || |
430 | !ustrnicmp(k, L"html-contents-depth-", 20)) { |
431 | /* |
432 | * Relic of old implementation: this directive used |
433 | * to be written as \cfg{html-contents-depth-3}{2} |
434 | * rather than the usual Halibut convention of |
435 | * \cfg{html-contents-depth}{3}{2}. We therefore |
436 | * support both. |
437 | */ |
438 | wchar_t *q = k[19] ? k+20 : uadv(k); |
439 | int n = 0; |
440 | if (uisdigit(*q)) { |
441 | n = utoi(q); |
442 | q = uadv(q); |
443 | } |
444 | if (n >= ret.ncdepths) { |
445 | int i; |
f1530049 |
446 | ret.contents_depths = |
447 | sresize(ret.contents_depths, n+1, int); |
78c73085 |
448 | for (i = ret.ncdepths; i <= n; i++) { |
449 | ret.contents_depths[i] = i+2; |
450 | } |
451 | ret.ncdepths = n+1; |
452 | } |
453 | ret.contents_depths[n] = utoi(q); |
454 | } else if (!ustricmp(k, L"html-head-end")) { |
455 | ret.head_end = adv(p->origkeyword); |
456 | } else if (!ustricmp(k, L"html-body-tag")) { |
457 | ret.body_tag = adv(p->origkeyword); |
458 | } else if (!ustricmp(k, L"html-body-start")) { |
459 | ret.body_start = adv(p->origkeyword); |
460 | } else if (!ustricmp(k, L"html-body-end")) { |
461 | ret.body_end = adv(p->origkeyword); |
462 | } else if (!ustricmp(k, L"html-address-start")) { |
463 | ret.addr_start = adv(p->origkeyword); |
464 | } else if (!ustricmp(k, L"html-address-end")) { |
465 | ret.addr_end = adv(p->origkeyword); |
466 | } else if (!ustricmp(k, L"html-navigation-attributes")) { |
467 | ret.nav_attr = adv(p->origkeyword); |
468 | } else if (!ustricmp(k, L"html-author")) { |
469 | ret.author = uadv(k); |
470 | } else if (!ustricmp(k, L"html-description")) { |
471 | ret.description = uadv(k); |
472 | } else if (!ustricmp(k, L"html-suppress-address")) { |
473 | ret.address_section = !utob(uadv(k)); |
474 | } else if (!ustricmp(k, L"html-versionid")) { |
475 | ret.visible_version_id = utob(uadv(k)); |
476 | } else if (!ustricmp(k, L"html-quotes")) { |
477 | if (*uadv(k) && *uadv(uadv(k))) { |
478 | ret.lquote = uadv(k); |
479 | ret.rquote = uadv(ret.lquote); |
480 | } |
481 | } else if (!ustricmp(k, L"html-leaf-contains-contents")) { |
482 | ret.leaf_contains_contents = utob(uadv(k)); |
483 | } else if (!ustricmp(k, L"html-leaf-smallest-contents")) { |
484 | ret.leaf_smallest_contents = utoi(uadv(k)); |
75a96e91 |
485 | } else if (!ustricmp(k, L"html-index-text")) { |
486 | ret.index_text = uadv(k); |
487 | } else if (!ustricmp(k, L"html-contents-text")) { |
488 | ret.contents_text = uadv(k); |
489 | } else if (!ustricmp(k, L"html-preamble-text")) { |
490 | ret.preamble_text = uadv(k); |
491 | } else if (!ustricmp(k, L"html-title-separator")) { |
492 | ret.title_separator = uadv(k); |
493 | } else if (!ustricmp(k, L"html-nav-prev-text")) { |
494 | ret.nav_prev_text = uadv(k); |
495 | } else if (!ustricmp(k, L"html-nav-next-text")) { |
496 | ret.nav_next_text = uadv(k); |
497 | } else if (!ustricmp(k, L"html-nav-separator")) { |
498 | ret.nav_separator = uadv(k); |
499 | } else if (!ustricmp(k, L"html-index-main-separator")) { |
500 | ret.index_main_sep = uadv(k); |
501 | } else if (!ustricmp(k, L"html-index-multiple-separator")) { |
502 | ret.index_multi_sep = uadv(k); |
503 | } else if (!ustricmp(k, L"html-pre-versionid")) { |
504 | ret.pre_versionid = uadv(k); |
505 | } else if (!ustricmp(k, L"html-post-versionid")) { |
506 | ret.post_versionid = uadv(k); |
f2ef00b5 |
507 | } else if (!ustricmp(k, L"html-mshtmlhelp-chm")) { |
508 | sfree(ret.chm_filename); |
509 | ret.chm_filename = dupstr(adv(p->origkeyword)); |
510 | } else if (!ustricmp(k, L"html-mshtmlhelp-project")) { |
511 | sfree(ret.hhp_filename); |
512 | ret.hhp_filename = dupstr(adv(p->origkeyword)); |
513 | } else if (!ustricmp(k, L"html-mshtmlhelp-contents")) { |
514 | sfree(ret.hhc_filename); |
515 | ret.hhc_filename = dupstr(adv(p->origkeyword)); |
516 | } else if (!ustricmp(k, L"html-mshtmlhelp-index")) { |
517 | sfree(ret.hhk_filename); |
518 | ret.hhk_filename = dupstr(adv(p->origkeyword)); |
78c73085 |
519 | } |
520 | } |
521 | } |
522 | |
523 | /* |
f2ef00b5 |
524 | * Enforce that the CHM and HHP filenames must either be both |
525 | * present or both absent. If one is present but not the other, |
526 | * turn both off. |
527 | */ |
528 | if (!ret.chm_filename ^ !ret.hhp_filename) { |
529 | error(err_chmnames); |
530 | sfree(ret.chm_filename); ret.chm_filename = NULL; |
531 | sfree(ret.hhp_filename); ret.hhp_filename = NULL; |
532 | } |
533 | /* |
534 | * And if we're not generating an HHP, there's no need for HHC |
535 | * or HHK. |
536 | */ |
537 | if (!ret.hhp_filename) { |
538 | sfree(ret.hhc_filename); ret.hhc_filename = NULL; |
539 | sfree(ret.hhk_filename); ret.hhk_filename = NULL; |
540 | } |
541 | |
542 | /* |
78c73085 |
543 | * Now process fallbacks on quote characters. |
544 | */ |
545 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) && |
546 | (!cvt_ok(ret.restrict_charset, ret.lquote) || |
547 | !cvt_ok(ret.restrict_charset, ret.rquote))) { |
548 | ret.lquote = uadv(ret.rquote); |
549 | ret.rquote = uadv(ret.lquote); |
550 | } |
551 | |
552 | return ret; |
553 | } |
554 | |
555 | paragraph *html_config_filename(char *filename) |
556 | { |
557 | /* |
558 | * If the user passes in a single filename as a parameter to |
559 | * the `--html' command-line option, then we should assume it |
560 | * to imply _two_ config directives: |
561 | * \cfg{html-single-filename}{whatever} and |
562 | * \cfg{html-leaf-level}{0}; the rationale being that the user |
563 | * wants their output _in that file_. |
564 | */ |
565 | paragraph *p, *q; |
566 | |
567 | p = cmdline_cfg_simple("html-single-filename", filename, NULL); |
568 | q = cmdline_cfg_simple("html-leaf-level", "0", NULL); |
569 | p->next = q; |
570 | return p; |
571 | } |
572 | |
573 | void html_backend(paragraph *sourceform, keywordlist *keywords, |
529a6c83 |
574 | indexdata *idx, void *unused) |
575 | { |
78c73085 |
576 | paragraph *p; |
f2ef00b5 |
577 | htmlsect *topsect; |
78c73085 |
578 | htmlconfig conf; |
f2ef00b5 |
579 | htmlfilelist files = { NULL, NULL, NULL, NULL, NULL, NULL }; |
78c73085 |
580 | htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL }; |
f2ef00b5 |
581 | char *hhk_filename; |
03fcb340 |
582 | int has_index; |
78c73085 |
583 | |
584 | IGNORE(unused); |
585 | |
586 | conf = html_configure(sourceform); |
587 | |
588 | /* |
589 | * We're going to make heavy use of paragraphs' private data |
590 | * fields in the forthcoming code. Clear them first, so we can |
591 | * reliably tell whether we have auxiliary data for a |
592 | * particular paragraph. |
593 | */ |
594 | for (p = sourceform; p; p = p->next) |
595 | p->private_data = NULL; |
596 | |
3e82de8f |
597 | files.frags = newtree234(html_fragment_compare); |
f2ef00b5 |
598 | files.files = newtree234(html_filename_compare); |
3e82de8f |
599 | |
78c73085 |
600 | /* |
601 | * Start by figuring out into which file each piece of the |
602 | * document should be put. We'll do this by inventing an |
603 | * `htmlsect' structure and stashing it in the private_data |
604 | * field of each section paragraph; we also need one additional |
605 | * htmlsect for the document index, which won't show up in the |
606 | * source form but needs to be consistently mentioned in |
607 | * contents links. |
608 | * |
12f0ee84 |
609 | * While we're here, we'll also invent the HTML fragment name(s) |
78c73085 |
610 | * for each section. |
611 | */ |
612 | { |
f2ef00b5 |
613 | htmlsect *sect; |
78c73085 |
614 | int d; |
615 | |
12f0ee84 |
616 | topsect = html_new_sect(§s, NULL, &conf); |
78c73085 |
617 | topsect->type = TOP; |
618 | topsect->title = NULL; |
619 | topsect->text = sourceform; |
620 | topsect->contents_depth = contents_depth(conf, 0); |
621 | html_file_section(&conf, &files, topsect, -1); |
78c73085 |
622 | |
623 | for (p = sourceform; p; p = p->next) |
624 | if (is_heading_type(p->type)) { |
625 | d = heading_depth(p); |
626 | |
627 | if (p->type == para_Title) { |
628 | topsect->title = p; |
629 | continue; |
630 | } |
631 | |
12f0ee84 |
632 | sect = html_new_sect(§s, p, &conf); |
78c73085 |
633 | sect->text = p->next; |
634 | |
635 | sect->contents_depth = contents_depth(conf, d+1) - (d+1); |
636 | |
637 | if (p->parent) { |
638 | sect->parent = (htmlsect *)p->parent->private_data; |
639 | assert(sect->parent != NULL); |
640 | } else |
641 | sect->parent = topsect; |
642 | p->private_data = sect; |
643 | |
644 | html_file_section(&conf, &files, sect, d); |
645 | |
12f0ee84 |
646 | { |
647 | int i; |
648 | for (i=0; i < conf.ntfragments; i++) { |
649 | sect->fragments[i] = |
650 | html_format(p, conf.template_fragments[i]); |
651 | sect->fragments[i] = |
652 | html_sanitise_fragment(&files, sect->file, |
653 | sect->fragments[i]); |
654 | } |
655 | } |
78c73085 |
656 | } |
657 | |
f2ef00b5 |
658 | /* |
659 | * And the index, if we have one. Note that we don't output |
660 | * an index as an HTML file if we're outputting one as a |
661 | * .HHK. |
662 | */ |
03fcb340 |
663 | has_index = (count234(idx->entries) > 0); |
f2ef00b5 |
664 | if (has_index && !conf.hhk_filename) { |
12f0ee84 |
665 | sect = html_new_sect(§s, NULL, &conf); |
03fcb340 |
666 | sect->text = NULL; |
667 | sect->type = INDEX; |
668 | sect->parent = topsect; |
cdb986cc |
669 | sect->contents_depth = 0; |
03fcb340 |
670 | html_file_section(&conf, &files, sect, 0); /* peer of chapters */ |
12f0ee84 |
671 | sect->fragments[0] = utoa_dup(conf.index_text, CS_ASCII); |
672 | sect->fragments[0] = html_sanitise_fragment(&files, sect->file, |
673 | sect->fragments[0]); |
03fcb340 |
674 | files.index = sect->file; |
675 | } |
78c73085 |
676 | } |
677 | |
678 | /* |
679 | * Go through the keyword list and sort out fragment IDs for |
680 | * all the potentially referenced paragraphs which _aren't_ |
681 | * headings. |
682 | */ |
683 | { |
684 | int i; |
685 | keyword *kw; |
686 | htmlsect *sect; |
687 | |
688 | for (i = 0; (kw = index234(keywords->keys, i)) != NULL; i++) { |
689 | paragraph *q, *p = kw->para; |
690 | |
691 | if (!is_heading_type(p->type)) { |
692 | htmlsect *parent; |
693 | |
694 | /* |
695 | * Find the paragraph's parent htmlsect, to |
696 | * determine which file it will end up in. |
697 | */ |
698 | q = p->parent; |
699 | if (!q) { |
700 | /* |
701 | * Preamble paragraphs have no parent. So if we |
702 | * have a non-heading with no parent, it must |
703 | * be preamble, and therefore its parent |
704 | * htmlsect must be the preamble one. |
705 | */ |
706 | assert(sects.head && |
707 | sects.head->type == TOP); |
708 | parent = sects.head; |
709 | } else |
710 | parent = (htmlsect *)q->private_data; |
711 | |
712 | /* |
713 | * Now we can construct an htmlsect for this |
714 | * paragraph itself, taking care to put it in the |
715 | * list of non-sections rather than the list of |
716 | * sections (so that traverses of the `sects' list |
717 | * won't attempt to add it to the contents or |
718 | * anything weird like that). |
719 | */ |
12f0ee84 |
720 | sect = html_new_sect(&nonsects, p, &conf); |
78c73085 |
721 | sect->file = parent->file; |
722 | sect->parent = parent; |
723 | p->private_data = sect; |
724 | |
725 | /* |
04781c84 |
726 | * Fragment IDs for these paragraphs will simply be |
727 | * `p' followed by an integer. |
78c73085 |
728 | */ |
12f0ee84 |
729 | sect->fragments[0] = snewn(40, char); |
730 | sprintf(sect->fragments[0], "p%d", |
04781c84 |
731 | sect->file->last_fragment_number++); |
12f0ee84 |
732 | sect->fragments[0] = html_sanitise_fragment(&files, sect->file, |
733 | sect->fragments[0]); |
78c73085 |
734 | } |
735 | } |
736 | } |
737 | |
738 | /* |
04781c84 |
739 | * Reset the fragment numbers in each file. I've just used them |
740 | * to generate `p' fragment IDs for non-section paragraphs |
741 | * (numbered list elements, bibliocited), and now I want to use |
742 | * them for `i' fragment IDs for index entries. |
743 | */ |
744 | { |
745 | htmlfile *file; |
746 | for (file = files.head; file; file = file->next) |
747 | file->last_fragment_number = 0; |
748 | } |
749 | |
750 | /* |
78c73085 |
751 | * Now sort out the index. This involves: |
752 | * |
753 | * - For each index term, we set up an htmlindex structure to |
754 | * store all the references to that term. |
755 | * |
756 | * - Then we make a pass over the actual document, finding |
757 | * every word_IndexRef; for each one, we actually figure out |
758 | * the HTML filename/fragment pair we will use to reference |
759 | * it, store that information in the private data field of |
760 | * the word_IndexRef itself (so we can recreate it when the |
761 | * time comes to output our HTML), and add a reference to it |
762 | * to the index term in question. |
763 | */ |
764 | { |
765 | int i; |
766 | indexentry *entry; |
767 | htmlsect *lastsect; |
768 | word *w; |
769 | |
770 | /* |
771 | * Set up the htmlindex structures. |
772 | */ |
773 | |
774 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
f1530049 |
775 | htmlindex *hi = snew(htmlindex); |
78c73085 |
776 | |
777 | hi->nrefs = hi->refsize = 0; |
778 | hi->refs = NULL; |
779 | |
780 | entry->backend_data = hi; |
781 | } |
782 | |
783 | /* |
784 | * Run over the document inventing fragments. Each fragment |
785 | * is of the form `i' followed by an integer. |
78c73085 |
786 | */ |
56a99eb6 |
787 | lastsect = sects.head; /* this is always the top section */ |
78c73085 |
788 | for (p = sourceform; p; p = p->next) { |
56a99eb6 |
789 | if (is_heading_type(p->type) && p->type != para_Title) |
78c73085 |
790 | lastsect = (htmlsect *)p->private_data; |
791 | |
792 | for (w = p->words; w; w = w->next) |
793 | if (w->type == word_IndexRef) { |
f1530049 |
794 | htmlindexref *hr = snew(htmlindexref); |
78c73085 |
795 | indextag *tag; |
796 | int i; |
797 | |
1b7bf715 |
798 | hr->referenced = hr->generated = FALSE; |
78c73085 |
799 | hr->section = lastsect; |
78c73085 |
800 | { |
801 | char buf[40]; |
802 | sprintf(buf, "i%d", |
803 | lastsect->file->last_fragment_number++); |
804 | hr->fragment = dupstr(buf); |
3e82de8f |
805 | hr->fragment = |
806 | html_sanitise_fragment(&files, hr->section->file, |
807 | hr->fragment); |
78c73085 |
808 | } |
809 | w->private_data = hr; |
810 | |
811 | tag = index_findtag(idx, w->text); |
812 | if (!tag) |
813 | break; |
814 | |
815 | for (i = 0; i < tag->nrefs; i++) { |
816 | indexentry *entry = tag->refs[i]; |
817 | htmlindex *hi = (htmlindex *)entry->backend_data; |
818 | |
819 | if (hi->nrefs >= hi->refsize) { |
820 | hi->refsize += 32; |
f1530049 |
821 | hi->refs = sresize(hi->refs, hi->refsize, word *); |
78c73085 |
822 | } |
823 | |
824 | hi->refs[hi->nrefs++] = w; |
825 | } |
826 | } |
827 | } |
828 | } |
829 | |
830 | /* |
831 | * Now we're ready to write out the actual HTML files. |
832 | * |
833 | * For each file: |
834 | * |
835 | * - we open that file and write its header |
836 | * - we run down the list of sections |
837 | * - for each section directly contained within that file, we |
838 | * output the section text |
839 | * - for each section which is not in the file but which has a |
840 | * parent that is, we output a contents entry for the |
841 | * section if appropriate |
842 | * - finally, we output the file trailer and close the file. |
843 | */ |
844 | { |
845 | htmlfile *f, *prevf; |
846 | htmlsect *s; |
847 | paragraph *p; |
848 | |
849 | prevf = NULL; |
850 | |
851 | for (f = files.head; f; f = f->next) { |
852 | htmloutput ho; |
853 | int displaying; |
854 | enum LISTTYPE { NOLIST, UL, OL, DL }; |
855 | enum ITEMTYPE { NOITEM, LI, DT, DD }; |
856 | struct stackelement { |
857 | struct stackelement *next; |
858 | enum LISTTYPE listtype; |
859 | enum ITEMTYPE itemtype; |
860 | } *stackhead; |
861 | |
862 | #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" ) |
863 | #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" ) |
864 | |
865 | ho.fp = fopen(f->filename, "w"); |
3ae196b4 |
866 | if (!ho.fp) |
867 | error(err_cantopenw, f->filename); |
868 | |
78c73085 |
869 | ho.charset = conf.output_charset; |
b7309494 |
870 | ho.restrict_charset = conf.restrict_charset; |
78c73085 |
871 | ho.cstate = charset_init_state; |
872 | ho.ver = conf.htmlver; |
873 | ho.state = HO_NEUTRAL; |
874 | ho.contents_level = 0; |
f2ef00b5 |
875 | ho.hackflags = 0; /* none of these thankyouverymuch */ |
876 | ho.hacklimit = -1; |
78c73085 |
877 | |
878 | /* <!DOCTYPE>. */ |
879 | switch (conf.htmlver) { |
880 | case HTML_3_2: |
3ae196b4 |
881 | if (ho.fp) |
882 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " |
883 | "HTML 3.2 Final//EN\">\n"); |
78c73085 |
884 | break; |
885 | case HTML_4: |
3ae196b4 |
886 | if (ho.fp) |
887 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML" |
888 | " 4.01//EN\"\n\"http://www.w3.org/TR/html4/" |
889 | "strict.dtd\">\n"); |
78c73085 |
890 | break; |
27bdc5ab |
891 | case ISO_HTML: |
3ae196b4 |
892 | if (ho.fp) |
893 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"ISO/IEC " |
894 | "15445:2000//DTD HTML//EN\">\n"); |
27bdc5ab |
895 | break; |
78c73085 |
896 | case XHTML_1_0_TRANSITIONAL: |
3ae196b4 |
897 | if (ho.fp) { |
898 | fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", |
899 | charset_to_mimeenc(conf.output_charset)); |
900 | fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML" |
901 | " 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/" |
902 | "xhtml1/DTD/xhtml1-transitional.dtd\">\n"); |
903 | } |
78c73085 |
904 | break; |
905 | case XHTML_1_0_STRICT: |
3ae196b4 |
906 | if (ho.fp) { |
907 | fprintf(ho.fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", |
908 | charset_to_mimeenc(conf.output_charset)); |
909 | fprintf(ho.fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML" |
910 | " 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/" |
911 | "DTD/xhtml1-strict.dtd\">\n"); |
912 | } |
78c73085 |
913 | break; |
914 | } |
915 | |
916 | element_open(&ho, "html"); |
917 | if (is_xhtml(conf.htmlver)) { |
918 | element_attr(&ho, "xmlns", "http://www.w3.org/1999/xhtml"); |
919 | } |
920 | html_nl(&ho); |
921 | |
922 | element_open(&ho, "head"); |
923 | html_nl(&ho); |
924 | |
925 | element_empty(&ho, "meta"); |
926 | element_attr(&ho, "http-equiv", "content-type"); |
927 | { |
928 | char buf[200]; |
929 | sprintf(buf, "text/html; charset=%.150s", |
930 | charset_to_mimeenc(conf.output_charset)); |
931 | element_attr(&ho, "content", buf); |
932 | } |
933 | html_nl(&ho); |
934 | |
935 | if (conf.author) { |
936 | element_empty(&ho, "meta"); |
937 | element_attr(&ho, "name", "author"); |
938 | element_attr_w(&ho, "content", conf.author); |
939 | html_nl(&ho); |
940 | } |
941 | |
942 | if (conf.description) { |
943 | element_empty(&ho, "meta"); |
944 | element_attr(&ho, "name", "description"); |
945 | element_attr_w(&ho, "content", conf.description); |
946 | html_nl(&ho); |
947 | } |
948 | |
949 | element_open(&ho, "title"); |
950 | if (f->first && f->first->title) { |
951 | html_words(&ho, f->first->title->words, NOTHING, |
952 | f, keywords, &conf); |
953 | |
954 | assert(f->last); |
955 | if (f->last != f->first && f->last->title) { |
56a99eb6 |
956 | html_text(&ho, conf.title_separator); |
78c73085 |
957 | html_words(&ho, f->last->title->words, NOTHING, |
958 | f, keywords, &conf); |
959 | } |
960 | } |
961 | element_close(&ho, "title"); |
962 | html_nl(&ho); |
963 | |
1aed5cf5 |
964 | if (conf.rellinks) { |
965 | |
966 | if (prevf) { |
967 | element_empty(&ho, "link"); |
968 | element_attr(&ho, "rel", "previous"); |
969 | element_attr(&ho, "href", prevf->filename); |
970 | html_nl(&ho); |
971 | } |
972 | |
973 | /* FIXME: link rel="up" */ |
974 | |
975 | if (f != files.head) { |
976 | element_empty(&ho, "link"); |
977 | element_attr(&ho, "rel", "ToC"); |
978 | element_attr(&ho, "href", files.head->filename); |
979 | html_nl(&ho); |
980 | } |
981 | |
982 | if (has_index && files.index && f != files.index) { |
983 | element_empty(&ho, "link"); |
984 | element_attr(&ho, "rel", "index"); |
985 | element_attr(&ho, "href", files.index->filename); |
986 | html_nl(&ho); |
987 | } |
988 | |
989 | if (f->next) { |
990 | element_empty(&ho, "link"); |
991 | element_attr(&ho, "rel", "next"); |
992 | element_attr(&ho, "href", f->next->filename); |
993 | html_nl(&ho); |
994 | } |
995 | |
996 | } |
997 | |
78c73085 |
998 | if (conf.head_end) |
999 | html_raw(&ho, conf.head_end); |
1000 | |
9acfce4f |
1001 | /* |
1002 | * Add any <head> data defined in specific sections |
1003 | * that go in this file. (This is mostly to allow <meta |
1004 | * name="AppleTitle"> tags for Mac online help.) |
1005 | */ |
1006 | for (s = sects.head; s; s = s->next) { |
1007 | if (s->file == f && s->text) { |
1008 | for (p = s->text; |
6f0bdcde |
1009 | p && (p == s->text || p->type == para_Title || |
1010 | !is_heading_type(p->type)); |
9acfce4f |
1011 | p = p->next) { |
1012 | if (p->type == para_Config) { |
1013 | if (!ustricmp(p->keyword, L"html-local-head")) { |
1014 | html_raw(&ho, adv(p->origkeyword)); |
1015 | } |
1016 | } |
1017 | } |
1018 | } |
1019 | } |
1020 | |
78c73085 |
1021 | element_close(&ho, "head"); |
1022 | html_nl(&ho); |
1023 | |
78c73085 |
1024 | if (conf.body_tag) |
1025 | html_raw(&ho, conf.body_tag); |
1026 | else |
1027 | element_open(&ho, "body"); |
1028 | html_nl(&ho); |
1029 | |
1030 | if (conf.body_start) |
1031 | html_raw(&ho, conf.body_start); |
1032 | |
1033 | /* |
1034 | * Write out a nav bar. Special case: we don't do this |
1035 | * if there is only one file. |
1036 | */ |
f2ef00b5 |
1037 | if (conf.navlinks && files.head != files.tail) { |
78c73085 |
1038 | element_open(&ho, "p"); |
1039 | if (conf.nav_attr) |
1040 | html_raw_as_attr(&ho, conf.nav_attr); |
1041 | |
1042 | if (prevf) { |
1043 | element_open(&ho, "a"); |
1044 | element_attr(&ho, "href", prevf->filename); |
1045 | } |
56a99eb6 |
1046 | html_text(&ho, conf.nav_prev_text); |
78c73085 |
1047 | if (prevf) |
1048 | element_close(&ho, "a"); |
1049 | |
56a99eb6 |
1050 | html_text(&ho, conf.nav_separator); |
78c73085 |
1051 | |
1052 | if (f != files.head) { |
1053 | element_open(&ho, "a"); |
1054 | element_attr(&ho, "href", files.head->filename); |
1055 | } |
56a99eb6 |
1056 | html_text(&ho, conf.contents_text); |
78c73085 |
1057 | if (f != files.head) |
1058 | element_close(&ho, "a"); |
1059 | |
f2ef00b5 |
1060 | if (has_index && files.index) { |
771d3da3 |
1061 | html_text(&ho, conf.nav_separator); |
03fcb340 |
1062 | if (f != files.index) { |
1063 | element_open(&ho, "a"); |
1064 | element_attr(&ho, "href", files.index->filename); |
1065 | } |
1066 | html_text(&ho, conf.index_text); |
1067 | if (f != files.index) |
1068 | element_close(&ho, "a"); |
78c73085 |
1069 | } |
78c73085 |
1070 | |
56a99eb6 |
1071 | html_text(&ho, conf.nav_separator); |
78c73085 |
1072 | |
1073 | if (f->next) { |
1074 | element_open(&ho, "a"); |
1075 | element_attr(&ho, "href", f->next->filename); |
1076 | } |
56a99eb6 |
1077 | html_text(&ho, conf.nav_next_text); |
78c73085 |
1078 | if (f->next) |
1079 | element_close(&ho, "a"); |
1080 | |
1081 | element_close(&ho, "p"); |
1082 | html_nl(&ho); |
1083 | } |
1084 | prevf = f; |
1085 | |
1086 | /* |
f5dee642 |
1087 | * Write out a prefix TOC for the file (if a leaf file). |
78c73085 |
1088 | * |
1089 | * We start by going through the section list and |
1090 | * collecting the sections which need to be added to |
1091 | * the contents. On the way, we also test to see if |
1092 | * this file is a leaf file (defined as one which |
1093 | * contains all descendants of any section it |
1094 | * contains), because this will play a part in our |
1095 | * decision on whether or not to _output_ the TOC. |
1096 | * |
1097 | * Special case: we absolutely do not do this if we're |
1098 | * in single-file mode. |
1099 | */ |
1100 | if (files.head != files.tail) { |
1101 | int ntoc = 0, tocsize = 0; |
1102 | htmlsect **toc = NULL; |
1103 | int leaf = TRUE; |
1104 | |
1105 | for (s = sects.head; s; s = s->next) { |
1106 | htmlsect *a, *ac; |
1107 | int depth, adepth; |
1108 | |
1109 | /* |
1110 | * Search up from this section until we find |
1111 | * the highest-level one which belongs in this |
1112 | * file. |
1113 | */ |
1114 | depth = adepth = 0; |
1115 | a = NULL; |
1116 | for (ac = s; ac; ac = ac->parent) { |
1117 | if (ac->file == f) { |
1118 | a = ac; |
1119 | adepth = depth; |
1120 | } |
1121 | depth++; |
1122 | } |
1123 | |
1124 | if (s->file != f && a != NULL) |
1125 | leaf = FALSE; |
1126 | |
1127 | if (a) { |
1128 | if (adepth <= a->contents_depth) { |
1129 | if (ntoc >= tocsize) { |
1130 | tocsize += 64; |
f1530049 |
1131 | toc = sresize(toc, tocsize, htmlsect *); |
78c73085 |
1132 | } |
1133 | toc[ntoc++] = s; |
1134 | } |
1135 | } |
1136 | } |
1137 | |
1138 | if (leaf && conf.leaf_contains_contents && |
1139 | ntoc >= conf.leaf_smallest_contents) { |
1140 | int i; |
1141 | |
1142 | for (i = 0; i < ntoc; i++) { |
1143 | htmlsect *s = toc[i]; |
1144 | int hlevel = (s->type == TOP ? -1 : |
1145 | s->type == INDEX ? 0 : |
1146 | heading_depth(s->title)) |
1147 | - f->min_heading_depth + 1; |
1148 | |
1149 | assert(hlevel >= 1); |
1150 | html_contents_entry(&ho, hlevel, s, |
1151 | f, keywords, &conf); |
1152 | } |
1153 | html_contents_entry(&ho, 0, NULL, f, keywords, &conf); |
1154 | } |
1155 | } |
1156 | |
1157 | /* |
1158 | * Now go through the document and output some real |
1159 | * text. |
1160 | */ |
1161 | displaying = FALSE; |
1162 | for (s = sects.head; s; s = s->next) { |
1163 | if (s->file == f) { |
1164 | /* |
1165 | * This section belongs in this file. |
1166 | * Display it. |
1167 | */ |
1168 | displaying = TRUE; |
1169 | } else { |
f5dee642 |
1170 | /* |
1171 | * Doesn't belong in this file, but it may be |
1172 | * a descendant of a section which does, in |
1173 | * which case we should consider it for the |
1174 | * main TOC of this file (for non-leaf files). |
1175 | */ |
78c73085 |
1176 | htmlsect *a, *ac; |
1177 | int depth, adepth; |
1178 | |
1179 | displaying = FALSE; |
1180 | |
1181 | /* |
1182 | * Search up from this section until we find |
1183 | * the highest-level one which belongs in this |
1184 | * file. |
1185 | */ |
1186 | depth = adepth = 0; |
1187 | a = NULL; |
1188 | for (ac = s; ac; ac = ac->parent) { |
1189 | if (ac->file == f) { |
1190 | a = ac; |
1191 | adepth = depth; |
1192 | } |
1193 | depth++; |
1194 | } |
1195 | |
1196 | if (a != NULL) { |
1197 | /* |
1198 | * This section does not belong in this |
1199 | * file, but an ancestor of it does. Write |
1200 | * out a contents table entry, if the depth |
1201 | * doesn't exceed the maximum contents |
1202 | * depth for the ancestor section. |
1203 | */ |
1204 | if (adepth <= a->contents_depth) { |
1205 | html_contents_entry(&ho, adepth, s, |
1206 | f, keywords, &conf); |
1207 | } |
1208 | } |
1209 | } |
1210 | |
1211 | if (displaying) { |
1212 | int hlevel; |
1213 | char htag[3]; |
1214 | |
1215 | html_contents_entry(&ho, 0, NULL, f, keywords, &conf); |
1216 | |
1217 | /* |
1218 | * Display the section heading. |
1219 | */ |
1220 | |
1221 | hlevel = (s->type == TOP ? -1 : |
1222 | s->type == INDEX ? 0 : |
1223 | heading_depth(s->title)) |
1224 | - f->min_heading_depth + 1; |
1225 | assert(hlevel >= 1); |
1226 | /* HTML headings only go up to <h6> */ |
1227 | if (hlevel > 6) |
1228 | hlevel = 6; |
1229 | htag[0] = 'h'; |
1230 | htag[1] = '0' + hlevel; |
1231 | htag[2] = '\0'; |
1232 | element_open(&ho, htag); |
1233 | |
1234 | /* |
12f0ee84 |
1235 | * Provide anchor(s) for cross-links to target. |
78c73085 |
1236 | * |
78c73085 |
1237 | * (Also we'll have to do this separately in |
1238 | * other paragraph types - NumberedList and |
1239 | * BiblioCited.) |
1240 | */ |
12f0ee84 |
1241 | { |
1242 | int i; |
1243 | for (i=0; i < conf.ntfragments; i++) |
1244 | if (s->fragments[i]) |
1245 | html_fragment(&ho, s->fragments[i]); |
1246 | } |
78c73085 |
1247 | |
23c9bbc2 |
1248 | html_section_title(&ho, s, f, keywords, &conf, TRUE); |
78c73085 |
1249 | |
1250 | element_close(&ho, htag); |
1251 | |
1252 | /* |
1253 | * Now display the section text. |
1254 | */ |
1255 | if (s->text) { |
f1530049 |
1256 | stackhead = snew(struct stackelement); |
78c73085 |
1257 | stackhead->next = NULL; |
1258 | stackhead->listtype = NOLIST; |
1259 | stackhead->itemtype = NOITEM; |
1260 | |
1261 | for (p = s->text;; p = p->next) { |
1262 | enum LISTTYPE listtype; |
1263 | struct stackelement *se; |
1264 | |
1265 | /* |
1266 | * Preliminary switch to figure out what |
1267 | * sort of list we expect to be inside at |
1268 | * this stage. |
1269 | * |
1270 | * Since p may still be NULL at this point, |
1271 | * I invent a harmless paragraph type for |
1272 | * it if it is. |
1273 | */ |
1274 | switch (p ? p->type : para_Normal) { |
1275 | case para_Rule: |
1276 | case para_Normal: |
1277 | case para_Copyright: |
1278 | case para_BiblioCited: |
1279 | case para_Code: |
1280 | case para_QuotePush: |
1281 | case para_QuotePop: |
1282 | case para_Chapter: |
1283 | case para_Appendix: |
1284 | case para_UnnumberedChapter: |
1285 | case para_Heading: |
1286 | case para_Subsect: |
1287 | case para_LcontPop: |
1288 | listtype = NOLIST; |
1289 | break; |
1290 | |
1291 | case para_Bullet: |
1292 | listtype = UL; |
1293 | break; |
1294 | |
1295 | case para_NumberedList: |
1296 | listtype = OL; |
1297 | break; |
1298 | |
1299 | case para_DescribedThing: |
1300 | case para_Description: |
1301 | listtype = DL; |
1302 | break; |
1303 | |
1304 | case para_LcontPush: |
f1530049 |
1305 | se = snew(struct stackelement); |
78c73085 |
1306 | se->next = stackhead; |
1307 | se->listtype = NOLIST; |
1308 | se->itemtype = NOITEM; |
1309 | stackhead = se; |
1310 | continue; |
1311 | |
1312 | default: /* some totally non-printing para */ |
1313 | continue; |
1314 | } |
1315 | |
1316 | html_nl(&ho); |
1317 | |
1318 | /* |
1319 | * Terminate the most recent list item, if |
1320 | * any. (We left this until after |
1321 | * processing LcontPush, since in that case |
1322 | * the list item won't want to be |
1323 | * terminated until after the corresponding |
1324 | * LcontPop.) |
1325 | */ |
1326 | if (stackhead->itemtype != NOITEM) { |
1327 | element_close(&ho, itemname(stackhead->itemtype)); |
1328 | html_nl(&ho); |
1329 | } |
1330 | stackhead->itemtype = NOITEM; |
1331 | |
1332 | /* |
1333 | * Terminate the current list, if it's not |
1334 | * the one we want to be in. |
1335 | */ |
1336 | if (listtype != stackhead->listtype && |
1337 | stackhead->listtype != NOLIST) { |
1338 | element_close(&ho, listname(stackhead->listtype)); |
1339 | html_nl(&ho); |
1340 | } |
1341 | |
1342 | /* |
1343 | * Leave the loop if our time has come. |
1344 | */ |
1345 | if (!p || (is_heading_type(p->type) && |
1346 | p->type != para_Title)) |
1347 | break; /* end of section text */ |
1348 | |
1349 | /* |
1350 | * Start a fresh list if necessary. |
1351 | */ |
1352 | if (listtype != stackhead->listtype && |
1353 | listtype != NOLIST) |
1354 | element_open(&ho, listname(listtype)); |
1355 | |
1356 | stackhead->listtype = listtype; |
1357 | |
1358 | switch (p->type) { |
1359 | case para_Rule: |
1360 | element_empty(&ho, "hr"); |
1361 | break; |
1362 | case para_Code: |
1363 | html_codepara(&ho, p->words); |
1364 | break; |
1365 | case para_Normal: |
1366 | case para_Copyright: |
1367 | element_open(&ho, "p"); |
1368 | html_nl(&ho); |
1369 | html_words(&ho, p->words, ALL, |
1370 | f, keywords, &conf); |
1371 | html_nl(&ho); |
1372 | element_close(&ho, "p"); |
1373 | break; |
1374 | case para_BiblioCited: |
1375 | element_open(&ho, "p"); |
1376 | if (p->private_data) { |
1377 | htmlsect *s = (htmlsect *)p->private_data; |
12f0ee84 |
1378 | int i; |
1379 | for (i=0; i < conf.ntfragments; i++) |
1380 | if (s->fragments[i]) |
1381 | html_fragment(&ho, s->fragments[i]); |
78c73085 |
1382 | } |
1383 | html_nl(&ho); |
1384 | html_words(&ho, p->kwtext, ALL, |
1385 | f, keywords, &conf); |
1386 | html_text(&ho, L" "); |
1387 | html_words(&ho, p->words, ALL, |
1388 | f, keywords, &conf); |
1389 | html_nl(&ho); |
1390 | element_close(&ho, "p"); |
1391 | break; |
1392 | case para_Bullet: |
1393 | case para_NumberedList: |
1394 | element_open(&ho, "li"); |
1395 | if (p->private_data) { |
1396 | htmlsect *s = (htmlsect *)p->private_data; |
12f0ee84 |
1397 | int i; |
1398 | for (i=0; i < conf.ntfragments; i++) |
1399 | if (s->fragments[i]) |
1400 | html_fragment(&ho, s->fragments[i]); |
78c73085 |
1401 | } |
1402 | html_nl(&ho); |
1403 | stackhead->itemtype = LI; |
1404 | html_words(&ho, p->words, ALL, |
1405 | f, keywords, &conf); |
1406 | break; |
1407 | case para_DescribedThing: |
1408 | element_open(&ho, "dt"); |
1409 | html_nl(&ho); |
1410 | stackhead->itemtype = DT; |
1411 | html_words(&ho, p->words, ALL, |
1412 | f, keywords, &conf); |
1413 | break; |
1414 | case para_Description: |
1415 | element_open(&ho, "dd"); |
1416 | html_nl(&ho); |
1417 | stackhead->itemtype = DD; |
1418 | html_words(&ho, p->words, ALL, |
1419 | f, keywords, &conf); |
1420 | break; |
1421 | |
1422 | case para_QuotePush: |
1423 | element_open(&ho, "blockquote"); |
1424 | break; |
1425 | case para_QuotePop: |
1426 | element_close(&ho, "blockquote"); |
1427 | break; |
1428 | |
1429 | case para_LcontPop: |
1430 | se = stackhead; |
1431 | stackhead = stackhead->next; |
1432 | assert(stackhead); |
1433 | sfree(se); |
1434 | break; |
1435 | } |
1436 | } |
1437 | |
1438 | assert(stackhead && !stackhead->next); |
1439 | sfree(stackhead); |
1440 | } |
1441 | |
1442 | if (s->type == INDEX) { |
1443 | indexentry *entry; |
1444 | int i; |
1445 | |
1446 | /* |
1447 | * This section is the index. I'll just |
1448 | * render it as a single paragraph, with a |
1449 | * colon between the index term and the |
1450 | * references, and <br> in between each |
1451 | * entry. |
1452 | */ |
1453 | element_open(&ho, "p"); |
1454 | |
1455 | for (i = 0; (entry = |
1456 | index234(idx->entries, i)) != NULL; i++) { |
1457 | htmlindex *hi = (htmlindex *)entry->backend_data; |
1458 | int j; |
1459 | |
1460 | if (i > 0) |
1461 | element_empty(&ho, "br"); |
1462 | html_nl(&ho); |
1463 | |
1464 | html_words(&ho, entry->text, MARKUP|LINKS, |
1465 | f, keywords, &conf); |
1466 | |
56a99eb6 |
1467 | html_text(&ho, conf.index_main_sep); |
78c73085 |
1468 | |
1469 | for (j = 0; j < hi->nrefs; j++) { |
1470 | htmlindexref *hr = |
1471 | (htmlindexref *)hi->refs[j]->private_data; |
1472 | paragraph *p = hr->section->title; |
1473 | |
1474 | if (j > 0) |
56a99eb6 |
1475 | html_text(&ho, conf.index_multi_sep); |
78c73085 |
1476 | |
1477 | html_href(&ho, f, hr->section->file, |
1478 | hr->fragment); |
1b7bf715 |
1479 | hr->referenced = TRUE; |
78c73085 |
1480 | if (p && p->kwtext) |
1481 | html_words(&ho, p->kwtext, MARKUP|LINKS, |
1482 | f, keywords, &conf); |
1483 | else if (p && p->words) |
1484 | html_words(&ho, p->words, MARKUP|LINKS, |
1485 | f, keywords, &conf); |
56a99eb6 |
1486 | else { |
1487 | /* |
1488 | * If there is no title at all, |
1489 | * this must be because our |
1490 | * target section is the |
1491 | * preamble section and there |
1492 | * is no title. So we use the |
1493 | * preamble_text. |
1494 | */ |
1495 | html_text(&ho, conf.preamble_text); |
1496 | } |
78c73085 |
1497 | element_close(&ho, "a"); |
1498 | } |
1499 | } |
1500 | element_close(&ho, "p"); |
1501 | } |
1502 | } |
1503 | } |
1504 | |
1505 | html_contents_entry(&ho, 0, NULL, f, keywords, &conf); |
1506 | html_nl(&ho); |
1507 | |
1508 | { |
1509 | /* |
1510 | * Footer. |
1511 | */ |
1512 | int done_version_ids = FALSE; |
1513 | |
f2ef00b5 |
1514 | if (conf.address_section) |
1515 | element_empty(&ho, "hr"); |
78c73085 |
1516 | |
1517 | if (conf.body_end) |
1518 | html_raw(&ho, conf.body_end); |
1519 | |
1520 | if (conf.address_section) { |
27bdc5ab |
1521 | int started = FALSE; |
1522 | if (conf.htmlver == ISO_HTML) { |
1523 | /* |
1524 | * The ISO-HTML validator complains if |
1525 | * there isn't a <div> tag surrounding the |
1526 | * <address> tag. I'm uncertain of why this |
1527 | * should be - there appears to be no |
1528 | * mention of this in the ISO-HTML spec, |
1529 | * suggesting that it doesn't represent a |
1530 | * change from HTML 4, but nonetheless the |
1531 | * HTML 4 validator doesn't seem to mind. |
1532 | */ |
1533 | element_open(&ho, "div"); |
1534 | } |
78c73085 |
1535 | element_open(&ho, "address"); |
1536 | if (conf.addr_start) { |
1537 | html_raw(&ho, conf.addr_start); |
1538 | html_nl(&ho); |
27bdc5ab |
1539 | started = TRUE; |
78c73085 |
1540 | } |
1541 | if (conf.visible_version_id) { |
78c73085 |
1542 | for (p = sourceform; p; p = p->next) |
1543 | if (p->type == para_VersionID) { |
27bdc5ab |
1544 | if (started) |
78c73085 |
1545 | element_empty(&ho, "br"); |
1546 | html_nl(&ho); |
56a99eb6 |
1547 | html_text(&ho, conf.pre_versionid); |
78c73085 |
1548 | html_words(&ho, p->words, NOTHING, |
1549 | f, keywords, &conf); |
56a99eb6 |
1550 | html_text(&ho, conf.post_versionid); |
78c73085 |
1551 | started = TRUE; |
1552 | } |
78c73085 |
1553 | done_version_ids = TRUE; |
1554 | } |
27bdc5ab |
1555 | if (conf.addr_end) { |
1556 | if (started) |
1557 | element_empty(&ho, "br"); |
78c73085 |
1558 | html_raw(&ho, conf.addr_end); |
27bdc5ab |
1559 | } |
78c73085 |
1560 | element_close(&ho, "address"); |
27bdc5ab |
1561 | if (conf.htmlver == ISO_HTML) |
1562 | element_close(&ho, "div"); |
78c73085 |
1563 | } |
1564 | |
1565 | if (!done_version_ids) { |
1566 | /* |
1567 | * If the user didn't want the version IDs |
1568 | * visible, I think we still have a duty to put |
1569 | * them in an HTML comment. |
1570 | */ |
1571 | int started = FALSE; |
1572 | for (p = sourceform; p; p = p->next) |
1573 | if (p->type == para_VersionID) { |
1574 | if (!started) { |
1575 | html_raw(&ho, "<!-- version IDs:\n"); |
1576 | started = TRUE; |
1577 | } |
1578 | html_words(&ho, p->words, NOTHING, |
1579 | f, keywords, &conf); |
1580 | html_nl(&ho); |
1581 | } |
1582 | if (started) |
1583 | html_raw(&ho, "-->\n"); |
1584 | } |
1585 | } |
1586 | |
1587 | element_close(&ho, "body"); |
1588 | html_nl(&ho); |
1589 | element_close(&ho, "html"); |
1590 | html_nl(&ho); |
1591 | cleanup(&ho); |
1592 | } |
1593 | } |
1594 | |
1595 | /* |
f2ef00b5 |
1596 | * Before we start outputting the HTML Help files, check |
1597 | * whether there's even going to _be_ an index file: we omit it |
1598 | * if the index contains nothing. |
1599 | */ |
1600 | hhk_filename = conf.hhk_filename; |
1601 | if (hhk_filename) { |
1602 | int ok = FALSE; |
1603 | int i; |
1604 | indexentry *entry; |
1605 | |
1606 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
1607 | htmlindex *hi = (htmlindex *)entry->backend_data; |
1608 | |
1609 | if (hi->nrefs > 0) { |
1610 | ok = TRUE; /* found an index entry */ |
1611 | break; |
1612 | } |
1613 | } |
1614 | |
1615 | if (!ok) |
1616 | hhk_filename = NULL; |
1617 | } |
1618 | |
1619 | /* |
1620 | * Output the MS HTML Help supporting files, if requested. |
376db5ee |
1621 | * |
1622 | * A good unofficial reference for these is <http://chmspec.nongnu.org/>. |
f2ef00b5 |
1623 | */ |
1624 | if (conf.hhp_filename) { |
1625 | htmlfile *f; |
1626 | htmloutput ho; |
1627 | |
1628 | ho.charset = CS_CP1252; /* as far as I know, HHP files are */ |
1629 | ho.restrict_charset = CS_CP1252; /* hardwired to this charset */ |
1630 | ho.cstate = charset_init_state; |
1631 | ho.ver = HTML_4; /* *shrug* */ |
1632 | ho.state = HO_NEUTRAL; |
1633 | ho.contents_level = 0; |
1634 | ho.hackflags = HO_HACK_QUOTENOTHING; |
1635 | |
1636 | ho.fp = fopen(conf.hhp_filename, "w"); |
1637 | if (!ho.fp) |
1638 | error(err_cantopenw, conf.hhp_filename); |
1639 | |
1640 | fprintf(ho.fp, |
1641 | "[OPTIONS]\n" |
376db5ee |
1642 | /* Binary TOC required for Next/Previous nav to work */ |
1643 | "Binary TOC=Yes\n" |
f2ef00b5 |
1644 | "Compatibility=1.1 or later\n" |
1645 | "Compiled file=%s\n" |
1646 | "Default Window=main\n" |
1647 | "Default topic=%s\n" |
1648 | "Display compile progress=Yes\n" |
1649 | "Full-text search=Yes\n" |
1650 | "Title=", conf.chm_filename, files.head->filename); |
1651 | |
1652 | ho.hacklimit = 255; |
1653 | html_words(&ho, topsect->title->words, NOTHING, |
1654 | NULL, keywords, &conf); |
1655 | |
1656 | fprintf(ho.fp, "\n"); |
1657 | |
1658 | /* |
1659 | * These two entries don't seem to be remotely necessary |
1660 | * for a successful run of the help _compiler_, but |
1661 | * omitting them causes the GUI Help Workshop to behave |
1662 | * rather strangely if you try to load the help project |
1663 | * into that and edit it. |
1664 | */ |
1665 | if (conf.hhc_filename) |
1666 | fprintf(ho.fp, "Contents file=%s\n", conf.hhc_filename); |
1667 | if (hhk_filename) |
1668 | fprintf(ho.fp, "Index file=%s\n", hhk_filename); |
1669 | |
1670 | fprintf(ho.fp, "\n[WINDOWS]\nmain=\""); |
1671 | |
1672 | ho.hackflags |= HO_HACK_OMITQUOTES; |
1673 | ho.hacklimit = 255; |
1674 | html_words(&ho, topsect->title->words, NOTHING, |
1675 | NULL, keywords, &conf); |
1676 | |
1677 | fprintf(ho.fp, "\",\"%s\",\"%s\",\"%s\",,,,,," |
376db5ee |
1678 | /* This first magic number is fsWinProperties, controlling |
1679 | * Navigation Pane options and the like. |
1680 | * Constants HHWIN_PROP_* in htmlhelp.h. */ |
1681 | "0x62520,," |
1682 | /* This second number is fsToolBarFlags, mainly controlling |
1683 | * toolbar buttons. Constants HHWIN_BUTTON_*. |
1684 | * NOTE: there are two pairs of bits for Next/Previous |
1685 | * buttons: 7/8 (which do nothing useful), and 21/22 |
1686 | * (which work). (Neither of these are exposed in the HHW |
1687 | * UI, but they work fine in HH.) We use the latter. */ |
1688 | "0x60304e,,,,,,,,0\n", |
f2ef00b5 |
1689 | conf.hhc_filename ? conf.hhc_filename : "", |
1690 | hhk_filename ? hhk_filename : "", |
1691 | files.head->filename); |
1692 | |
1693 | /* |
1694 | * The [FILES] section is also not necessary for |
1695 | * compilation (hhc appears to build up a list of needed |
1696 | * files just by following links from the given starting |
1697 | * points), but useful for loading the project into HHW. |
1698 | */ |
1699 | fprintf(ho.fp, "\n[FILES]\n"); |
1700 | for (f = files.head; f; f = f->next) |
1701 | fprintf(ho.fp, "%s\n", f->filename); |
1702 | |
1703 | fclose(ho.fp); |
1704 | } |
1705 | if (conf.hhc_filename) { |
1706 | htmlfile *f; |
1707 | htmlsect *s, *a; |
1708 | htmloutput ho; |
1709 | int currdepth = 0; |
1710 | |
1711 | ho.fp = fopen(conf.hhc_filename, "w"); |
1712 | if (!ho.fp) |
1713 | error(err_cantopenw, conf.hhc_filename); |
1714 | |
1715 | ho.charset = CS_CP1252; /* as far as I know, HHC files are */ |
1716 | ho.restrict_charset = CS_CP1252; /* hardwired to this charset */ |
1717 | ho.cstate = charset_init_state; |
1718 | ho.ver = HTML_4; /* *shrug* */ |
1719 | ho.state = HO_NEUTRAL; |
1720 | ho.contents_level = 0; |
1721 | ho.hackflags = HO_HACK_QUOTEQUOTES; |
1722 | |
1723 | /* |
1724 | * Magic DOCTYPE which seems to work for .HHC files. I'm |
1725 | * wary of trying to change it! |
1726 | */ |
1727 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n" |
1728 | "<HTML><HEAD>\n" |
1729 | "<META HTTP-EQUIV=\"Content-Type\" " |
1730 | "CONTENT=\"text/html; charset=%s\">\n" |
1731 | "</HEAD><BODY><UL>\n", |
1732 | charset_to_mimeenc(conf.output_charset)); |
1733 | |
1734 | for (f = files.head; f; f = f->next) { |
1735 | /* |
1736 | * For each HTML file, write out a contents entry. |
1737 | */ |
1738 | int depth, leaf = TRUE; |
1739 | |
1740 | /* |
1741 | * Determine the depth of this file in the contents |
1742 | * tree. |
1743 | * |
1744 | * If the file contains no sections, it is assumed to |
1745 | * have depth zero. |
1746 | */ |
1747 | depth = 0; |
1748 | if (f->first) |
1749 | for (a = f->first->parent; a && a->type != TOP; a = a->parent) |
1750 | depth++; |
1751 | |
1752 | /* |
1753 | * Determine if this file is a leaf file, by |
1754 | * trawling the section list to see if there's any |
1755 | * section with an ancestor in this file but which |
1756 | * is not itself in this file. |
1757 | * |
1758 | * Special case: for contents purposes, the TOP |
1759 | * file is not considered to be the parent of the |
1760 | * chapter files, so it's always a leaf. |
1761 | * |
1762 | * A file with no sections in it is also a leaf. |
1763 | */ |
1764 | if (f->first && f->first->type != TOP) { |
1765 | for (s = f->first; s; s = s->next) { |
1766 | htmlsect *a; |
1767 | |
1768 | if (leaf && s->file != f) { |
1769 | for (a = s; a; a = a->parent) |
1770 | if (a->file == f) { |
1771 | leaf = FALSE; |
1772 | break; |
1773 | } |
1774 | } |
1775 | } |
1776 | } |
1777 | |
1778 | /* |
1779 | * Now write out our contents entry. |
1780 | */ |
1781 | while (currdepth < depth) { |
1782 | fprintf(ho.fp, "<UL>\n"); |
1783 | currdepth++; |
1784 | } |
1785 | while (currdepth > depth) { |
1786 | fprintf(ho.fp, "</UL>\n"); |
1787 | currdepth--; |
1788 | } |
1789 | /* fprintf(ho.fp, "<!-- depth=%d -->", depth); */ |
1790 | fprintf(ho.fp, "<LI><OBJECT TYPE=\"text/sitemap\">" |
1791 | "<PARAM NAME=\"Name\" VALUE=\""); |
1792 | ho.hacklimit = 255; |
1793 | if (f->first->title) |
1794 | html_words(&ho, f->first->title->words, NOTHING, |
1795 | NULL, keywords, &conf); |
1796 | else if (f->first->type == INDEX) |
1797 | html_text(&ho, conf.index_text); |
1798 | fprintf(ho.fp, "\"><PARAM NAME=\"Local\" VALUE=\"%s\">" |
1799 | "<PARAM NAME=\"ImageNumber\" VALUE=\"%d\"></OBJECT>\n", |
1800 | f->filename, leaf ? 11 : 1); |
1801 | } |
1802 | |
1803 | while (currdepth > 0) { |
1804 | fprintf(ho.fp, "</UL>\n"); |
1805 | currdepth--; |
1806 | } |
1807 | |
1808 | fprintf(ho.fp, "</UL></BODY></HTML>\n"); |
1809 | |
1810 | cleanup(&ho); |
1811 | } |
1812 | if (hhk_filename) { |
1813 | htmlfile *f; |
1814 | htmloutput ho; |
1815 | indexentry *entry; |
1816 | int i; |
1817 | |
1818 | /* |
1819 | * First make a pass over all HTML files and set their |
1820 | * `temp' fields to zero, because we're about to use them. |
1821 | */ |
1822 | for (f = files.head; f; f = f->next) |
1823 | f->temp = 0; |
1824 | |
1825 | ho.fp = fopen(hhk_filename, "w"); |
1826 | if (!ho.fp) |
1827 | error(err_cantopenw, hhk_filename); |
1828 | |
1829 | ho.charset = CS_CP1252; /* as far as I know, HHK files are */ |
1830 | ho.restrict_charset = CS_CP1252; /* hardwired to this charset */ |
1831 | ho.cstate = charset_init_state; |
1832 | ho.ver = HTML_4; /* *shrug* */ |
1833 | ho.state = HO_NEUTRAL; |
1834 | ho.contents_level = 0; |
1835 | ho.hackflags = HO_HACK_QUOTEQUOTES; |
1836 | |
1837 | /* |
1838 | * Magic DOCTYPE which seems to work for .HHK files. I'm |
1839 | * wary of trying to change it! |
1840 | */ |
1841 | fprintf(ho.fp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n" |
1842 | "<HTML><HEAD>\n" |
1843 | "<META HTTP-EQUIV=\"Content-Type\" " |
1844 | "CONTENT=\"text/html; charset=%s\">\n" |
1845 | "</HEAD><BODY><UL>\n", |
1846 | charset_to_mimeenc(conf.output_charset)); |
1847 | |
1848 | /* |
1849 | * Go through the index terms and output each one. |
1850 | */ |
1851 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
1852 | htmlindex *hi = (htmlindex *)entry->backend_data; |
1853 | int j; |
1854 | |
1855 | if (hi->nrefs > 0) { |
1856 | fprintf(ho.fp, "<LI><OBJECT TYPE=\"text/sitemap\">\n" |
1857 | "<PARAM NAME=\"Name\" VALUE=\""); |
1858 | ho.hacklimit = 255; |
1859 | html_words(&ho, entry->text, NOTHING, |
1860 | NULL, keywords, &conf); |
1861 | fprintf(ho.fp, "\">\n"); |
1862 | |
1863 | for (j = 0; j < hi->nrefs; j++) { |
1864 | htmlindexref *hr = |
1865 | (htmlindexref *)hi->refs[j]->private_data; |
1866 | |
1867 | /* |
1868 | * Use the temp field to ensure we don't |
1869 | * reference the same file more than once. |
1870 | */ |
1871 | if (!hr->section->file->temp) { |
1872 | fprintf(ho.fp, "<PARAM NAME=\"Local\" VALUE=\"%s\">\n", |
1873 | hr->section->file->filename); |
1874 | hr->section->file->temp = 1; |
1875 | } |
1876 | |
1877 | hr->referenced = TRUE; |
1878 | } |
1879 | |
1880 | fprintf(ho.fp, "</OBJECT>\n"); |
1881 | |
1882 | /* |
1883 | * Now go through those files and re-clear the temp |
1884 | * fields ready for the _next_ index term. |
1885 | */ |
1886 | for (j = 0; j < hi->nrefs; j++) { |
1887 | htmlindexref *hr = |
1888 | (htmlindexref *)hi->refs[j]->private_data; |
1889 | hr->section->file->temp = 0; |
1890 | } |
1891 | } |
1892 | } |
1893 | |
1894 | fprintf(ho.fp, "</UL></BODY></HTML>\n"); |
1895 | cleanup(&ho); |
1896 | } |
1897 | |
1898 | /* |
1b7bf715 |
1899 | * Go through and check that no index fragments were referenced |
1900 | * without being generated, or indeed vice versa. |
1901 | * |
1902 | * (When I actually get round to freeing everything, this can |
1903 | * probably be the freeing loop as well.) |
1904 | */ |
1905 | for (p = sourceform; p; p = p->next) { |
1906 | word *w; |
1907 | for (w = p->words; w; w = w->next) |
1908 | if (w->type == word_IndexRef) { |
1909 | htmlindexref *hr = (htmlindexref *)w->private_data; |
1910 | |
1911 | assert(!hr->referenced == !hr->generated); |
1912 | } |
1913 | } |
1914 | |
1915 | /* |
529a6c83 |
1916 | * Free all the working data. |
78c73085 |
1917 | */ |
529a6c83 |
1918 | { |
1919 | htmlfragment *frag; |
1920 | while ( (frag = (htmlfragment *)delpos234(files.frags, 0)) != NULL ) { |
1921 | /* |
1922 | * frag->fragment is dynamically allocated, but will be |
1923 | * freed when we process the htmlsect structure which |
1924 | * it is attached to. |
1925 | */ |
1926 | sfree(frag); |
1927 | } |
1928 | freetree234(files.frags); |
1929 | } |
f2ef00b5 |
1930 | /* |
1931 | * The strings in files.files are all owned by their containing |
1932 | * htmlfile structures, so there's no need to free them here. |
1933 | */ |
1934 | freetree234(files.files); |
529a6c83 |
1935 | { |
1936 | htmlsect *sect, *tmp; |
1937 | sect = sects.head; |
1938 | while (sect) { |
12f0ee84 |
1939 | int i; |
529a6c83 |
1940 | tmp = sect->next; |
12f0ee84 |
1941 | for (i=0; i < conf.ntfragments; i++) |
1942 | sfree(sect->fragments[i]); |
1943 | sfree(sect->fragments); |
529a6c83 |
1944 | sfree(sect); |
1945 | sect = tmp; |
1946 | } |
1947 | sect = nonsects.head; |
1948 | while (sect) { |
12f0ee84 |
1949 | int i; |
529a6c83 |
1950 | tmp = sect->next; |
12f0ee84 |
1951 | for (i=0; i < conf.ntfragments; i++) |
1952 | sfree(sect->fragments[i]); |
1953 | sfree(sect->fragments); |
529a6c83 |
1954 | sfree(sect); |
1955 | sect = tmp; |
1956 | } |
1957 | } |
1958 | { |
1959 | htmlfile *file, *tmp; |
1960 | file = files.head; |
1961 | while (file) { |
1962 | tmp = file->next; |
1963 | sfree(file->filename); |
1964 | sfree(file); |
1965 | file = tmp; |
1966 | } |
1967 | } |
1968 | { |
1969 | int i; |
1970 | indexentry *entry; |
1971 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) { |
1972 | htmlindex *hi = (htmlindex *)entry->backend_data; |
1973 | sfree(hi); |
1974 | } |
1975 | } |
1976 | { |
1977 | paragraph *p; |
1978 | word *w; |
1979 | for (p = sourceform; p; p = p->next) |
1980 | for (w = p->words; w; w = w->next) |
1981 | if (w->type == word_IndexRef) { |
1982 | htmlindexref *hr = (htmlindexref *)w->private_data; |
1983 | assert(hr != NULL); |
1984 | sfree(hr->fragment); |
1985 | sfree(hr); |
1986 | } |
1987 | } |
12f0ee84 |
1988 | sfree(conf.asect); |
1989 | sfree(conf.single_filename); |
1990 | sfree(conf.contents_filename); |
1991 | sfree(conf.index_filename); |
1992 | sfree(conf.template_filename); |
1993 | while (conf.ntfragments--) |
1994 | sfree(conf.template_fragments[conf.ntfragments]); |
1995 | sfree(conf.template_fragments); |
78c73085 |
1996 | } |
1997 | |
1998 | static void html_file_section(htmlconfig *cfg, htmlfilelist *files, |
1999 | htmlsect *sect, int depth) |
2000 | { |
2001 | htmlfile *file; |
2002 | int ldepth; |
2003 | |
2004 | /* |
2005 | * `depth' is derived from the heading_depth() macro at the top |
2006 | * of this file, which counts title as -1, chapter as 0, |
2007 | * heading as 1 and subsection as 2. However, the semantics of |
2008 | * cfg->leaf_level are defined to count chapter as 1, heading |
2009 | * as 2 etc. So first I increment depth :-( |
2010 | */ |
2011 | ldepth = depth + 1; |
2012 | |
2013 | if (cfg->leaf_level == 0) { |
2014 | /* |
2015 | * leaf_level==0 is a special case, in which everything is |
2016 | * put into a single file. |
2017 | */ |
2018 | if (!files->single) |
2019 | files->single = html_new_file(files, cfg->single_filename); |
2020 | |
2021 | file = files->single; |
2022 | } else { |
2023 | /* |
2024 | * If the depth of this section is at or above leaf_level, |
2025 | * we invent a fresh file and put this section at its head. |
2026 | * Otherwise, we put it in the same file as its parent |
2027 | * section. |
f2ef00b5 |
2028 | * |
2029 | * Another special value of cfg->leaf_level is -1, which |
2030 | * means infinity (i.e. it's considered to always be |
2031 | * greater than depth). |
78c73085 |
2032 | */ |
f2ef00b5 |
2033 | if (cfg->leaf_level > 0 && ldepth > cfg->leaf_level) { |
78c73085 |
2034 | /* |
2035 | * We know that sect->parent cannot be NULL. The only |
2036 | * circumstance in which it can be is if sect is at |
2037 | * chapter or appendix level, i.e. ldepth==1; and if |
2038 | * that's the case, then we cannot have entered this |
2039 | * branch unless cfg->leaf_level==0, in which case we |
2040 | * would be in the single-file case above and not here |
2041 | * at all. |
2042 | */ |
2043 | assert(sect->parent); |
2044 | |
2045 | file = sect->parent->file; |
2046 | } else { |
2047 | if (sect->type == TOP) { |
2048 | file = html_new_file(files, cfg->contents_filename); |
2049 | } else if (sect->type == INDEX) { |
2050 | file = html_new_file(files, cfg->index_filename); |
2051 | } else { |
2052 | char *title; |
2053 | |
2054 | assert(ldepth > 0 && sect->title); |
2055 | title = html_format(sect->title, cfg->template_filename); |
2056 | file = html_new_file(files, title); |
2057 | sfree(title); |
2058 | } |
2059 | } |
2060 | } |
2061 | |
2062 | sect->file = file; |
2063 | |
2064 | if (file->min_heading_depth > depth) { |
2065 | /* |
2066 | * This heading is at a higher level than any heading we |
2067 | * have so far placed in this file; so we set the `first' |
2068 | * pointer. |
2069 | */ |
2070 | file->min_heading_depth = depth; |
2071 | file->first = sect; |
2072 | } |
2073 | |
2074 | if (file->min_heading_depth == depth) |
2075 | file->last = sect; |
2076 | } |
2077 | |
2078 | static htmlfile *html_new_file(htmlfilelist *list, char *filename) |
2079 | { |
f1530049 |
2080 | htmlfile *ret = snew(htmlfile); |
78c73085 |
2081 | |
2082 | ret->next = NULL; |
2083 | if (list->tail) |
2084 | list->tail->next = ret; |
2085 | else |
2086 | list->head = ret; |
2087 | list->tail = ret; |
2088 | |
f2ef00b5 |
2089 | ret->filename = html_sanitise_filename(list, dupstr(filename)); |
2090 | add234(list->files, ret->filename); |
78c73085 |
2091 | ret->last_fragment_number = 0; |
2092 | ret->min_heading_depth = INT_MAX; |
2093 | ret->first = ret->last = NULL; |
2094 | |
2095 | return ret; |
2096 | } |
2097 | |
12f0ee84 |
2098 | static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title, |
2099 | htmlconfig *cfg) |
78c73085 |
2100 | { |
f1530049 |
2101 | htmlsect *ret = snew(htmlsect); |
78c73085 |
2102 | |
2103 | ret->next = NULL; |
2104 | if (list->tail) |
2105 | list->tail->next = ret; |
2106 | else |
2107 | list->head = ret; |
2108 | list->tail = ret; |
2109 | |
2110 | ret->title = title; |
2111 | ret->file = NULL; |
2112 | ret->parent = NULL; |
2113 | ret->type = NORMAL; |
2114 | |
12f0ee84 |
2115 | ret->fragments = snewn(cfg->ntfragments, char *); |
2116 | { |
2117 | int i; |
2118 | for (i=0; i < cfg->ntfragments; i++) |
2119 | ret->fragments[i] = NULL; |
2120 | } |
2121 | |
78c73085 |
2122 | return ret; |
2123 | } |
2124 | |
2125 | static void html_words(htmloutput *ho, word *words, int flags, |
2126 | htmlfile *file, keywordlist *keywords, htmlconfig *cfg) |
2127 | { |
2128 | word *w; |
2129 | char *c; |
2130 | int style, type; |
2131 | |
2132 | for (w = words; w; w = w->next) switch (w->type) { |
2133 | case word_HyperLink: |
2134 | if (flags & LINKS) { |
2135 | element_open(ho, "a"); |
2136 | c = utoa_dup(w->text, CS_ASCII); |
2137 | element_attr(ho, "href", c); |
2138 | sfree(c); |
2139 | } |
2140 | break; |
2141 | case word_UpperXref: |
2142 | case word_LowerXref: |
2143 | if (flags & LINKS) { |
2144 | keyword *kwl = kw_lookup(keywords, w->text); |
2b0986a3 |
2145 | paragraph *p; |
2146 | htmlsect *s; |
2147 | |
2148 | assert(kwl); |
2149 | p = kwl->para; |
2150 | s = (htmlsect *)p->private_data; |
78c73085 |
2151 | |
2152 | assert(s); |
2153 | |
12f0ee84 |
2154 | html_href(ho, file, s->file, s->fragments[0]); |
78c73085 |
2155 | } |
2156 | break; |
2157 | case word_HyperEnd: |
2158 | case word_XrefEnd: |
2159 | if (flags & LINKS) |
2160 | element_close(ho, "a"); |
2161 | break; |
2162 | case word_IndexRef: |
2163 | if (flags & INDEXENTS) { |
2164 | htmlindexref *hr = (htmlindexref *)w->private_data; |
27bdc5ab |
2165 | html_fragment(ho, hr->fragment); |
1b7bf715 |
2166 | hr->generated = TRUE; |
78c73085 |
2167 | } |
2168 | break; |
2169 | case word_Normal: |
2170 | case word_Emph: |
2171 | case word_Code: |
2172 | case word_WeakCode: |
2173 | case word_WhiteSpace: |
2174 | case word_EmphSpace: |
2175 | case word_CodeSpace: |
2176 | case word_WkCodeSpace: |
2177 | case word_Quote: |
2178 | case word_EmphQuote: |
2179 | case word_CodeQuote: |
2180 | case word_WkCodeQuote: |
2181 | style = towordstyle(w->type); |
2182 | type = removeattr(w->type); |
2183 | if (style == word_Emph && |
2184 | (attraux(w->aux) == attr_First || |
2185 | attraux(w->aux) == attr_Only) && |
2186 | (flags & MARKUP)) |
2187 | element_open(ho, "em"); |
2188 | else if ((style == word_Code || style == word_WeakCode) && |
2189 | (attraux(w->aux) == attr_First || |
2190 | attraux(w->aux) == attr_Only) && |
2191 | (flags & MARKUP)) |
2192 | element_open(ho, "code"); |
2193 | |
2194 | if (type == word_WhiteSpace) |
2195 | html_text(ho, L" "); |
2196 | else if (type == word_Quote) { |
2197 | if (quoteaux(w->aux) == quote_Open) |
2198 | html_text(ho, cfg->lquote); |
2199 | else |
2200 | html_text(ho, cfg->rquote); |
2201 | } else { |
35b123a0 |
2202 | if (!w->alt || cvt_ok(ho->restrict_charset, w->text)) |
2203 | html_text_nbsp(ho, w->text); |
78c73085 |
2204 | else |
2205 | html_words(ho, w->alt, flags, file, keywords, cfg); |
2206 | } |
2207 | |
2208 | if (style == word_Emph && |
2209 | (attraux(w->aux) == attr_Last || |
2210 | attraux(w->aux) == attr_Only) && |
2211 | (flags & MARKUP)) |
2212 | element_close(ho, "em"); |
2213 | else if ((style == word_Code || style == word_WeakCode) && |
2214 | (attraux(w->aux) == attr_Last || |
2215 | attraux(w->aux) == attr_Only) && |
2216 | (flags & MARKUP)) |
2217 | element_close(ho, "code"); |
2218 | |
2219 | break; |
2220 | } |
2221 | } |
2222 | |
2223 | static void html_codepara(htmloutput *ho, word *words) |
2224 | { |
2225 | element_open(ho, "pre"); |
2226 | element_open(ho, "code"); |
2227 | for (; words; words = words->next) if (words->type == word_WeakCode) { |
2228 | char *open_tag; |
2229 | wchar_t *t, *e; |
2230 | |
2231 | t = words->text; |
2232 | if (words->next && words->next->type == word_Emph) { |
2233 | e = words->next->text; |
2234 | words = words->next; |
2235 | } else |
2236 | e = NULL; |
2237 | |
2238 | while (e && *e && *t) { |
2239 | int n; |
2240 | int ec = *e; |
2241 | |
2242 | for (n = 0; t[n] && e[n] && e[n] == ec; n++); |
2243 | |
2244 | open_tag = NULL; |
2245 | if (ec == 'i') |
2246 | open_tag = "em"; |
2247 | else if (ec == 'b') |
2248 | open_tag = "b"; |
2249 | if (open_tag) |
2250 | element_open(ho, open_tag); |
2251 | |
2252 | html_text_limit(ho, t, n); |
2253 | |
2254 | if (open_tag) |
2255 | element_close(ho, open_tag); |
2256 | |
2257 | t += n; |
2258 | e += n; |
2259 | } |
2260 | html_text(ho, t); |
2261 | html_nl(ho); |
2262 | } |
2263 | element_close(ho, "code"); |
2264 | element_close(ho, "pre"); |
2265 | } |
2266 | |
2267 | static void html_charset_cleanup(htmloutput *ho) |
2268 | { |
2269 | char outbuf[256]; |
2270 | int bytes; |
2271 | |
2272 | bytes = charset_from_unicode(NULL, NULL, outbuf, lenof(outbuf), |
2273 | ho->charset, &ho->cstate, NULL); |
3ae196b4 |
2274 | if (ho->fp && bytes > 0) |
78c73085 |
2275 | fwrite(outbuf, 1, bytes, ho->fp); |
2276 | } |
2277 | |
35b123a0 |
2278 | static void return_mostly_to_neutral(htmloutput *ho) |
78c73085 |
2279 | { |
3ae196b4 |
2280 | if (ho->fp) { |
2281 | if (ho->state == HO_IN_EMPTY_TAG && is_xhtml(ho->ver)) { |
2282 | fprintf(ho->fp, " />"); |
2283 | } else if (ho->state == HO_IN_EMPTY_TAG || ho->state == HO_IN_TAG) { |
2284 | fprintf(ho->fp, ">"); |
2285 | } |
78c73085 |
2286 | } |
2287 | |
2288 | ho->state = HO_NEUTRAL; |
2289 | } |
2290 | |
35b123a0 |
2291 | static void return_to_neutral(htmloutput *ho) |
2292 | { |
2293 | if (ho->state == HO_IN_TEXT) { |
2294 | html_charset_cleanup(ho); |
2295 | } |
2296 | |
2297 | return_mostly_to_neutral(ho); |
2298 | } |
2299 | |
78c73085 |
2300 | static void element_open(htmloutput *ho, char const *name) |
2301 | { |
2302 | return_to_neutral(ho); |
3ae196b4 |
2303 | if (ho->fp) |
2304 | fprintf(ho->fp, "<%s", name); |
78c73085 |
2305 | ho->state = HO_IN_TAG; |
2306 | } |
2307 | |
2308 | static void element_close(htmloutput *ho, char const *name) |
2309 | { |
2310 | return_to_neutral(ho); |
3ae196b4 |
2311 | if (ho->fp) |
2312 | fprintf(ho->fp, "</%s>", name); |
78c73085 |
2313 | ho->state = HO_NEUTRAL; |
2314 | } |
2315 | |
2316 | static void element_empty(htmloutput *ho, char const *name) |
2317 | { |
2318 | return_to_neutral(ho); |
3ae196b4 |
2319 | if (ho->fp) |
2320 | fprintf(ho->fp, "<%s", name); |
78c73085 |
2321 | ho->state = HO_IN_EMPTY_TAG; |
2322 | } |
2323 | |
2324 | static void html_nl(htmloutput *ho) |
2325 | { |
2326 | return_to_neutral(ho); |
3ae196b4 |
2327 | if (ho->fp) |
2328 | fputc('\n', ho->fp); |
78c73085 |
2329 | } |
2330 | |
2331 | static void html_raw(htmloutput *ho, char *text) |
2332 | { |
2333 | return_to_neutral(ho); |
3ae196b4 |
2334 | if (ho->fp) |
2335 | fputs(text, ho->fp); |
78c73085 |
2336 | } |
2337 | |
2338 | static void html_raw_as_attr(htmloutput *ho, char *text) |
2339 | { |
2340 | assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG); |
3ae196b4 |
2341 | if (ho->fp) { |
2342 | fputc(' ', ho->fp); |
2343 | fputs(text, ho->fp); |
2344 | } |
78c73085 |
2345 | } |
2346 | |
2347 | static void element_attr(htmloutput *ho, char const *name, char const *value) |
2348 | { |
2349 | html_charset_cleanup(ho); |
2350 | assert(ho->state == HO_IN_TAG || ho->state == HO_IN_EMPTY_TAG); |
3ae196b4 |
2351 | if (ho->fp) |
2352 | fprintf(ho->fp, " %s=\"%s\"", name, value); |
78c73085 |
2353 | } |
2354 | |
2355 | static void element_attr_w(htmloutput *ho, char const *name, |
2356 | wchar_t const *value) |
2357 | { |
2358 | html_charset_cleanup(ho); |
3ae196b4 |
2359 | if (ho->fp) |
2360 | fprintf(ho->fp, " %s=\"", name); |
35b123a0 |
2361 | html_text_limit_internal(ho, value, 0, TRUE, FALSE); |
78c73085 |
2362 | html_charset_cleanup(ho); |
3ae196b4 |
2363 | if (ho->fp) |
2364 | fputc('"', ho->fp); |
78c73085 |
2365 | } |
2366 | |
2367 | static void html_text(htmloutput *ho, wchar_t const *text) |
2368 | { |
35b123a0 |
2369 | return_mostly_to_neutral(ho); |
2370 | html_text_limit_internal(ho, text, 0, FALSE, FALSE); |
2371 | } |
2372 | |
2373 | static void html_text_nbsp(htmloutput *ho, wchar_t const *text) |
2374 | { |
2375 | return_mostly_to_neutral(ho); |
2376 | html_text_limit_internal(ho, text, 0, FALSE, TRUE); |
78c73085 |
2377 | } |
2378 | |
2379 | static void html_text_limit(htmloutput *ho, wchar_t const *text, int maxlen) |
2380 | { |
35b123a0 |
2381 | return_mostly_to_neutral(ho); |
2382 | html_text_limit_internal(ho, text, maxlen, FALSE, FALSE); |
78c73085 |
2383 | } |
2384 | |
2385 | static void html_text_limit_internal(htmloutput *ho, wchar_t const *text, |
35b123a0 |
2386 | int maxlen, int quote_quotes, int nbsp) |
78c73085 |
2387 | { |
2388 | int textlen = ustrlen(text); |
2389 | char outbuf[256]; |
2390 | int bytes, err; |
2391 | |
f2ef00b5 |
2392 | if (ho->hackflags & (HO_HACK_QUOTEQUOTES | HO_HACK_OMITQUOTES)) |
2393 | quote_quotes = TRUE; /* override the input value */ |
2394 | |
78c73085 |
2395 | if (maxlen > 0 && textlen > maxlen) |
2396 | textlen = maxlen; |
f2ef00b5 |
2397 | if (ho->hacklimit >= 0) { |
2398 | if (textlen > ho->hacklimit) |
2399 | textlen = ho->hacklimit; |
2400 | ho->hacklimit -= textlen; |
2401 | } |
78c73085 |
2402 | |
2403 | while (textlen > 0) { |
2404 | /* Scan ahead for characters we really can't display in HTML. */ |
2405 | int lenbefore, lenafter; |
2406 | for (lenbefore = 0; lenbefore < textlen; lenbefore++) |
2407 | if (text[lenbefore] == L'<' || |
2408 | text[lenbefore] == L'>' || |
2409 | text[lenbefore] == L'&' || |
35b123a0 |
2410 | (text[lenbefore] == L'"' && quote_quotes) || |
2411 | (text[lenbefore] == L' ' && nbsp)) |
78c73085 |
2412 | break; |
2413 | lenafter = lenbefore; |
2414 | bytes = charset_from_unicode(&text, &lenafter, outbuf, lenof(outbuf), |
2415 | ho->charset, &ho->cstate, &err); |
2416 | textlen -= (lenbefore - lenafter); |
3ae196b4 |
2417 | if (bytes > 0 && ho->fp) |
78c73085 |
2418 | fwrite(outbuf, 1, bytes, ho->fp); |
2419 | if (err) { |
2420 | /* |
2421 | * We have encountered a character that cannot be |
2422 | * displayed in the selected output charset. Therefore, |
2423 | * we use an HTML numeric entity reference. |
2424 | */ |
2425 | assert(textlen > 0); |
3ae196b4 |
2426 | if (ho->fp) |
2427 | fprintf(ho->fp, "&#%ld;", (long int)*text); |
78c73085 |
2428 | text++, textlen--; |
2429 | } else if (lenafter == 0 && textlen > 0) { |
2430 | /* |
2431 | * We have encountered a character which is special to |
2432 | * HTML. |
2433 | */ |
3ae196b4 |
2434 | if (ho->fp) { |
f2ef00b5 |
2435 | if (*text == L'"' && (ho->hackflags & HO_HACK_OMITQUOTES)) { |
2436 | fputc('\'', ho->fp); |
2437 | } else if (ho->hackflags & HO_HACK_QUOTENOTHING) { |
2438 | fputc(*text, ho->fp); |
2439 | } else { |
2440 | if (*text == L'<') |
2441 | fprintf(ho->fp, "<"); |
2442 | else if (*text == L'>') |
2443 | fprintf(ho->fp, ">"); |
2444 | else if (*text == L'&') |
2445 | fprintf(ho->fp, "&"); |
2446 | else if (*text == L'"') |
2447 | fprintf(ho->fp, """); |
2448 | else if (*text == L' ') { |
2449 | assert(nbsp); |
2450 | fprintf(ho->fp, " "); |
2451 | } else |
2452 | assert(!"Can't happen"); |
2453 | } |
3ae196b4 |
2454 | } |
78c73085 |
2455 | text++, textlen--; |
2456 | } |
2457 | } |
2458 | } |
2459 | |
2460 | static void cleanup(htmloutput *ho) |
2461 | { |
2462 | return_to_neutral(ho); |
3ae196b4 |
2463 | if (ho->fp) |
2464 | fclose(ho->fp); |
78c73085 |
2465 | } |
2466 | |
2467 | static void html_href(htmloutput *ho, htmlfile *thisfile, |
2468 | htmlfile *targetfile, char *targetfrag) |
2469 | { |
2470 | rdstringc rs = { 0, 0, NULL }; |
2471 | char *url; |
2472 | |
2473 | if (targetfile != thisfile) |
2474 | rdaddsc(&rs, targetfile->filename); |
2475 | if (targetfrag) { |
2476 | rdaddc(&rs, '#'); |
2477 | rdaddsc(&rs, targetfrag); |
2478 | } |
2479 | url = rs.text; |
2480 | |
2481 | element_open(ho, "a"); |
2482 | element_attr(ho, "href", url); |
2483 | sfree(url); |
2484 | } |
2485 | |
27bdc5ab |
2486 | static void html_fragment(htmloutput *ho, char const *fragment) |
2487 | { |
2488 | element_open(ho, "a"); |
2489 | element_attr(ho, "name", fragment); |
2490 | if (is_xhtml(ho->ver)) |
2491 | element_attr(ho, "id", fragment); |
2492 | element_close(ho, "a"); |
2493 | } |
2494 | |
78c73085 |
2495 | static char *html_format(paragraph *p, char *template_string) |
2496 | { |
2497 | char *c, *t; |
2498 | word *w; |
2499 | wchar_t *ws, wsbuf[2]; |
2500 | rdstringc rs = { 0, 0, NULL }; |
2501 | |
2502 | t = template_string; |
2503 | while (*t) { |
2504 | if (*t == '%' && t[1]) { |
2505 | int fmt; |
2506 | |
2507 | t++; |
2508 | fmt = *t++; |
2509 | |
2510 | if (fmt == '%') { |
2511 | rdaddc(&rs, fmt); |
2512 | continue; |
2513 | } |
2514 | |
2515 | w = NULL; |
2516 | ws = NULL; |
2517 | |
2518 | if (p->kwtext && fmt == 'n') |
2519 | w = p->kwtext; |
2520 | else if (p->kwtext2 && fmt == 'b') { |
2521 | /* |
2522 | * HTML fragment names must start with a letter, so |
2523 | * simply `1.2.3' is not adequate. In this case I'm |
2524 | * going to cheat slightly by prepending the first |
2525 | * character of the first word of kwtext, so that |
2526 | * we get `C1' for chapter 1, `S2.3' for section |
2527 | * 2.3 etc. |
2528 | */ |
2529 | if (p->kwtext && p->kwtext->text[0]) { |
2530 | ws = wsbuf; |
2531 | wsbuf[1] = '\0'; |
2532 | wsbuf[0] = p->kwtext->text[0]; |
2533 | } |
2534 | w = p->kwtext2; |
2535 | } else if (p->keyword && *p->keyword && fmt == 'k') |
2536 | ws = p->keyword; |
2537 | else |
46c7302f |
2538 | /* %N comes here; also failure cases of other fmts */ |
78c73085 |
2539 | w = p->words; |
2540 | |
2541 | if (ws) { |
2542 | c = utoa_dup(ws, CS_ASCII); |
2543 | rdaddsc(&rs,c); |
2544 | sfree(c); |
2545 | } |
2546 | |
2547 | while (w) { |
2548 | if (removeattr(w->type) == word_Normal) { |
2549 | c = utoa_dup(w->text, CS_ASCII); |
2550 | rdaddsc(&rs,c); |
2551 | sfree(c); |
2552 | } |
2553 | w = w->next; |
2554 | } |
2555 | } else { |
2556 | rdaddc(&rs, *t++); |
2557 | } |
2558 | } |
2559 | |
2560 | return rdtrimc(&rs); |
2561 | } |
2562 | |
3e82de8f |
2563 | static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file, |
2564 | char *text) |
78c73085 |
2565 | { |
2566 | /* |
2567 | * The HTML 4 spec's strictest definition of fragment names (<a |
2568 | * name> and "id" attributes) says that they `must begin with a |
2569 | * letter and may be followed by any number of letters, digits, |
2570 | * hyphens, underscores, colons, and periods'. |
2571 | * |
2572 | * So here we unceremoniously rip out any characters not |
2573 | * conforming to this limitation. |
2574 | */ |
2575 | char *p = text, *q = text; |
2576 | |
2577 | while (*p && !((*p>='A' && *p<='Z') || (*p>='a' && *p<='z'))) |
2578 | p++; |
3e82de8f |
2579 | if ((*q++ = *p++) != '\0') { |
2580 | while (*p) { |
2581 | if ((*p>='A' && *p<='Z') || |
2582 | (*p>='a' && *p<='z') || |
2583 | (*p>='0' && *p<='9') || |
2584 | *p=='-' || *p=='_' || *p==':' || *p=='.') |
2585 | *q++ = *p; |
2586 | p++; |
2587 | } |
2588 | |
2589 | *q = '\0'; |
2590 | } |
2591 | |
12f0ee84 |
2592 | /* If there's nothing left, make something valid up */ |
2593 | if (!*text) { |
0bac815b |
2594 | static const char anonfrag[] = "anon"; |
12f0ee84 |
2595 | text = sresize(text, lenof(anonfrag), char); |
2596 | strcpy(text, anonfrag); |
2597 | } |
2598 | |
3e82de8f |
2599 | /* |
2600 | * Now we check for clashes with other fragment names, and |
2601 | * adjust this one if necessary by appending a hyphen followed |
2602 | * by a number. |
2603 | */ |
2604 | { |
2605 | htmlfragment *frag = snew(htmlfragment); |
2606 | int len = 0; /* >0 indicates we have resized */ |
2607 | int suffix = 1; |
2608 | |
2609 | frag->file = file; |
2610 | frag->fragment = text; |
2611 | |
2612 | while (add234(files->frags, frag) != frag) { |
2613 | if (!len) { |
2614 | len = strlen(text); |
2615 | frag->fragment = text = sresize(text, len+20, char); |
2616 | } |
2617 | |
2618 | sprintf(text + len, "-%d", ++suffix); |
2619 | } |
78c73085 |
2620 | } |
f2ef00b5 |
2621 | |
2622 | return text; |
2623 | } |
2624 | |
2625 | static char *html_sanitise_filename(htmlfilelist *files, char *text) |
2626 | { |
2627 | /* |
2628 | * Unceremoniously rip out any character that might cause |
2629 | * difficulty in some filesystem or another, or be otherwise |
2630 | * inconvenient. |
2631 | * |
2632 | * That doesn't leave much punctuation. I permit alphanumerics |
2633 | * and +-.=_ only. |
2634 | */ |
2635 | char *p = text, *q = text; |
2636 | |
2637 | while (*p) { |
2638 | if ((*p>='A' && *p<='Z') || |
2639 | (*p>='a' && *p<='z') || |
2640 | (*p>='0' && *p<='9') || |
2641 | *p=='-' || *p=='_' || *p=='+' || *p=='.' || *p=='=') |
2642 | *q++ = *p; |
2643 | p++; |
2644 | } |
2645 | *q = '\0'; |
2646 | |
2647 | /* If there's nothing left, make something valid up */ |
2648 | if (!*text) { |
2649 | static const char anonfrag[] = "anon.html"; |
2650 | text = sresize(text, lenof(anonfrag), char); |
2651 | strcpy(text, anonfrag); |
2652 | } |
2653 | |
2654 | /* |
2655 | * Now we check for clashes with other filenames, and adjust |
2656 | * this one if necessary by appending a hyphen followed by a |
2657 | * number just before the file extension (if any). |
2658 | */ |
2659 | { |
2660 | int len, extpos; |
2661 | int suffix = 1; |
2662 | |
2663 | p = NULL; |
2664 | |
2665 | while (find234(files->files, text, NULL)) { |
2666 | if (!p) { |
2667 | len = strlen(text); |
2668 | p = text; |
2669 | text = snewn(len+20, char); |
2670 | |
2671 | for (extpos = len; extpos > 0 && p[extpos-1] != '.'; extpos--); |
2672 | if (extpos > 0) |
2673 | extpos--; |
2674 | else |
2675 | extpos = len; |
2676 | } |
2677 | |
2678 | sprintf(text, "%.*s-%d%s", extpos, p, ++suffix, p+extpos); |
2679 | } |
2680 | |
2681 | if (p) |
2682 | sfree(p); |
2683 | } |
78c73085 |
2684 | |
3e82de8f |
2685 | return text; |
78c73085 |
2686 | } |
2687 | |
2688 | static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s, |
2689 | htmlfile *thisfile, keywordlist *keywords, |
2690 | htmlconfig *cfg) |
2691 | { |
f5dee642 |
2692 | if (ho->contents_level >= depth && ho->contents_level > 0) { |
2693 | element_close(ho, "li"); |
2694 | html_nl(ho); |
2695 | } |
2696 | |
78c73085 |
2697 | while (ho->contents_level > depth) { |
2698 | element_close(ho, "ul"); |
2699 | ho->contents_level--; |
f5dee642 |
2700 | if (ho->contents_level > 0) { |
2701 | element_close(ho, "li"); |
2702 | } |
2703 | html_nl(ho); |
78c73085 |
2704 | } |
2705 | |
2706 | while (ho->contents_level < depth) { |
f5dee642 |
2707 | html_nl(ho); |
78c73085 |
2708 | element_open(ho, "ul"); |
f5dee642 |
2709 | html_nl(ho); |
78c73085 |
2710 | ho->contents_level++; |
2711 | } |
2712 | |
2713 | if (!s) |
2714 | return; |
2715 | |
2716 | element_open(ho, "li"); |
12f0ee84 |
2717 | html_href(ho, thisfile, s->file, s->fragments[0]); |
23c9bbc2 |
2718 | html_section_title(ho, s, thisfile, keywords, cfg, FALSE); |
78c73085 |
2719 | element_close(ho, "a"); |
f5dee642 |
2720 | /* <li> will be closed by a later invocation */ |
78c73085 |
2721 | } |
2722 | |
2723 | static void html_section_title(htmloutput *ho, htmlsect *s, htmlfile *thisfile, |
23c9bbc2 |
2724 | keywordlist *keywords, htmlconfig *cfg, |
2725 | int real) |
78c73085 |
2726 | { |
2727 | if (s->title) { |
2728 | sectlevel *sl; |
2729 | word *number; |
2730 | int depth = heading_depth(s->title); |
2731 | |
2732 | if (depth < 0) |
2733 | sl = NULL; |
2734 | else if (depth == 0) |
2735 | sl = &cfg->achapter; |
2736 | else if (depth <= cfg->nasect) |
2737 | sl = &cfg->asect[depth-1]; |
2738 | else |
2739 | sl = &cfg->asect[cfg->nasect-1]; |
2740 | |
2741 | if (!sl) |
2742 | number = NULL; |
2743 | else if (sl->just_numbers) |
2744 | number = s->title->kwtext2; |
2745 | else |
2746 | number = s->title->kwtext; |
2747 | |
2748 | if (number) { |
2749 | html_words(ho, number, MARKUP, |
2750 | thisfile, keywords, cfg); |
2751 | html_text(ho, sl->number_suffix); |
2752 | } |
2753 | |
23c9bbc2 |
2754 | html_words(ho, s->title->words, real ? ALL : MARKUP, |
78c73085 |
2755 | thisfile, keywords, cfg); |
2756 | } else { |
2757 | assert(s->type != NORMAL); |
56a99eb6 |
2758 | /* |
2759 | * If we're printing the full document title for _real_ and |
2760 | * there isn't one, we don't want to print `Preamble' at |
2761 | * the top of what ought to just be some text. If we need |
2762 | * it in any other context such as TOCs, we need to print |
2763 | * `Preamble'. |
2764 | */ |
2765 | if (s->type == TOP && !real) |
2766 | html_text(ho, cfg->preamble_text); |
78c73085 |
2767 | else if (s->type == INDEX) |
56a99eb6 |
2768 | html_text(ho, cfg->index_text); |
78c73085 |
2769 | } |
2770 | } |