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 void whlp_backend(paragraph
*sourceform
, keywordlist
*keywords
,
49 char *filename
, *cntname
;
50 paragraph
*p
, *lastsect
;
51 struct bk_whlp_state state
;
52 WHLP_TOPIC contents_topic
;
56 int done_contents_topic
= FALSE
;
58 h
= state
.h
= whlp_new();
59 state
.keywords
= keywords
;
62 whlp_start_macro(h
, "CB(\"btn_about\",\"&About\",\"About()\")");
63 whlp_start_macro(h
, "CB(\"btn_up\",\"&Up\",\"Contents()\")");
64 whlp_start_macro(h
, "BrowseButtons()");
66 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
68 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
69 WHLP_FONT_ITALIC
, 0, 0, 0);
70 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
72 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
73 WHLP_FONT_ITALIC
, 0, 0, 0);
74 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
75 WHLP_FONT_BOLD
, 0, 0, 0);
76 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
77 WHLP_FONT_BOLD
, 0, 0, 0);
78 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
79 WHLP_FONT_BOLD
|WHLP_FONT_ITALIC
, 0, 0, 0);
80 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 30,
81 WHLP_FONT_BOLD
, 0, 0, 0);
82 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_SANS
, 18,
83 WHLP_FONT_STRIKEOUT
, 0, 0, 0);
86 * Loop over the source form finding out whether the user has
87 * specified particular help topic names for anything. Also
88 * pick out the output file name at this stage.
90 filename
= dupstr("output.hlp");
91 for (p
= sourceform
; p
; p
= p
->next
) {
92 p
->private_data
= NULL
;
93 if (p
->type
== para_Config
&& p
->parent
) {
94 if (!ustricmp(p
->keyword
, L
"winhelp-topic")) {
96 whlp_convert(uadv(p
->keyword
), 0, &topicname
, 0);
97 /* Store the topic name in the private_data field of the
98 * containing section. */
99 p
->parent
->private_data
= topicname
;
100 } else if (!ustricmp(p
->keyword
, L
"winhelp-filename")) {
102 filename
= utoa_dup(uadv(p
->keyword
));
108 * Ensure the output file name has a .hlp extension. This is
109 * required since we must create the .cnt file in parallel with
113 int len
= strlen(filename
);
114 if (len
< 4 || filename
[len
-4] != '.' ||
115 tolower(filename
[len
-3] != 'h') ||
116 tolower(filename
[len
-2] != 'l') ||
117 tolower(filename
[len
-1] != 'p')) {
119 newf
= mknewa(char, len
+ 5);
120 sprintf(newf
, "%s.hlp", filename
);
125 cntname
= mknewa(char, len
);
126 sprintf(cntname
, "%.*s.cnt", len
-4, filename
);
129 state
.cntfp
= fopen(cntname
, "wb");
130 state
.cnt_last_level
= -1; state
.cnt_workaround
= 0;
133 * Loop over the source form registering WHLP_TOPICs for
137 contents_topic
= whlp_register_topic(h
, "Top", NULL
);
138 whlp_primary_topic(h
, contents_topic
);
139 for (p
= sourceform
; p
; p
= p
->next
) {
140 if (p
->type
== para_Chapter
||
141 p
->type
== para_Appendix
||
142 p
->type
== para_UnnumberedChapter
||
143 p
->type
== para_Heading
||
144 p
->type
== para_Subsect
) {
145 char *topicid
= p
->private_data
;
148 p
->private_data
= whlp_register_topic(h
, topicid
, &errstr
);
149 if (!p
->private_data
) {
150 p
->private_data
= whlp_register_topic(h
, NULL
, NULL
);
151 error(err_winhelp_ctxclash
, &p
->fpos
, topicid
, errstr
);
158 * Loop over the index entries, preparing final text forms for
161 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
162 rdstringc rs
= {0, 0, NULL
};
163 whlp_rdaddwc(&rs
, ie
->text
);
164 ie
->backend_data
= rs
.text
;
169 /* ------------------------------------------------------------------
170 * Begin the contents page.
173 whlp_begin_topic(h
, contents_topic
, "Contents", "DB(\"btn_up\")", NULL
);
176 * The manual title goes in the non-scroll region, and also
177 * goes into the system title slot.
180 rdstringc rs
= {0, 0, NULL
};
181 for (p
= sourceform
; p
; p
= p
->next
) {
182 if (p
->type
== para_Title
) {
183 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
184 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
185 whlp_rdaddwc(&rs
, p
->words
);
190 whlp_title(h
, rs
.text
);
191 fprintf(state
.cntfp
, ":Title %s\r\n", rs
.text
);
194 whlp_contents_write(&state
, 1, "Title page", contents_topic
);
195 /* FIXME: configurability in that string */
199 * Put the copyright into the system section.
202 rdstringc rs
= {0, 0, NULL
};
203 for (p
= sourceform
; p
; p
= p
->next
) {
204 if (p
->type
== para_Copyright
)
205 whlp_rdaddwc(&rs
, p
->words
);
208 whlp_copyright(h
, rs
.text
);
215 /* ------------------------------------------------------------------
216 * Now we've done the contents page, we're ready to go through
217 * and do the main manual text. Ooh.
220 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
222 * Things we ignore because we've already processed them or
223 * aren't going to touch them in this pass.
227 case para_Biblio
: /* only touch BiblioCited */
244 * Chapter and section titles: start a new Help topic.
248 case para_UnnumberedChapter
:
252 if (!done_contents_topic
) {
256 * If this is the first section title we've seen, then
257 * we're currently still in the contents topic. We
258 * should therefore finish up the contents page by
259 * writing a nav menu.
261 for (p
= sourceform
; p
; p
= p
->next
) {
262 if (p
->type
== para_Chapter
||
263 p
->type
== para_Appendix
||
264 p
->type
== para_UnnumberedChapter
)
265 whlp_navmenu(&state
, p
);
268 state
.curr_topic
= contents_topic
;
270 done_contents_topic
= TRUE
;
273 if (lastsect
&& lastsect
->child
) {
276 * Do a navigation menu for the previous section we
279 for (q
= lastsect
->child
; q
; q
= q
->sibling
)
280 whlp_navmenu(&state
, q
);
283 rdstringc rs
= {0, 0, NULL
};
284 WHLP_TOPIC new_topic
, parent_topic
;
285 char *macro
, *topicid
;
287 new_topic
= p
->private_data
;
288 whlp_browse_link(h
, state
.curr_topic
, new_topic
);
289 state
.curr_topic
= new_topic
;
292 whlp_rdaddwc(&rs
, p
->kwtext
);
293 rdaddsc(&rs
, ": "); /* FIXME: configurability */
295 whlp_rdaddwc(&rs
, p
->words
);
296 if (p
->parent
== NULL
)
297 parent_topic
= contents_topic
;
299 parent_topic
= (WHLP_TOPIC
)p
->parent
->private_data
;
300 topicid
= whlp_topic_id(parent_topic
);
301 macro
= smalloc(100+strlen(topicid
));
303 "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
305 whlp_begin_topic(h
, new_topic
,
306 rs
.text ? rs
.text
: "",
312 * Output the .cnt entry.
314 * WinHelp has a bug involving having an internal
315 * node followed by a leaf at the same level: the
316 * leaf is output at the wrong level. We can mostly
317 * work around this by modifying the leaf level
318 * itself (see whlp_contents_write), but this
319 * doesn't work for top-level sections since we
320 * can't turn a level-1 leaf into a level-0 one. So
321 * for top-level leaf sections (Bibliography
322 * springs to mind), we output an internal node
323 * containing only the leaf for that section.
328 /* Count up the level. */
330 for (q
= p
; q
->parent
; q
= q
->parent
) i
++;
332 if (p
->child
|| !p
->parent
) {
334 * If p has children then it needs to be a
335 * folder; if it has no parent then it needs to
336 * be a folder to work around the bug.
338 whlp_contents_write(&state
, i
, rs
.text
, NULL
);
341 whlp_contents_write(&state
, i
, rs
.text
, new_topic
);
346 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
348 whlp_mkparagraph(&state
, FONT_TITLE
, p
->kwtext
, FALSE
);
349 whlp_set_font(h
, FONT_TITLE
);
350 whlp_text(h
, ": "); /* FIXME: configurability */
352 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
360 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
361 whlp_para_attr(h
, WHLP_PARA_ALIGNMENT
, WHLP_ALIGN_CENTRE
);
362 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
363 whlp_set_font(h
, FONT_RULE
);
364 #define TEN "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"
365 #define TWENTY TEN TEN
366 #define FORTY TWENTY TWENTY
367 #define EIGHTY FORTY FORTY
368 whlp_text(h
, EIGHTY
);
378 case para_DescribedThing
:
379 case para_Description
:
380 case para_BiblioCited
:
382 case para_NumberedList
:
383 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
384 if (p
->type
== para_Bullet
|| p
->type
== para_NumberedList
) {
385 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
+ 72);
386 whlp_para_attr(h
, WHLP_PARA_FIRSTLINEINDENT
, -36);
387 whlp_set_tabstop(h
, 72, WHLP_ALIGN_LEFT
);
388 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
389 whlp_set_font(h
, FONT_NORMAL
);
390 if (p
->type
== para_Bullet
) {
391 whlp_text(h
, "\x95");
393 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
398 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
,
399 72*nesting
+ (p
->type
==para_Description ?
72 : 0));
400 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
403 if (p
->type
== para_BiblioCited
) {
404 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
408 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
414 * In a code paragraph, each individual word is a line. For
415 * Help files, we will have to output this as a set of
416 * paragraphs, all but the last of which don't set
424 for (w
= p
->words
; w
; w
= w
->next
) if (w
->type
== word_WeakCode
) {
426 if (w
->next
&& w
->next
->type
== word_Emph
) {
433 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
435 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
);
436 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
437 while (e
&& *e
&& *t
) {
441 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
443 whlp_set_font(h
, FONT_ITAL_CODE
);
445 whlp_set_font(h
, FONT_BOLD_CODE
);
447 whlp_set_font(h
, FONT_CODE
);
448 whlp_convert(t
, n
, &c
, FALSE
);
454 whlp_set_font(h
, FONT_CODE
);
455 whlp_convert(t
, 0, &c
, FALSE
);
465 whlp_close(h
, filename
);
468 * Loop over the index entries, cleaning up our final text
471 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
472 sfree(ie
->backend_data
);
479 static void whlp_contents_write(struct bk_whlp_state
*state
,
480 int level
, char *text
, WHLP_TOPIC topic
) {
482 * Horrifying bug in WinHelp. When dropping a section level or
483 * more without using a folder-type entry, WinHelp accidentally
484 * adds one to the section level. So we correct for that here.
486 if (state
->cnt_last_level
> level
&& topic
)
487 state
->cnt_workaround
= -1;
489 state
->cnt_workaround
= 0;
490 state
->cnt_last_level
= level
;
492 fprintf(state
->cntfp
, "%d ", level
+ state
->cnt_workaround
);
495 fputc('\\', state
->cntfp
);
496 fputc(*text
, state
->cntfp
);
500 fprintf(state
->cntfp
, "=%s", whlp_topic_id(topic
));
501 fputc('\n', state
->cntfp
);
504 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
) {
505 whlp_begin_para(state
->h
, WHLP_PARA_NONSCROLL
);
506 whlp_start_hyperlink(state
->h
, (WHLP_TOPIC
)p
->private_data
);
508 whlp_mkparagraph(state
, FONT_NORMAL
, p
->kwtext
, TRUE
);
509 whlp_set_font(state
->h
, FONT_NORMAL
);
510 whlp_text(state
->h
, ": "); /* FIXME: configurability */
512 whlp_mkparagraph(state
, FONT_NORMAL
, p
->words
, TRUE
);
513 whlp_end_hyperlink(state
->h
);
514 whlp_end_para(state
->h
);
518 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
519 int font
, word
*text
, int subsidiary
) {
525 paragraph
*xref_target
= NULL
;
527 for (; text
; text
= text
->next
) switch (text
->type
) {
533 if (subsidiary
) break; /* disabled in subsidiary bits */
535 indextag
*tag
= index_findtag(state
->idx
, text
->text
);
539 for (i
= 0; i
< tag
->nrefs
; i
++)
540 whlp_index_term(state
->h
, tag
->refs
[i
]->backend_data
,
547 if (subsidiary
) break; /* disabled in subsidiary bits */
548 kwl
= kw_lookup(state
->keywords
, text
->text
);
549 assert(xref_target
== NULL
);
551 if (kwl
->para
->type
== para_NumberedList
) {
552 break; /* don't xref to numbered list items */
553 } else if (kwl
->para
->type
== para_BiblioCited
) {
555 * An xref to a bibliography item jumps to the section
558 if (kwl
->para
->parent
)
559 xref_target
= kwl
->para
->parent
;
563 xref_target
= kwl
->para
;
565 whlp_start_hyperlink(state
->h
,
566 (WHLP_TOPIC
)xref_target
->private_data
);
571 if (subsidiary
) break; /* disabled in subsidiary bits */
573 whlp_end_hyperlink(state
->h
);
581 case word_WhiteSpace
:
584 case word_WkCodeSpace
:
588 case word_WkCodeQuote
:
589 if (towordstyle(text
->type
) == word_Emph
)
590 newfont
= deffont
+ FONT_EMPH
;
591 else if (towordstyle(text
->type
) == word_Code
||
592 towordstyle(text
->type
) == word_WeakCode
)
593 newfont
= deffont
+ FONT_CODE
;
596 if (newfont
!= currfont
) {
598 whlp_set_font(state
->h
, newfont
);
600 if (removeattr(text
->type
) == word_Normal
) {
601 if (whlp_convert(text
->text
, 0, &c
, TRUE
))
602 whlp_text(state
->h
, c
);
604 whlp_mkparagraph(state
, deffont
, text
->alt
, FALSE
);
606 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
607 whlp_text(state
->h
, " ");
608 } else if (removeattr(text
->type
) == word_Quote
) {
610 quoteaux(text
->aux
) == quote_Open ?
"\x91" : "\x92");
611 /* FIXME: configurability */
617 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
) {
620 for (; text
; text
= text
->next
) switch (text
->type
) {
633 case word_WhiteSpace
:
636 case word_WkCodeSpace
:
640 case word_WkCodeQuote
:
641 assert(text
->type
!= word_CodeQuote
&&
642 text
->type
!= word_WkCodeQuote
);
643 if (removeattr(text
->type
) == word_Normal
) {
644 if (whlp_convert(text
->text
, 0, &c
, FALSE
))
647 whlp_rdaddwc(rs
, text
->alt
);
649 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
651 } else if (removeattr(text
->type
) == word_Quote
) {
652 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'\x91' : '\x92');
653 /* FIXME: configurability */
660 * Convert a wide string into a string of chars. If `result' is
661 * non-NULL, mallocs the resulting string and stores a pointer to
662 * it in `*result'. If `result' is NULL, merely checks whether all
663 * characters in the string are feasible for the output character
666 * Return is nonzero if all characters are OK. If not all
667 * characters are OK but `result' is non-NULL, a result _will_
668 * still be generated!
670 static int whlp_convert(wchar_t *s
, int maxlen
,
671 char **result
, int hard_spaces
) {
673 * FIXME. Currently this is ISO8859-1 only.
675 int doing
= (result
!= 0);
678 int plen
= 0, psize
= 0;
683 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
687 if ((c
>= 32 && c
<= 126) ||
688 (c
>= 160 && c
<= 255)) {
690 if (c
== 32 && hard_spaces
)
695 /* Char is not OK. */
697 outc
= 0xBF; /* approximate the good old DEC `uh?' */
702 p
= resize(p
, psize
);
708 p
= resize(p
, plen
+1);