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.
37 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
);
38 static int whlp_convert(wchar_t *s
, char **result
, int hard_spaces
);
39 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
40 int font
, word
*text
, int subsidiary
);
41 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
);
42 static void whlp_contents_write(struct bk_whlp_state
*state
,
43 int level
, char *text
, WHLP_TOPIC topic
);
45 void whlp_backend(paragraph
*sourceform
, keywordlist
*keywords
,
48 char *filename
, *cntname
;
49 paragraph
*p
, *lastsect
;
50 struct bk_whlp_state state
;
51 WHLP_TOPIC contents_topic
;
56 filename
= "output.hlp"; /* FIXME: configurability */
57 cntname
= "output.cnt"; /* corresponding contents file */
59 state
.cntfp
= fopen(cntname
, "wb");
60 state
.cnt_last_level
= -1; state
.cnt_workaround
= 0;
62 h
= state
.h
= whlp_new();
63 state
.keywords
= keywords
;
66 whlp_start_macro(h
, "CB(\"btn_about\",\"&About\",\"About()\")");
67 whlp_start_macro(h
, "CB(\"btn_up\",\"&Up\",\"Contents()\")");
68 whlp_start_macro(h
, "BrowseButtons()");
70 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
72 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
73 WHLP_FONT_ITALIC
, 0, 0, 0);
74 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
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.
89 for (p
= sourceform
; p
; p
= p
->next
) {
90 p
->private_data
= NULL
;
91 if (p
->type
== para_Config
&& p
->parent
) {
92 if (!ustricmp(p
->keyword
, L
"winhelp-topic")) {
94 whlp_convert(uadv(p
->keyword
), &topicname
, 0);
95 /* Store the topic name in the private_data field of the
96 * containing section. */
97 p
->parent
->private_data
= topicname
;
103 * Loop over the source form registering WHLP_TOPICs for
107 contents_topic
= whlp_register_topic(h
, "Top", NULL
);
108 whlp_primary_topic(h
, contents_topic
);
109 for (p
= sourceform
; p
; p
= p
->next
) {
110 if (p
->type
== para_Chapter
||
111 p
->type
== para_Appendix
||
112 p
->type
== para_UnnumberedChapter
||
113 p
->type
== para_Heading
||
114 p
->type
== para_Subsect
) {
115 char *topicid
= p
->private_data
;
118 p
->private_data
= whlp_register_topic(h
, topicid
, &errstr
);
119 if (!p
->private_data
) {
120 p
->private_data
= whlp_register_topic(h
, NULL
, NULL
);
121 error(err_winhelp_ctxclash
, &p
->fpos
, topicid
, errstr
);
128 * Loop over the index entries, preparing final text forms for
131 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
132 rdstringc rs
= {0, 0, NULL
};
133 whlp_rdaddwc(&rs
, ie
->text
);
134 ie
->backend_data
= rs
.text
;
139 /* ------------------------------------------------------------------
140 * Do the contents page, containing title, preamble and
144 whlp_begin_topic(h
, contents_topic
, "Contents", "DB(\"btn_up\")", NULL
);
147 * The manual title goes in the non-scroll region, and also
148 * goes into the system title slot.
151 rdstringc rs
= {0, 0, NULL
};
152 for (p
= sourceform
; p
; p
= p
->next
) {
153 if (p
->type
== para_Title
) {
154 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
155 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
156 whlp_rdaddwc(&rs
, p
->words
);
161 whlp_title(h
, rs
.text
);
162 fprintf(state
.cntfp
, ":Title %s\r\n", rs
.text
);
165 whlp_contents_write(&state
, 1, "Title page", contents_topic
);
166 /* FIXME: configurability in that string */
170 * Next comes the preamble, which just goes into the ordinary
173 for (p
= sourceform
; p
; p
= p
->next
) {
174 if (p
->type
== para_Preamble
) {
175 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
176 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
177 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
183 * The copyright goes to two places, again: into the contents
184 * page and also into the system section.
187 rdstringc rs
= {0, 0, NULL
};
188 for (p
= sourceform
; p
; p
= p
->next
) {
189 if (p
->type
== para_Copyright
) {
190 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
191 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
192 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
194 whlp_rdaddwc(&rs
, p
->words
);
198 whlp_copyright(h
, rs
.text
);
204 * Now do the primary navigation menu.
206 for (p
= sourceform
; p
; p
= p
->next
) {
207 if (p
->type
== para_Chapter
||
208 p
->type
== para_Appendix
||
209 p
->type
== para_UnnumberedChapter
)
210 whlp_navmenu(&state
, p
);
213 state
.curr_topic
= contents_topic
;
216 /* ------------------------------------------------------------------
217 * Now we've done the contents page, we're ready to go through
218 * and do the main manual text. Ooh.
221 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
223 * Things we ignore because we've already processed them or
224 * aren't going to touch them in this pass.
228 case para_Biblio
: /* only touch BiblioCited */
245 * Chapter and section titles: start a new Help topic.
249 case para_UnnumberedChapter
:
252 if (lastsect
&& lastsect
->child
) {
255 * Do a navigation menu for the previous section we
258 for (q
= lastsect
->child
; q
; q
= q
->sibling
)
259 whlp_navmenu(&state
, q
);
262 rdstringc rs
= {0, 0, NULL
};
263 WHLP_TOPIC new_topic
, parent_topic
;
264 char *macro
, *topicid
;
266 new_topic
= p
->private_data
;
267 whlp_browse_link(h
, state
.curr_topic
, new_topic
);
268 state
.curr_topic
= new_topic
;
271 whlp_rdaddwc(&rs
, p
->kwtext
);
272 rdaddsc(&rs
, ": "); /* FIXME: configurability */
274 whlp_rdaddwc(&rs
, p
->words
);
275 if (p
->parent
== NULL
)
276 parent_topic
= contents_topic
;
278 parent_topic
= (WHLP_TOPIC
)p
->parent
->private_data
;
279 topicid
= whlp_topic_id(parent_topic
);
280 macro
= smalloc(100+strlen(topicid
));
282 "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
284 whlp_begin_topic(h
, new_topic
,
285 rs
.text ? rs
.text
: "",
291 * Output the .cnt entry.
293 * WinHelp has a bug involving having an internal
294 * node followed by a leaf at the same level: the
295 * leaf is output at the wrong level. We can mostly
296 * work around this by modifying the leaf level
297 * itself (see whlp_contents_write), but this
298 * doesn't work for top-level sections since we
299 * can't turn a level-1 leaf into a level-0 one. So
300 * for top-level leaf sections (Bibliography
301 * springs to mind), we output an internal node
302 * containing only the leaf for that section.
307 /* Count up the level. */
309 for (q
= p
; q
->parent
; q
= q
->parent
) i
++;
311 if (p
->child
|| !p
->parent
) {
313 * If p has children then it needs to be a
314 * folder; if it has no parent then it needs to
315 * be a folder to work around the bug.
317 whlp_contents_write(&state
, i
, rs
.text
, NULL
);
320 whlp_contents_write(&state
, i
, rs
.text
, new_topic
);
325 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
327 whlp_mkparagraph(&state
, FONT_TITLE
, p
->kwtext
, FALSE
);
328 whlp_set_font(h
, FONT_TITLE
);
329 whlp_text(h
, ": "); /* FIXME: configurability */
331 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
339 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
340 whlp_para_attr(h
, WHLP_PARA_ALIGNMENT
, WHLP_ALIGN_CENTRE
);
341 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
342 whlp_set_font(h
, FONT_RULE
);
343 #define TEN "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"
344 #define TWENTY TEN TEN
345 #define FORTY TWENTY TWENTY
346 #define EIGHTY FORTY FORTY
347 whlp_text(h
, EIGHTY
);
356 case para_DescribedThing
:
357 case para_Description
:
358 case para_BiblioCited
:
360 case para_NumberedList
:
361 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
362 if (p
->type
== para_Bullet
|| p
->type
== para_NumberedList
) {
363 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
+ 72);
364 whlp_para_attr(h
, WHLP_PARA_FIRSTLINEINDENT
, -36);
365 whlp_set_tabstop(h
, 72, WHLP_ALIGN_LEFT
);
366 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
367 whlp_set_font(h
, FONT_NORMAL
);
368 if (p
->type
== para_Bullet
) {
369 whlp_text(h
, "\x95");
371 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
376 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
,
377 72*nesting
+ (p
->type
==para_Description ?
72 : 0));
378 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
381 if (p
->type
== para_BiblioCited
) {
382 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
386 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
392 * In a code paragraph, each individual word is a line. For
393 * Help files, we will have to output this as a set of
394 * paragraphs, all but the last of which don't set
400 for (w
= p
->words
; w
; w
= w
->next
) {
402 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
403 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
);
404 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
405 whlp_set_font(h
, FONT_CODE
);
406 whlp_convert(w
->text
, &c
, FALSE
);
416 whlp_close(h
, filename
);
419 * Loop over the index entries, cleaning up our final text
422 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
423 sfree(ie
->backend_data
);
427 static void whlp_contents_write(struct bk_whlp_state
*state
,
428 int level
, char *text
, WHLP_TOPIC topic
) {
430 * Horrifying bug in WinHelp. When dropping a section level or
431 * more without using a folder-type entry, WinHelp accidentally
432 * adds one to the section level. So we correct for that here.
434 if (state
->cnt_last_level
> level
&& topic
)
435 state
->cnt_workaround
= -1;
437 state
->cnt_workaround
= 0;
438 state
->cnt_last_level
= level
;
440 fprintf(state
->cntfp
, "%d ", level
+ state
->cnt_workaround
);
443 fputc('\\', state
->cntfp
);
444 fputc(*text
, state
->cntfp
);
448 fprintf(state
->cntfp
, "=%s", whlp_topic_id(topic
));
449 fputc('\n', state
->cntfp
);
452 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
) {
453 whlp_begin_para(state
->h
, WHLP_PARA_NONSCROLL
);
454 whlp_start_hyperlink(state
->h
, (WHLP_TOPIC
)p
->private_data
);
456 whlp_mkparagraph(state
, FONT_NORMAL
, p
->kwtext
, TRUE
);
457 whlp_set_font(state
->h
, FONT_NORMAL
);
458 whlp_text(state
->h
, ": "); /* FIXME: configurability */
460 whlp_mkparagraph(state
, FONT_NORMAL
, p
->words
, TRUE
);
461 whlp_end_hyperlink(state
->h
);
462 whlp_end_para(state
->h
);
466 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
467 int font
, word
*text
, int subsidiary
) {
473 paragraph
*xref_target
= NULL
;
475 for (; text
; text
= text
->next
) switch (text
->type
) {
481 if (subsidiary
) break; /* disabled in subsidiary bits */
483 indextag
*tag
= index_findtag(state
->idx
, text
->text
);
487 for (i
= 0; i
< tag
->nrefs
; i
++)
488 whlp_index_term(state
->h
, tag
->refs
[i
]->backend_data
,
495 if (subsidiary
) break; /* disabled in subsidiary bits */
496 kwl
= kw_lookup(state
->keywords
, text
->text
);
497 assert(xref_target
== NULL
);
499 if (kwl
->para
->type
== para_NumberedList
) {
500 break; /* don't xref to numbered list items */
501 } else if (kwl
->para
->type
== para_BiblioCited
) {
503 * An xref to a bibliography item jumps to the section
506 if (kwl
->para
->parent
)
507 xref_target
= kwl
->para
->parent
;
511 xref_target
= kwl
->para
;
513 whlp_start_hyperlink(state
->h
,
514 (WHLP_TOPIC
)xref_target
->private_data
);
519 if (subsidiary
) break; /* disabled in subsidiary bits */
521 whlp_end_hyperlink(state
->h
);
529 case word_WhiteSpace
:
532 case word_WkCodeSpace
:
536 case word_WkCodeQuote
:
537 if (towordstyle(text
->type
) == word_Emph
)
538 newfont
= deffont
+ FONT_EMPH
;
539 else if (towordstyle(text
->type
) == word_Code
||
540 towordstyle(text
->type
) == word_WeakCode
)
541 newfont
= deffont
+ FONT_CODE
;
544 if (newfont
!= currfont
) {
546 whlp_set_font(state
->h
, newfont
);
548 if (removeattr(text
->type
) == word_Normal
) {
549 if (whlp_convert(text
->text
, &c
, TRUE
))
550 whlp_text(state
->h
, c
);
552 whlp_mkparagraph(state
, deffont
, text
->alt
, FALSE
);
554 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
555 whlp_text(state
->h
, " ");
556 } else if (removeattr(text
->type
) == word_Quote
) {
558 quoteaux(text
->aux
) == quote_Open ?
"\x91" : "\x92");
559 /* FIXME: configurability */
565 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
) {
568 for (; text
; text
= text
->next
) switch (text
->type
) {
581 case word_WhiteSpace
:
584 case word_WkCodeSpace
:
588 case word_WkCodeQuote
:
589 assert(text
->type
!= word_CodeQuote
&&
590 text
->type
!= word_WkCodeQuote
);
591 if (removeattr(text
->type
) == word_Normal
) {
592 if (whlp_convert(text
->text
, &c
, FALSE
))
595 whlp_rdaddwc(rs
, text
->alt
);
597 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
599 } else if (removeattr(text
->type
) == word_Quote
) {
600 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'\x91' : '\x92');
601 /* FIXME: configurability */
608 * Convert a wide string into a string of chars. If `result' is
609 * non-NULL, mallocs the resulting string and stores a pointer to
610 * it in `*result'. If `result' is NULL, merely checks whether all
611 * characters in the string are feasible for the output character
614 * Return is nonzero if all characters are OK. If not all
615 * characters are OK but `result' is non-NULL, a result _will_
616 * still be generated!
618 static int whlp_convert(wchar_t *s
, char **result
, int hard_spaces
) {
620 * FIXME. Currently this is ISO8859-1 only.
622 int doing
= (result
!= 0);
625 int plen
= 0, psize
= 0;
631 if ((c
>= 32 && c
<= 126) ||
632 (c
>= 160 && c
<= 255)) {
634 if (c
== 32 && hard_spaces
)
639 /* Char is not OK. */
641 outc
= 0xBF; /* approximate the good old DEC `uh?' */
646 p
= resize(p
, psize
);
652 p
= resize(p
, plen
+1);