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