d7482997 |
1 | /* |
2 | * Windows Help backend for Halibut |
d7482997 |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
50d6b4bd |
7 | #include <ctype.h> |
d7482997 |
8 | #include <assert.h> |
9 | |
10 | #include "halibut.h" |
11 | #include "winhelp.h" |
12 | |
13 | struct bk_whlp_state { |
14 | WHLP h; |
15 | indexdata *idx; |
16 | keywordlist *keywords; |
17 | WHLP_TOPIC curr_topic; |
08e78486 |
18 | int charset; |
19 | charset_state cstate; |
d7482997 |
20 | FILE *cntfp; |
21 | int cnt_last_level, cnt_workaround; |
22 | }; |
23 | |
08e78486 |
24 | typedef struct { |
25 | int charset; |
26 | wchar_t *bullet, *lquote, *rquote, *titlepage, *sectsuffix, *listsuffix; |
f6220253 |
27 | wchar_t *contents_text; |
08e78486 |
28 | char *filename; |
29 | } whlpconf; |
30 | |
d7482997 |
31 | /* |
32 | * Indexes of fonts in our standard font descriptor set. |
33 | */ |
34 | enum { |
35 | FONT_NORMAL, |
36 | FONT_EMPH, |
37 | FONT_CODE, |
4b3c5afb |
38 | FONT_ITAL_CODE, |
39 | FONT_BOLD_CODE, |
d7482997 |
40 | FONT_TITLE, |
41 | FONT_TITLE_EMPH, |
42 | FONT_TITLE_CODE, |
43 | FONT_RULE |
44 | }; |
45 | |
08e78486 |
46 | static void whlp_rdaddwc(rdstringc *rs, word *text, whlpconf *conf, |
47 | charset_state *state); |
48 | static void whlp_rdadds(rdstringc *rs, const wchar_t *text, whlpconf *conf, |
49 | charset_state *state); |
d7482997 |
50 | static void whlp_mkparagraph(struct bk_whlp_state *state, |
08e78486 |
51 | int font, word *text, int subsidiary, |
52 | whlpconf *conf); |
53 | static void whlp_navmenu(struct bk_whlp_state *state, paragraph *p, |
54 | whlpconf *conf); |
d7482997 |
55 | static void whlp_contents_write(struct bk_whlp_state *state, |
56 | int level, char *text, WHLP_TOPIC topic); |
08e78486 |
57 | static void whlp_wtext(struct bk_whlp_state *state, const wchar_t *text); |
d7482997 |
58 | |
ba9c1487 |
59 | paragraph *whlp_config_filename(char *filename) |
60 | { |
e4ea58f8 |
61 | return cmdline_cfg_simple("winhelp-filename", filename, NULL); |
ba9c1487 |
62 | } |
63 | |
08e78486 |
64 | static whlpconf whlp_configure(paragraph *source) { |
65 | paragraph *p; |
66 | whlpconf ret; |
67 | |
68 | /* |
69 | * Defaults. |
70 | */ |
71 | ret.charset = CS_CP1252; |
72 | ret.bullet = L"\x2022\0-\0\0"; |
73 | ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0"; |
74 | ret.rquote = uadv(ret.lquote); |
75 | ret.filename = dupstr("output.hlp"); |
76 | ret.titlepage = L"Title page"; |
f6220253 |
77 | ret.contents_text = L"Contents"; |
08e78486 |
78 | ret.sectsuffix = L": "; |
79 | ret.listsuffix = L"."; |
80 | |
81 | /* |
82 | * Two-pass configuration so that we can pick up global config |
83 | * (e.g. `quotes') before having it overridden by specific |
84 | * config (`win-quotes'), irrespective of the order in which |
85 | * they occur. |
86 | */ |
87 | for (p = source; p; p = p->next) { |
88 | if (p->type == para_Config) { |
89 | if (!ustricmp(p->keyword, L"quotes")) { |
90 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
91 | ret.lquote = uadv(p->keyword); |
92 | ret.rquote = uadv(ret.lquote); |
93 | } |
94 | } |
95 | } |
96 | } |
97 | |
98 | for (p = source; p; p = p->next) { |
99 | p->private_data = NULL; |
100 | if (p->type == para_Config) { |
101 | /* |
102 | * In principle we should support a `winhelp-charset' |
103 | * here. We don't, because my WinHelp output code |
104 | * doesn't know how to change character set. Once I |
105 | * find out, I'll support it. |
106 | */ |
107 | if (p->parent && !ustricmp(p->keyword, L"winhelp-topic")) { |
108 | /* Store the topic name in the private_data field of the |
109 | * containing section. */ |
110 | p->parent->private_data = uadv(p->keyword); |
111 | } else if (!ustricmp(p->keyword, L"winhelp-filename")) { |
112 | sfree(ret.filename); |
113 | ret.filename = dupstr(adv(p->origkeyword)); |
114 | } else if (!ustricmp(p->keyword, L"winhelp-bullet")) { |
115 | ret.bullet = uadv(p->keyword); |
116 | } else if (!ustricmp(p->keyword, L"winhelp-section-suffix")) { |
117 | ret.sectsuffix = uadv(p->keyword); |
118 | } else if (!ustricmp(p->keyword, L"winhelp-list-suffix")) { |
119 | ret.listsuffix = uadv(p->keyword); |
120 | } else if (!ustricmp(p->keyword, L"winhelp-contents-titlepage")) { |
121 | ret.titlepage = uadv(p->keyword); |
122 | } else if (!ustricmp(p->keyword, L"winhelp-quotes")) { |
123 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
124 | ret.lquote = uadv(p->keyword); |
125 | ret.rquote = uadv(ret.lquote); |
126 | } |
f6220253 |
127 | } else if (!ustricmp(p->keyword, L"contents")) { |
128 | ret.contents_text = uadv(p->keyword); |
08e78486 |
129 | } |
130 | } |
131 | } |
132 | |
133 | /* |
134 | * Now process fallbacks on quote characters and bullets. |
135 | */ |
136 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) && |
137 | (!cvt_ok(ret.charset, ret.lquote) || |
138 | !cvt_ok(ret.charset, ret.rquote))) { |
139 | ret.lquote = uadv(ret.rquote); |
140 | ret.rquote = uadv(ret.lquote); |
141 | } |
142 | |
143 | while (*ret.bullet && *uadv(ret.bullet) && |
144 | !cvt_ok(ret.charset, ret.bullet)) |
145 | ret.bullet = uadv(ret.bullet); |
146 | |
147 | return ret; |
148 | } |
149 | |
d7482997 |
150 | void whlp_backend(paragraph *sourceform, keywordlist *keywords, |
43341922 |
151 | indexdata *idx, void *unused) { |
d7482997 |
152 | WHLP h; |
08e78486 |
153 | char *cntname; |
d7482997 |
154 | paragraph *p, *lastsect; |
155 | struct bk_whlp_state state; |
156 | WHLP_TOPIC contents_topic; |
157 | int i; |
7136a6c7 |
158 | int nesting; |
d7482997 |
159 | indexentry *ie; |
de967e1b |
160 | int done_contents_topic = FALSE; |
08e78486 |
161 | whlpconf conf; |
d7482997 |
162 | |
43341922 |
163 | IGNORE(unused); |
164 | |
d7482997 |
165 | h = state.h = whlp_new(); |
166 | state.keywords = keywords; |
167 | state.idx = idx; |
168 | |
169 | whlp_start_macro(h, "CB(\"btn_about\",\"&About\",\"About()\")"); |
170 | whlp_start_macro(h, "CB(\"btn_up\",\"&Up\",\"Contents()\")"); |
171 | whlp_start_macro(h, "BrowseButtons()"); |
172 | |
173 | whlp_create_font(h, "Times New Roman", WHLP_FONTFAM_SERIF, 24, |
174 | 0, 0, 0, 0); |
175 | whlp_create_font(h, "Times New Roman", WHLP_FONTFAM_SERIF, 24, |
176 | WHLP_FONT_ITALIC, 0, 0, 0); |
177 | whlp_create_font(h, "Courier New", WHLP_FONTFAM_FIXED, 24, |
178 | 0, 0, 0, 0); |
4b3c5afb |
179 | whlp_create_font(h, "Courier New", WHLP_FONTFAM_FIXED, 24, |
180 | WHLP_FONT_ITALIC, 0, 0, 0); |
181 | whlp_create_font(h, "Courier New", WHLP_FONTFAM_FIXED, 24, |
182 | WHLP_FONT_BOLD, 0, 0, 0); |
d7482997 |
183 | whlp_create_font(h, "Arial", WHLP_FONTFAM_SERIF, 30, |
184 | WHLP_FONT_BOLD, 0, 0, 0); |
185 | whlp_create_font(h, "Arial", WHLP_FONTFAM_SERIF, 30, |
186 | WHLP_FONT_BOLD|WHLP_FONT_ITALIC, 0, 0, 0); |
187 | whlp_create_font(h, "Courier New", WHLP_FONTFAM_FIXED, 30, |
188 | WHLP_FONT_BOLD, 0, 0, 0); |
189 | whlp_create_font(h, "Courier New", WHLP_FONTFAM_SANS, 18, |
190 | WHLP_FONT_STRIKEOUT, 0, 0, 0); |
191 | |
08e78486 |
192 | conf = whlp_configure(sourceform); |
193 | |
194 | state.charset = conf.charset; |
d7482997 |
195 | |
196 | /* |
50d6b4bd |
197 | * Ensure the output file name has a .hlp extension. This is |
198 | * required since we must create the .cnt file in parallel with |
199 | * it. |
200 | */ |
201 | { |
08e78486 |
202 | int len = strlen(conf.filename); |
203 | if (len < 4 || conf.filename[len-4] != '.' || |
204 | tolower(conf.filename[len-3] != 'h') || |
205 | tolower(conf.filename[len-2] != 'l') || |
206 | tolower(conf.filename[len-1] != 'p')) { |
50d6b4bd |
207 | char *newf; |
f1530049 |
208 | newf = snewn(len + 5, char); |
08e78486 |
209 | sprintf(newf, "%s.hlp", conf.filename); |
210 | sfree(conf.filename); |
211 | conf.filename = newf; |
50d6b4bd |
212 | len = strlen(newf); |
213 | } |
f1530049 |
214 | cntname = snewn(len+1, char); |
08e78486 |
215 | sprintf(cntname, "%.*s.cnt", len-4, conf.filename); |
50d6b4bd |
216 | } |
217 | |
218 | state.cntfp = fopen(cntname, "wb"); |
3ae196b4 |
219 | if (!state.cntfp) { |
220 | error(err_cantopenw, cntname); |
221 | return; |
222 | } |
50d6b4bd |
223 | state.cnt_last_level = -1; state.cnt_workaround = 0; |
224 | |
225 | /* |
d7482997 |
226 | * Loop over the source form registering WHLP_TOPICs for |
227 | * everything. |
228 | */ |
229 | |
230 | contents_topic = whlp_register_topic(h, "Top", NULL); |
231 | whlp_primary_topic(h, contents_topic); |
232 | for (p = sourceform; p; p = p->next) { |
233 | if (p->type == para_Chapter || |
234 | p->type == para_Appendix || |
235 | p->type == para_UnnumberedChapter || |
236 | p->type == para_Heading || |
237 | p->type == para_Subsect) { |
08e78486 |
238 | |
239 | rdstringc rs = { 0, 0, NULL }; |
d7482997 |
240 | char *errstr; |
241 | |
08e78486 |
242 | whlp_rdadds(&rs, (wchar_t *)p->private_data, &conf, NULL); |
243 | |
244 | p->private_data = whlp_register_topic(h, rs.text, &errstr); |
d7482997 |
245 | if (!p->private_data) { |
246 | p->private_data = whlp_register_topic(h, NULL, NULL); |
08e78486 |
247 | error(err_winhelp_ctxclash, &p->fpos, rs.text, errstr); |
d7482997 |
248 | } |
08e78486 |
249 | sfree(rs.text); |
d7482997 |
250 | } |
251 | } |
252 | |
253 | /* |
254 | * Loop over the index entries, preparing final text forms for |
255 | * each one. |
256 | */ |
7c570bf3 |
257 | { |
258 | indexentry *ie_prev = NULL; |
259 | int nspaces = 1; |
260 | |
261 | for (i = 0; (ie = index234(idx->entries, i)) != NULL; i++) { |
262 | rdstringc rs = {0, 0, NULL}; |
263 | charset_state state = CHARSET_INIT_STATE; |
264 | whlp_rdaddwc(&rs, ie->text, &conf, &state); |
265 | |
266 | if (ie_prev) { |
267 | /* |
268 | * It appears that Windows Help's index mechanism |
c770011a |
269 | * is inherently case-insensitive. Therefore, if two |
7c570bf3 |
270 | * adjacent index terms compare equal apart from |
271 | * case, I'm going to append nonbreaking spaces to |
272 | * the end of the second one so that Windows will |
273 | * treat them as distinct. |
274 | * |
275 | * This is nasty because we're depending on our |
276 | * case-insensitive comparison having the same |
277 | * semantics as the Windows one :-/ but I see no |
278 | * alternative. |
279 | */ |
280 | wchar_t *a, *b; |
281 | |
282 | a = ufroma_dup((char *)ie_prev->backend_data, conf.charset); |
283 | b = ufroma_dup(rs.text, conf.charset); |
284 | if (!ustricmp(a, b)) { |
285 | int j; |
286 | for (j = 0; j < nspaces; j++) |
287 | whlp_rdadds(&rs, L"\xA0", &conf, &state); |
288 | /* |
289 | * Add one to nspaces, so that if another term |
290 | * appears which is equivalent to the previous |
291 | * two it'll acquire one more space. |
292 | */ |
293 | nspaces++; |
294 | } else |
295 | nspaces = 1; |
296 | sfree(a); |
297 | sfree(b); |
298 | } |
299 | |
300 | whlp_rdadds(&rs, NULL, &conf, &state); |
301 | |
302 | ie->backend_data = rs.text; |
303 | |
304 | /* |
305 | * Only move ie_prev on if nspaces==1 (since when we |
306 | * have three or more adjacent terms differing only in |
307 | * case, we will want to compare with the _first_ of |
308 | * them because that won't have had any extra spaces |
309 | * added on which will foul up the comparison). |
310 | */ |
311 | if (nspaces == 1) |
312 | ie_prev = ie; |
313 | } |
d7482997 |
314 | } |
315 | |
316 | whlp_prepare(h); |
317 | |
318 | /* ------------------------------------------------------------------ |
8902e0ed |
319 | * Begin the contents page. |
d7482997 |
320 | */ |
f6220253 |
321 | { |
322 | rdstringc rs = {0, 0, NULL}; |
323 | whlp_rdadds(&rs, conf.contents_text, &conf, NULL); |
324 | whlp_begin_topic(h, contents_topic, rs.text, "DB(\"btn_up\")", NULL); |
325 | state.curr_topic = contents_topic; |
326 | sfree(rs.text); |
327 | } |
d7482997 |
328 | |
329 | /* |
330 | * The manual title goes in the non-scroll region, and also |
331 | * goes into the system title slot. |
332 | */ |
333 | { |
334 | rdstringc rs = {0, 0, NULL}; |
335 | for (p = sourceform; p; p = p->next) { |
336 | if (p->type == para_Title) { |
337 | whlp_begin_para(h, WHLP_PARA_NONSCROLL); |
08e78486 |
338 | state.cstate = charset_init_state; |
339 | whlp_mkparagraph(&state, FONT_TITLE, p->words, FALSE, &conf); |
340 | whlp_wtext(&state, NULL); |
d7482997 |
341 | whlp_end_para(h); |
08e78486 |
342 | whlp_rdaddwc(&rs, p->words, &conf, NULL); |
d7482997 |
343 | } |
344 | } |
345 | if (rs.text) { |
346 | whlp_title(h, rs.text); |
347 | fprintf(state.cntfp, ":Title %s\r\n", rs.text); |
348 | sfree(rs.text); |
349 | } |
08e78486 |
350 | { |
351 | rdstringc rs2 = {0,0,NULL}; |
352 | whlp_rdadds(&rs2, conf.titlepage, &conf, NULL); |
353 | whlp_contents_write(&state, 1, rs2.text, contents_topic); |
354 | sfree(rs2.text); |
355 | } |
d7482997 |
356 | } |
357 | |
9057a0a8 |
358 | /* |
359 | * Put the copyright into the system section. |
360 | */ |
361 | { |
362 | rdstringc rs = {0, 0, NULL}; |
363 | for (p = sourceform; p; p = p->next) { |
364 | if (p->type == para_Copyright) |
08e78486 |
365 | whlp_rdaddwc(&rs, p->words, &conf, NULL); |
9057a0a8 |
366 | } |
367 | if (rs.text) { |
368 | whlp_copyright(h, rs.text); |
369 | sfree(rs.text); |
370 | } |
371 | } |
372 | |
d7482997 |
373 | lastsect = NULL; |
374 | |
375 | /* ------------------------------------------------------------------ |
376 | * Now we've done the contents page, we're ready to go through |
377 | * and do the main manual text. Ooh. |
378 | */ |
7136a6c7 |
379 | nesting = 0; |
d7482997 |
380 | for (p = sourceform; p; p = p->next) switch (p->type) { |
381 | /* |
382 | * Things we ignore because we've already processed them or |
383 | * aren't going to touch them in this pass. |
384 | */ |
385 | case para_IM: |
386 | case para_BR: |
387 | case para_Biblio: /* only touch BiblioCited */ |
388 | case para_VersionID: |
d7482997 |
389 | case para_NoCite: |
390 | case para_Title: |
391 | break; |
392 | |
7136a6c7 |
393 | case para_LcontPush: |
2614b01d |
394 | case para_QuotePush: |
7136a6c7 |
395 | nesting++; |
396 | break; |
397 | case para_LcontPop: |
2614b01d |
398 | case para_QuotePop: |
7136a6c7 |
399 | assert(nesting > 0); |
400 | nesting--; |
401 | break; |
402 | |
d7482997 |
403 | /* |
404 | * Chapter and section titles: start a new Help topic. |
405 | */ |
406 | case para_Chapter: |
407 | case para_Appendix: |
408 | case para_UnnumberedChapter: |
409 | case para_Heading: |
410 | case para_Subsect: |
8902e0ed |
411 | |
412 | if (!done_contents_topic) { |
413 | paragraph *p; |
414 | |
415 | /* |
416 | * If this is the first section title we've seen, then |
417 | * we're currently still in the contents topic. We |
418 | * should therefore finish up the contents page by |
9057a0a8 |
419 | * writing a nav menu. |
8902e0ed |
420 | */ |
421 | for (p = sourceform; p; p = p->next) { |
422 | if (p->type == para_Chapter || |
423 | p->type == para_Appendix || |
424 | p->type == para_UnnumberedChapter) |
08e78486 |
425 | whlp_navmenu(&state, p, &conf); |
8902e0ed |
426 | } |
427 | |
8902e0ed |
428 | done_contents_topic = TRUE; |
429 | } |
430 | |
d7482997 |
431 | if (lastsect && lastsect->child) { |
432 | paragraph *q; |
433 | /* |
434 | * Do a navigation menu for the previous section we |
435 | * were in. |
436 | */ |
437 | for (q = lastsect->child; q; q = q->sibling) |
08e78486 |
438 | whlp_navmenu(&state, q, &conf); |
d7482997 |
439 | } |
440 | { |
441 | rdstringc rs = {0, 0, NULL}; |
442 | WHLP_TOPIC new_topic, parent_topic; |
443 | char *macro, *topicid; |
08e78486 |
444 | charset_state cstate = CHARSET_INIT_STATE; |
d7482997 |
445 | |
446 | new_topic = p->private_data; |
447 | whlp_browse_link(h, state.curr_topic, new_topic); |
448 | state.curr_topic = new_topic; |
449 | |
450 | if (p->kwtext) { |
08e78486 |
451 | whlp_rdaddwc(&rs, p->kwtext, &conf, &cstate); |
452 | whlp_rdadds(&rs, conf.sectsuffix, &conf, &cstate); |
d7482997 |
453 | } |
08e78486 |
454 | whlp_rdaddwc(&rs, p->words, &conf, &cstate); |
455 | whlp_rdadds(&rs, NULL, &conf, &cstate); |
456 | |
d7482997 |
457 | if (p->parent == NULL) |
458 | parent_topic = contents_topic; |
459 | else |
460 | parent_topic = (WHLP_TOPIC)p->parent->private_data; |
461 | topicid = whlp_topic_id(parent_topic); |
462 | macro = smalloc(100+strlen(topicid)); |
463 | sprintf(macro, |
464 | "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")", |
465 | topicid); |
466 | whlp_begin_topic(h, new_topic, |
467 | rs.text ? rs.text : "", |
468 | macro, NULL); |
469 | sfree(macro); |
470 | |
471 | { |
472 | /* |
473 | * Output the .cnt entry. |
474 | * |
475 | * WinHelp has a bug involving having an internal |
476 | * node followed by a leaf at the same level: the |
477 | * leaf is output at the wrong level. We can mostly |
478 | * work around this by modifying the leaf level |
479 | * itself (see whlp_contents_write), but this |
480 | * doesn't work for top-level sections since we |
481 | * can't turn a level-1 leaf into a level-0 one. So |
482 | * for top-level leaf sections (Bibliography |
483 | * springs to mind), we output an internal node |
484 | * containing only the leaf for that section. |
485 | */ |
486 | int i; |
487 | paragraph *q; |
488 | |
489 | /* Count up the level. */ |
490 | i = 1; |
491 | for (q = p; q->parent; q = q->parent) i++; |
492 | |
493 | if (p->child || !p->parent) { |
494 | /* |
495 | * If p has children then it needs to be a |
496 | * folder; if it has no parent then it needs to |
497 | * be a folder to work around the bug. |
498 | */ |
499 | whlp_contents_write(&state, i, rs.text, NULL); |
500 | i++; |
501 | } |
502 | whlp_contents_write(&state, i, rs.text, new_topic); |
503 | } |
504 | |
505 | sfree(rs.text); |
506 | |
507 | whlp_begin_para(h, WHLP_PARA_NONSCROLL); |
08e78486 |
508 | state.cstate = charset_init_state; |
d7482997 |
509 | if (p->kwtext) { |
08e78486 |
510 | whlp_mkparagraph(&state, FONT_TITLE, p->kwtext, FALSE, &conf); |
d7482997 |
511 | whlp_set_font(h, FONT_TITLE); |
08e78486 |
512 | whlp_wtext(&state, conf.sectsuffix); |
d7482997 |
513 | } |
08e78486 |
514 | whlp_mkparagraph(&state, FONT_TITLE, p->words, FALSE, &conf); |
515 | whlp_wtext(&state, NULL); |
d7482997 |
516 | whlp_end_para(h); |
517 | |
518 | lastsect = p; |
519 | } |
520 | break; |
521 | |
522 | case para_Rule: |
523 | whlp_para_attr(h, WHLP_PARA_SPACEBELOW, 12); |
524 | whlp_para_attr(h, WHLP_PARA_ALIGNMENT, WHLP_ALIGN_CENTRE); |
525 | whlp_begin_para(h, WHLP_PARA_SCROLL); |
526 | whlp_set_font(h, FONT_RULE); |
08e78486 |
527 | state.cstate = charset_init_state; |
528 | #define TEN L"\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0" |
d7482997 |
529 | #define TWENTY TEN TEN |
530 | #define FORTY TWENTY TWENTY |
531 | #define EIGHTY FORTY FORTY |
08e78486 |
532 | state.cstate = charset_init_state; |
533 | whlp_wtext(&state, EIGHTY); |
534 | whlp_wtext(&state, NULL); |
d7482997 |
535 | #undef TEN |
536 | #undef TWENTY |
537 | #undef FORTY |
538 | #undef EIGHTY |
539 | whlp_end_para(h); |
540 | break; |
541 | |
542 | case para_Normal: |
9057a0a8 |
543 | case para_Copyright: |
7136a6c7 |
544 | case para_DescribedThing: |
545 | case para_Description: |
d7482997 |
546 | case para_BiblioCited: |
547 | case para_Bullet: |
548 | case para_NumberedList: |
549 | whlp_para_attr(h, WHLP_PARA_SPACEBELOW, 12); |
550 | if (p->type == para_Bullet || p->type == para_NumberedList) { |
7136a6c7 |
551 | whlp_para_attr(h, WHLP_PARA_LEFTINDENT, 72*nesting + 72); |
d7482997 |
552 | whlp_para_attr(h, WHLP_PARA_FIRSTLINEINDENT, -36); |
553 | whlp_set_tabstop(h, 72, WHLP_ALIGN_LEFT); |
554 | whlp_begin_para(h, WHLP_PARA_SCROLL); |
555 | whlp_set_font(h, FONT_NORMAL); |
08e78486 |
556 | state.cstate = charset_init_state; |
d7482997 |
557 | if (p->type == para_Bullet) { |
08e78486 |
558 | whlp_wtext(&state, conf.bullet); |
d7482997 |
559 | } else { |
08e78486 |
560 | whlp_mkparagraph(&state, FONT_NORMAL, p->kwtext, FALSE, &conf); |
561 | whlp_wtext(&state, conf.listsuffix); |
d7482997 |
562 | } |
08e78486 |
563 | whlp_wtext(&state, NULL); |
d7482997 |
564 | whlp_tab(h); |
565 | } else { |
7136a6c7 |
566 | whlp_para_attr(h, WHLP_PARA_LEFTINDENT, |
567 | 72*nesting + (p->type==para_Description ? 72 : 0)); |
d7482997 |
568 | whlp_begin_para(h, WHLP_PARA_SCROLL); |
569 | } |
570 | |
08e78486 |
571 | state.cstate = charset_init_state; |
572 | |
d7482997 |
573 | if (p->type == para_BiblioCited) { |
08e78486 |
574 | whlp_mkparagraph(&state, FONT_NORMAL, p->kwtext, FALSE, &conf); |
575 | whlp_wtext(&state, L" "); |
d7482997 |
576 | } |
577 | |
08e78486 |
578 | whlp_mkparagraph(&state, FONT_NORMAL, p->words, FALSE, &conf); |
579 | whlp_wtext(&state, NULL); |
d7482997 |
580 | whlp_end_para(h); |
581 | break; |
582 | |
583 | case para_Code: |
584 | /* |
585 | * In a code paragraph, each individual word is a line. For |
586 | * Help files, we will have to output this as a set of |
587 | * paragraphs, all but the last of which don't set |
588 | * SPACEBELOW. |
589 | */ |
590 | { |
591 | word *w; |
08e78486 |
592 | wchar_t *t, *e, *tmp; |
4b3c5afb |
593 | |
594 | for (w = p->words; w; w = w->next) if (w->type == word_WeakCode) { |
595 | t = w->text; |
596 | if (w->next && w->next->type == word_Emph) { |
597 | w = w->next; |
598 | e = w->text; |
599 | } else |
600 | e = NULL; |
601 | |
d7482997 |
602 | if (!w->next) |
603 | whlp_para_attr(h, WHLP_PARA_SPACEBELOW, 12); |
4b3c5afb |
604 | |
7136a6c7 |
605 | whlp_para_attr(h, WHLP_PARA_LEFTINDENT, 72*nesting); |
d7482997 |
606 | whlp_begin_para(h, WHLP_PARA_SCROLL); |
08e78486 |
607 | state.cstate = charset_init_state; |
4b3c5afb |
608 | while (e && *e && *t) { |
609 | int n; |
610 | int ec = *e; |
611 | |
612 | for (n = 0; t[n] && e[n] && e[n] == ec; n++); |
613 | if (ec == 'i') |
614 | whlp_set_font(h, FONT_ITAL_CODE); |
615 | else if (ec == 'b') |
616 | whlp_set_font(h, FONT_BOLD_CODE); |
617 | else |
618 | whlp_set_font(h, FONT_CODE); |
f1530049 |
619 | tmp = snewn(n+1, wchar_t); |
08e78486 |
620 | ustrncpy(tmp, t, n); |
621 | tmp[n] = L'\0'; |
622 | whlp_wtext(&state, tmp); |
623 | whlp_wtext(&state, NULL); |
624 | state.cstate = charset_init_state; |
625 | sfree(tmp); |
4b3c5afb |
626 | t += n; |
627 | e += n; |
628 | } |
d7482997 |
629 | whlp_set_font(h, FONT_CODE); |
08e78486 |
630 | whlp_wtext(&state, t); |
631 | whlp_wtext(&state, NULL); |
d7482997 |
632 | whlp_end_para(h); |
633 | } |
634 | } |
635 | break; |
636 | } |
637 | |
638 | fclose(state.cntfp); |
08e78486 |
639 | whlp_close(h, conf.filename); |
d7482997 |
640 | |
641 | /* |
642 | * Loop over the index entries, cleaning up our final text |
643 | * forms. |
644 | */ |
645 | for (i = 0; (ie = index234(idx->entries, i)) != NULL; i++) { |
646 | sfree(ie->backend_data); |
647 | } |
50d6b4bd |
648 | |
08e78486 |
649 | sfree(conf.filename); |
50d6b4bd |
650 | sfree(cntname); |
d7482997 |
651 | } |
652 | |
653 | static void whlp_contents_write(struct bk_whlp_state *state, |
654 | int level, char *text, WHLP_TOPIC topic) { |
655 | /* |
656 | * Horrifying bug in WinHelp. When dropping a section level or |
657 | * more without using a folder-type entry, WinHelp accidentally |
658 | * adds one to the section level. So we correct for that here. |
659 | */ |
660 | if (state->cnt_last_level > level && topic) |
661 | state->cnt_workaround = -1; |
662 | else if (!topic) |
663 | state->cnt_workaround = 0; |
664 | state->cnt_last_level = level; |
665 | |
666 | fprintf(state->cntfp, "%d ", level + state->cnt_workaround); |
667 | while (*text) { |
668 | if (*text == '=') |
669 | fputc('\\', state->cntfp); |
670 | fputc(*text, state->cntfp); |
671 | text++; |
672 | } |
673 | if (topic) |
674 | fprintf(state->cntfp, "=%s", whlp_topic_id(topic)); |
675 | fputc('\n', state->cntfp); |
676 | } |
677 | |
08e78486 |
678 | static void whlp_navmenu(struct bk_whlp_state *state, paragraph *p, |
679 | whlpconf *conf) { |
23f85ecc |
680 | whlp_begin_para(state->h, WHLP_PARA_SCROLL); |
d7482997 |
681 | whlp_start_hyperlink(state->h, (WHLP_TOPIC)p->private_data); |
08e78486 |
682 | state->cstate = charset_init_state; |
d7482997 |
683 | if (p->kwtext) { |
08e78486 |
684 | whlp_mkparagraph(state, FONT_NORMAL, p->kwtext, TRUE, conf); |
d7482997 |
685 | whlp_set_font(state->h, FONT_NORMAL); |
08e78486 |
686 | whlp_wtext(state, conf->sectsuffix); |
d7482997 |
687 | } |
08e78486 |
688 | whlp_mkparagraph(state, FONT_NORMAL, p->words, TRUE, conf); |
689 | whlp_wtext(state, NULL); |
d7482997 |
690 | whlp_end_hyperlink(state->h); |
691 | whlp_end_para(state->h); |
692 | |
693 | } |
694 | |
695 | static void whlp_mkparagraph(struct bk_whlp_state *state, |
08e78486 |
696 | int font, word *text, int subsidiary, |
697 | whlpconf *conf) { |
d7482997 |
698 | keyword *kwl; |
699 | int deffont = font; |
700 | int currfont = -1; |
701 | int newfont; |
d7482997 |
702 | paragraph *xref_target = NULL; |
703 | |
704 | for (; text; text = text->next) switch (text->type) { |
705 | case word_HyperLink: |
706 | case word_HyperEnd: |
707 | break; |
708 | |
709 | case word_IndexRef: |
710 | if (subsidiary) break; /* disabled in subsidiary bits */ |
711 | { |
712 | indextag *tag = index_findtag(state->idx, text->text); |
713 | int i; |
714 | if (!tag) |
715 | break; |
716 | for (i = 0; i < tag->nrefs; i++) |
717 | whlp_index_term(state->h, tag->refs[i]->backend_data, |
718 | state->curr_topic); |
719 | } |
720 | break; |
721 | |
722 | case word_UpperXref: |
723 | case word_LowerXref: |
724 | if (subsidiary) break; /* disabled in subsidiary bits */ |
725 | kwl = kw_lookup(state->keywords, text->text); |
7cf305b7 |
726 | assert(xref_target == NULL); |
727 | if (kwl) { |
728 | if (kwl->para->type == para_NumberedList) { |
729 | break; /* don't xref to numbered list items */ |
730 | } else if (kwl->para->type == para_BiblioCited) { |
731 | /* |
732 | * An xref to a bibliography item jumps to the section |
733 | * containing it. |
734 | */ |
735 | if (kwl->para->parent) |
736 | xref_target = kwl->para->parent; |
737 | else |
738 | break; |
739 | } else { |
740 | xref_target = kwl->para; |
741 | } |
742 | whlp_start_hyperlink(state->h, |
743 | (WHLP_TOPIC)xref_target->private_data); |
744 | } |
d7482997 |
745 | break; |
746 | |
747 | case word_XrefEnd: |
748 | if (subsidiary) break; /* disabled in subsidiary bits */ |
749 | if (xref_target) |
750 | whlp_end_hyperlink(state->h); |
751 | xref_target = NULL; |
752 | break; |
753 | |
754 | case word_Normal: |
755 | case word_Emph: |
756 | case word_Code: |
757 | case word_WeakCode: |
758 | case word_WhiteSpace: |
759 | case word_EmphSpace: |
760 | case word_CodeSpace: |
761 | case word_WkCodeSpace: |
762 | case word_Quote: |
763 | case word_EmphQuote: |
764 | case word_CodeQuote: |
765 | case word_WkCodeQuote: |
766 | if (towordstyle(text->type) == word_Emph) |
767 | newfont = deffont + FONT_EMPH; |
768 | else if (towordstyle(text->type) == word_Code || |
769 | towordstyle(text->type) == word_WeakCode) |
770 | newfont = deffont + FONT_CODE; |
771 | else |
772 | newfont = deffont; |
773 | if (newfont != currfont) { |
774 | currfont = newfont; |
775 | whlp_set_font(state->h, newfont); |
776 | } |
777 | if (removeattr(text->type) == word_Normal) { |
08e78486 |
778 | if (cvt_ok(conf->charset, text->text) || !text->alt) |
779 | whlp_wtext(state, text->text); |
d7482997 |
780 | else |
08e78486 |
781 | whlp_mkparagraph(state, deffont, text->alt, FALSE, conf); |
d7482997 |
782 | } else if (removeattr(text->type) == word_WhiteSpace) { |
08e78486 |
783 | whlp_wtext(state, L" "); |
d7482997 |
784 | } else if (removeattr(text->type) == word_Quote) { |
08e78486 |
785 | whlp_wtext(state, |
786 | quoteaux(text->aux) == quote_Open ? |
787 | conf->lquote : conf->rquote); |
d7482997 |
788 | } |
789 | break; |
790 | } |
791 | } |
792 | |
08e78486 |
793 | static void whlp_rdaddwc(rdstringc *rs, word *text, whlpconf *conf, |
794 | charset_state *state) { |
795 | charset_state ourstate = CHARSET_INIT_STATE; |
796 | |
797 | if (!state) |
798 | state = &ourstate; |
d7482997 |
799 | |
800 | for (; text; text = text->next) switch (text->type) { |
801 | case word_HyperLink: |
802 | case word_HyperEnd: |
803 | case word_UpperXref: |
804 | case word_LowerXref: |
805 | case word_XrefEnd: |
806 | case word_IndexRef: |
807 | break; |
808 | |
809 | case word_Normal: |
810 | case word_Emph: |
811 | case word_Code: |
812 | case word_WeakCode: |
813 | case word_WhiteSpace: |
814 | case word_EmphSpace: |
815 | case word_CodeSpace: |
816 | case word_WkCodeSpace: |
817 | case word_Quote: |
818 | case word_EmphQuote: |
819 | case word_CodeQuote: |
820 | case word_WkCodeQuote: |
821 | assert(text->type != word_CodeQuote && |
822 | text->type != word_WkCodeQuote); |
823 | if (removeattr(text->type) == word_Normal) { |
08e78486 |
824 | if (cvt_ok(conf->charset, text->text) || !text->alt) |
825 | whlp_rdadds(rs, text->text, conf, state); |
d7482997 |
826 | else |
08e78486 |
827 | whlp_rdaddwc(rs, text->alt, conf, state); |
d7482997 |
828 | } else if (removeattr(text->type) == word_WhiteSpace) { |
08e78486 |
829 | whlp_rdadds(rs, L" ", conf, state); |
d7482997 |
830 | } else if (removeattr(text->type) == word_Quote) { |
08e78486 |
831 | whlp_rdadds(rs, quoteaux(text->aux) == quote_Open ? |
832 | conf->lquote : conf->rquote, conf, state); |
d7482997 |
833 | } |
834 | break; |
835 | } |
2223c2dd |
836 | |
08e78486 |
837 | if (state == &ourstate) |
838 | whlp_rdadds(rs, NULL, conf, state); |
839 | } |
2223c2dd |
840 | |
08e78486 |
841 | static void whlp_rdadds(rdstringc *rs, const wchar_t *text, whlpconf *conf, |
842 | charset_state *state) |
843 | { |
844 | charset_state ourstate = CHARSET_INIT_STATE; |
845 | int textlen = text ? ustrlen(text) : 0; |
846 | char outbuf[256]; |
847 | int ret; |
848 | |
849 | if (!state) |
850 | state = &ourstate; |
851 | |
852 | while (textlen > 0 && |
853 | (ret = charset_from_unicode(&text, &textlen, outbuf, |
854 | lenof(outbuf)-1, |
855 | conf->charset, state, NULL)) > 0) { |
856 | outbuf[ret] = '\0'; |
857 | rdaddsc(rs, outbuf); |
d7482997 |
858 | } |
2223c2dd |
859 | |
08e78486 |
860 | if (text == NULL || state == &ourstate) { |
861 | if ((ret = charset_from_unicode(NULL, 0, outbuf, lenof(outbuf)-1, |
862 | conf->charset, state, NULL)) > 0) { |
863 | outbuf[ret] = '\0'; |
864 | rdaddsc(rs, outbuf); |
865 | } |
866 | } |
867 | } |
2223c2dd |
868 | |
08e78486 |
869 | static void whlp_wtext(struct bk_whlp_state *state, const wchar_t *text) |
870 | { |
871 | int textlen = text ? ustrlen(text) : 0; |
872 | char outbuf[256]; |
873 | int ret; |
874 | |
875 | while (textlen > 0 && |
876 | (ret = charset_from_unicode(&text, &textlen, outbuf, |
877 | lenof(outbuf)-1, |
878 | state->charset, &state->cstate, |
879 | NULL)) > 0) { |
880 | outbuf[ret] = '\0'; |
881 | whlp_text(state->h, outbuf); |
882 | } |
2223c2dd |
883 | |
08e78486 |
884 | if (text == NULL) { |
885 | if ((ret = charset_from_unicode(NULL, 0, outbuf, lenof(outbuf)-1, |
886 | state->charset, &state->cstate, |
887 | NULL)) > 0) { |
888 | outbuf[ret] = '\0'; |
889 | whlp_text(state->h, outbuf); |
890 | } |
891 | } |
d7482997 |
892 | } |