2 * Windows Help backend for Halibut
5 * - allow user to specify section contexts.
15 struct bk_whlp_state
{
18 keywordlist
*keywords
;
19 WHLP_TOPIC curr_topic
;
21 int cnt_last_level
, cnt_workaround
;
25 * Indexes of fonts in our standard font descriptor set.
39 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
);
40 static int whlp_convert(wchar_t *s
, int maxlen
,
41 char **result
, int hard_spaces
);
42 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
43 int font
, word
*text
, int subsidiary
);
44 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
);
45 static void whlp_contents_write(struct bk_whlp_state
*state
,
46 int level
, char *text
, WHLP_TOPIC topic
);
48 void whlp_backend(paragraph
*sourceform
, keywordlist
*keywords
,
51 char *filename
, *cntname
;
52 paragraph
*p
, *lastsect
;
53 struct bk_whlp_state state
;
54 WHLP_TOPIC contents_topic
;
58 int done_contents_topic
;
60 filename
= "output.hlp"; /* FIXME: configurability */
61 cntname
= "output.cnt"; /* corresponding contents file */
63 state
.cntfp
= fopen(cntname
, "wb");
64 state
.cnt_last_level
= -1; state
.cnt_workaround
= 0;
66 h
= state
.h
= whlp_new();
67 state
.keywords
= keywords
;
70 whlp_start_macro(h
, "CB(\"btn_about\",\"&About\",\"About()\")");
71 whlp_start_macro(h
, "CB(\"btn_up\",\"&Up\",\"Contents()\")");
72 whlp_start_macro(h
, "BrowseButtons()");
74 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
76 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
77 WHLP_FONT_ITALIC
, 0, 0, 0);
78 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
80 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
81 WHLP_FONT_ITALIC
, 0, 0, 0);
82 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
83 WHLP_FONT_BOLD
, 0, 0, 0);
84 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
85 WHLP_FONT_BOLD
, 0, 0, 0);
86 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
87 WHLP_FONT_BOLD
|WHLP_FONT_ITALIC
, 0, 0, 0);
88 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 30,
89 WHLP_FONT_BOLD
, 0, 0, 0);
90 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_SANS
, 18,
91 WHLP_FONT_STRIKEOUT
, 0, 0, 0);
94 * Loop over the source form finding out whether the user has
95 * specified particular help topic names for anything.
97 for (p
= sourceform
; p
; p
= p
->next
) {
98 p
->private_data
= NULL
;
99 if (p
->type
== para_Config
&& p
->parent
) {
100 if (!ustricmp(p
->keyword
, L
"winhelp-topic")) {
102 whlp_convert(uadv(p
->keyword
), 0, &topicname
, 0);
103 /* Store the topic name in the private_data field of the
104 * containing section. */
105 p
->parent
->private_data
= topicname
;
111 * Loop over the source form registering WHLP_TOPICs for
115 contents_topic
= whlp_register_topic(h
, "Top", NULL
);
116 whlp_primary_topic(h
, contents_topic
);
117 for (p
= sourceform
; p
; p
= p
->next
) {
118 if (p
->type
== para_Chapter
||
119 p
->type
== para_Appendix
||
120 p
->type
== para_UnnumberedChapter
||
121 p
->type
== para_Heading
||
122 p
->type
== para_Subsect
) {
123 char *topicid
= p
->private_data
;
126 p
->private_data
= whlp_register_topic(h
, topicid
, &errstr
);
127 if (!p
->private_data
) {
128 p
->private_data
= whlp_register_topic(h
, NULL
, NULL
);
129 error(err_winhelp_ctxclash
, &p
->fpos
, topicid
, errstr
);
136 * Loop over the index entries, preparing final text forms for
139 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
140 rdstringc rs
= {0, 0, NULL
};
141 whlp_rdaddwc(&rs
, ie
->text
);
142 ie
->backend_data
= rs
.text
;
147 /* ------------------------------------------------------------------
148 * Begin the contents page.
151 whlp_begin_topic(h
, contents_topic
, "Contents", "DB(\"btn_up\")", NULL
);
154 * The manual title goes in the non-scroll region, and also
155 * goes into the system title slot.
158 rdstringc rs
= {0, 0, NULL
};
159 for (p
= sourceform
; p
; p
= p
->next
) {
160 if (p
->type
== para_Title
) {
161 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
162 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
163 whlp_rdaddwc(&rs
, p
->words
);
168 whlp_title(h
, rs
.text
);
169 fprintf(state
.cntfp
, ":Title %s\r\n", rs
.text
);
172 whlp_contents_write(&state
, 1, "Title page", contents_topic
);
173 /* FIXME: configurability in that string */
178 /* ------------------------------------------------------------------
179 * Now we've done the contents page, we're ready to go through
180 * and do the main manual text. Ooh.
183 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
185 * Things we ignore because we've already processed them or
186 * aren't going to touch them in this pass.
190 case para_Biblio
: /* only touch BiblioCited */
208 * Chapter and section titles: start a new Help topic.
212 case para_UnnumberedChapter
:
216 if (!done_contents_topic
) {
220 * If this is the first section title we've seen, then
221 * we're currently still in the contents topic. We
222 * should therefore finish up the contents page by
223 * writing the copyright notice and a nav menu.
227 * The copyright goes to two places, again: into the
228 * contents page and also into the system section.
231 rdstringc rs
= {0, 0, NULL
};
232 for (p
= sourceform
; p
; p
= p
->next
) {
233 if (p
->type
== para_Copyright
) {
234 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
235 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
236 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
238 whlp_rdaddwc(&rs
, p
->words
);
242 whlp_copyright(h
, rs
.text
);
248 * Now do the primary navigation menu.
250 for (p
= sourceform
; p
; p
= p
->next
) {
251 if (p
->type
== para_Chapter
||
252 p
->type
== para_Appendix
||
253 p
->type
== para_UnnumberedChapter
)
254 whlp_navmenu(&state
, p
);
257 state
.curr_topic
= contents_topic
;
259 done_contents_topic
= TRUE
;
262 if (lastsect
&& lastsect
->child
) {
265 * Do a navigation menu for the previous section we
268 for (q
= lastsect
->child
; q
; q
= q
->sibling
)
269 whlp_navmenu(&state
, q
);
272 rdstringc rs
= {0, 0, NULL
};
273 WHLP_TOPIC new_topic
, parent_topic
;
274 char *macro
, *topicid
;
276 new_topic
= p
->private_data
;
277 whlp_browse_link(h
, state
.curr_topic
, new_topic
);
278 state
.curr_topic
= new_topic
;
281 whlp_rdaddwc(&rs
, p
->kwtext
);
282 rdaddsc(&rs
, ": "); /* FIXME: configurability */
284 whlp_rdaddwc(&rs
, p
->words
);
285 if (p
->parent
== NULL
)
286 parent_topic
= contents_topic
;
288 parent_topic
= (WHLP_TOPIC
)p
->parent
->private_data
;
289 topicid
= whlp_topic_id(parent_topic
);
290 macro
= smalloc(100+strlen(topicid
));
292 "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
294 whlp_begin_topic(h
, new_topic
,
295 rs
.text ? rs
.text
: "",
301 * Output the .cnt entry.
303 * WinHelp has a bug involving having an internal
304 * node followed by a leaf at the same level: the
305 * leaf is output at the wrong level. We can mostly
306 * work around this by modifying the leaf level
307 * itself (see whlp_contents_write), but this
308 * doesn't work for top-level sections since we
309 * can't turn a level-1 leaf into a level-0 one. So
310 * for top-level leaf sections (Bibliography
311 * springs to mind), we output an internal node
312 * containing only the leaf for that section.
317 /* Count up the level. */
319 for (q
= p
; q
->parent
; q
= q
->parent
) i
++;
321 if (p
->child
|| !p
->parent
) {
323 * If p has children then it needs to be a
324 * folder; if it has no parent then it needs to
325 * be a folder to work around the bug.
327 whlp_contents_write(&state
, i
, rs
.text
, NULL
);
330 whlp_contents_write(&state
, i
, rs
.text
, new_topic
);
335 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
337 whlp_mkparagraph(&state
, FONT_TITLE
, p
->kwtext
, FALSE
);
338 whlp_set_font(h
, FONT_TITLE
);
339 whlp_text(h
, ": "); /* FIXME: configurability */
341 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
349 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
350 whlp_para_attr(h
, WHLP_PARA_ALIGNMENT
, WHLP_ALIGN_CENTRE
);
351 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
352 whlp_set_font(h
, FONT_RULE
);
353 #define TEN "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"
354 #define TWENTY TEN TEN
355 #define FORTY TWENTY TWENTY
356 #define EIGHTY FORTY FORTY
357 whlp_text(h
, EIGHTY
);
366 case para_DescribedThing
:
367 case para_Description
:
368 case para_BiblioCited
:
370 case para_NumberedList
:
371 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
372 if (p
->type
== para_Bullet
|| p
->type
== para_NumberedList
) {
373 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
+ 72);
374 whlp_para_attr(h
, WHLP_PARA_FIRSTLINEINDENT
, -36);
375 whlp_set_tabstop(h
, 72, WHLP_ALIGN_LEFT
);
376 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
377 whlp_set_font(h
, FONT_NORMAL
);
378 if (p
->type
== para_Bullet
) {
379 whlp_text(h
, "\x95");
381 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
386 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
,
387 72*nesting
+ (p
->type
==para_Description ?
72 : 0));
388 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
391 if (p
->type
== para_BiblioCited
) {
392 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
396 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
402 * In a code paragraph, each individual word is a line. For
403 * Help files, we will have to output this as a set of
404 * paragraphs, all but the last of which don't set
412 for (w
= p
->words
; w
; w
= w
->next
) if (w
->type
== word_WeakCode
) {
414 if (w
->next
&& w
->next
->type
== word_Emph
) {
421 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
423 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
);
424 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
425 while (e
&& *e
&& *t
) {
429 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
431 whlp_set_font(h
, FONT_ITAL_CODE
);
433 whlp_set_font(h
, FONT_BOLD_CODE
);
435 whlp_set_font(h
, FONT_CODE
);
436 whlp_convert(t
, n
, &c
, FALSE
);
442 whlp_set_font(h
, FONT_CODE
);
443 whlp_convert(t
, 0, &c
, FALSE
);
453 whlp_close(h
, filename
);
456 * Loop over the index entries, cleaning up our final text
459 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
460 sfree(ie
->backend_data
);
464 static void whlp_contents_write(struct bk_whlp_state
*state
,
465 int level
, char *text
, WHLP_TOPIC topic
) {
467 * Horrifying bug in WinHelp. When dropping a section level or
468 * more without using a folder-type entry, WinHelp accidentally
469 * adds one to the section level. So we correct for that here.
471 if (state
->cnt_last_level
> level
&& topic
)
472 state
->cnt_workaround
= -1;
474 state
->cnt_workaround
= 0;
475 state
->cnt_last_level
= level
;
477 fprintf(state
->cntfp
, "%d ", level
+ state
->cnt_workaround
);
480 fputc('\\', state
->cntfp
);
481 fputc(*text
, state
->cntfp
);
485 fprintf(state
->cntfp
, "=%s", whlp_topic_id(topic
));
486 fputc('\n', state
->cntfp
);
489 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
) {
490 whlp_begin_para(state
->h
, WHLP_PARA_NONSCROLL
);
491 whlp_start_hyperlink(state
->h
, (WHLP_TOPIC
)p
->private_data
);
493 whlp_mkparagraph(state
, FONT_NORMAL
, p
->kwtext
, TRUE
);
494 whlp_set_font(state
->h
, FONT_NORMAL
);
495 whlp_text(state
->h
, ": "); /* FIXME: configurability */
497 whlp_mkparagraph(state
, FONT_NORMAL
, p
->words
, TRUE
);
498 whlp_end_hyperlink(state
->h
);
499 whlp_end_para(state
->h
);
503 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
504 int font
, word
*text
, int subsidiary
) {
510 paragraph
*xref_target
= NULL
;
512 for (; text
; text
= text
->next
) switch (text
->type
) {
518 if (subsidiary
) break; /* disabled in subsidiary bits */
520 indextag
*tag
= index_findtag(state
->idx
, text
->text
);
524 for (i
= 0; i
< tag
->nrefs
; i
++)
525 whlp_index_term(state
->h
, tag
->refs
[i
]->backend_data
,
532 if (subsidiary
) break; /* disabled in subsidiary bits */
533 kwl
= kw_lookup(state
->keywords
, text
->text
);
534 assert(xref_target
== NULL
);
536 if (kwl
->para
->type
== para_NumberedList
) {
537 break; /* don't xref to numbered list items */
538 } else if (kwl
->para
->type
== para_BiblioCited
) {
540 * An xref to a bibliography item jumps to the section
543 if (kwl
->para
->parent
)
544 xref_target
= kwl
->para
->parent
;
548 xref_target
= kwl
->para
;
550 whlp_start_hyperlink(state
->h
,
551 (WHLP_TOPIC
)xref_target
->private_data
);
556 if (subsidiary
) break; /* disabled in subsidiary bits */
558 whlp_end_hyperlink(state
->h
);
566 case word_WhiteSpace
:
569 case word_WkCodeSpace
:
573 case word_WkCodeQuote
:
574 if (towordstyle(text
->type
) == word_Emph
)
575 newfont
= deffont
+ FONT_EMPH
;
576 else if (towordstyle(text
->type
) == word_Code
||
577 towordstyle(text
->type
) == word_WeakCode
)
578 newfont
= deffont
+ FONT_CODE
;
581 if (newfont
!= currfont
) {
583 whlp_set_font(state
->h
, newfont
);
585 if (removeattr(text
->type
) == word_Normal
) {
586 if (whlp_convert(text
->text
, 0, &c
, TRUE
))
587 whlp_text(state
->h
, c
);
589 whlp_mkparagraph(state
, deffont
, text
->alt
, FALSE
);
591 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
592 whlp_text(state
->h
, " ");
593 } else if (removeattr(text
->type
) == word_Quote
) {
595 quoteaux(text
->aux
) == quote_Open ?
"\x91" : "\x92");
596 /* FIXME: configurability */
602 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
) {
605 for (; text
; text
= text
->next
) switch (text
->type
) {
618 case word_WhiteSpace
:
621 case word_WkCodeSpace
:
625 case word_WkCodeQuote
:
626 assert(text
->type
!= word_CodeQuote
&&
627 text
->type
!= word_WkCodeQuote
);
628 if (removeattr(text
->type
) == word_Normal
) {
629 if (whlp_convert(text
->text
, 0, &c
, FALSE
))
632 whlp_rdaddwc(rs
, text
->alt
);
634 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
636 } else if (removeattr(text
->type
) == word_Quote
) {
637 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'\x91' : '\x92');
638 /* FIXME: configurability */
645 * Convert a wide string into a string of chars. If `result' is
646 * non-NULL, mallocs the resulting string and stores a pointer to
647 * it in `*result'. If `result' is NULL, merely checks whether all
648 * characters in the string are feasible for the output character
651 * Return is nonzero if all characters are OK. If not all
652 * characters are OK but `result' is non-NULL, a result _will_
653 * still be generated!
655 static int whlp_convert(wchar_t *s
, int maxlen
,
656 char **result
, int hard_spaces
) {
658 * FIXME. Currently this is ISO8859-1 only.
660 int doing
= (result
!= 0);
663 int plen
= 0, psize
= 0;
668 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
672 if ((c
>= 32 && c
<= 126) ||
673 (c
>= 160 && c
<= 255)) {
675 if (c
== 32 && hard_spaces
)
680 /* Char is not OK. */
682 outc
= 0xBF; /* approximate the good old DEC `uh?' */
687 p
= resize(p
, psize
);
693 p
= resize(p
, plen
+1);