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
= FALSE
;
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 */
177 * Put the copyright into the system section.
180 rdstringc rs
= {0, 0, NULL
};
181 for (p
= sourceform
; p
; p
= p
->next
) {
182 if (p
->type
== para_Copyright
)
183 whlp_rdaddwc(&rs
, p
->words
);
186 whlp_copyright(h
, rs
.text
);
193 /* ------------------------------------------------------------------
194 * Now we've done the contents page, we're ready to go through
195 * and do the main manual text. Ooh.
198 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
200 * Things we ignore because we've already processed them or
201 * aren't going to touch them in this pass.
205 case para_Biblio
: /* only touch BiblioCited */
222 * Chapter and section titles: start a new Help topic.
226 case para_UnnumberedChapter
:
230 if (!done_contents_topic
) {
234 * If this is the first section title we've seen, then
235 * we're currently still in the contents topic. We
236 * should therefore finish up the contents page by
237 * writing a nav menu.
239 for (p
= sourceform
; p
; p
= p
->next
) {
240 if (p
->type
== para_Chapter
||
241 p
->type
== para_Appendix
||
242 p
->type
== para_UnnumberedChapter
)
243 whlp_navmenu(&state
, p
);
246 state
.curr_topic
= contents_topic
;
248 done_contents_topic
= TRUE
;
251 if (lastsect
&& lastsect
->child
) {
254 * Do a navigation menu for the previous section we
257 for (q
= lastsect
->child
; q
; q
= q
->sibling
)
258 whlp_navmenu(&state
, q
);
261 rdstringc rs
= {0, 0, NULL
};
262 WHLP_TOPIC new_topic
, parent_topic
;
263 char *macro
, *topicid
;
265 new_topic
= p
->private_data
;
266 whlp_browse_link(h
, state
.curr_topic
, new_topic
);
267 state
.curr_topic
= new_topic
;
270 whlp_rdaddwc(&rs
, p
->kwtext
);
271 rdaddsc(&rs
, ": "); /* FIXME: configurability */
273 whlp_rdaddwc(&rs
, p
->words
);
274 if (p
->parent
== NULL
)
275 parent_topic
= contents_topic
;
277 parent_topic
= (WHLP_TOPIC
)p
->parent
->private_data
;
278 topicid
= whlp_topic_id(parent_topic
);
279 macro
= smalloc(100+strlen(topicid
));
281 "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
283 whlp_begin_topic(h
, new_topic
,
284 rs
.text ? rs
.text
: "",
290 * Output the .cnt entry.
292 * WinHelp has a bug involving having an internal
293 * node followed by a leaf at the same level: the
294 * leaf is output at the wrong level. We can mostly
295 * work around this by modifying the leaf level
296 * itself (see whlp_contents_write), but this
297 * doesn't work for top-level sections since we
298 * can't turn a level-1 leaf into a level-0 one. So
299 * for top-level leaf sections (Bibliography
300 * springs to mind), we output an internal node
301 * containing only the leaf for that section.
306 /* Count up the level. */
308 for (q
= p
; q
->parent
; q
= q
->parent
) i
++;
310 if (p
->child
|| !p
->parent
) {
312 * If p has children then it needs to be a
313 * folder; if it has no parent then it needs to
314 * be a folder to work around the bug.
316 whlp_contents_write(&state
, i
, rs
.text
, NULL
);
319 whlp_contents_write(&state
, i
, rs
.text
, new_topic
);
324 whlp_begin_para(h
, WHLP_PARA_NONSCROLL
);
326 whlp_mkparagraph(&state
, FONT_TITLE
, p
->kwtext
, FALSE
);
327 whlp_set_font(h
, FONT_TITLE
);
328 whlp_text(h
, ": "); /* FIXME: configurability */
330 whlp_mkparagraph(&state
, FONT_TITLE
, p
->words
, FALSE
);
338 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
339 whlp_para_attr(h
, WHLP_PARA_ALIGNMENT
, WHLP_ALIGN_CENTRE
);
340 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
341 whlp_set_font(h
, FONT_RULE
);
342 #define TEN "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"
343 #define TWENTY TEN TEN
344 #define FORTY TWENTY TWENTY
345 #define EIGHTY FORTY FORTY
346 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
402 for (w
= p
->words
; w
; w
= w
->next
) if (w
->type
== word_WeakCode
) {
404 if (w
->next
&& w
->next
->type
== word_Emph
) {
411 whlp_para_attr(h
, WHLP_PARA_SPACEBELOW
, 12);
413 whlp_para_attr(h
, WHLP_PARA_LEFTINDENT
, 72*nesting
);
414 whlp_begin_para(h
, WHLP_PARA_SCROLL
);
415 while (e
&& *e
&& *t
) {
419 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
421 whlp_set_font(h
, FONT_ITAL_CODE
);
423 whlp_set_font(h
, FONT_BOLD_CODE
);
425 whlp_set_font(h
, FONT_CODE
);
426 whlp_convert(t
, n
, &c
, FALSE
);
432 whlp_set_font(h
, FONT_CODE
);
433 whlp_convert(t
, 0, &c
, FALSE
);
443 whlp_close(h
, filename
);
446 * Loop over the index entries, cleaning up our final text
449 for (i
= 0; (ie
= index234(idx
->entries
, i
)) != NULL
; i
++) {
450 sfree(ie
->backend_data
);
454 static void whlp_contents_write(struct bk_whlp_state
*state
,
455 int level
, char *text
, WHLP_TOPIC topic
) {
457 * Horrifying bug in WinHelp. When dropping a section level or
458 * more without using a folder-type entry, WinHelp accidentally
459 * adds one to the section level. So we correct for that here.
461 if (state
->cnt_last_level
> level
&& topic
)
462 state
->cnt_workaround
= -1;
464 state
->cnt_workaround
= 0;
465 state
->cnt_last_level
= level
;
467 fprintf(state
->cntfp
, "%d ", level
+ state
->cnt_workaround
);
470 fputc('\\', state
->cntfp
);
471 fputc(*text
, state
->cntfp
);
475 fprintf(state
->cntfp
, "=%s", whlp_topic_id(topic
));
476 fputc('\n', state
->cntfp
);
479 static void whlp_navmenu(struct bk_whlp_state
*state
, paragraph
*p
) {
480 whlp_begin_para(state
->h
, WHLP_PARA_NONSCROLL
);
481 whlp_start_hyperlink(state
->h
, (WHLP_TOPIC
)p
->private_data
);
483 whlp_mkparagraph(state
, FONT_NORMAL
, p
->kwtext
, TRUE
);
484 whlp_set_font(state
->h
, FONT_NORMAL
);
485 whlp_text(state
->h
, ": "); /* FIXME: configurability */
487 whlp_mkparagraph(state
, FONT_NORMAL
, p
->words
, TRUE
);
488 whlp_end_hyperlink(state
->h
);
489 whlp_end_para(state
->h
);
493 static void whlp_mkparagraph(struct bk_whlp_state
*state
,
494 int font
, word
*text
, int subsidiary
) {
500 paragraph
*xref_target
= NULL
;
502 for (; text
; text
= text
->next
) switch (text
->type
) {
508 if (subsidiary
) break; /* disabled in subsidiary bits */
510 indextag
*tag
= index_findtag(state
->idx
, text
->text
);
514 for (i
= 0; i
< tag
->nrefs
; i
++)
515 whlp_index_term(state
->h
, tag
->refs
[i
]->backend_data
,
522 if (subsidiary
) break; /* disabled in subsidiary bits */
523 kwl
= kw_lookup(state
->keywords
, text
->text
);
524 assert(xref_target
== NULL
);
526 if (kwl
->para
->type
== para_NumberedList
) {
527 break; /* don't xref to numbered list items */
528 } else if (kwl
->para
->type
== para_BiblioCited
) {
530 * An xref to a bibliography item jumps to the section
533 if (kwl
->para
->parent
)
534 xref_target
= kwl
->para
->parent
;
538 xref_target
= kwl
->para
;
540 whlp_start_hyperlink(state
->h
,
541 (WHLP_TOPIC
)xref_target
->private_data
);
546 if (subsidiary
) break; /* disabled in subsidiary bits */
548 whlp_end_hyperlink(state
->h
);
556 case word_WhiteSpace
:
559 case word_WkCodeSpace
:
563 case word_WkCodeQuote
:
564 if (towordstyle(text
->type
) == word_Emph
)
565 newfont
= deffont
+ FONT_EMPH
;
566 else if (towordstyle(text
->type
) == word_Code
||
567 towordstyle(text
->type
) == word_WeakCode
)
568 newfont
= deffont
+ FONT_CODE
;
571 if (newfont
!= currfont
) {
573 whlp_set_font(state
->h
, newfont
);
575 if (removeattr(text
->type
) == word_Normal
) {
576 if (whlp_convert(text
->text
, 0, &c
, TRUE
))
577 whlp_text(state
->h
, c
);
579 whlp_mkparagraph(state
, deffont
, text
->alt
, FALSE
);
581 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
582 whlp_text(state
->h
, " ");
583 } else if (removeattr(text
->type
) == word_Quote
) {
585 quoteaux(text
->aux
) == quote_Open ?
"\x91" : "\x92");
586 /* FIXME: configurability */
592 static void whlp_rdaddwc(rdstringc
*rs
, word
*text
) {
595 for (; text
; text
= text
->next
) switch (text
->type
) {
608 case word_WhiteSpace
:
611 case word_WkCodeSpace
:
615 case word_WkCodeQuote
:
616 assert(text
->type
!= word_CodeQuote
&&
617 text
->type
!= word_WkCodeQuote
);
618 if (removeattr(text
->type
) == word_Normal
) {
619 if (whlp_convert(text
->text
, 0, &c
, FALSE
))
622 whlp_rdaddwc(rs
, text
->alt
);
624 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
626 } else if (removeattr(text
->type
) == word_Quote
) {
627 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'\x91' : '\x92');
628 /* FIXME: configurability */
635 * Convert a wide string into a string of chars. If `result' is
636 * non-NULL, mallocs the resulting string and stores a pointer to
637 * it in `*result'. If `result' is NULL, merely checks whether all
638 * characters in the string are feasible for the output character
641 * Return is nonzero if all characters are OK. If not all
642 * characters are OK but `result' is non-NULL, a result _will_
643 * still be generated!
645 static int whlp_convert(wchar_t *s
, int maxlen
,
646 char **result
, int hard_spaces
) {
648 * FIXME. Currently this is ISO8859-1 only.
650 int doing
= (result
!= 0);
653 int plen
= 0, psize
= 0;
658 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
662 if ((c
>= 32 && c
<= 126) ||
663 (c
>= 160 && c
<= 255)) {
665 if (c
== 32 && hard_spaces
)
670 /* Char is not OK. */
672 outc
= 0xBF; /* approximate the good old DEC `uh?' */
677 p
= resize(p
, psize
);
683 p
= resize(p
, plen
+1);