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
;
59 filename
= "output.hlp"; /* FIXME: configurability */
60 cntname
= "output.cnt"; /* corresponding contents file */
62 state
.cntfp
= fopen(cntname
, "wb");
63 state
.cnt_last_level
= -1; state
.cnt_workaround
= 0;
65 h
= state
.h
= whlp_new();
66 state
.keywords
= keywords
;
69 whlp_start_macro(h
, "CB(\"btn_about\",\"&About\",\"About()\")");
70 whlp_start_macro(h
, "CB(\"btn_up\",\"&Up\",\"Contents()\")");
71 whlp_start_macro(h
, "BrowseButtons()");
73 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
75 whlp_create_font(h
, "Times New Roman", WHLP_FONTFAM_SERIF
, 24,
76 WHLP_FONT_ITALIC
, 0, 0, 0);
77 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
79 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
80 WHLP_FONT_ITALIC
, 0, 0, 0);
81 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 24,
82 WHLP_FONT_BOLD
, 0, 0, 0);
83 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
84 WHLP_FONT_BOLD
, 0, 0, 0);
85 whlp_create_font(h
, "Arial", WHLP_FONTFAM_SERIF
, 30,
86 WHLP_FONT_BOLD
|WHLP_FONT_ITALIC
, 0, 0, 0);
87 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_FIXED
, 30,
88 WHLP_FONT_BOLD
, 0, 0, 0);
89 whlp_create_font(h
, "Courier New", WHLP_FONTFAM_SANS
, 18,
90 WHLP_FONT_STRIKEOUT
, 0, 0, 0);
93 * Loop over the source form finding out whether the user has
94 * specified particular help topic names for anything.
96 for (p
= sourceform
; p
; p
= p
->next
) {
97 p
->private_data
= NULL
;
98 if (p
->type
== para_Config
&& p
->parent
) {
99 if (!ustricmp(p
->keyword
, L
"winhelp-topic")) {
101 whlp_convert(uadv(p
->keyword
), 0, &topicname
, 0);
102 /* Store the topic name in the private_data field of the
103 * containing section. */
104 p
->parent
->private_data
= topicname
;
110 * Loop over the source form registering WHLP_TOPICs for
114 contents_topic
= whlp_register_topic(h
, "Top", NULL
);
115 whlp_primary_topic(h
, contents_topic
);
116 for (p
= sourceform
; p
; p
= p
->next
) {
117 if (p
->type
== para_Chapter
||
118 p
->type
== para_Appendix
||
119 p
->type
== para_UnnumberedChapter
||
120 p
->type
== para_Heading
||
121 p
->type
== para_Subsect
) {
122 char *topicid
= p
->private_data
;
125 p
->private_data
= whlp_register_topic(h
, topicid
, &errstr
);
126 if (!p
->private_data
) {
127 p
->private_data
= whlp_register_topic(h
, NULL
, NULL
);
128 error(err_winhelp_ctxclash
, &p
->fpos
, topicid
, errstr
);
135 * Loop over the index entries, preparing final text forms for
138 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
139 rdstringc rs
= {0, 0, NULL
};
140 whlp_rdaddwc(&rs
, ie
->text
);
141 ie
->backend_data
= rs
.text
;
146 /* ------------------------------------------------------------------
147 * Do the contents page, containing title, preamble and
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 */
177 * Next comes the preamble, which just goes into the ordinary
180 for (p
= sourceform
; p
; p
= p
->next
) {
181 if (p
->type
== para_Preamble
) {
182 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
183 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
184 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
190 * The copyright goes to two places, again: into the contents
191 * page and also into the system section.
194 rdstringc rs
= {0, 0, NULL
};
195 for (p
= sourceform
; p
; p
= p
->next
) {
196 if (p
->type
== para_Copyright
) {
197 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
198 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
199 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
201 whlp_rdaddwc(&rs
, p
->words
);
205 whlp_copyright(h
, rs
.text
);
211 * Now do the primary navigation menu.
213 for (p
= sourceform
; p
; p
= p
->next
) {
214 if (p
->type
== para_Chapter
||
215 p
->type
== para_Appendix
||
216 p
->type
== para_UnnumberedChapter
)
217 whlp_navmenu(&state
, p
);
220 state
.curr_topic
= contents_topic
;
223 /* ------------------------------------------------------------------
224 * Now we've done the contents page, we're ready to go through
225 * and do the main manual text. Ooh.
228 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
230 * Things we ignore because we've already processed them or
231 * aren't going to touch them in this pass.
235 case para_Biblio
: /* only touch BiblioCited */
254 * Chapter and section titles: start a new Help topic.
258 case para_UnnumberedChapter
:
261 if (lastsect
&& lastsect
->child
) {
264 * Do a navigation menu for the previous section we
267 for (q
= lastsect
->child
; q
; q
= q
->sibling
)
268 whlp_navmenu(&state
, q
);
271 rdstringc rs
= {0, 0, NULL
};
272 WHLP_TOPIC new_topic
, parent_topic
;
273 char *macro
, *topicid
;
275 new_topic
= p
->private_data
;
276 whlp_browse_link(h
, state
.curr_topic
, new_topic
);
277 state
.curr_topic
= new_topic
;
280 whlp_rdaddwc(&rs
, p
->kwtext
);
281 rdaddsc(&rs
, ": "); /* FIXME: configurability */
283 whlp_rdaddwc(&rs
, p
->words
);
284 if (p
->parent
== NULL
)
285 parent_topic
= contents_topic
;
287 parent_topic
= (WHLP_TOPIC
)p
->parent
->private_data
;
288 topicid
= whlp_topic_id(parent_topic
);
289 macro
= smalloc(100+strlen(topicid
));
291 "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
293 whlp_begin_topic(h
, new_topic
,
294 rs
.text ? rs
.text
: "",
300 * Output the .cnt entry.
302 * WinHelp has a bug involving having an internal
303 * node followed by a leaf at the same level: the
304 * leaf is output at the wrong level. We can mostly
305 * work around this by modifying the leaf level
306 * itself (see whlp_contents_write), but this
307 * doesn't work for top-level sections since we
308 * can't turn a level-1 leaf into a level-0 one. So
309 * for top-level leaf sections (Bibliography
310 * springs to mind), we output an internal node
311 * containing only the leaf for that section.
316 /* Count up the level. */
318 for (q
= p
; q
->parent
; q
= q
->parent
) i
++;
320 if (p
->child
|| !p
->parent
) {
322 * If p has children then it needs to be a
323 * folder; if it has no parent then it needs to
324 * be a folder to work around the bug.
326 whlp_contents_write(&state
, i
, rs
.text
, NULL
);
329 whlp_contents_write(&state
, i
, rs
.text
, new_topic
);
334 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
336 whlp_mkparagraph(&state
, FONT_TITLE
, p
->kwtext
, FALSE
);
337 whlp_set_font(h
, FONT_TITLE
);
338 whlp_text(h
, ": "); /* FIXME: configurability */
340 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
348 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
349 whlp_para_attr(h
, WHLP_PARA_ALIGNMENT
, WHLP_ALIGN_CENTRE
);
350 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
351 whlp_set_font(h
, FONT_RULE
);
352 #define TEN "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"
353 #define TWENTY TEN TEN
354 #define FORTY TWENTY TWENTY
355 #define EIGHTY FORTY FORTY
356 whlp_text(h
, EIGHTY
);
365 case para_DescribedThing
:
366 case para_Description
:
367 case para_BiblioCited
:
369 case para_NumberedList
:
370 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
371 if (p
->type
== para_Bullet
|| p
->type
== para_NumberedList
) {
372 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
+ 72);
373 whlp_para_attr(h
, WHLP_PARA_FIRSTLINEINDENT
, -36);
374 whlp_set_tabstop(h
, 72, WHLP_ALIGN_LEFT
);
375 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
376 whlp_set_font(h
, FONT_NORMAL
);
377 if (p
->type
== para_Bullet
) {
378 whlp_text(h
, "\x95");
380 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
385 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
,
386 72*nesting
+ (p
->type
==para_Description ?
72 : 0));
387 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
390 if (p
->type
== para_BiblioCited
) {
391 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->kwtext
, FALSE
);
395 whlp_mkparagraph(&state
, FONT_NORMAL
, p
->words
, FALSE
);
401 * In a code paragraph, each individual word is a line. For
402 * Help files, we will have to output this as a set of
403 * paragraphs, all but the last of which don't set
411 for (w
= p
->words
; w
; w
= w
->next
) if (w
->type
== word_WeakCode
) {
413 if (w
->next
&& w
->next
->type
== word_Emph
) {
420 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
422 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
);
423 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
424 while (e
&& *e
&& *t
) {
428 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
430 whlp_set_font(h
, FONT_ITAL_CODE
);
432 whlp_set_font(h
, FONT_BOLD_CODE
);
434 whlp_set_font(h
, FONT_CODE
);
435 whlp_convert(t
, n
, &c
, FALSE
);
441 whlp_set_font(h
, FONT_CODE
);
442 whlp_convert(t
, 0, &c
, FALSE
);
452 whlp_close(h
, filename
);
455 * Loop over the index entries, cleaning up our final text
458 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
459 sfree(ie
->backend_data
);
463 static void whlp_contents_write(struct bk_whlp_state
*state
,
464 int level
, char *text
, WHLP_TOPIC topic
) {
466 * Horrifying bug in WinHelp. When dropping a section level or
467 * more without using a folder-type entry, WinHelp accidentally
468 * adds one to the section level. So we correct for that here.
470 if (state
->cnt_last_level
> level
&& topic
)
471 state
->cnt_workaround
= -1;
473 state
->cnt_workaround
= 0;
474 state
->cnt_last_level
= level
;
476 fprintf(state
->cntfp
, "%d ", level
+ state
->cnt_workaround
);
479 fputc('\\', state
->cntfp
);
480 fputc(*text
, state
->cntfp
);
484 fprintf(state
->cntfp
, "=%s", whlp_topic_id(topic
));
485 fputc('\n', state
->cntfp
);
488 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
) {
489 whlp_begin_para(state
->h
, WHLP_PARA_NONSCROLL
);
490 whlp_start_hyperlink(state
->h
, (WHLP_TOPIC
)p
->private_data
);
492 whlp_mkparagraph(state
, FONT_NORMAL
, p
->kwtext
, TRUE
);
493 whlp_set_font(state
->h
, FONT_NORMAL
);
494 whlp_text(state
->h
, ": "); /* FIXME: configurability */
496 whlp_mkparagraph(state
, FONT_NORMAL
, p
->words
, TRUE
);
497 whlp_end_hyperlink(state
->h
);
498 whlp_end_para(state
->h
);
502 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
503 int font
, word
*text
, int subsidiary
) {
509 paragraph
*xref_target
= NULL
;
511 for (; text
; text
= text
->next
) switch (text
->type
) {
517 if (subsidiary
) break; /* disabled in subsidiary bits */
519 indextag
*tag
= index_findtag(state
->idx
, text
->text
);
523 for (i
= 0; i
< tag
->nrefs
; i
++)
524 whlp_index_term(state
->h
, tag
->refs
[i
]->backend_data
,
531 if (subsidiary
) break; /* disabled in subsidiary bits */
532 kwl
= kw_lookup(state
->keywords
, text
->text
);
533 assert(xref_target
== NULL
);
535 if (kwl
->para
->type
== para_NumberedList
) {
536 break; /* don't xref to numbered list items */
537 } else if (kwl
->para
->type
== para_BiblioCited
) {
539 * An xref to a bibliography item jumps to the section
542 if (kwl
->para
->parent
)
543 xref_target
= kwl
->para
->parent
;
547 xref_target
= kwl
->para
;
549 whlp_start_hyperlink(state
->h
,
550 (WHLP_TOPIC
)xref_target
->private_data
);
555 if (subsidiary
) break; /* disabled in subsidiary bits */
557 whlp_end_hyperlink(state
->h
);
565 case word_WhiteSpace
:
568 case word_WkCodeSpace
:
572 case word_WkCodeQuote
:
573 if (towordstyle(text
->type
) == word_Emph
)
574 newfont
= deffont
+ FONT_EMPH
;
575 else if (towordstyle(text
->type
) == word_Code
||
576 towordstyle(text
->type
) == word_WeakCode
)
577 newfont
= deffont
+ FONT_CODE
;
580 if (newfont
!= currfont
) {
582 whlp_set_font(state
->h
, newfont
);
584 if (removeattr(text
->type
) == word_Normal
) {
585 if (whlp_convert(text
->text
, 0, &c
, TRUE
))
586 whlp_text(state
->h
, c
);
588 whlp_mkparagraph(state
, deffont
, text
->alt
, FALSE
);
590 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
591 whlp_text(state
->h
, " ");
592 } else if (removeattr(text
->type
) == word_Quote
) {
594 quoteaux(text
->aux
) == quote_Open ?
"\x91" : "\x92");
595 /* FIXME: configurability */
601 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
) {
604 for (; text
; text
= text
->next
) switch (text
->type
) {
617 case word_WhiteSpace
:
620 case word_WkCodeSpace
:
624 case word_WkCodeQuote
:
625 assert(text
->type
!= word_CodeQuote
&&
626 text
->type
!= word_WkCodeQuote
);
627 if (removeattr(text
->type
) == word_Normal
) {
628 if (whlp_convert(text
->text
, 0, &c
, FALSE
))
631 whlp_rdaddwc(rs
, text
->alt
);
633 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
635 } else if (removeattr(text
->type
) == word_Quote
) {
636 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'\x91' : '\x92');
637 /* FIXME: configurability */
644 * Convert a wide string into a string of chars. If `result' is
645 * non-NULL, mallocs the resulting string and stores a pointer to
646 * it in `*result'. If `result' is NULL, merely checks whether all
647 * characters in the string are feasible for the output character
650 * Return is nonzero if all characters are OK. If not all
651 * characters are OK but `result' is non-NULL, a result _will_
652 * still be generated!
654 static int whlp_convert(wchar_t *s
, int maxlen
,
655 char **result
, int hard_spaces
) {
657 * FIXME. Currently this is ISO8859-1 only.
659 int doing
= (result
!= 0);
662 int plen
= 0, psize
= 0;
667 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
671 if ((c
>= 32 && c
<= 126) ||
672 (c
>= 160 && c
<= 255)) {
674 if (c
== 32 && hard_spaces
)
679 /* Char is not OK. */
681 outc
= 0xBF; /* approximate the good old DEC `uh?' */
686 p
= resize(p
, psize
);
692 p
= resize(p
, plen
+1);