2 * Windows Help backend for Halibut
13 struct bk_whlp_state
{
16 keywordlist
*keywords
;
17 WHLP_TOPIC curr_topic
;
19 int cnt_last_level
, cnt_workaround
;
23 * Indexes of fonts in our standard font descriptor set.
37 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
);
38 static int whlp_convert(wchar_t *s
, int maxlen
,
39 char **result
, int hard_spaces
);
40 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
41 int font
, word
*text
, int subsidiary
);
42 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
);
43 static void whlp_contents_write(struct bk_whlp_state
*state
,
44 int level
, char *text
, WHLP_TOPIC topic
);
46 paragraph
*whlp_config_filename(char *filename
)
49 wchar_t *ufilename
, *up
;
53 memset(p
, 0, sizeof(*p
));
54 p
->type
= para_Config
;
56 p
->fpos
.filename
= "<command line>";
57 p
->fpos
.line
= p
->fpos
.col
= -1;
59 ufilename
= ufroma_dup(filename
);
60 len
= ustrlen(ufilename
) + 2 + lenof(L
"winhelp-filename");
61 p
->keyword
= mknewa(wchar_t, len
);
63 ustrcpy(up
, L
"winhelp-filename");
65 ustrcpy(up
, ufilename
);
68 assert(up
- p
->keyword
< len
);
74 void whlp_backend(paragraph
*sourceform
, keywordlist
*keywords
,
75 indexdata
*idx
, void *unused
) {
77 char *filename
, *cntname
;
78 paragraph
*p
, *lastsect
;
79 struct bk_whlp_state state
;
80 WHLP_TOPIC contents_topic
;
84 int done_contents_topic
= FALSE
;
88 h
= state
.h
= whlp_new();
89 state
.keywords
= keywords
;
92 whlp_start_macro(h
, "CB(\"btn_about\",\"&About\",\"About()\")");
93 whlp_start_macro(h
, "CB(\"btn_up\",\"&Up\",\"Contents()\")");
94 whlp_start_macro(h
, "BrowseButtons()");
96 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
98 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
99 WHLP_FONT_ITALIC
, 0, 0, 0);
100 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
102 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
103 WHLP_FONT_ITALIC
, 0, 0, 0);
104 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
105 WHLP_FONT_BOLD
, 0, 0, 0);
106 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
107 WHLP_FONT_BOLD
, 0, 0, 0);
108 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
109 WHLP_FONT_BOLD
|WHLP_FONT_ITALIC
, 0, 0, 0);
110 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 30,
111 WHLP_FONT_BOLD
, 0, 0, 0);
112 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_SANS
, 18,
113 WHLP_FONT_STRIKEOUT
, 0, 0, 0);
116 * Loop over the source form finding out whether the user has
117 * specified particular help topic names for anything. Also
118 * pick out the output file name at this stage.
120 filename
= dupstr("output.hlp");
121 for (p
= sourceform
; p
; p
= p
->next
) {
122 p
->private_data
= NULL
;
123 if (p
->type
== para_Config
&& p
->parent
) {
124 if (!ustricmp(p
->keyword
, L
"winhelp-topic")) {
126 whlp_convert(uadv(p
->keyword
), 0, &topicname
, 0);
127 /* Store the topic name in the private_data field of the
128 * containing section. */
129 p
->parent
->private_data
= topicname
;
130 } else if (!ustricmp(p
->keyword
, L
"winhelp-filename")) {
132 filename
= utoa_dup(uadv(p
->keyword
));
138 * Ensure the output file name has a .hlp extension. This is
139 * required since we must create the .cnt file in parallel with
143 int len
= strlen(filename
);
144 if (len
< 4 || filename
[len
-4] != '.' ||
145 tolower(filename
[len
-3] != 'h') ||
146 tolower(filename
[len
-2] != 'l') ||
147 tolower(filename
[len
-1] != 'p')) {
149 newf
= mknewa(char, len
+ 5);
150 sprintf(newf
, "%s.hlp", filename
);
155 cntname
= mknewa(char, len
);
156 sprintf(cntname
, "%.*s.cnt", len
-4, filename
);
159 state
.cntfp
= fopen(cntname
, "wb");
160 state
.cnt_last_level
= -1; state
.cnt_workaround
= 0;
163 * Loop over the source form registering WHLP_TOPICs for
167 contents_topic
= whlp_register_topic(h
, "Top", NULL
);
168 whlp_primary_topic(h
, contents_topic
);
169 for (p
= sourceform
; p
; p
= p
->next
) {
170 if (p
->type
== para_Chapter
||
171 p
->type
== para_Appendix
||
172 p
->type
== para_UnnumberedChapter
||
173 p
->type
== para_Heading
||
174 p
->type
== para_Subsect
) {
175 char *topicid
= p
->private_data
;
178 p
->private_data
= whlp_register_topic(h
, topicid
, &errstr
);
179 if (!p
->private_data
) {
180 p
->private_data
= whlp_register_topic(h
, NULL
, NULL
);
181 error(err_winhelp_ctxclash
, &p
->fpos
, topicid
, errstr
);
188 * Loop over the index entries, preparing final text forms for
191 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
192 rdstringc rs
= {0, 0, NULL
};
193 whlp_rdaddwc(&rs
, ie
->text
);
194 ie
->backend_data
= rs
.text
;
199 /* ------------------------------------------------------------------
200 * Begin the contents page.
203 whlp_begin_topic(h
, contents_topic
, "Contents", "DB(\"btn_up\")", NULL
);
206 * The manual title goes in the non-scroll region, and also
207 * goes into the system title slot.
210 rdstringc rs
= {0, 0, NULL
};
211 for (p
= sourceform
; p
; p
= p
->next
) {
212 if (p
->type
== para_Title
) {
213 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
214 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
215 whlp_rdaddwc(&rs
, p
->words
);
220 whlp_title(h
, rs
.text
);
221 fprintf(state
.cntfp
, ":Title %s\r\n", rs
.text
);
224 whlp_contents_write(&state
, 1, "Title page", contents_topic
);
225 /* FIXME: configurability in that string */
229 * Put the copyright into the system section.
232 rdstringc rs
= {0, 0, NULL
};
233 for (p
= sourceform
; p
; p
= p
->next
) {
234 if (p
->type
== para_Copyright
)
235 whlp_rdaddwc(&rs
, p
->words
);
238 whlp_copyright(h
, rs
.text
);
245 /* ------------------------------------------------------------------
246 * Now we've done the contents page, we're ready to go through
247 * and do the main manual text. Ooh.
250 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
252 * Things we ignore because we've already processed them or
253 * aren't going to touch them in this pass.
257 case para_Biblio
: /* only touch BiblioCited */
274 * Chapter and section titles: start a new Help topic.
278 case para_UnnumberedChapter
:
282 if (!done_contents_topic
) {
286 * If this is the first section title we've seen, then
287 * we're currently still in the contents topic. We
288 * should therefore finish up the contents page by
289 * writing a nav menu.
291 for (p
= sourceform
; p
; p
= p
->next
) {
292 if (p
->type
== para_Chapter
||
293 p
->type
== para_Appendix
||
294 p
->type
== para_UnnumberedChapter
)
295 whlp_navmenu(&state
, p
);
298 state
.curr_topic
= contents_topic
;
300 done_contents_topic
= TRUE
;
303 if (lastsect
&& lastsect
->child
) {
306 * Do a navigation menu for the previous section we
309 for (q
= lastsect
->child
; q
; q
= q
->sibling
)
310 whlp_navmenu(&state
, q
);
313 rdstringc rs
= {0, 0, NULL
};
314 WHLP_TOPIC new_topic
, parent_topic
;
315 char *macro
, *topicid
;
317 new_topic
= p
->private_data
;
318 whlp_browse_link(h
, state
.curr_topic
, new_topic
);
319 state
.curr_topic
= new_topic
;
322 whlp_rdaddwc(&rs
, p
->kwtext
);
323 rdaddsc(&rs
, ": "); /* FIXME: configurability */
325 whlp_rdaddwc(&rs
, p
->words
);
326 if (p
->parent
== NULL
)
327 parent_topic
= contents_topic
;
329 parent_topic
= (WHLP_TOPIC
)p
->parent
->private_data
;
330 topicid
= whlp_topic_id(parent_topic
);
331 macro
= smalloc(100+strlen(topicid
));
333 "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
335 whlp_begin_topic(h
, new_topic
,
336 rs
.text ? rs
.text
: "",
342 * Output the .cnt entry.
344 * WinHelp has a bug involving having an internal
345 * node followed by a leaf at the same level: the
346 * leaf is output at the wrong level. We can mostly
347 * work around this by modifying the leaf level
348 * itself (see whlp_contents_write), but this
349 * doesn't work for top-level sections since we
350 * can't turn a level-1 leaf into a level-0 one. So
351 * for top-level leaf sections (Bibliography
352 * springs to mind), we output an internal node
353 * containing only the leaf for that section.
358 /* Count up the level. */
360 for (q
= p
; q
->parent
; q
= q
->parent
) i
++;
362 if (p
->child
|| !p
->parent
) {
364 * If p has children then it needs to be a
365 * folder; if it has no parent then it needs to
366 * be a folder to work around the bug.
368 whlp_contents_write(&state
, i
, rs
.text
, NULL
);
371 whlp_contents_write(&state
, i
, rs
.text
, new_topic
);
376 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
378 whlp_mkparagraph(&state
, FONT_TITLE
, p
->kwtext
, FALSE
);
379 whlp_set_font(h
, FONT_TITLE
);
380 whlp_text(h
, ": "); /* FIXME: configurability */
382 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
390 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
391 whlp_para_attr(h
, WHLP_PARA_ALIGNMENT
, WHLP_ALIGN_CENTRE
);
392 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
393 whlp_set_font(h
, FONT_RULE
);
394 #define TEN "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"
395 #define TWENTY TEN TEN
396 #define FORTY TWENTY TWENTY
397 #define EIGHTY FORTY FORTY
398 whlp_text(h
, EIGHTY
);
408 case para_DescribedThing
:
409 case para_Description
:
410 case para_BiblioCited
:
412 case para_NumberedList
:
413 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
414 if (p
->type
== para_Bullet
|| p
->type
== para_NumberedList
) {
415 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
+ 72);
416 whlp_para_attr(h
, WHLP_PARA_FIRSTLINEINDENT
, -36);
417 whlp_set_tabstop(h
, 72, WHLP_ALIGN_LEFT
);
418 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
419 whlp_set_font(h
, FONT_NORMAL
);
420 if (p
->type
== para_Bullet
) {
421 whlp_text(h
, "\x95");
423 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
428 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
,
429 72*nesting
+ (p
->type
==para_Description ?
72 : 0));
430 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
433 if (p
->type
== para_BiblioCited
) {
434 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
438 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
444 * In a code paragraph, each individual word is a line. For
445 * Help files, we will have to output this as a set of
446 * paragraphs, all but the last of which don't set
454 for (w
= p
->words
; w
; w
= w
->next
) if (w
->type
== word_WeakCode
) {
456 if (w
->next
&& w
->next
->type
== word_Emph
) {
463 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
465 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
);
466 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
467 while (e
&& *e
&& *t
) {
471 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
473 whlp_set_font(h
, FONT_ITAL_CODE
);
475 whlp_set_font(h
, FONT_BOLD_CODE
);
477 whlp_set_font(h
, FONT_CODE
);
478 whlp_convert(t
, n
, &c
, FALSE
);
484 whlp_set_font(h
, FONT_CODE
);
485 whlp_convert(t
, 0, &c
, FALSE
);
495 whlp_close(h
, filename
);
498 * Loop over the index entries, cleaning up our final text
501 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
502 sfree(ie
->backend_data
);
509 static void whlp_contents_write(struct bk_whlp_state
*state
,
510 int level
, char *text
, WHLP_TOPIC topic
) {
512 * Horrifying bug in WinHelp. When dropping a section level or
513 * more without using a folder-type entry, WinHelp accidentally
514 * adds one to the section level. So we correct for that here.
516 if (state
->cnt_last_level
> level
&& topic
)
517 state
->cnt_workaround
= -1;
519 state
->cnt_workaround
= 0;
520 state
->cnt_last_level
= level
;
522 fprintf(state
->cntfp
, "%d ", level
+ state
->cnt_workaround
);
525 fputc('\\', state
->cntfp
);
526 fputc(*text
, state
->cntfp
);
530 fprintf(state
->cntfp
, "=%s", whlp_topic_id(topic
));
531 fputc('\n', state
->cntfp
);
534 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
) {
535 whlp_begin_para(state
->h
, WHLP_PARA_NONSCROLL
);
536 whlp_start_hyperlink(state
->h
, (WHLP_TOPIC
)p
->private_data
);
538 whlp_mkparagraph(state
, FONT_NORMAL
, p
->kwtext
, TRUE
);
539 whlp_set_font(state
->h
, FONT_NORMAL
);
540 whlp_text(state
->h
, ": "); /* FIXME: configurability */
542 whlp_mkparagraph(state
, FONT_NORMAL
, p
->words
, TRUE
);
543 whlp_end_hyperlink(state
->h
);
544 whlp_end_para(state
->h
);
548 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
549 int font
, word
*text
, int subsidiary
) {
555 paragraph
*xref_target
= NULL
;
557 for (; text
; text
= text
->next
) switch (text
->type
) {
563 if (subsidiary
) break; /* disabled in subsidiary bits */
565 indextag
*tag
= index_findtag(state
->idx
, text
->text
);
569 for (i
= 0; i
< tag
->nrefs
; i
++)
570 whlp_index_term(state
->h
, tag
->refs
[i
]->backend_data
,
577 if (subsidiary
) break; /* disabled in subsidiary bits */
578 kwl
= kw_lookup(state
->keywords
, text
->text
);
579 assert(xref_target
== NULL
);
581 if (kwl
->para
->type
== para_NumberedList
) {
582 break; /* don't xref to numbered list items */
583 } else if (kwl
->para
->type
== para_BiblioCited
) {
585 * An xref to a bibliography item jumps to the section
588 if (kwl
->para
->parent
)
589 xref_target
= kwl
->para
->parent
;
593 xref_target
= kwl
->para
;
595 whlp_start_hyperlink(state
->h
,
596 (WHLP_TOPIC
)xref_target
->private_data
);
601 if (subsidiary
) break; /* disabled in subsidiary bits */
603 whlp_end_hyperlink(state
->h
);
611 case word_WhiteSpace
:
614 case word_WkCodeSpace
:
618 case word_WkCodeQuote
:
619 if (towordstyle(text
->type
) == word_Emph
)
620 newfont
= deffont
+ FONT_EMPH
;
621 else if (towordstyle(text
->type
) == word_Code
||
622 towordstyle(text
->type
) == word_WeakCode
)
623 newfont
= deffont
+ FONT_CODE
;
626 if (newfont
!= currfont
) {
628 whlp_set_font(state
->h
, newfont
);
630 if (removeattr(text
->type
) == word_Normal
) {
631 if (whlp_convert(text
->text
, 0, &c
, TRUE
))
632 whlp_text(state
->h
, c
);
634 whlp_mkparagraph(state
, deffont
, text
->alt
, FALSE
);
636 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
637 whlp_text(state
->h
, " ");
638 } else if (removeattr(text
->type
) == word_Quote
) {
640 quoteaux(text
->aux
) == quote_Open ?
"\x91" : "\x92");
641 /* FIXME: configurability */
647 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
) {
650 for (; text
; text
= text
->next
) switch (text
->type
) {
663 case word_WhiteSpace
:
666 case word_WkCodeSpace
:
670 case word_WkCodeQuote
:
671 assert(text
->type
!= word_CodeQuote
&&
672 text
->type
!= word_WkCodeQuote
);
673 if (removeattr(text
->type
) == word_Normal
) {
674 if (whlp_convert(text
->text
, 0, &c
, FALSE
))
677 whlp_rdaddwc(rs
, text
->alt
);
679 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
681 } else if (removeattr(text
->type
) == word_Quote
) {
682 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'\x91' : '\x92');
683 /* FIXME: configurability */
690 * Convert a wide string into a string of chars. If `result' is
691 * non-NULL, mallocs the resulting string and stores a pointer to
692 * it in `*result'. If `result' is NULL, merely checks whether all
693 * characters in the string are feasible for the output character
696 * Return is nonzero if all characters are OK. If not all
697 * characters are OK but `result' is non-NULL, a result _will_
698 * still be generated!
700 static int whlp_convert(wchar_t *s
, int maxlen
,
701 char **result
, int hard_spaces
) {
703 * FIXME. Currently this is ISO8859-1 only.
705 int doing
= (result
!= 0);
708 int plen
= 0, psize
= 0;
713 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
717 if ((c
>= 32 && c
<= 126) ||
718 (c
>= 160 && c
<= 255)) {
720 if (c
== 32 && hard_spaces
)
725 /* Char is not OK. */
727 outc
= 0xBF; /* approximate the good old DEC `uh?' */
732 p
= resize(p
, psize
);
738 p
= resize(p
, plen
+1);